1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
30 #include "coretypes.h"
34 #include "langhooks.h"
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);
101 static void output_pending_init_elements (int);
102 static int set_designator (int);
103 static void push_range_stack (tree
);
104 static void add_pending_init (tree
, tree
, tree
, bool);
105 static void set_nonincremental_init (void);
106 static void set_nonincremental_init_from_string (tree
);
107 static tree
find_init_member (tree
);
108 static void readonly_error (tree
, enum lvalue_use
);
109 static void readonly_warning (tree
, enum lvalue_use
);
110 static int lvalue_or_else (const_tree
, enum lvalue_use
);
111 static void record_maybe_used_decl (tree
);
112 static int comptypes_internal (const_tree
, const_tree
, bool *);
114 /* Return true if EXP is a null pointer constant, false otherwise. */
117 null_pointer_constant_p (const_tree expr
)
119 /* This should really operate on c_expr structures, but they aren't
120 yet available everywhere required. */
121 tree type
= TREE_TYPE (expr
);
122 return (TREE_CODE (expr
) == INTEGER_CST
123 && !TREE_OVERFLOW (expr
)
124 && integer_zerop (expr
)
125 && (INTEGRAL_TYPE_P (type
)
126 || (TREE_CODE (type
) == POINTER_TYPE
127 && VOID_TYPE_P (TREE_TYPE (type
))
128 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
131 /* EXPR may appear in an unevaluated part of an integer constant
132 expression, but not in an evaluated part. Wrap it in a
133 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
134 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
137 note_integer_operands (tree expr
)
140 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
142 ret
= copy_node (expr
);
143 TREE_OVERFLOW (ret
) = 1;
147 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
148 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
153 /* Having checked whether EXPR may appear in an unevaluated part of an
154 integer constant expression and found that it may, remove any
155 C_MAYBE_CONST_EXPR noting this fact and return the resulting
159 remove_c_maybe_const_expr (tree expr
)
161 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
162 return C_MAYBE_CONST_EXPR_EXPR (expr
);
167 \f/* This is a cache to hold if two types are compatible or not. */
169 struct tagged_tu_seen_cache
{
170 const struct tagged_tu_seen_cache
* next
;
173 /* The return value of tagged_types_tu_compatible_p if we had seen
174 these two types already. */
178 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
179 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
181 /* Do `exp = require_complete_type (exp);' to make sure exp
182 does not have an incomplete type. (That includes void types.) */
185 require_complete_type (tree value
)
187 tree type
= TREE_TYPE (value
);
189 if (value
== error_mark_node
|| type
== error_mark_node
)
190 return error_mark_node
;
192 /* First, detect a valid value with a complete type. */
193 if (COMPLETE_TYPE_P (type
))
196 c_incomplete_type_error (value
, type
);
197 return error_mark_node
;
200 /* Print an error message for invalid use of an incomplete type.
201 VALUE is the expression that was used (or 0 if that isn't known)
202 and TYPE is the type that was invalid. */
205 c_incomplete_type_error (const_tree value
, const_tree type
)
207 const char *type_code_string
;
209 /* Avoid duplicate error message. */
210 if (TREE_CODE (type
) == ERROR_MARK
)
213 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
214 || TREE_CODE (value
) == PARM_DECL
))
215 error ("%qD has an incomplete type", value
);
219 /* We must print an error message. Be clever about what it says. */
221 switch (TREE_CODE (type
))
224 type_code_string
= "struct";
228 type_code_string
= "union";
232 type_code_string
= "enum";
236 error ("invalid use of void expression");
240 if (TYPE_DOMAIN (type
))
242 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
244 error ("invalid use of flexible array member");
247 type
= TREE_TYPE (type
);
250 error ("invalid use of array with unspecified bounds");
257 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
258 error ("invalid use of undefined type %<%s %E%>",
259 type_code_string
, TYPE_NAME (type
));
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
270 c_type_promotes_to (tree type
)
272 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
273 return double_type_node
;
275 if (c_promoting_integer_type_p (type
))
277 /* Preserve unsignedness if not really getting any wider. */
278 if (TYPE_UNSIGNED (type
)
279 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
280 return unsigned_type_node
;
281 return integer_type_node
;
287 /* Return a variant of TYPE which has all the type qualifiers of LIKE
288 as well as those of TYPE. */
291 qualify_type (tree type
, tree like
)
293 return c_build_qualified_type (type
,
294 TYPE_QUALS (type
) | TYPE_QUALS (like
));
297 /* Return true iff the given tree T is a variable length array. */
300 c_vla_type_p (const_tree t
)
302 if (TREE_CODE (t
) == ARRAY_TYPE
303 && C_TYPE_VARIABLE_SIZE (t
))
308 /* Return the composite type of two compatible types.
310 We assume that comptypes has already been done and returned
311 nonzero; if that isn't so, this may crash. In particular, we
312 assume that qualifiers match. */
315 composite_type (tree t1
, tree t2
)
317 enum tree_code code1
;
318 enum tree_code code2
;
321 /* Save time if the two types are the same. */
323 if (t1
== t2
) return t1
;
325 /* If one type is nonsense, use the other. */
326 if (t1
== error_mark_node
)
328 if (t2
== error_mark_node
)
331 code1
= TREE_CODE (t1
);
332 code2
= TREE_CODE (t2
);
334 /* Merge the attributes. */
335 attributes
= targetm
.merge_type_attributes (t1
, t2
);
337 /* If one is an enumerated type and the other is the compatible
338 integer type, the composite type might be either of the two
339 (DR#013 question 3). For consistency, use the enumerated type as
340 the composite type. */
342 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
344 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
347 gcc_assert (code1
== code2
);
352 /* For two pointers, do this recursively on the target type. */
354 tree pointed_to_1
= TREE_TYPE (t1
);
355 tree pointed_to_2
= TREE_TYPE (t2
);
356 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
357 t1
= build_pointer_type (target
);
358 t1
= build_type_attribute_variant (t1
, attributes
);
359 return qualify_type (t1
, t2
);
364 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
367 tree d1
= TYPE_DOMAIN (t1
);
368 tree d2
= TYPE_DOMAIN (t2
);
369 bool d1_variable
, d2_variable
;
370 bool d1_zero
, d2_zero
;
371 bool t1_complete
, t2_complete
;
373 /* We should not have any type quals on arrays at all. */
374 gcc_assert (!TYPE_QUALS (t1
) && !TYPE_QUALS (t2
));
376 t1_complete
= COMPLETE_TYPE_P (t1
);
377 t2_complete
= COMPLETE_TYPE_P (t2
);
379 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
380 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
382 d1_variable
= (!d1_zero
383 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
384 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
385 d2_variable
= (!d2_zero
386 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
387 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
388 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
389 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
391 /* Save space: see if the result is identical to one of the args. */
392 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
393 && (d2_variable
|| d2_zero
|| !d1_variable
))
394 return build_type_attribute_variant (t1
, attributes
);
395 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
396 && (d1_variable
|| d1_zero
|| !d2_variable
))
397 return build_type_attribute_variant (t2
, attributes
);
399 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
400 return build_type_attribute_variant (t1
, attributes
);
401 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
402 return build_type_attribute_variant (t2
, attributes
);
404 /* Merge the element types, and have a size if either arg has
405 one. We may have qualifiers on the element types. To set
406 up TYPE_MAIN_VARIANT correctly, we need to form the
407 composite of the unqualified types and add the qualifiers
409 quals
= TYPE_QUALS (strip_array_types (elt
));
410 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
411 t1
= build_array_type (unqual_elt
,
412 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
418 /* Ensure a composite type involving a zero-length array type
419 is a zero-length type not an incomplete type. */
420 if (d1_zero
&& d2_zero
421 && (t1_complete
|| t2_complete
)
422 && !COMPLETE_TYPE_P (t1
))
424 TYPE_SIZE (t1
) = bitsize_zero_node
;
425 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
427 t1
= c_build_qualified_type (t1
, quals
);
428 return build_type_attribute_variant (t1
, attributes
);
434 if (attributes
!= NULL
)
436 /* Try harder not to create a new aggregate type. */
437 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
439 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
442 return build_type_attribute_variant (t1
, attributes
);
445 /* Function types: prefer the one that specified arg types.
446 If both do, merge the arg types. Also merge the return types. */
448 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
449 tree p1
= TYPE_ARG_TYPES (t1
);
450 tree p2
= TYPE_ARG_TYPES (t2
);
455 /* Save space: see if the result is identical to one of the args. */
456 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
457 return build_type_attribute_variant (t1
, attributes
);
458 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
459 return build_type_attribute_variant (t2
, attributes
);
461 /* Simple way if one arg fails to specify argument types. */
462 if (TYPE_ARG_TYPES (t1
) == 0)
464 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
465 t1
= build_type_attribute_variant (t1
, attributes
);
466 return qualify_type (t1
, t2
);
468 if (TYPE_ARG_TYPES (t2
) == 0)
470 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
471 t1
= build_type_attribute_variant (t1
, attributes
);
472 return qualify_type (t1
, t2
);
475 /* If both args specify argument types, we must merge the two
476 lists, argument by argument. */
477 /* Tell global_bindings_p to return false so that variable_size
478 doesn't die on VLAs in parameter types. */
479 c_override_global_bindings_to_false
= true;
481 len
= list_length (p1
);
484 for (i
= 0; i
< len
; i
++)
485 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
490 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
492 /* A null type means arg type is not specified.
493 Take whatever the other function type has. */
494 if (TREE_VALUE (p1
) == 0)
496 TREE_VALUE (n
) = TREE_VALUE (p2
);
499 if (TREE_VALUE (p2
) == 0)
501 TREE_VALUE (n
) = TREE_VALUE (p1
);
505 /* Given wait (union {union wait *u; int *i} *)
506 and wait (union wait *),
507 prefer union wait * as type of parm. */
508 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
509 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
512 tree mv2
= TREE_VALUE (p2
);
513 if (mv2
&& mv2
!= error_mark_node
514 && TREE_CODE (mv2
) != ARRAY_TYPE
)
515 mv2
= TYPE_MAIN_VARIANT (mv2
);
516 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
517 memb
; memb
= TREE_CHAIN (memb
))
519 tree mv3
= TREE_TYPE (memb
);
520 if (mv3
&& mv3
!= error_mark_node
521 && TREE_CODE (mv3
) != ARRAY_TYPE
)
522 mv3
= TYPE_MAIN_VARIANT (mv3
);
523 if (comptypes (mv3
, mv2
))
525 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
527 pedwarn (input_location
, OPT_pedantic
,
528 "function types not truly compatible in ISO C");
533 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
534 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
537 tree mv1
= TREE_VALUE (p1
);
538 if (mv1
&& mv1
!= error_mark_node
539 && TREE_CODE (mv1
) != ARRAY_TYPE
)
540 mv1
= TYPE_MAIN_VARIANT (mv1
);
541 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
542 memb
; memb
= TREE_CHAIN (memb
))
544 tree mv3
= TREE_TYPE (memb
);
545 if (mv3
&& mv3
!= error_mark_node
546 && TREE_CODE (mv3
) != ARRAY_TYPE
)
547 mv3
= TYPE_MAIN_VARIANT (mv3
);
548 if (comptypes (mv3
, mv1
))
550 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
552 pedwarn (input_location
, OPT_pedantic
,
553 "function types not truly compatible in ISO C");
558 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
562 c_override_global_bindings_to_false
= false;
563 t1
= build_function_type (valtype
, newargs
);
564 t1
= qualify_type (t1
, t2
);
565 /* ... falls through ... */
569 return build_type_attribute_variant (t1
, attributes
);
574 /* Return the type of a conditional expression between pointers to
575 possibly differently qualified versions of compatible types.
577 We assume that comp_target_types has already been done and returned
578 nonzero; if that isn't so, this may crash. */
581 common_pointer_type (tree t1
, tree t2
)
584 tree pointed_to_1
, mv1
;
585 tree pointed_to_2
, mv2
;
587 unsigned target_quals
;
589 /* Save time if the two types are the same. */
591 if (t1
== t2
) return t1
;
593 /* If one type is nonsense, use the other. */
594 if (t1
== error_mark_node
)
596 if (t2
== error_mark_node
)
599 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
600 && TREE_CODE (t2
) == POINTER_TYPE
);
602 /* Merge the attributes. */
603 attributes
= targetm
.merge_type_attributes (t1
, t2
);
605 /* Find the composite type of the target types, and combine the
606 qualifiers of the two types' targets. Do not lose qualifiers on
607 array element types by taking the TYPE_MAIN_VARIANT. */
608 mv1
= pointed_to_1
= TREE_TYPE (t1
);
609 mv2
= pointed_to_2
= TREE_TYPE (t2
);
610 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
611 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
612 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
613 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
614 target
= composite_type (mv1
, mv2
);
616 /* For function types do not merge const qualifiers, but drop them
617 if used inconsistently. The middle-end uses these to mark const
618 and noreturn functions. */
619 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
620 target_quals
= TYPE_QUALS (pointed_to_1
) & TYPE_QUALS (pointed_to_2
);
622 target_quals
= TYPE_QUALS (pointed_to_1
) | TYPE_QUALS (pointed_to_2
);
623 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
624 return build_type_attribute_variant (t1
, attributes
);
627 /* Return the common type for two arithmetic types under the usual
628 arithmetic conversions. The default conversions have already been
629 applied, and enumerated types converted to their compatible integer
630 types. The resulting type is unqualified and has no attributes.
632 This is the type for the result of most arithmetic operations
633 if the operands have the given two types. */
636 c_common_type (tree t1
, tree t2
)
638 enum tree_code code1
;
639 enum tree_code code2
;
641 /* If one type is nonsense, use the other. */
642 if (t1
== error_mark_node
)
644 if (t2
== error_mark_node
)
647 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
648 t1
= TYPE_MAIN_VARIANT (t1
);
650 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
651 t2
= TYPE_MAIN_VARIANT (t2
);
653 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
654 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
656 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
657 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
659 /* Save time if the two types are the same. */
661 if (t1
== t2
) return t1
;
663 code1
= TREE_CODE (t1
);
664 code2
= TREE_CODE (t2
);
666 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
667 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
668 || code1
== INTEGER_TYPE
);
669 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
670 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
671 || code2
== INTEGER_TYPE
);
673 /* When one operand is a decimal float type, the other operand cannot be
674 a generic float type or a complex type. We also disallow vector types
676 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
677 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
679 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
681 error ("can%'t mix operands of decimal float and vector types");
682 return error_mark_node
;
684 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
686 error ("can%'t mix operands of decimal float and complex types");
687 return error_mark_node
;
689 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
691 error ("can%'t mix operands of decimal float and other float types");
692 return error_mark_node
;
696 /* If one type is a vector type, return that type. (How the usual
697 arithmetic conversions apply to the vector types extension is not
698 precisely specified.) */
699 if (code1
== VECTOR_TYPE
)
702 if (code2
== VECTOR_TYPE
)
705 /* If one type is complex, form the common type of the non-complex
706 components, then make that complex. Use T1 or T2 if it is the
708 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
710 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
711 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
712 tree subtype
= c_common_type (subtype1
, subtype2
);
714 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
716 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
719 return build_complex_type (subtype
);
722 /* If only one is real, use it as the result. */
724 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
727 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
730 /* If both are real and either are decimal floating point types, use
731 the decimal floating point type with the greater precision. */
733 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
735 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
736 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
737 return dfloat128_type_node
;
738 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
739 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
740 return dfloat64_type_node
;
741 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
742 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
743 return dfloat32_type_node
;
746 /* Deal with fixed-point types. */
747 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
749 unsigned int unsignedp
= 0, satp
= 0;
750 enum machine_mode m1
, m2
;
751 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
756 /* If one input type is saturating, the result type is saturating. */
757 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
760 /* If both fixed-point types are unsigned, the result type is unsigned.
761 When mixing fixed-point and integer types, follow the sign of the
763 Otherwise, the result type is signed. */
764 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
765 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
766 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
767 && TYPE_UNSIGNED (t1
))
768 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
769 && TYPE_UNSIGNED (t2
)))
772 /* The result type is signed. */
775 /* If the input type is unsigned, we need to convert to the
777 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
779 enum mode_class mclass
= (enum mode_class
) 0;
780 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
782 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
786 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
788 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
790 enum mode_class mclass
= (enum mode_class
) 0;
791 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
793 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
797 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
801 if (code1
== FIXED_POINT_TYPE
)
803 fbit1
= GET_MODE_FBIT (m1
);
804 ibit1
= GET_MODE_IBIT (m1
);
809 /* Signed integers need to subtract one sign bit. */
810 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
813 if (code2
== FIXED_POINT_TYPE
)
815 fbit2
= GET_MODE_FBIT (m2
);
816 ibit2
= GET_MODE_IBIT (m2
);
821 /* Signed integers need to subtract one sign bit. */
822 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
825 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
826 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
827 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
831 /* Both real or both integers; use the one with greater precision. */
833 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
835 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
838 /* Same precision. Prefer long longs to longs to ints when the
839 same precision, following the C99 rules on integer type rank
840 (which are equivalent to the C90 rules for C90 types). */
842 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
843 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
844 return long_long_unsigned_type_node
;
846 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
847 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
849 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
850 return long_long_unsigned_type_node
;
852 return long_long_integer_type_node
;
855 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
856 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
857 return long_unsigned_type_node
;
859 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
860 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
862 /* But preserve unsignedness from the other type,
863 since long cannot hold all the values of an unsigned int. */
864 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
865 return long_unsigned_type_node
;
867 return long_integer_type_node
;
870 /* Likewise, prefer long double to double even if same size. */
871 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
872 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
873 return long_double_type_node
;
875 /* Otherwise prefer the unsigned one. */
877 if (TYPE_UNSIGNED (t1
))
883 /* Wrapper around c_common_type that is used by c-common.c and other
884 front end optimizations that remove promotions. ENUMERAL_TYPEs
885 are allowed here and are converted to their compatible integer types.
886 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
887 preferably a non-Boolean type as the common type. */
889 common_type (tree t1
, tree t2
)
891 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
892 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
893 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
894 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
896 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
897 if (TREE_CODE (t1
) == BOOLEAN_TYPE
898 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
899 return boolean_type_node
;
901 /* If either type is BOOLEAN_TYPE, then return the other. */
902 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
904 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
907 return c_common_type (t1
, t2
);
910 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
911 or various other operations. Return 2 if they are compatible
912 but a warning may be needed if you use them together. */
915 comptypes (tree type1
, tree type2
)
917 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
920 val
= comptypes_internal (type1
, type2
, NULL
);
921 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
926 /* Like comptypes, but if it returns non-zero because enum and int are
927 compatible, it sets *ENUM_AND_INT_P to true. */
930 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
932 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
935 val
= comptypes_internal (type1
, type2
, enum_and_int_p
);
936 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
941 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
942 or various other operations. Return 2 if they are compatible
943 but a warning may be needed if you use them together. If
944 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
945 compatible integer type, then this sets *ENUM_AND_INT_P to true;
946 *ENUM_AND_INT_P is never set to false. This differs from
947 comptypes, in that we don't free the seen types. */
950 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
)
952 const_tree t1
= type1
;
953 const_tree t2
= type2
;
956 /* Suppress errors caused by previously reported errors. */
958 if (t1
== t2
|| !t1
|| !t2
959 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
962 /* If either type is the internal version of sizetype, return the
964 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
965 && TYPE_ORIG_SIZE_TYPE (t1
))
966 t1
= TYPE_ORIG_SIZE_TYPE (t1
);
968 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
969 && TYPE_ORIG_SIZE_TYPE (t2
))
970 t2
= TYPE_ORIG_SIZE_TYPE (t2
);
973 /* Enumerated types are compatible with integer types, but this is
974 not transitive: two enumerated types in the same translation unit
975 are compatible with each other only if they are the same type. */
977 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
979 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
980 if (enum_and_int_p
!= NULL
&& TREE_CODE (t2
) != VOID_TYPE
)
981 *enum_and_int_p
= true;
983 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
985 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
986 if (enum_and_int_p
!= NULL
&& TREE_CODE (t1
) != VOID_TYPE
)
987 *enum_and_int_p
= true;
993 /* Different classes of types can't be compatible. */
995 if (TREE_CODE (t1
) != TREE_CODE (t2
))
998 /* Qualifiers must match. C99 6.7.3p9 */
1000 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1003 /* Allow for two different type nodes which have essentially the same
1004 definition. Note that we already checked for equality of the type
1005 qualifiers (just above). */
1007 if (TREE_CODE (t1
) != ARRAY_TYPE
1008 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1011 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1012 if (!(attrval
= targetm
.comp_type_attributes (t1
, t2
)))
1015 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1018 switch (TREE_CODE (t1
))
1021 /* Do not remove mode or aliasing information. */
1022 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1023 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1025 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1026 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1031 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
);
1036 tree d1
= TYPE_DOMAIN (t1
);
1037 tree d2
= TYPE_DOMAIN (t2
);
1038 bool d1_variable
, d2_variable
;
1039 bool d1_zero
, d2_zero
;
1042 /* Target types must match incl. qualifiers. */
1043 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1044 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1048 /* Sizes must match unless one is missing or variable. */
1049 if (d1
== 0 || d2
== 0 || d1
== d2
)
1052 d1_zero
= !TYPE_MAX_VALUE (d1
);
1053 d2_zero
= !TYPE_MAX_VALUE (d2
);
1055 d1_variable
= (!d1_zero
1056 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1057 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1058 d2_variable
= (!d2_zero
1059 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1060 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1061 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1062 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1064 if (d1_variable
|| d2_variable
)
1066 if (d1_zero
&& d2_zero
)
1068 if (d1_zero
|| d2_zero
1069 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1070 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1079 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1081 tree a1
= TYPE_ATTRIBUTES (t1
);
1082 tree a2
= TYPE_ATTRIBUTES (t2
);
1084 if (! attribute_list_contained (a1
, a2
)
1085 && ! attribute_list_contained (a2
, a1
))
1089 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
);
1090 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
);
1095 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1096 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1103 return attrval
== 2 && val
== 1 ? 2 : val
;
1106 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1107 ignoring their qualifiers. */
1110 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1114 bool enum_and_int_p
;
1116 /* Do not lose qualifiers on element types of array types that are
1117 pointer targets by taking their TYPE_MAIN_VARIANT. */
1118 mvl
= TREE_TYPE (ttl
);
1119 mvr
= TREE_TYPE (ttr
);
1120 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
1121 mvl
= TYPE_MAIN_VARIANT (mvl
);
1122 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
1123 mvr
= TYPE_MAIN_VARIANT (mvr
);
1124 enum_and_int_p
= false;
1125 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1128 pedwarn (location
, OPT_pedantic
, "types are not quite compatible");
1130 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1131 warning_at (location
, OPT_Wc___compat
,
1132 "pointer target types incompatible in C++");
1137 /* Subroutines of `comptypes'. */
1139 /* Determine whether two trees derive from the same translation unit.
1140 If the CONTEXT chain ends in a null, that tree's context is still
1141 being parsed, so if two trees have context chains ending in null,
1142 they're in the same translation unit. */
1144 same_translation_unit_p (const_tree t1
, const_tree t2
)
1146 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1147 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1149 case tcc_declaration
:
1150 t1
= DECL_CONTEXT (t1
); break;
1152 t1
= TYPE_CONTEXT (t1
); break;
1153 case tcc_exceptional
:
1154 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1155 default: gcc_unreachable ();
1158 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1159 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1161 case tcc_declaration
:
1162 t2
= DECL_CONTEXT (t2
); break;
1164 t2
= TYPE_CONTEXT (t2
); break;
1165 case tcc_exceptional
:
1166 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1167 default: gcc_unreachable ();
1173 /* Allocate the seen two types, assuming that they are compatible. */
1175 static struct tagged_tu_seen_cache
*
1176 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1178 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1179 tu
->next
= tagged_tu_seen_base
;
1183 tagged_tu_seen_base
= tu
;
1185 /* The C standard says that two structures in different translation
1186 units are compatible with each other only if the types of their
1187 fields are compatible (among other things). We assume that they
1188 are compatible until proven otherwise when building the cache.
1189 An example where this can occur is:
1194 If we are comparing this against a similar struct in another TU,
1195 and did not assume they were compatible, we end up with an infinite
1201 /* Free the seen types until we get to TU_TIL. */
1204 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1206 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1207 while (tu
!= tu_til
)
1209 const struct tagged_tu_seen_cache
*const tu1
1210 = (const struct tagged_tu_seen_cache
*) tu
;
1212 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1214 tagged_tu_seen_base
= tu_til
;
1217 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1218 compatible. If the two types are not the same (which has been
1219 checked earlier), this can only happen when multiple translation
1220 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1221 rules. ENUM_AND_INT_P is as in comptypes_internal. */
1224 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1225 bool *enum_and_int_p
)
1228 bool needs_warning
= false;
1230 /* We have to verify that the tags of the types are the same. This
1231 is harder than it looks because this may be a typedef, so we have
1232 to go look at the original type. It may even be a typedef of a
1234 In the case of compiler-created builtin structs the TYPE_DECL
1235 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1236 while (TYPE_NAME (t1
)
1237 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1238 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1239 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1241 while (TYPE_NAME (t2
)
1242 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1243 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1244 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1246 /* C90 didn't have the requirement that the two tags be the same. */
1247 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1250 /* C90 didn't say what happened if one or both of the types were
1251 incomplete; we choose to follow C99 rules here, which is that they
1253 if (TYPE_SIZE (t1
) == NULL
1254 || TYPE_SIZE (t2
) == NULL
)
1258 const struct tagged_tu_seen_cache
* tts_i
;
1259 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1260 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1264 switch (TREE_CODE (t1
))
1268 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1269 /* Speed up the case where the type values are in the same order. */
1270 tree tv1
= TYPE_VALUES (t1
);
1271 tree tv2
= TYPE_VALUES (t2
);
1278 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1280 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1282 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1289 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1293 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1299 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1305 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1307 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1309 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1320 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1321 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1327 /* Speed up the common case where the fields are in the same order. */
1328 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1329 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
1333 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1335 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1338 if (result
!= 1 && !DECL_NAME (s1
))
1346 needs_warning
= true;
1348 if (TREE_CODE (s1
) == FIELD_DECL
1349 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1350 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1358 tu
->val
= needs_warning
? 2 : 1;
1362 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= TREE_CHAIN (s1
))
1366 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= TREE_CHAIN (s2
))
1367 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1371 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1374 if (result
!= 1 && !DECL_NAME (s1
))
1382 needs_warning
= true;
1384 if (TREE_CODE (s1
) == FIELD_DECL
1385 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1386 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1398 tu
->val
= needs_warning
? 2 : 10;
1404 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1406 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1408 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
1411 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1412 || DECL_NAME (s1
) != DECL_NAME (s2
))
1414 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1419 needs_warning
= true;
1421 if (TREE_CODE (s1
) == FIELD_DECL
1422 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1423 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1429 tu
->val
= needs_warning
? 2 : 1;
1438 /* Return 1 if two function types F1 and F2 are compatible.
1439 If either type specifies no argument types,
1440 the other must specify a fixed number of self-promoting arg types.
1441 Otherwise, if one type specifies only the number of arguments,
1442 the other must specify that number of self-promoting arg types.
1443 Otherwise, the argument types must match.
1444 ENUM_AND_INT_P is as in comptypes_internal. */
1447 function_types_compatible_p (const_tree f1
, const_tree f2
,
1448 bool *enum_and_int_p
)
1451 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1456 ret1
= TREE_TYPE (f1
);
1457 ret2
= TREE_TYPE (f2
);
1459 /* 'volatile' qualifiers on a function's return type used to mean
1460 the function is noreturn. */
1461 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1462 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1463 if (TYPE_VOLATILE (ret1
))
1464 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1465 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1466 if (TYPE_VOLATILE (ret2
))
1467 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1468 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1469 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
);
1473 args1
= TYPE_ARG_TYPES (f1
);
1474 args2
= TYPE_ARG_TYPES (f2
);
1476 /* An unspecified parmlist matches any specified parmlist
1477 whose argument types don't need default promotions. */
1481 if (!self_promoting_args_p (args2
))
1483 /* If one of these types comes from a non-prototype fn definition,
1484 compare that with the other type's arglist.
1485 If they don't match, ask for a warning (but no error). */
1486 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1487 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1494 if (!self_promoting_args_p (args1
))
1496 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1497 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1503 /* Both types have argument lists: compare them and propagate results. */
1504 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
);
1505 return val1
!= 1 ? val1
: val
;
1508 /* Check two lists of types for compatibility, returning 0 for
1509 incompatible, 1 for compatible, or 2 for compatible with
1510 warning. ENUM_AND_INT_P is as in comptypes_internal. */
1513 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1514 bool *enum_and_int_p
)
1516 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1522 tree a1
, mv1
, a2
, mv2
;
1523 if (args1
== 0 && args2
== 0)
1525 /* If one list is shorter than the other,
1526 they fail to match. */
1527 if (args1
== 0 || args2
== 0)
1529 mv1
= a1
= TREE_VALUE (args1
);
1530 mv2
= a2
= TREE_VALUE (args2
);
1531 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1532 mv1
= TYPE_MAIN_VARIANT (mv1
);
1533 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1534 mv2
= TYPE_MAIN_VARIANT (mv2
);
1535 /* A null pointer instead of a type
1536 means there is supposed to be an argument
1537 but nothing is specified about what type it has.
1538 So match anything that self-promotes. */
1541 if (c_type_promotes_to (a2
) != a2
)
1546 if (c_type_promotes_to (a1
) != a1
)
1549 /* If one of the lists has an error marker, ignore this arg. */
1550 else if (TREE_CODE (a1
) == ERROR_MARK
1551 || TREE_CODE (a2
) == ERROR_MARK
)
1553 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
)))
1555 /* Allow wait (union {union wait *u; int *i} *)
1556 and wait (union wait *) to be compatible. */
1557 if (TREE_CODE (a1
) == UNION_TYPE
1558 && (TYPE_NAME (a1
) == 0
1559 || TYPE_TRANSPARENT_UNION (a1
))
1560 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1561 && tree_int_cst_equal (TYPE_SIZE (a1
),
1565 for (memb
= TYPE_FIELDS (a1
);
1566 memb
; memb
= TREE_CHAIN (memb
))
1568 tree mv3
= TREE_TYPE (memb
);
1569 if (mv3
&& mv3
!= error_mark_node
1570 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1571 mv3
= TYPE_MAIN_VARIANT (mv3
);
1572 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
))
1578 else if (TREE_CODE (a2
) == UNION_TYPE
1579 && (TYPE_NAME (a2
) == 0
1580 || TYPE_TRANSPARENT_UNION (a2
))
1581 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1582 && tree_int_cst_equal (TYPE_SIZE (a2
),
1586 for (memb
= TYPE_FIELDS (a2
);
1587 memb
; memb
= TREE_CHAIN (memb
))
1589 tree mv3
= TREE_TYPE (memb
);
1590 if (mv3
&& mv3
!= error_mark_node
1591 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1592 mv3
= TYPE_MAIN_VARIANT (mv3
);
1593 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
))
1603 /* comptypes said ok, but record if it said to warn. */
1607 args1
= TREE_CHAIN (args1
);
1608 args2
= TREE_CHAIN (args2
);
1612 /* Compute the size to increment a pointer by. */
1615 c_size_in_bytes (const_tree type
)
1617 enum tree_code code
= TREE_CODE (type
);
1619 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
1620 return size_one_node
;
1622 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1624 error ("arithmetic on pointer to an incomplete type");
1625 return size_one_node
;
1628 /* Convert in case a char is more than one unit. */
1629 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1630 size_int (TYPE_PRECISION (char_type_node
)
1634 /* Return either DECL or its known constant value (if it has one). */
1637 decl_constant_value (tree decl
)
1639 if (/* Don't change a variable array bound or initial value to a constant
1640 in a place where a variable is invalid. Note that DECL_INITIAL
1641 isn't valid for a PARM_DECL. */
1642 current_function_decl
!= 0
1643 && TREE_CODE (decl
) != PARM_DECL
1644 && !TREE_THIS_VOLATILE (decl
)
1645 && TREE_READONLY (decl
)
1646 && DECL_INITIAL (decl
) != 0
1647 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1648 /* This is invalid if initial value is not constant.
1649 If it has either a function call, a memory reference,
1650 or a variable, then re-evaluating it could give different results. */
1651 && TREE_CONSTANT (DECL_INITIAL (decl
))
1652 /* Check for cases where this is sub-optimal, even though valid. */
1653 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1654 return DECL_INITIAL (decl
);
1658 /* Convert the array expression EXP to a pointer. */
1660 array_to_pointer_conversion (location_t loc
, tree exp
)
1662 tree orig_exp
= exp
;
1663 tree type
= TREE_TYPE (exp
);
1665 tree restype
= TREE_TYPE (type
);
1668 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1670 STRIP_TYPE_NOPS (exp
);
1672 if (TREE_NO_WARNING (orig_exp
))
1673 TREE_NO_WARNING (exp
) = 1;
1675 ptrtype
= build_pointer_type (restype
);
1677 if (TREE_CODE (exp
) == INDIRECT_REF
)
1678 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1680 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1681 return convert (ptrtype
, adr
);
1684 /* Convert the function expression EXP to a pointer. */
1686 function_to_pointer_conversion (location_t loc
, tree exp
)
1688 tree orig_exp
= exp
;
1690 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1692 STRIP_TYPE_NOPS (exp
);
1694 if (TREE_NO_WARNING (orig_exp
))
1695 TREE_NO_WARNING (exp
) = 1;
1697 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1700 /* Perform the default conversion of arrays and functions to pointers.
1701 Return the result of converting EXP. For any other expression, just
1704 LOC is the location of the expression. */
1707 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1709 tree orig_exp
= exp
.value
;
1710 tree type
= TREE_TYPE (exp
.value
);
1711 enum tree_code code
= TREE_CODE (type
);
1717 bool not_lvalue
= false;
1718 bool lvalue_array_p
;
1720 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1721 || CONVERT_EXPR_P (exp
.value
))
1722 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1724 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1726 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1729 if (TREE_NO_WARNING (orig_exp
))
1730 TREE_NO_WARNING (exp
.value
) = 1;
1732 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1733 if (!flag_isoc99
&& !lvalue_array_p
)
1735 /* Before C99, non-lvalue arrays do not decay to pointers.
1736 Normally, using such an array would be invalid; but it can
1737 be used correctly inside sizeof or as a statement expression.
1738 Thus, do not give an error here; an error will result later. */
1742 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1746 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1756 /* EXP is an expression of integer type. Apply the integer promotions
1757 to it and return the promoted value. */
1760 perform_integral_promotions (tree exp
)
1762 tree type
= TREE_TYPE (exp
);
1763 enum tree_code code
= TREE_CODE (type
);
1765 gcc_assert (INTEGRAL_TYPE_P (type
));
1767 /* Normally convert enums to int,
1768 but convert wide enums to something wider. */
1769 if (code
== ENUMERAL_TYPE
)
1771 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
1772 TYPE_PRECISION (integer_type_node
)),
1773 ((TYPE_PRECISION (type
)
1774 >= TYPE_PRECISION (integer_type_node
))
1775 && TYPE_UNSIGNED (type
)));
1777 return convert (type
, exp
);
1780 /* ??? This should no longer be needed now bit-fields have their
1782 if (TREE_CODE (exp
) == COMPONENT_REF
1783 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
1784 /* If it's thinner than an int, promote it like a
1785 c_promoting_integer_type_p, otherwise leave it alone. */
1786 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
1787 TYPE_PRECISION (integer_type_node
)))
1788 return convert (integer_type_node
, exp
);
1790 if (c_promoting_integer_type_p (type
))
1792 /* Preserve unsignedness if not really getting any wider. */
1793 if (TYPE_UNSIGNED (type
)
1794 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1795 return convert (unsigned_type_node
, exp
);
1797 return convert (integer_type_node
, exp
);
1804 /* Perform default promotions for C data used in expressions.
1805 Enumeral types or short or char are converted to int.
1806 In addition, manifest constants symbols are replaced by their values. */
1809 default_conversion (tree exp
)
1812 tree type
= TREE_TYPE (exp
);
1813 enum tree_code code
= TREE_CODE (type
);
1816 /* Functions and arrays have been converted during parsing. */
1817 gcc_assert (code
!= FUNCTION_TYPE
);
1818 if (code
== ARRAY_TYPE
)
1821 /* Constants can be used directly unless they're not loadable. */
1822 if (TREE_CODE (exp
) == CONST_DECL
)
1823 exp
= DECL_INITIAL (exp
);
1825 /* Strip no-op conversions. */
1827 STRIP_TYPE_NOPS (exp
);
1829 if (TREE_NO_WARNING (orig_exp
))
1830 TREE_NO_WARNING (exp
) = 1;
1832 if (code
== VOID_TYPE
)
1834 error ("void value not ignored as it ought to be");
1835 return error_mark_node
;
1838 exp
= require_complete_type (exp
);
1839 if (exp
== error_mark_node
)
1840 return error_mark_node
;
1842 promoted_type
= targetm
.promoted_type (type
);
1844 return convert (promoted_type
, exp
);
1846 if (INTEGRAL_TYPE_P (type
))
1847 return perform_integral_promotions (exp
);
1852 /* Look up COMPONENT in a structure or union DECL.
1854 If the component name is not found, returns NULL_TREE. Otherwise,
1855 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1856 stepping down the chain to the component, which is in the last
1857 TREE_VALUE of the list. Normally the list is of length one, but if
1858 the component is embedded within (nested) anonymous structures or
1859 unions, the list steps down the chain to the component. */
1862 lookup_field (tree decl
, tree component
)
1864 tree type
= TREE_TYPE (decl
);
1867 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1868 to the field elements. Use a binary search on this array to quickly
1869 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1870 will always be set for structures which have many elements. */
1872 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
1875 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
1877 field
= TYPE_FIELDS (type
);
1879 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
1880 while (top
- bot
> 1)
1882 half
= (top
- bot
+ 1) >> 1;
1883 field
= field_array
[bot
+half
];
1885 if (DECL_NAME (field
) == NULL_TREE
)
1887 /* Step through all anon unions in linear fashion. */
1888 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
1890 field
= field_array
[bot
++];
1891 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1892 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1894 tree anon
= lookup_field (field
, component
);
1897 return tree_cons (NULL_TREE
, field
, anon
);
1901 /* Entire record is only anon unions. */
1905 /* Restart the binary search, with new lower bound. */
1909 if (DECL_NAME (field
) == component
)
1911 if (DECL_NAME (field
) < component
)
1917 if (DECL_NAME (field_array
[bot
]) == component
)
1918 field
= field_array
[bot
];
1919 else if (DECL_NAME (field
) != component
)
1924 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1926 if (DECL_NAME (field
) == NULL_TREE
1927 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1928 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
1930 tree anon
= lookup_field (field
, component
);
1933 return tree_cons (NULL_TREE
, field
, anon
);
1936 if (DECL_NAME (field
) == component
)
1940 if (field
== NULL_TREE
)
1944 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
1947 /* Make an expression to refer to the COMPONENT field of structure or
1948 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
1949 location of the COMPONENT_REF. */
1952 build_component_ref (location_t loc
, tree datum
, tree component
)
1954 tree type
= TREE_TYPE (datum
);
1955 enum tree_code code
= TREE_CODE (type
);
1958 bool datum_lvalue
= lvalue_p (datum
);
1960 if (!objc_is_public (datum
, component
))
1961 return error_mark_node
;
1963 /* See if there is a field or component with name COMPONENT. */
1965 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1967 if (!COMPLETE_TYPE_P (type
))
1969 c_incomplete_type_error (NULL_TREE
, type
);
1970 return error_mark_node
;
1973 field
= lookup_field (datum
, component
);
1977 error_at (loc
, "%qT has no member named %qE", type
, component
);
1978 return error_mark_node
;
1981 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1982 This might be better solved in future the way the C++ front
1983 end does it - by giving the anonymous entities each a
1984 separate name and type, and then have build_component_ref
1985 recursively call itself. We can't do that here. */
1988 tree subdatum
= TREE_VALUE (field
);
1991 bool use_datum_quals
;
1993 if (TREE_TYPE (subdatum
) == error_mark_node
)
1994 return error_mark_node
;
1996 /* If this is an rvalue, it does not have qualifiers in C
1997 standard terms and we must avoid propagating such
1998 qualifiers down to a non-lvalue array that is then
1999 converted to a pointer. */
2000 use_datum_quals
= (datum_lvalue
2001 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2003 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2004 if (use_datum_quals
)
2005 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2006 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2008 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2010 SET_EXPR_LOCATION (ref
, loc
);
2011 if (TREE_READONLY (subdatum
)
2012 || (use_datum_quals
&& TREE_READONLY (datum
)))
2013 TREE_READONLY (ref
) = 1;
2014 if (TREE_THIS_VOLATILE (subdatum
)
2015 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2016 TREE_THIS_VOLATILE (ref
) = 1;
2018 if (TREE_DEPRECATED (subdatum
))
2019 warn_deprecated_use (subdatum
, NULL_TREE
);
2023 field
= TREE_CHAIN (field
);
2029 else if (code
!= ERROR_MARK
)
2031 "request for member %qE in something not a structure or union",
2034 return error_mark_node
;
2037 /* Given an expression PTR for a pointer, return an expression
2038 for the value pointed to.
2039 ERRORSTRING is the name of the operator to appear in error messages.
2041 LOC is the location to use for the generated tree. */
2044 build_indirect_ref (location_t loc
, tree ptr
, const char *errorstring
)
2046 tree pointer
= default_conversion (ptr
);
2047 tree type
= TREE_TYPE (pointer
);
2050 if (TREE_CODE (type
) == POINTER_TYPE
)
2052 if (CONVERT_EXPR_P (pointer
)
2053 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2055 /* If a warning is issued, mark it to avoid duplicates from
2056 the backend. This only needs to be done at
2057 warn_strict_aliasing > 2. */
2058 if (warn_strict_aliasing
> 2)
2059 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2060 type
, TREE_OPERAND (pointer
, 0)))
2061 TREE_NO_WARNING (pointer
) = 1;
2064 if (TREE_CODE (pointer
) == ADDR_EXPR
2065 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2066 == TREE_TYPE (type
)))
2068 ref
= TREE_OPERAND (pointer
, 0);
2069 protected_set_expr_location (ref
, loc
);
2074 tree t
= TREE_TYPE (type
);
2076 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2078 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2080 error_at (loc
, "dereferencing pointer to incomplete type");
2081 return error_mark_node
;
2083 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2084 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2086 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2087 so that we get the proper error message if the result is used
2088 to assign to. Also, &* is supposed to be a no-op.
2089 And ANSI C seems to specify that the type of the result
2090 should be the const type. */
2091 /* A de-reference of a pointer to const is not a const. It is valid
2092 to change it via some other pointer. */
2093 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2094 TREE_SIDE_EFFECTS (ref
)
2095 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2096 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2097 protected_set_expr_location (ref
, loc
);
2101 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2103 "invalid type argument of %qs (have %qT)", errorstring
, type
);
2104 return error_mark_node
;
2107 /* This handles expressions of the form "a[i]", which denotes
2110 This is logically equivalent in C to *(a+i), but we may do it differently.
2111 If A is a variable or a member, we generate a primitive ARRAY_REF.
2112 This avoids forcing the array out of registers, and can work on
2113 arrays that are not lvalues (for example, members of structures returned
2116 LOC is the location to use for the returned expression. */
2119 build_array_ref (location_t loc
, tree array
, tree index
)
2122 bool swapped
= false;
2123 if (TREE_TYPE (array
) == error_mark_node
2124 || TREE_TYPE (index
) == error_mark_node
)
2125 return error_mark_node
;
2127 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2128 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
)
2131 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2132 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2134 error_at (loc
, "subscripted value is neither array nor pointer");
2135 return error_mark_node
;
2143 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2145 error_at (loc
, "array subscript is not an integer");
2146 return error_mark_node
;
2149 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2151 error_at (loc
, "subscripted value is pointer to function");
2152 return error_mark_node
;
2155 /* ??? Existing practice has been to warn only when the char
2156 index is syntactically the index, not for char[array]. */
2158 warn_array_subscript_with_type_char (index
);
2160 /* Apply default promotions *after* noticing character types. */
2161 index
= default_conversion (index
);
2163 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2165 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2169 /* An array that is indexed by a non-constant
2170 cannot be stored in a register; we must be able to do
2171 address arithmetic on its address.
2172 Likewise an array of elements of variable size. */
2173 if (TREE_CODE (index
) != INTEGER_CST
2174 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2175 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2177 if (!c_mark_addressable (array
))
2178 return error_mark_node
;
2180 /* An array that is indexed by a constant value which is not within
2181 the array bounds cannot be stored in a register either; because we
2182 would get a crash in store_bit_field/extract_bit_field when trying
2183 to access a non-existent part of the register. */
2184 if (TREE_CODE (index
) == INTEGER_CST
2185 && TYPE_DOMAIN (TREE_TYPE (array
))
2186 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2188 if (!c_mark_addressable (array
))
2189 return error_mark_node
;
2195 while (TREE_CODE (foo
) == COMPONENT_REF
)
2196 foo
= TREE_OPERAND (foo
, 0);
2197 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2198 pedwarn (loc
, OPT_pedantic
,
2199 "ISO C forbids subscripting %<register%> array");
2200 else if (!flag_isoc99
&& !lvalue_p (foo
))
2201 pedwarn (loc
, OPT_pedantic
,
2202 "ISO C90 forbids subscripting non-lvalue array");
2205 type
= TREE_TYPE (TREE_TYPE (array
));
2206 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2207 /* Array ref is const/volatile if the array elements are
2208 or if the array is. */
2209 TREE_READONLY (rval
)
2210 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2211 | TREE_READONLY (array
));
2212 TREE_SIDE_EFFECTS (rval
)
2213 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2214 | TREE_SIDE_EFFECTS (array
));
2215 TREE_THIS_VOLATILE (rval
)
2216 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2217 /* This was added by rms on 16 Nov 91.
2218 It fixes vol struct foo *a; a->elts[1]
2219 in an inline function.
2220 Hope it doesn't break something else. */
2221 | TREE_THIS_VOLATILE (array
));
2222 ret
= require_complete_type (rval
);
2223 protected_set_expr_location (ret
, loc
);
2228 tree ar
= default_conversion (array
);
2230 if (ar
== error_mark_node
)
2233 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2234 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2236 return build_indirect_ref
2237 (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
, index
, 0),
2242 /* Build an external reference to identifier ID. FUN indicates
2243 whether this will be used for a function call. LOC is the source
2244 location of the identifier. This sets *TYPE to the type of the
2245 identifier, which is not the same as the type of the returned value
2246 for CONST_DECLs defined as enum constants. If the type of the
2247 identifier is not available, *TYPE is set to NULL. */
2249 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2252 tree decl
= lookup_name (id
);
2254 /* In Objective-C, an instance variable (ivar) may be preferred to
2255 whatever lookup_name() found. */
2256 decl
= objc_lookup_ivar (decl
, id
);
2259 if (decl
&& decl
!= error_mark_node
)
2262 *type
= TREE_TYPE (ref
);
2265 /* Implicit function declaration. */
2266 ref
= implicitly_declare (loc
, id
);
2267 else if (decl
== error_mark_node
)
2268 /* Don't complain about something that's already been
2269 complained about. */
2270 return error_mark_node
;
2273 undeclared_variable (loc
, id
);
2274 return error_mark_node
;
2277 if (TREE_TYPE (ref
) == error_mark_node
)
2278 return error_mark_node
;
2280 if (TREE_DEPRECATED (ref
))
2281 warn_deprecated_use (ref
, NULL_TREE
);
2283 /* Recursive call does not count as usage. */
2284 if (ref
!= current_function_decl
)
2286 TREE_USED (ref
) = 1;
2289 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2291 if (!in_sizeof
&& !in_typeof
)
2292 C_DECL_USED (ref
) = 1;
2293 else if (DECL_INITIAL (ref
) == 0
2294 && DECL_EXTERNAL (ref
)
2295 && !TREE_PUBLIC (ref
))
2296 record_maybe_used_decl (ref
);
2299 if (TREE_CODE (ref
) == CONST_DECL
)
2301 used_types_insert (TREE_TYPE (ref
));
2304 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2305 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2307 warning_at (loc
, OPT_Wc___compat
,
2308 ("enum constant defined in struct or union "
2309 "is not visible in C++"));
2310 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2313 ref
= DECL_INITIAL (ref
);
2314 TREE_CONSTANT (ref
) = 1;
2316 else if (current_function_decl
!= 0
2317 && !DECL_FILE_SCOPE_P (current_function_decl
)
2318 && (TREE_CODE (ref
) == VAR_DECL
2319 || TREE_CODE (ref
) == PARM_DECL
2320 || TREE_CODE (ref
) == FUNCTION_DECL
))
2322 tree context
= decl_function_context (ref
);
2324 if (context
!= 0 && context
!= current_function_decl
)
2325 DECL_NONLOCAL (ref
) = 1;
2327 /* C99 6.7.4p3: An inline definition of a function with external
2328 linkage ... shall not contain a reference to an identifier with
2329 internal linkage. */
2330 else if (current_function_decl
!= 0
2331 && DECL_DECLARED_INLINE_P (current_function_decl
)
2332 && DECL_EXTERNAL (current_function_decl
)
2333 && VAR_OR_FUNCTION_DECL_P (ref
)
2334 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2335 && ! TREE_PUBLIC (ref
)
2336 && DECL_CONTEXT (ref
) != current_function_decl
)
2337 record_inline_static (loc
, current_function_decl
, ref
,
2343 /* Record details of decls possibly used inside sizeof or typeof. */
2344 struct maybe_used_decl
2348 /* The level seen at (in_sizeof + in_typeof). */
2350 /* The next one at this level or above, or NULL. */
2351 struct maybe_used_decl
*next
;
2354 static struct maybe_used_decl
*maybe_used_decls
;
2356 /* Record that DECL, an undefined static function reference seen
2357 inside sizeof or typeof, might be used if the operand of sizeof is
2358 a VLA type or the operand of typeof is a variably modified
2362 record_maybe_used_decl (tree decl
)
2364 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2366 t
->level
= in_sizeof
+ in_typeof
;
2367 t
->next
= maybe_used_decls
;
2368 maybe_used_decls
= t
;
2371 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2372 USED is false, just discard them. If it is true, mark them used
2373 (if no longer inside sizeof or typeof) or move them to the next
2374 level up (if still inside sizeof or typeof). */
2377 pop_maybe_used (bool used
)
2379 struct maybe_used_decl
*p
= maybe_used_decls
;
2380 int cur_level
= in_sizeof
+ in_typeof
;
2381 while (p
&& p
->level
> cur_level
)
2386 C_DECL_USED (p
->decl
) = 1;
2388 p
->level
= cur_level
;
2392 if (!used
|| cur_level
== 0)
2393 maybe_used_decls
= p
;
2396 /* Return the result of sizeof applied to EXPR. */
2399 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2402 if (expr
.value
== error_mark_node
)
2404 ret
.value
= error_mark_node
;
2405 ret
.original_code
= ERROR_MARK
;
2406 ret
.original_type
= NULL
;
2407 pop_maybe_used (false);
2411 bool expr_const_operands
= true;
2412 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2413 &expr_const_operands
);
2414 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2415 ret
.original_code
= ERROR_MARK
;
2416 ret
.original_type
= NULL
;
2417 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2419 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2420 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2421 folded_expr
, ret
.value
);
2422 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2423 SET_EXPR_LOCATION (ret
.value
, loc
);
2425 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2430 /* Return the result of sizeof applied to T, a structure for the type
2431 name passed to sizeof (rather than the type itself). LOC is the
2432 location of the original expression. */
2435 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2439 tree type_expr
= NULL_TREE
;
2440 bool type_expr_const
= true;
2441 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2442 ret
.value
= c_sizeof (loc
, type
);
2443 ret
.original_code
= ERROR_MARK
;
2444 ret
.original_type
= NULL
;
2445 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2446 && c_vla_type_p (type
))
2448 /* If the type is a [*] array, it is a VLA but is represented as
2449 having a size of zero. In such a case we must ensure that
2450 the result of sizeof does not get folded to a constant by
2451 c_fully_fold, because if the size is evaluated the result is
2452 not constant and so constraints on zero or negative size
2453 arrays must not be applied when this sizeof call is inside
2454 another array declarator. */
2456 type_expr
= integer_zero_node
;
2457 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2458 type_expr
, ret
.value
);
2459 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2461 pop_maybe_used (type
!= error_mark_node
2462 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2466 /* Build a function call to function FUNCTION with parameters PARAMS.
2467 The function call is at LOC.
2468 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2469 TREE_VALUE of each node is a parameter-expression.
2470 FUNCTION's data type may be a function type or a pointer-to-function. */
2473 build_function_call (location_t loc
, tree function
, tree params
)
2478 vec
= VEC_alloc (tree
, gc
, list_length (params
));
2479 for (; params
; params
= TREE_CHAIN (params
))
2480 VEC_quick_push (tree
, vec
, TREE_VALUE (params
));
2481 ret
= build_function_call_vec (loc
, function
, vec
, NULL
);
2482 VEC_free (tree
, gc
, vec
);
2486 /* Build a function call to function FUNCTION with parameters PARAMS.
2487 ORIGTYPES, if not NULL, is a vector of types; each element is
2488 either NULL or the original type of the corresponding element in
2489 PARAMS. The original type may differ from TREE_TYPE of the
2490 parameter for enums. FUNCTION's data type may be a function type
2491 or pointer-to-function. This function changes the elements of
2495 build_function_call_vec (location_t loc
, tree function
, VEC(tree
,gc
) *params
,
2496 VEC(tree
,gc
) *origtypes
)
2498 tree fntype
, fundecl
= 0;
2499 tree name
= NULL_TREE
, result
;
2505 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2506 STRIP_TYPE_NOPS (function
);
2508 /* Convert anything with function type to a pointer-to-function. */
2509 if (TREE_CODE (function
) == FUNCTION_DECL
)
2511 /* Implement type-directed function overloading for builtins.
2512 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2513 handle all the type checking. The result is a complete expression
2514 that implements this function call. */
2515 tem
= resolve_overloaded_builtin (loc
, function
, params
);
2519 name
= DECL_NAME (function
);
2522 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2523 function
= function_to_pointer_conversion (loc
, function
);
2525 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2526 expressions, like those used for ObjC messenger dispatches. */
2527 if (!VEC_empty (tree
, params
))
2528 function
= objc_rewrite_function_call (function
,
2529 VEC_index (tree
, params
, 0));
2531 function
= c_fully_fold (function
, false, NULL
);
2533 fntype
= TREE_TYPE (function
);
2535 if (TREE_CODE (fntype
) == ERROR_MARK
)
2536 return error_mark_node
;
2538 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2539 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2541 error_at (loc
, "called object %qE is not a function", function
);
2542 return error_mark_node
;
2545 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2546 current_function_returns_abnormally
= 1;
2548 /* fntype now gets the type of function pointed to. */
2549 fntype
= TREE_TYPE (fntype
);
2551 /* Convert the parameters to the types declared in the
2552 function prototype, or apply default promotions. */
2554 nargs
= convert_arguments (TYPE_ARG_TYPES (fntype
), params
, origtypes
,
2557 return error_mark_node
;
2559 /* Check that the function is called through a compatible prototype.
2560 If it is not, replace the call by a trap, wrapped up in a compound
2561 expression if necessary. This has the nice side-effect to prevent
2562 the tree-inliner from generating invalid assignment trees which may
2563 blow up in the RTL expander later. */
2564 if (CONVERT_EXPR_P (function
)
2565 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2566 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2567 && !comptypes (fntype
, TREE_TYPE (tem
)))
2569 tree return_type
= TREE_TYPE (fntype
);
2570 tree trap
= build_function_call (loc
, built_in_decls
[BUILT_IN_TRAP
],
2574 /* This situation leads to run-time undefined behavior. We can't,
2575 therefore, simply error unless we can prove that all possible
2576 executions of the program must execute the code. */
2577 if (warning_at (loc
, 0, "function called through a non-compatible type"))
2578 /* We can, however, treat "undefined" any way we please.
2579 Call abort to encourage the user to fix the program. */
2580 inform (loc
, "if this code is reached, the program will abort");
2581 /* Before the abort, allow the function arguments to exit or
2583 for (i
= 0; i
< nargs
; i
++)
2584 trap
= build2 (COMPOUND_EXPR
, void_type_node
,
2585 VEC_index (tree
, params
, i
), trap
);
2587 if (VOID_TYPE_P (return_type
))
2589 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2591 "function with qualified void return type called");
2598 if (AGGREGATE_TYPE_P (return_type
))
2599 rhs
= build_compound_literal (loc
, return_type
,
2600 build_constructor (return_type
, 0),
2603 rhs
= fold_convert_loc (loc
, return_type
, integer_zero_node
);
2605 return require_complete_type (build2 (COMPOUND_EXPR
, return_type
,
2610 argarray
= VEC_address (tree
, params
);
2612 /* Check that arguments to builtin functions match the expectations. */
2614 && DECL_BUILT_IN (fundecl
)
2615 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2616 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2617 return error_mark_node
;
2619 /* Check that the arguments to the function are valid. */
2620 check_function_arguments (TYPE_ATTRIBUTES (fntype
), nargs
, argarray
,
2621 TYPE_ARG_TYPES (fntype
));
2623 if (name
!= NULL_TREE
2624 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2626 if (require_constant_value
)
2628 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2629 function
, nargs
, argarray
);
2631 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2632 function
, nargs
, argarray
);
2633 if (TREE_CODE (result
) == NOP_EXPR
2634 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2635 STRIP_TYPE_NOPS (result
);
2638 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2639 function
, nargs
, argarray
);
2641 if (VOID_TYPE_P (TREE_TYPE (result
)))
2643 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2645 "function with qualified void return type called");
2648 return require_complete_type (result
);
2651 /* Convert the argument expressions in the vector VALUES
2652 to the types in the list TYPELIST.
2654 If TYPELIST is exhausted, or when an element has NULL as its type,
2655 perform the default conversions.
2657 ORIGTYPES is the original types of the expressions in VALUES. This
2658 holds the type of enum values which have been converted to integral
2659 types. It may be NULL.
2661 FUNCTION is a tree for the called function. It is used only for
2662 error messages, where it is formatted with %qE.
2664 This is also where warnings about wrong number of args are generated.
2666 Returns the actual number of arguments processed (which may be less
2667 than the length of VALUES in some error situations), or -1 on
2671 convert_arguments (tree typelist
, VEC(tree
,gc
) *values
,
2672 VEC(tree
,gc
) *origtypes
, tree function
, tree fundecl
)
2675 unsigned int parmnum
;
2676 const bool type_generic
= fundecl
2677 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl
)));
2678 bool type_generic_remove_excess_precision
= false;
2681 /* Change pointer to function to the function itself for
2683 if (TREE_CODE (function
) == ADDR_EXPR
2684 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
2685 function
= TREE_OPERAND (function
, 0);
2687 /* Handle an ObjC selector specially for diagnostics. */
2688 selector
= objc_message_selector ();
2690 /* For type-generic built-in functions, determine whether excess
2691 precision should be removed (classification) or not
2694 && DECL_BUILT_IN (fundecl
)
2695 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
2697 switch (DECL_FUNCTION_CODE (fundecl
))
2699 case BUILT_IN_ISFINITE
:
2700 case BUILT_IN_ISINF
:
2701 case BUILT_IN_ISINF_SIGN
:
2702 case BUILT_IN_ISNAN
:
2703 case BUILT_IN_ISNORMAL
:
2704 case BUILT_IN_FPCLASSIFY
:
2705 type_generic_remove_excess_precision
= true;
2709 type_generic_remove_excess_precision
= false;
2714 /* Scan the given expressions and types, producing individual
2715 converted arguments. */
2717 for (typetail
= typelist
, parmnum
= 0;
2718 VEC_iterate (tree
, values
, parmnum
, val
);
2721 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2722 tree valtype
= TREE_TYPE (val
);
2723 tree rname
= function
;
2724 int argnum
= parmnum
+ 1;
2725 const char *invalid_func_diag
;
2726 bool excess_precision
= false;
2730 if (type
== void_type_node
)
2732 error ("too many arguments to function %qE", function
);
2736 if (selector
&& argnum
> 2)
2742 npc
= null_pointer_constant_p (val
);
2744 /* If there is excess precision and a prototype, convert once to
2745 the required type rather than converting via the semantic
2746 type. Likewise without a prototype a float value represented
2747 as long double should be converted once to double. But for
2748 type-generic classification functions excess precision must
2750 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
2751 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
2753 val
= TREE_OPERAND (val
, 0);
2754 excess_precision
= true;
2756 val
= c_fully_fold (val
, false, NULL
);
2757 STRIP_TYPE_NOPS (val
);
2759 val
= require_complete_type (val
);
2763 /* Formal parm type is specified by a function prototype. */
2765 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
2767 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
2774 /* Optionally warn about conversions that
2775 differ from the default conversions. */
2776 if (warn_traditional_conversion
|| warn_traditional
)
2778 unsigned int formal_prec
= TYPE_PRECISION (type
);
2780 if (INTEGRAL_TYPE_P (type
)
2781 && TREE_CODE (valtype
) == REAL_TYPE
)
2782 warning (0, "passing argument %d of %qE as integer "
2783 "rather than floating due to prototype",
2785 if (INTEGRAL_TYPE_P (type
)
2786 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
2787 warning (0, "passing argument %d of %qE as integer "
2788 "rather than complex due to prototype",
2790 else if (TREE_CODE (type
) == COMPLEX_TYPE
2791 && TREE_CODE (valtype
) == REAL_TYPE
)
2792 warning (0, "passing argument %d of %qE as complex "
2793 "rather than floating due to prototype",
2795 else if (TREE_CODE (type
) == REAL_TYPE
2796 && INTEGRAL_TYPE_P (valtype
))
2797 warning (0, "passing argument %d of %qE as floating "
2798 "rather than integer due to prototype",
2800 else if (TREE_CODE (type
) == COMPLEX_TYPE
2801 && INTEGRAL_TYPE_P (valtype
))
2802 warning (0, "passing argument %d of %qE as complex "
2803 "rather than integer due to prototype",
2805 else if (TREE_CODE (type
) == REAL_TYPE
2806 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
2807 warning (0, "passing argument %d of %qE as floating "
2808 "rather than complex due to prototype",
2810 /* ??? At some point, messages should be written about
2811 conversions between complex types, but that's too messy
2813 else if (TREE_CODE (type
) == REAL_TYPE
2814 && TREE_CODE (valtype
) == REAL_TYPE
)
2816 /* Warn if any argument is passed as `float',
2817 since without a prototype it would be `double'. */
2818 if (formal_prec
== TYPE_PRECISION (float_type_node
)
2819 && type
!= dfloat32_type_node
)
2820 warning (0, "passing argument %d of %qE as %<float%> "
2821 "rather than %<double%> due to prototype",
2824 /* Warn if mismatch between argument and prototype
2825 for decimal float types. Warn of conversions with
2826 binary float types and of precision narrowing due to
2828 else if (type
!= valtype
2829 && (type
== dfloat32_type_node
2830 || type
== dfloat64_type_node
2831 || type
== dfloat128_type_node
2832 || valtype
== dfloat32_type_node
2833 || valtype
== dfloat64_type_node
2834 || valtype
== dfloat128_type_node
)
2836 <= TYPE_PRECISION (valtype
)
2837 || (type
== dfloat128_type_node
2839 != dfloat64_type_node
2841 != dfloat32_type_node
)))
2842 || (type
== dfloat64_type_node
2844 != dfloat32_type_node
))))
2845 warning (0, "passing argument %d of %qE as %qT "
2846 "rather than %qT due to prototype",
2847 argnum
, rname
, type
, valtype
);
2850 /* Detect integer changing in width or signedness.
2851 These warnings are only activated with
2852 -Wtraditional-conversion, not with -Wtraditional. */
2853 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
2854 && INTEGRAL_TYPE_P (valtype
))
2856 tree would_have_been
= default_conversion (val
);
2857 tree type1
= TREE_TYPE (would_have_been
);
2859 if (TREE_CODE (type
) == ENUMERAL_TYPE
2860 && (TYPE_MAIN_VARIANT (type
)
2861 == TYPE_MAIN_VARIANT (valtype
)))
2862 /* No warning if function asks for enum
2863 and the actual arg is that enum type. */
2865 else if (formal_prec
!= TYPE_PRECISION (type1
))
2866 warning (OPT_Wtraditional_conversion
,
2867 "passing argument %d of %qE "
2868 "with different width due to prototype",
2870 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
2872 /* Don't complain if the formal parameter type
2873 is an enum, because we can't tell now whether
2874 the value was an enum--even the same enum. */
2875 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
2877 else if (TREE_CODE (val
) == INTEGER_CST
2878 && int_fits_type_p (val
, type
))
2879 /* Change in signedness doesn't matter
2880 if a constant value is unaffected. */
2882 /* If the value is extended from a narrower
2883 unsigned type, it doesn't matter whether we
2884 pass it as signed or unsigned; the value
2885 certainly is the same either way. */
2886 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
2887 && TYPE_UNSIGNED (valtype
))
2889 else if (TYPE_UNSIGNED (type
))
2890 warning (OPT_Wtraditional_conversion
,
2891 "passing argument %d of %qE "
2892 "as unsigned due to prototype",
2895 warning (OPT_Wtraditional_conversion
,
2896 "passing argument %d of %qE "
2897 "as signed due to prototype", argnum
, rname
);
2901 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2902 sake of better warnings from convert_and_check. */
2903 if (excess_precision
)
2904 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
2905 origtype
= (origtypes
== NULL
2907 : VEC_index (tree
, origtypes
, parmnum
));
2908 parmval
= convert_for_assignment (input_location
, type
, val
,
2909 origtype
, ic_argpass
, npc
,
2913 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
2914 && INTEGRAL_TYPE_P (type
)
2915 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2916 parmval
= default_conversion (parmval
);
2919 else if (TREE_CODE (valtype
) == REAL_TYPE
2920 && (TYPE_PRECISION (valtype
)
2921 < TYPE_PRECISION (double_type_node
))
2922 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
2927 /* Convert `float' to `double'. */
2928 parmval
= convert (double_type_node
, val
);
2930 else if (excess_precision
&& !type_generic
)
2931 /* A "double" argument with excess precision being passed
2932 without a prototype or in variable arguments. */
2933 parmval
= convert (valtype
, val
);
2934 else if ((invalid_func_diag
=
2935 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
2937 error (invalid_func_diag
);
2941 /* Convert `short' and `char' to full-size `int'. */
2942 parmval
= default_conversion (val
);
2944 VEC_replace (tree
, values
, parmnum
, parmval
);
2947 typetail
= TREE_CHAIN (typetail
);
2950 gcc_assert (parmnum
== VEC_length (tree
, values
));
2952 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
2954 error ("too few arguments to function %qE", function
);
2961 /* This is the entry point used by the parser to build unary operators
2962 in the input. CODE, a tree_code, specifies the unary operator, and
2963 ARG is the operand. For unary plus, the C parser currently uses
2964 CONVERT_EXPR for code.
2966 LOC is the location to use for the tree generated.
2970 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
2972 struct c_expr result
;
2974 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
2975 result
.original_code
= code
;
2976 result
.original_type
= NULL
;
2978 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
2979 overflow_warning (loc
, result
.value
);
2984 /* This is the entry point used by the parser to build binary operators
2985 in the input. CODE, a tree_code, specifies the binary operator, and
2986 ARG1 and ARG2 are the operands. In addition to constructing the
2987 expression, we check for operands that were written with other binary
2988 operators in a way that is likely to confuse the user.
2990 LOCATION is the location of the binary operator. */
2993 parser_build_binary_op (location_t location
, enum tree_code code
,
2994 struct c_expr arg1
, struct c_expr arg2
)
2996 struct c_expr result
;
2998 enum tree_code code1
= arg1
.original_code
;
2999 enum tree_code code2
= arg2
.original_code
;
3000 tree type1
= (arg1
.original_type
3001 ? arg1
.original_type
3002 : TREE_TYPE (arg1
.value
));
3003 tree type2
= (arg2
.original_type
3004 ? arg2
.original_type
3005 : TREE_TYPE (arg2
.value
));
3007 result
.value
= build_binary_op (location
, code
,
3008 arg1
.value
, arg2
.value
, 1);
3009 result
.original_code
= code
;
3010 result
.original_type
= NULL
;
3012 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3015 if (location
!= UNKNOWN_LOCATION
)
3016 protected_set_expr_location (result
.value
, location
);
3018 /* Check for cases such as x+y<<z which users are likely
3020 if (warn_parentheses
)
3021 warn_about_parentheses (code
, code1
, arg1
.value
, code2
, arg2
.value
);
3023 if (warn_logical_op
)
3024 warn_logical_operator (input_location
, code
, TREE_TYPE (result
.value
),
3025 code1
, arg1
.value
, code2
, arg2
.value
);
3027 /* Warn about comparisons against string literals, with the exception
3028 of testing for equality or inequality of a string literal with NULL. */
3029 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3031 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3032 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3033 warning_at (location
, OPT_Waddress
,
3034 "comparison with string literal results in unspecified behavior");
3036 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3037 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3038 warning_at (location
, OPT_Waddress
,
3039 "comparison with string literal results in unspecified behavior");
3041 if (TREE_OVERFLOW_P (result
.value
)
3042 && !TREE_OVERFLOW_P (arg1
.value
)
3043 && !TREE_OVERFLOW_P (arg2
.value
))
3044 overflow_warning (location
, result
.value
);
3046 /* Warn about comparisons of different enum types. */
3047 if (warn_enum_compare
3048 && TREE_CODE_CLASS (code
) == tcc_comparison
3049 && TREE_CODE (type1
) == ENUMERAL_TYPE
3050 && TREE_CODE (type2
) == ENUMERAL_TYPE
3051 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3052 warning_at (location
, OPT_Wenum_compare
,
3053 "comparison between %qT and %qT",
3059 /* Return a tree for the difference of pointers OP0 and OP1.
3060 The resulting tree has type int. */
3063 pointer_diff (location_t loc
, tree op0
, tree op1
)
3065 tree restype
= ptrdiff_type_node
;
3067 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3068 tree con0
, con1
, lit0
, lit1
;
3069 tree orig_op1
= op1
;
3071 if (TREE_CODE (target_type
) == VOID_TYPE
)
3072 pedwarn (loc
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3073 "pointer of type %<void *%> used in subtraction");
3074 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3075 pedwarn (loc
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3076 "pointer to a function used in subtraction");
3078 /* If the conversion to ptrdiff_type does anything like widening or
3079 converting a partial to an integral mode, we get a convert_expression
3080 that is in the way to do any simplifications.
3081 (fold-const.c doesn't know that the extra bits won't be needed.
3082 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3083 different mode in place.)
3084 So first try to find a common term here 'by hand'; we want to cover
3085 at least the cases that occur in legal static initializers. */
3086 if (CONVERT_EXPR_P (op0
)
3087 && (TYPE_PRECISION (TREE_TYPE (op0
))
3088 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))))
3089 con0
= TREE_OPERAND (op0
, 0);
3092 if (CONVERT_EXPR_P (op1
)
3093 && (TYPE_PRECISION (TREE_TYPE (op1
))
3094 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))))
3095 con1
= TREE_OPERAND (op1
, 0);
3099 if (TREE_CODE (con0
) == PLUS_EXPR
)
3101 lit0
= TREE_OPERAND (con0
, 1);
3102 con0
= TREE_OPERAND (con0
, 0);
3105 lit0
= integer_zero_node
;
3107 if (TREE_CODE (con1
) == PLUS_EXPR
)
3109 lit1
= TREE_OPERAND (con1
, 1);
3110 con1
= TREE_OPERAND (con1
, 0);
3113 lit1
= integer_zero_node
;
3115 if (operand_equal_p (con0
, con1
, 0))
3122 /* First do the subtraction as integers;
3123 then drop through to build the divide operator.
3124 Do not do default conversions on the minus operator
3125 in case restype is a short type. */
3127 op0
= build_binary_op (loc
,
3128 MINUS_EXPR
, convert (restype
, op0
),
3129 convert (restype
, op1
), 0);
3130 /* This generates an error if op1 is pointer to incomplete type. */
3131 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3132 error_at (loc
, "arithmetic on pointer to an incomplete type");
3134 /* This generates an error if op0 is pointer to incomplete type. */
3135 op1
= c_size_in_bytes (target_type
);
3137 /* Divide by the size, in easiest possible way. */
3138 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, restype
,
3139 op0
, convert (restype
, op1
));
3142 /* Construct and perhaps optimize a tree representation
3143 for a unary operation. CODE, a tree_code, specifies the operation
3144 and XARG is the operand.
3145 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3146 the default promotions (such as from short to int).
3147 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3148 allows non-lvalues; this is only used to handle conversion of non-lvalue
3149 arrays to pointers in C99.
3151 LOCATION is the location of the operator. */
3154 build_unary_op (location_t location
,
3155 enum tree_code code
, tree xarg
, int flag
)
3157 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3160 enum tree_code typecode
;
3162 tree ret
= error_mark_node
;
3163 tree eptype
= NULL_TREE
;
3164 int noconvert
= flag
;
3165 const char *invalid_op_diag
;
3168 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3170 arg
= remove_c_maybe_const_expr (arg
);
3172 if (code
!= ADDR_EXPR
)
3173 arg
= require_complete_type (arg
);
3175 typecode
= TREE_CODE (TREE_TYPE (arg
));
3176 if (typecode
== ERROR_MARK
)
3177 return error_mark_node
;
3178 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3179 typecode
= INTEGER_TYPE
;
3181 if ((invalid_op_diag
3182 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3184 error_at (location
, invalid_op_diag
);
3185 return error_mark_node
;
3188 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3190 eptype
= TREE_TYPE (arg
);
3191 arg
= TREE_OPERAND (arg
, 0);
3197 /* This is used for unary plus, because a CONVERT_EXPR
3198 is enough to prevent anybody from looking inside for
3199 associativity, but won't generate any code. */
3200 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3201 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3202 || typecode
== VECTOR_TYPE
))
3204 error_at (location
, "wrong type argument to unary plus");
3205 return error_mark_node
;
3207 else if (!noconvert
)
3208 arg
= default_conversion (arg
);
3209 arg
= non_lvalue_loc (location
, arg
);
3213 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3214 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3215 || typecode
== VECTOR_TYPE
))
3217 error_at (location
, "wrong type argument to unary minus");
3218 return error_mark_node
;
3220 else if (!noconvert
)
3221 arg
= default_conversion (arg
);
3225 /* ~ works on integer types and non float vectors. */
3226 if (typecode
== INTEGER_TYPE
3227 || (typecode
== VECTOR_TYPE
3228 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3231 arg
= default_conversion (arg
);
3233 else if (typecode
== COMPLEX_TYPE
)
3236 pedwarn (location
, OPT_pedantic
,
3237 "ISO C does not support %<~%> for complex conjugation");
3239 arg
= default_conversion (arg
);
3243 error_at (location
, "wrong type argument to bit-complement");
3244 return error_mark_node
;
3249 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3251 error_at (location
, "wrong type argument to abs");
3252 return error_mark_node
;
3254 else if (!noconvert
)
3255 arg
= default_conversion (arg
);
3259 /* Conjugating a real value is a no-op, but allow it anyway. */
3260 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3261 || typecode
== COMPLEX_TYPE
))
3263 error_at (location
, "wrong type argument to conjugation");
3264 return error_mark_node
;
3266 else if (!noconvert
)
3267 arg
= default_conversion (arg
);
3270 case TRUTH_NOT_EXPR
:
3271 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3272 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3273 && typecode
!= COMPLEX_TYPE
)
3276 "wrong type argument to unary exclamation mark");
3277 return error_mark_node
;
3279 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3280 ret
= invert_truthvalue_loc (location
, arg
);
3281 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3282 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3283 location
= EXPR_LOCATION (ret
);
3284 goto return_build_unary_op
;
3287 if (TREE_CODE (arg
) == COMPLEX_CST
)
3288 ret
= TREE_REALPART (arg
);
3289 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3290 ret
= fold_build1_loc (location
,
3291 REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
);
3294 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3295 eptype
= TREE_TYPE (eptype
);
3296 goto return_build_unary_op
;
3299 if (TREE_CODE (arg
) == COMPLEX_CST
)
3300 ret
= TREE_IMAGPART (arg
);
3301 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3302 ret
= fold_build1_loc (location
,
3303 IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
);
3305 ret
= omit_one_operand_loc (location
, TREE_TYPE (arg
),
3306 integer_zero_node
, arg
);
3307 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3308 eptype
= TREE_TYPE (eptype
);
3309 goto return_build_unary_op
;
3311 case PREINCREMENT_EXPR
:
3312 case POSTINCREMENT_EXPR
:
3313 case PREDECREMENT_EXPR
:
3314 case POSTDECREMENT_EXPR
:
3316 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3318 tree inner
= build_unary_op (location
, code
,
3319 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3320 if (inner
== error_mark_node
)
3321 return error_mark_node
;
3322 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3323 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3324 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3325 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3326 goto return_build_unary_op
;
3329 /* Complain about anything that is not a true lvalue. */
3330 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
3331 || code
== POSTINCREMENT_EXPR
)
3334 return error_mark_node
;
3336 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3338 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3339 warning_at (location
, OPT_Wc___compat
,
3340 "increment of enumeration value is invalid in C++");
3342 warning_at (location
, OPT_Wc___compat
,
3343 "decrement of enumeration value is invalid in C++");
3346 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3347 arg
= c_fully_fold (arg
, false, NULL
);
3349 /* Increment or decrement the real part of the value,
3350 and don't change the imaginary part. */
3351 if (typecode
== COMPLEX_TYPE
)
3355 pedwarn (location
, OPT_pedantic
,
3356 "ISO C does not support %<++%> and %<--%> on complex types");
3358 arg
= stabilize_reference (arg
);
3359 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
3360 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
3361 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
3362 if (real
== error_mark_node
|| imag
== error_mark_node
)
3363 return error_mark_node
;
3364 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
3366 goto return_build_unary_op
;
3369 /* Report invalid types. */
3371 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3372 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
3374 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3375 error_at (location
, "wrong type argument to increment");
3377 error_at (location
, "wrong type argument to decrement");
3379 return error_mark_node
;
3385 argtype
= TREE_TYPE (arg
);
3387 /* Compute the increment. */
3389 if (typecode
== POINTER_TYPE
)
3391 /* If pointer target is an undefined struct,
3392 we just cannot know how to do the arithmetic. */
3393 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
3395 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3397 "increment of pointer to unknown structure");
3400 "decrement of pointer to unknown structure");
3402 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
3403 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
3405 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3406 pedwarn (location
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3407 "wrong type argument to increment");
3409 pedwarn (location
, pedantic
? OPT_pedantic
: OPT_Wpointer_arith
,
3410 "wrong type argument to decrement");
3413 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
3414 inc
= fold_convert_loc (location
, sizetype
, inc
);
3416 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
3418 /* For signed fract types, we invert ++ to -- or
3419 -- to ++, and change inc from 1 to -1, because
3420 it is not possible to represent 1 in signed fract constants.
3421 For unsigned fract types, the result always overflows and
3422 we get an undefined (original) or the maximum value. */
3423 if (code
== PREINCREMENT_EXPR
)
3424 code
= PREDECREMENT_EXPR
;
3425 else if (code
== PREDECREMENT_EXPR
)
3426 code
= PREINCREMENT_EXPR
;
3427 else if (code
== POSTINCREMENT_EXPR
)
3428 code
= POSTDECREMENT_EXPR
;
3429 else /* code == POSTDECREMENT_EXPR */
3430 code
= POSTINCREMENT_EXPR
;
3432 inc
= integer_minus_one_node
;
3433 inc
= convert (argtype
, inc
);
3437 inc
= integer_one_node
;
3438 inc
= convert (argtype
, inc
);
3441 /* Report a read-only lvalue. */
3442 if (TYPE_READONLY (argtype
))
3444 readonly_error (arg
,
3445 ((code
== PREINCREMENT_EXPR
3446 || code
== POSTINCREMENT_EXPR
)
3447 ? lv_increment
: lv_decrement
));
3448 return error_mark_node
;
3450 else if (TREE_READONLY (arg
))
3451 readonly_warning (arg
,
3452 ((code
== PREINCREMENT_EXPR
3453 || code
== POSTINCREMENT_EXPR
)
3454 ? lv_increment
: lv_decrement
));
3456 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3457 val
= boolean_increment (code
, arg
);
3459 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
3460 TREE_SIDE_EFFECTS (val
) = 1;
3461 if (TREE_CODE (val
) != code
)
3462 TREE_NO_WARNING (val
) = 1;
3464 goto return_build_unary_op
;
3468 /* Note that this operation never does default_conversion. */
3470 /* The operand of unary '&' must be an lvalue (which excludes
3471 expressions of type void), or, in C99, the result of a [] or
3472 unary '*' operator. */
3473 if (VOID_TYPE_P (TREE_TYPE (arg
))
3474 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
3475 && (TREE_CODE (arg
) != INDIRECT_REF
3477 pedwarn (location
, 0, "taking address of expression of type %<void%>");
3479 /* Let &* cancel out to simplify resulting code. */
3480 if (TREE_CODE (arg
) == INDIRECT_REF
)
3482 /* Don't let this be an lvalue. */
3483 if (lvalue_p (TREE_OPERAND (arg
, 0)))
3484 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
3485 ret
= TREE_OPERAND (arg
, 0);
3486 goto return_build_unary_op
;
3489 /* For &x[y], return x+y */
3490 if (TREE_CODE (arg
) == ARRAY_REF
)
3492 tree op0
= TREE_OPERAND (arg
, 0);
3493 if (!c_mark_addressable (op0
))
3494 return error_mark_node
;
3495 return build_binary_op (location
, PLUS_EXPR
,
3496 (TREE_CODE (TREE_TYPE (op0
)) == ARRAY_TYPE
3497 ? array_to_pointer_conversion (location
,
3500 TREE_OPERAND (arg
, 1), 1);
3503 /* Anything not already handled and not a true memory reference
3504 or a non-lvalue array is an error. */
3505 else if (typecode
!= FUNCTION_TYPE
&& !flag
3506 && !lvalue_or_else (arg
, lv_addressof
))
3507 return error_mark_node
;
3509 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3511 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3513 tree inner
= build_unary_op (location
, code
,
3514 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3515 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3516 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3517 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3518 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
3519 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
3520 goto return_build_unary_op
;
3523 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3524 argtype
= TREE_TYPE (arg
);
3526 /* If the lvalue is const or volatile, merge that into the type
3527 to which the address will point. Note that you can't get a
3528 restricted pointer by taking the address of something, so we
3529 only have to deal with `const' and `volatile' here. */
3530 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
3531 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
)))
3532 argtype
= c_build_type_variant (argtype
,
3533 TREE_READONLY (arg
),
3534 TREE_THIS_VOLATILE (arg
));
3536 if (!c_mark_addressable (arg
))
3537 return error_mark_node
;
3539 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
3540 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
3542 argtype
= build_pointer_type (argtype
);
3544 /* ??? Cope with user tricks that amount to offsetof. Delete this
3545 when we have proper support for integer constant expressions. */
3546 val
= get_base_address (arg
);
3547 if (val
&& TREE_CODE (val
) == INDIRECT_REF
3548 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
3550 tree op0
= fold_convert_loc (location
, sizetype
,
3551 fold_offsetof (arg
, val
)), op1
;
3553 op1
= fold_convert_loc (location
, argtype
, TREE_OPERAND (val
, 0));
3554 ret
= fold_build2_loc (location
, POINTER_PLUS_EXPR
, argtype
, op1
, op0
);
3555 goto return_build_unary_op
;
3558 val
= build1 (ADDR_EXPR
, argtype
, arg
);
3561 goto return_build_unary_op
;
3568 argtype
= TREE_TYPE (arg
);
3569 if (TREE_CODE (arg
) == INTEGER_CST
)
3570 ret
= (require_constant_value
3571 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
3572 : fold_build1_loc (location
, code
, argtype
, arg
));
3574 ret
= build1 (code
, argtype
, arg
);
3575 return_build_unary_op
:
3576 gcc_assert (ret
!= error_mark_node
);
3577 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
3578 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
3579 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
3580 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
3581 ret
= note_integer_operands (ret
);
3583 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
3584 protected_set_expr_location (ret
, location
);
3588 /* Return nonzero if REF is an lvalue valid for this language.
3589 Lvalues can be assigned, unless their type has TYPE_READONLY.
3590 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3593 lvalue_p (const_tree ref
)
3595 const enum tree_code code
= TREE_CODE (ref
);
3602 return lvalue_p (TREE_OPERAND (ref
, 0));
3604 case C_MAYBE_CONST_EXPR
:
3605 return lvalue_p (TREE_OPERAND (ref
, 1));
3607 case COMPOUND_LITERAL_EXPR
:
3617 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
3618 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
3621 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
3628 /* Give an error for storing in something that is 'const'. */
3631 readonly_error (tree arg
, enum lvalue_use use
)
3633 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
3635 /* Using this macro rather than (for example) arrays of messages
3636 ensures that all the format strings are checked at compile
3638 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3639 : (use == lv_increment ? (I) \
3640 : (use == lv_decrement ? (D) : (AS))))
3641 if (TREE_CODE (arg
) == COMPONENT_REF
)
3643 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3644 readonly_error (TREE_OPERAND (arg
, 0), use
);
3646 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3647 G_("increment of read-only member %qD"),
3648 G_("decrement of read-only member %qD"),
3649 G_("read-only member %qD used as %<asm%> output")),
3650 TREE_OPERAND (arg
, 1));
3652 else if (TREE_CODE (arg
) == VAR_DECL
)
3653 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3654 G_("increment of read-only variable %qD"),
3655 G_("decrement of read-only variable %qD"),
3656 G_("read-only variable %qD used as %<asm%> output")),
3659 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3660 G_("increment of read-only location %qE"),
3661 G_("decrement of read-only location %qE"),
3662 G_("read-only location %qE used as %<asm%> output")),
3666 /* Give a warning for storing in something that is read-only in GCC
3667 terms but not const in ISO C terms. */
3670 readonly_warning (tree arg
, enum lvalue_use use
)
3675 warning (0, "assignment of read-only location %qE", arg
);
3678 warning (0, "increment of read-only location %qE", arg
);
3681 warning (0, "decrement of read-only location %qE", arg
);
3690 /* Return nonzero if REF is an lvalue valid for this language;
3691 otherwise, print an error message and return zero. USE says
3692 how the lvalue is being used and so selects the error message. */
3695 lvalue_or_else (const_tree ref
, enum lvalue_use use
)
3697 int win
= lvalue_p (ref
);
3705 /* Mark EXP saying that we need to be able to take the
3706 address of it; it should not be allocated in a register.
3707 Returns true if successful. */
3710 c_mark_addressable (tree exp
)
3715 switch (TREE_CODE (x
))
3718 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
3721 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
3725 /* ... fall through ... */
3731 x
= TREE_OPERAND (x
, 0);
3734 case COMPOUND_LITERAL_EXPR
:
3736 TREE_ADDRESSABLE (x
) = 1;
3743 if (C_DECL_REGISTER (x
)
3744 && DECL_NONLOCAL (x
))
3746 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
3749 ("global register variable %qD used in nested function", x
);
3752 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
3754 else if (C_DECL_REGISTER (x
))
3756 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
3757 error ("address of global register variable %qD requested", x
);
3759 error ("address of register variable %qD requested", x
);
3765 TREE_ADDRESSABLE (x
) = 1;
3772 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3773 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3774 if folded to an integer constant then the unselected half may
3775 contain arbitrary operations not normally permitted in constant
3776 expressions. Set the location of the expression to LOC. */
3779 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
3780 tree op1
, tree op1_original_type
, tree op2
,
3781 tree op2_original_type
)
3785 enum tree_code code1
;
3786 enum tree_code code2
;
3787 tree result_type
= NULL
;
3788 tree ep_result_type
= NULL
;
3789 tree orig_op1
= op1
, orig_op2
= op2
;
3790 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
3791 bool ifexp_int_operands
;
3795 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
3796 if (op1_int_operands
)
3797 op1
= remove_c_maybe_const_expr (op1
);
3798 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
3799 if (op2_int_operands
)
3800 op2
= remove_c_maybe_const_expr (op2
);
3801 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
3802 if (ifexp_int_operands
)
3803 ifexp
= remove_c_maybe_const_expr (ifexp
);
3805 /* Promote both alternatives. */
3807 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
3808 op1
= default_conversion (op1
);
3809 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
3810 op2
= default_conversion (op2
);
3812 if (TREE_CODE (ifexp
) == ERROR_MARK
3813 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
3814 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
3815 return error_mark_node
;
3817 type1
= TREE_TYPE (op1
);
3818 code1
= TREE_CODE (type1
);
3819 type2
= TREE_TYPE (op2
);
3820 code2
= TREE_CODE (type2
);
3822 /* C90 does not permit non-lvalue arrays in conditional expressions.
3823 In C99 they will be pointers by now. */
3824 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
3826 error_at (colon_loc
, "non-lvalue array in conditional expression");
3827 return error_mark_node
;
3830 objc_ok
= objc_compare_types (type1
, type2
, -3, NULL_TREE
);
3832 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
3833 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
3834 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3835 || code1
== COMPLEX_TYPE
)
3836 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
3837 || code2
== COMPLEX_TYPE
))
3839 ep_result_type
= c_common_type (type1
, type2
);
3840 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
3842 op1
= TREE_OPERAND (op1
, 0);
3843 type1
= TREE_TYPE (op1
);
3844 gcc_assert (TREE_CODE (type1
) == code1
);
3846 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
3848 op2
= TREE_OPERAND (op2
, 0);
3849 type2
= TREE_TYPE (op2
);
3850 gcc_assert (TREE_CODE (type2
) == code2
);
3854 if (warn_cxx_compat
)
3856 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
3857 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
3859 if (TREE_CODE (t1
) == ENUMERAL_TYPE
3860 && TREE_CODE (t2
) == ENUMERAL_TYPE
3861 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
3862 warning_at (colon_loc
, OPT_Wc___compat
,
3863 ("different enum types in conditional is "
3864 "invalid in C++: %qT vs %qT"),
3868 /* Quickly detect the usual case where op1 and op2 have the same type
3870 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
3873 result_type
= type1
;
3875 result_type
= TYPE_MAIN_VARIANT (type1
);
3877 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3878 || code1
== COMPLEX_TYPE
)
3879 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
3880 || code2
== COMPLEX_TYPE
))
3882 result_type
= c_common_type (type1
, type2
);
3884 /* If -Wsign-compare, warn here if type1 and type2 have
3885 different signedness. We'll promote the signed to unsigned
3886 and later code won't know it used to be different.
3887 Do this check on the original types, so that explicit casts
3888 will be considered, but default promotions won't. */
3889 if (c_inhibit_evaluation_warnings
== 0)
3891 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
3892 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
3894 if (unsigned_op1
^ unsigned_op2
)
3898 /* Do not warn if the result type is signed, since the
3899 signed type will only be chosen if it can represent
3900 all the values of the unsigned type. */
3901 if (!TYPE_UNSIGNED (result_type
))
3905 bool op1_maybe_const
= true;
3906 bool op2_maybe_const
= true;
3908 /* Do not warn if the signed quantity is an
3909 unsuffixed integer literal (or some static
3910 constant expression involving such literals) and
3911 it is non-negative. This warning requires the
3912 operands to be folded for best results, so do
3913 that folding in this case even without
3914 warn_sign_compare to avoid warning options
3915 possibly affecting code generation. */
3916 c_inhibit_evaluation_warnings
3917 += (ifexp
== truthvalue_false_node
);
3918 op1
= c_fully_fold (op1
, require_constant_value
,
3920 c_inhibit_evaluation_warnings
3921 -= (ifexp
== truthvalue_false_node
);
3923 c_inhibit_evaluation_warnings
3924 += (ifexp
== truthvalue_true_node
);
3925 op2
= c_fully_fold (op2
, require_constant_value
,
3927 c_inhibit_evaluation_warnings
3928 -= (ifexp
== truthvalue_true_node
);
3930 if (warn_sign_compare
)
3933 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
3935 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
3938 warning_at (colon_loc
, OPT_Wsign_compare
,
3939 ("signed and unsigned type in "
3940 "conditional expression"));
3942 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
3943 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
3944 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
3945 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
3950 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
3952 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
3953 pedwarn (colon_loc
, OPT_pedantic
,
3954 "ISO C forbids conditional expr with only one void side");
3955 result_type
= void_type_node
;
3957 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
3959 if (comp_target_types (colon_loc
, type1
, type2
))
3960 result_type
= common_pointer_type (type1
, type2
);
3961 else if (null_pointer_constant_p (orig_op1
))
3962 result_type
= qualify_type (type2
, type1
);
3963 else if (null_pointer_constant_p (orig_op2
))
3964 result_type
= qualify_type (type1
, type2
);
3965 else if (VOID_TYPE_P (TREE_TYPE (type1
)))
3967 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
3968 pedwarn (colon_loc
, OPT_pedantic
,
3969 "ISO C forbids conditional expr between "
3970 "%<void *%> and function pointer");
3971 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
3972 TREE_TYPE (type2
)));
3974 else if (VOID_TYPE_P (TREE_TYPE (type2
)))
3976 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
3977 pedwarn (colon_loc
, OPT_pedantic
,
3978 "ISO C forbids conditional expr between "
3979 "%<void *%> and function pointer");
3980 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
3981 TREE_TYPE (type1
)));
3986 pedwarn (colon_loc
, 0,
3987 "pointer type mismatch in conditional expression");
3988 result_type
= build_pointer_type (void_type_node
);
3991 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
3993 if (!null_pointer_constant_p (orig_op2
))
3994 pedwarn (colon_loc
, 0,
3995 "pointer/integer type mismatch in conditional expression");
3998 op2
= null_pointer_node
;
4000 result_type
= type1
;
4002 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4004 if (!null_pointer_constant_p (orig_op1
))
4005 pedwarn (colon_loc
, 0,
4006 "pointer/integer type mismatch in conditional expression");
4009 op1
= null_pointer_node
;
4011 result_type
= type2
;
4016 if (flag_cond_mismatch
)
4017 result_type
= void_type_node
;
4020 error_at (colon_loc
, "type mismatch in conditional expression");
4021 return error_mark_node
;
4025 /* Merge const and volatile flags of the incoming types. */
4027 = build_type_variant (result_type
,
4028 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4029 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4031 if (result_type
!= type1
)
4032 op1
= convert_and_check (result_type
, op1
);
4033 if (result_type
!= type2
)
4034 op2
= convert_and_check (result_type
, op2
);
4036 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4038 op2_int_operands
= true;
4039 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4041 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4043 op1_int_operands
= true;
4044 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4046 int_const
= int_operands
= (ifexp_int_operands
4048 && op2_int_operands
);
4051 int_const
= ((ifexp
== truthvalue_true_node
4052 && TREE_CODE (orig_op1
) == INTEGER_CST
4053 && !TREE_OVERFLOW (orig_op1
))
4054 || (ifexp
== truthvalue_false_node
4055 && TREE_CODE (orig_op2
) == INTEGER_CST
4056 && !TREE_OVERFLOW (orig_op2
)));
4058 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4059 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4062 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4064 ret
= note_integer_operands (ret
);
4067 ret
= build1 (EXCESS_PRECISION_EXPR
, ep_result_type
, ret
);
4069 protected_set_expr_location (ret
, colon_loc
);
4073 /* Return a compound expression that performs two expressions and
4074 returns the value of the second of them.
4076 LOC is the location of the COMPOUND_EXPR. */
4079 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4081 bool expr1_int_operands
, expr2_int_operands
;
4082 tree eptype
= NULL_TREE
;
4085 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4086 if (expr1_int_operands
)
4087 expr1
= remove_c_maybe_const_expr (expr1
);
4088 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4089 if (expr2_int_operands
)
4090 expr2
= remove_c_maybe_const_expr (expr2
);
4092 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4093 expr1
= TREE_OPERAND (expr1
, 0);
4094 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4096 eptype
= TREE_TYPE (expr2
);
4097 expr2
= TREE_OPERAND (expr2
, 0);
4100 if (!TREE_SIDE_EFFECTS (expr1
))
4102 /* The left-hand operand of a comma expression is like an expression
4103 statement: with -Wunused, we should warn if it doesn't have
4104 any side-effects, unless it was explicitly cast to (void). */
4105 if (warn_unused_value
)
4107 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4108 && CONVERT_EXPR_P (expr1
))
4110 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4111 && TREE_CODE (expr1
) == COMPOUND_EXPR
4112 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4113 ; /* (void) a, (void) b, c */
4115 warning_at (loc
, OPT_Wunused_value
,
4116 "left-hand operand of comma expression has no effect");
4120 /* With -Wunused, we should also warn if the left-hand operand does have
4121 side-effects, but computes a value which is not used. For example, in
4122 `foo() + bar(), baz()' the result of the `+' operator is not used,
4123 so we should issue a warning. */
4124 else if (warn_unused_value
)
4125 warn_if_unused_value (expr1
, loc
);
4127 if (expr2
== error_mark_node
)
4128 return error_mark_node
;
4130 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4133 && expr1_int_operands
4134 && expr2_int_operands
)
4135 ret
= note_integer_operands (ret
);
4138 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4140 protected_set_expr_location (ret
, loc
);
4144 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4145 which we are casting. OTYPE is the type of the expression being
4146 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4147 on the command line. */
4150 handle_warn_cast_qual (tree type
, tree otype
)
4152 tree in_type
= type
;
4153 tree in_otype
= otype
;
4158 /* Check that the qualifiers on IN_TYPE are a superset of the
4159 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4160 nodes is uninteresting and we stop as soon as we hit a
4161 non-POINTER_TYPE node on either type. */
4164 in_otype
= TREE_TYPE (in_otype
);
4165 in_type
= TREE_TYPE (in_type
);
4167 /* GNU C allows cv-qualified function types. 'const' means the
4168 function is very pure, 'volatile' means it can't return. We
4169 need to warn when such qualifiers are added, not when they're
4171 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4172 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4173 added
|= (TYPE_QUALS (in_type
) & ~TYPE_QUALS (in_otype
));
4175 discarded
|= (TYPE_QUALS (in_otype
) & ~TYPE_QUALS (in_type
));
4177 while (TREE_CODE (in_type
) == POINTER_TYPE
4178 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4181 warning (OPT_Wcast_qual
, "cast adds new qualifiers to function type");
4184 /* There are qualifiers present in IN_OTYPE that are not present
4186 warning (OPT_Wcast_qual
,
4187 "cast discards qualifiers from pointer target type");
4189 if (added
|| discarded
)
4192 /* A cast from **T to const **T is unsafe, because it can cause a
4193 const value to be changed with no additional warning. We only
4194 issue this warning if T is the same on both sides, and we only
4195 issue the warning if there are the same number of pointers on
4196 both sides, as otherwise the cast is clearly unsafe anyhow. A
4197 cast is unsafe when a qualifier is added at one level and const
4198 is not present at all outer levels.
4200 To issue this warning, we check at each level whether the cast
4201 adds new qualifiers not already seen. We don't need to special
4202 case function types, as they won't have the same
4203 TYPE_MAIN_VARIANT. */
4205 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4207 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4212 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4215 in_type
= TREE_TYPE (in_type
);
4216 in_otype
= TREE_TYPE (in_otype
);
4217 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4220 warning (OPT_Wcast_qual
,
4221 ("new qualifiers in middle of multi-level non-const cast "
4226 is_const
= TYPE_READONLY (in_type
);
4228 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4231 /* Build an expression representing a cast to type TYPE of expression EXPR.
4232 LOC is the location of the cast-- typically the open paren of the cast. */
4235 build_c_cast (location_t loc
, tree type
, tree expr
)
4239 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4240 expr
= TREE_OPERAND (expr
, 0);
4244 if (type
== error_mark_node
|| expr
== error_mark_node
)
4245 return error_mark_node
;
4247 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4248 only in <protocol> qualifications. But when constructing cast expressions,
4249 the protocols do matter and must be kept around. */
4250 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4251 return build1 (NOP_EXPR
, type
, expr
);
4253 type
= TYPE_MAIN_VARIANT (type
);
4255 if (TREE_CODE (type
) == ARRAY_TYPE
)
4257 error_at (loc
, "cast specifies array type");
4258 return error_mark_node
;
4261 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4263 error_at (loc
, "cast specifies function type");
4264 return error_mark_node
;
4267 if (!VOID_TYPE_P (type
))
4269 value
= require_complete_type (value
);
4270 if (value
== error_mark_node
)
4271 return error_mark_node
;
4274 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4276 if (TREE_CODE (type
) == RECORD_TYPE
4277 || TREE_CODE (type
) == UNION_TYPE
)
4278 pedwarn (loc
, OPT_pedantic
,
4279 "ISO C forbids casting nonscalar to the same type");
4281 else if (TREE_CODE (type
) == UNION_TYPE
)
4285 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
4286 if (TREE_TYPE (field
) != error_mark_node
4287 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4288 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4295 pedwarn (loc
, OPT_pedantic
, "ISO C forbids casts to union type");
4296 t
= digest_init (loc
, type
,
4297 build_constructor_single (type
, field
, value
),
4298 NULL_TREE
, false, true, 0);
4299 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4302 error_at (loc
, "cast to union type from type not present in union");
4303 return error_mark_node
;
4309 if (type
== void_type_node
)
4311 tree t
= build1 (CONVERT_EXPR
, type
, value
);
4312 SET_EXPR_LOCATION (t
, loc
);
4316 otype
= TREE_TYPE (value
);
4318 /* Optionally warn about potentially worrisome casts. */
4320 && TREE_CODE (type
) == POINTER_TYPE
4321 && TREE_CODE (otype
) == POINTER_TYPE
)
4322 handle_warn_cast_qual (type
, otype
);
4324 /* Warn about possible alignment problems. */
4325 if (STRICT_ALIGNMENT
4326 && TREE_CODE (type
) == POINTER_TYPE
4327 && TREE_CODE (otype
) == POINTER_TYPE
4328 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
4329 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
4330 /* Don't warn about opaque types, where the actual alignment
4331 restriction is unknown. */
4332 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
4333 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
4334 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
4335 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
4336 warning_at (loc
, OPT_Wcast_align
,
4337 "cast increases required alignment of target type");
4339 if (TREE_CODE (type
) == INTEGER_TYPE
4340 && TREE_CODE (otype
) == POINTER_TYPE
4341 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
4342 /* Unlike conversion of integers to pointers, where the
4343 warning is disabled for converting constants because
4344 of cases such as SIG_*, warn about converting constant
4345 pointers to integers. In some cases it may cause unwanted
4346 sign extension, and a warning is appropriate. */
4347 warning_at (loc
, OPT_Wpointer_to_int_cast
,
4348 "cast from pointer to integer of different size");
4350 if (TREE_CODE (value
) == CALL_EXPR
4351 && TREE_CODE (type
) != TREE_CODE (otype
))
4352 warning_at (loc
, OPT_Wbad_function_cast
,
4353 "cast from function call of type %qT "
4354 "to non-matching type %qT", otype
, type
);
4356 if (TREE_CODE (type
) == POINTER_TYPE
4357 && TREE_CODE (otype
) == INTEGER_TYPE
4358 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
4359 /* Don't warn about converting any constant. */
4360 && !TREE_CONSTANT (value
))
4362 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
4363 "of different size");
4365 if (warn_strict_aliasing
<= 2)
4366 strict_aliasing_warning (otype
, type
, expr
);
4368 /* If pedantic, warn for conversions between function and object
4369 pointer types, except for converting a null pointer constant
4370 to function pointer type. */
4372 && TREE_CODE (type
) == POINTER_TYPE
4373 && TREE_CODE (otype
) == POINTER_TYPE
4374 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
4375 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
4376 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
4377 "conversion of function pointer to object pointer type");
4380 && TREE_CODE (type
) == POINTER_TYPE
4381 && TREE_CODE (otype
) == POINTER_TYPE
4382 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
4383 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
4384 && !null_pointer_constant_p (value
))
4385 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
4386 "conversion of object pointer to function pointer type");
4389 value
= convert (type
, value
);
4391 /* Ignore any integer overflow caused by the cast. */
4392 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
4394 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
4396 if (!TREE_OVERFLOW (value
))
4398 /* Avoid clobbering a shared constant. */
4399 value
= copy_node (value
);
4400 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
4403 else if (TREE_OVERFLOW (value
))
4404 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4405 value
= build_int_cst_wide (TREE_TYPE (value
),
4406 TREE_INT_CST_LOW (value
),
4407 TREE_INT_CST_HIGH (value
));
4411 /* Don't let a cast be an lvalue. */
4413 value
= non_lvalue_loc (loc
, value
);
4415 /* Don't allow the results of casting to floating-point or complex
4416 types be confused with actual constants, or casts involving
4417 integer and pointer types other than direct integer-to-integer
4418 and integer-to-pointer be confused with integer constant
4419 expressions and null pointer constants. */
4420 if (TREE_CODE (value
) == REAL_CST
4421 || TREE_CODE (value
) == COMPLEX_CST
4422 || (TREE_CODE (value
) == INTEGER_CST
4423 && !((TREE_CODE (expr
) == INTEGER_CST
4424 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
4425 || TREE_CODE (expr
) == REAL_CST
4426 || TREE_CODE (expr
) == COMPLEX_CST
)))
4427 value
= build1 (NOP_EXPR
, type
, value
);
4429 if (CAN_HAVE_LOCATION_P (value
))
4430 SET_EXPR_LOCATION (value
, loc
);
4434 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4435 location of the open paren of the cast, or the position of the cast
4438 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
4441 tree type_expr
= NULL_TREE
;
4442 bool type_expr_const
= true;
4444 int saved_wsp
= warn_strict_prototypes
;
4446 /* This avoids warnings about unprototyped casts on
4447 integers. E.g. "#define SIG_DFL (void(*)())0". */
4448 if (TREE_CODE (expr
) == INTEGER_CST
)
4449 warn_strict_prototypes
= 0;
4450 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
4451 warn_strict_prototypes
= saved_wsp
;
4453 ret
= build_c_cast (loc
, type
, expr
);
4456 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
4457 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !type_expr_const
;
4458 SET_EXPR_LOCATION (ret
, loc
);
4461 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
4462 SET_EXPR_LOCATION (ret
, loc
);
4464 /* C++ does not permits types to be defined in a cast. */
4465 if (warn_cxx_compat
&& type_name
->specs
->tag_defined_p
)
4466 warning_at (loc
, OPT_Wc___compat
,
4467 "defining a type in a cast is invalid in C++");
4472 /* Build an assignment expression of lvalue LHS from value RHS.
4473 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4474 may differ from TREE_TYPE (LHS) for an enum bitfield.
4475 MODIFYCODE is the code for a binary operator that we use
4476 to combine the old value of LHS with RHS to get the new value.
4477 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4478 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4479 which may differ from TREE_TYPE (RHS) for an enum value.
4481 LOCATION is the location of the MODIFYCODE operator.
4482 RHS_LOC is the location of the RHS. */
4485 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
4486 enum tree_code modifycode
,
4487 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
4491 tree rhs_semantic_type
= NULL_TREE
;
4492 tree lhstype
= TREE_TYPE (lhs
);
4493 tree olhstype
= lhstype
;
4496 /* Types that aren't fully specified cannot be used in assignments. */
4497 lhs
= require_complete_type (lhs
);
4499 /* Avoid duplicate error messages from operands that had errors. */
4500 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
4501 return error_mark_node
;
4503 if (!lvalue_or_else (lhs
, lv_assign
))
4504 return error_mark_node
;
4506 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4508 rhs_semantic_type
= TREE_TYPE (rhs
);
4509 rhs
= TREE_OPERAND (rhs
, 0);
4514 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
4516 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
4517 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
4519 if (inner
== error_mark_node
)
4520 return error_mark_node
;
4521 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4522 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
4523 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
4524 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
4525 protected_set_expr_location (result
, location
);
4529 /* If a binary op has been requested, combine the old LHS value with the RHS
4530 producing the value we should actually store into the LHS. */
4532 if (modifycode
!= NOP_EXPR
)
4534 lhs
= c_fully_fold (lhs
, false, NULL
);
4535 lhs
= stabilize_reference (lhs
);
4536 newrhs
= build_binary_op (location
,
4537 modifycode
, lhs
, rhs
, 1);
4539 /* The original type of the right hand side is no longer
4541 rhs_origtype
= NULL_TREE
;
4544 /* Give an error for storing in something that is 'const'. */
4546 if (TYPE_READONLY (lhstype
)
4547 || ((TREE_CODE (lhstype
) == RECORD_TYPE
4548 || TREE_CODE (lhstype
) == UNION_TYPE
)
4549 && C_TYPE_FIELDS_READONLY (lhstype
)))
4551 readonly_error (lhs
, lv_assign
);
4552 return error_mark_node
;
4554 else if (TREE_READONLY (lhs
))
4555 readonly_warning (lhs
, lv_assign
);
4557 /* If storing into a structure or union member,
4558 it has probably been given type `int'.
4559 Compute the type that would go with
4560 the actual amount of storage the member occupies. */
4562 if (TREE_CODE (lhs
) == COMPONENT_REF
4563 && (TREE_CODE (lhstype
) == INTEGER_TYPE
4564 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
4565 || TREE_CODE (lhstype
) == REAL_TYPE
4566 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
4567 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
4569 /* If storing in a field that is in actuality a short or narrower than one,
4570 we must store in the field in its actual type. */
4572 if (lhstype
!= TREE_TYPE (lhs
))
4574 lhs
= copy_node (lhs
);
4575 TREE_TYPE (lhs
) = lhstype
;
4578 /* Issue -Wc++-compat warnings about an assignment to an enum type
4579 when LHS does not have its original type. This happens for,
4580 e.g., an enum bitfield in a struct. */
4582 && lhs_origtype
!= NULL_TREE
4583 && lhs_origtype
!= lhstype
4584 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
4586 tree checktype
= (rhs_origtype
!= NULL_TREE
4589 if (checktype
!= error_mark_node
4590 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
))
4591 warning_at (location
, OPT_Wc___compat
,
4592 "enum conversion in assignment is invalid in C++");
4595 /* Convert new value to destination type. Fold it first, then
4596 restore any excess precision information, for the sake of
4597 conversion warnings. */
4599 npc
= null_pointer_constant_p (newrhs
);
4600 newrhs
= c_fully_fold (newrhs
, false, NULL
);
4601 if (rhs_semantic_type
)
4602 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
4603 newrhs
= convert_for_assignment (location
, lhstype
, newrhs
, rhs_origtype
,
4604 ic_assign
, npc
, NULL_TREE
, NULL_TREE
, 0);
4605 if (TREE_CODE (newrhs
) == ERROR_MARK
)
4606 return error_mark_node
;
4608 /* Emit ObjC write barrier, if necessary. */
4609 if (c_dialect_objc () && flag_objc_gc
)
4611 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
4614 protected_set_expr_location (result
, location
);
4619 /* Scan operands. */
4621 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
4622 TREE_SIDE_EFFECTS (result
) = 1;
4623 protected_set_expr_location (result
, location
);
4625 /* If we got the LHS in a different type for storing in,
4626 convert the result back to the nominal type of LHS
4627 so that the value we return always has the same type
4628 as the LHS argument. */
4630 if (olhstype
== TREE_TYPE (result
))
4633 result
= convert_for_assignment (location
, olhstype
, result
, rhs_origtype
,
4634 ic_assign
, false, NULL_TREE
, NULL_TREE
, 0);
4635 protected_set_expr_location (result
, location
);
4639 /* Convert value RHS to type TYPE as preparation for an assignment to
4640 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4641 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4642 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4643 constant before any folding.
4644 The real work of conversion is done by `convert'.
4645 The purpose of this function is to generate error messages
4646 for assignments that are not allowed in C.
4647 ERRTYPE says whether it is argument passing, assignment,
4648 initialization or return.
4650 LOCATION is the location of the RHS.
4651 FUNCTION is a tree for the function being called.
4652 PARMNUM is the number of the argument, for printing in error messages. */
4655 convert_for_assignment (location_t location
, tree type
, tree rhs
,
4656 tree origtype
, enum impl_conv errtype
,
4657 bool null_pointer_constant
, tree fundecl
,
4658 tree function
, int parmnum
)
4660 enum tree_code codel
= TREE_CODE (type
);
4661 tree orig_rhs
= rhs
;
4663 enum tree_code coder
;
4664 tree rname
= NULL_TREE
;
4665 bool objc_ok
= false;
4667 if (errtype
== ic_argpass
)
4670 /* Change pointer to function to the function itself for
4672 if (TREE_CODE (function
) == ADDR_EXPR
4673 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
4674 function
= TREE_OPERAND (function
, 0);
4676 /* Handle an ObjC selector specially for diagnostics. */
4677 selector
= objc_message_selector ();
4679 if (selector
&& parmnum
> 2)
4686 /* This macro is used to emit diagnostics to ensure that all format
4687 strings are complete sentences, visible to gettext and checked at
4689 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4694 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4695 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4696 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4697 "expected %qT but argument is of type %qT", \
4701 pedwarn (LOCATION, OPT, AS); \
4704 pedwarn (LOCATION, OPT, IN); \
4707 pedwarn (LOCATION, OPT, RE); \
4710 gcc_unreachable (); \
4714 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4715 rhs
= TREE_OPERAND (rhs
, 0);
4717 rhstype
= TREE_TYPE (rhs
);
4718 coder
= TREE_CODE (rhstype
);
4720 if (coder
== ERROR_MARK
)
4721 return error_mark_node
;
4723 if (c_dialect_objc ())
4746 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
4749 if (warn_cxx_compat
)
4751 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
4752 if (checktype
!= error_mark_node
4753 && TREE_CODE (type
) == ENUMERAL_TYPE
4754 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
4756 WARN_FOR_ASSIGNMENT (input_location
, OPT_Wc___compat
,
4757 G_("enum conversion when passing argument "
4758 "%d of %qE is invalid in C++"),
4759 G_("enum conversion in assignment is "
4761 G_("enum conversion in initialization is "
4763 G_("enum conversion in return is "
4768 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
4771 if (coder
== VOID_TYPE
)
4773 /* Except for passing an argument to an unprototyped function,
4774 this is a constraint violation. When passing an argument to
4775 an unprototyped function, it is compile-time undefined;
4776 making it a constraint in that case was rejected in
4778 error_at (location
, "void value not ignored as it ought to be");
4779 return error_mark_node
;
4781 rhs
= require_complete_type (rhs
);
4782 if (rhs
== error_mark_node
)
4783 return error_mark_node
;
4784 /* A type converts to a reference to it.
4785 This code doesn't fully support references, it's just for the
4786 special case of va_start and va_copy. */
4787 if (codel
== REFERENCE_TYPE
4788 && comptypes (TREE_TYPE (type
), TREE_TYPE (rhs
)) == 1)
4790 if (!lvalue_p (rhs
))
4792 error_at (location
, "cannot pass rvalue to reference parameter");
4793 return error_mark_node
;
4795 if (!c_mark_addressable (rhs
))
4796 return error_mark_node
;
4797 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
4798 SET_EXPR_LOCATION (rhs
, location
);
4800 /* We already know that these two types are compatible, but they
4801 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4802 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4803 likely to be va_list, a typedef to __builtin_va_list, which
4804 is different enough that it will cause problems later. */
4805 if (TREE_TYPE (TREE_TYPE (rhs
)) != TREE_TYPE (type
))
4807 rhs
= build1 (NOP_EXPR
, build_pointer_type (TREE_TYPE (type
)), rhs
);
4808 SET_EXPR_LOCATION (rhs
, location
);
4811 rhs
= build1 (NOP_EXPR
, type
, rhs
);
4812 SET_EXPR_LOCATION (rhs
, location
);
4815 /* Some types can interconvert without explicit casts. */
4816 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
4817 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
4818 return convert (type
, rhs
);
4819 /* Arithmetic types all interconvert, and enum is treated like int. */
4820 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
4821 || codel
== FIXED_POINT_TYPE
4822 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
4823 || codel
== BOOLEAN_TYPE
)
4824 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
4825 || coder
== FIXED_POINT_TYPE
4826 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
4827 || coder
== BOOLEAN_TYPE
))
4830 bool save
= in_late_binary_op
;
4831 if (codel
== BOOLEAN_TYPE
)
4832 in_late_binary_op
= true;
4833 ret
= convert_and_check (type
, orig_rhs
);
4834 if (codel
== BOOLEAN_TYPE
)
4835 in_late_binary_op
= save
;
4839 /* Aggregates in different TUs might need conversion. */
4840 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
4842 && comptypes (type
, rhstype
))
4843 return convert_and_check (type
, rhs
);
4845 /* Conversion to a transparent union from its member types.
4846 This applies only to function arguments. */
4847 if (codel
== UNION_TYPE
&& TYPE_TRANSPARENT_UNION (type
)
4848 && errtype
== ic_argpass
)
4850 tree memb
, marginal_memb
= NULL_TREE
;
4852 for (memb
= TYPE_FIELDS (type
); memb
; memb
= TREE_CHAIN (memb
))
4854 tree memb_type
= TREE_TYPE (memb
);
4856 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
4857 TYPE_MAIN_VARIANT (rhstype
)))
4860 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
4863 if (coder
== POINTER_TYPE
)
4865 tree ttl
= TREE_TYPE (memb_type
);
4866 tree ttr
= TREE_TYPE (rhstype
);
4868 /* Any non-function converts to a [const][volatile] void *
4869 and vice versa; otherwise, targets must be the same.
4870 Meanwhile, the lhs target must have all the qualifiers of
4872 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
4873 || comp_target_types (location
, memb_type
, rhstype
))
4875 /* If this type won't generate any warnings, use it. */
4876 if (TYPE_QUALS (ttl
) == TYPE_QUALS (ttr
)
4877 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
4878 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
4879 ? ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
4880 == TYPE_QUALS (ttr
))
4881 : ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
4882 == TYPE_QUALS (ttl
))))
4885 /* Keep looking for a better type, but remember this one. */
4887 marginal_memb
= memb
;
4891 /* Can convert integer zero to any pointer type. */
4892 if (null_pointer_constant
)
4894 rhs
= null_pointer_node
;
4899 if (memb
|| marginal_memb
)
4903 /* We have only a marginally acceptable member type;
4904 it needs a warning. */
4905 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
4906 tree ttr
= TREE_TYPE (rhstype
);
4908 /* Const and volatile mean something different for function
4909 types, so the usual warnings are not appropriate. */
4910 if (TREE_CODE (ttr
) == FUNCTION_TYPE
4911 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
4913 /* Because const and volatile on functions are
4914 restrictions that say the function will not do
4915 certain things, it is okay to use a const or volatile
4916 function where an ordinary one is wanted, but not
4918 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
4919 WARN_FOR_ASSIGNMENT (location
, 0,
4920 G_("passing argument %d of %qE "
4921 "makes qualified function "
4922 "pointer from unqualified"),
4923 G_("assignment makes qualified "
4924 "function pointer from "
4926 G_("initialization makes qualified "
4927 "function pointer from "
4929 G_("return makes qualified function "
4930 "pointer from unqualified"));
4932 else if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
4933 WARN_FOR_ASSIGNMENT (location
, 0,
4934 G_("passing argument %d of %qE discards "
4935 "qualifiers from pointer target type"),
4936 G_("assignment discards qualifiers "
4937 "from pointer target type"),
4938 G_("initialization discards qualifiers "
4939 "from pointer target type"),
4940 G_("return discards qualifiers from "
4941 "pointer target type"));
4943 memb
= marginal_memb
;
4946 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
4947 pedwarn (location
, OPT_pedantic
,
4948 "ISO C prohibits argument conversion to union type");
4950 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
4951 return build_constructor_single (type
, memb
, rhs
);
4955 /* Conversions among pointers */
4956 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
4957 && (coder
== codel
))
4959 tree ttl
= TREE_TYPE (type
);
4960 tree ttr
= TREE_TYPE (rhstype
);
4963 bool is_opaque_pointer
;
4964 int target_cmp
= 0; /* Cache comp_target_types () result. */
4966 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
4967 mvl
= TYPE_MAIN_VARIANT (mvl
);
4968 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
4969 mvr
= TYPE_MAIN_VARIANT (mvr
);
4970 /* Opaque pointers are treated like void pointers. */
4971 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
4973 /* C++ does not allow the implicit conversion void* -> T*. However,
4974 for the purpose of reducing the number of false positives, we
4975 tolerate the special case of
4979 where NULL is typically defined in C to be '(void *) 0'. */
4980 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
4981 warning_at (location
, OPT_Wc___compat
,
4982 "request for implicit conversion "
4983 "from %qT to %qT not permitted in C++", rhstype
, type
);
4985 /* Check if the right-hand side has a format attribute but the
4986 left-hand side doesn't. */
4987 if (warn_missing_format_attribute
4988 && check_missing_format_attribute (type
, rhstype
))
4993 warning_at (location
, OPT_Wmissing_format_attribute
,
4994 "argument %d of %qE might be "
4995 "a candidate for a format attribute",
4999 warning_at (location
, OPT_Wmissing_format_attribute
,
5000 "assignment left-hand side might be "
5001 "a candidate for a format attribute");
5004 warning_at (location
, OPT_Wmissing_format_attribute
,
5005 "initialization left-hand side might be "
5006 "a candidate for a format attribute");
5009 warning_at (location
, OPT_Wmissing_format_attribute
,
5010 "return type might be "
5011 "a candidate for a format attribute");
5018 /* Any non-function converts to a [const][volatile] void *
5019 and vice versa; otherwise, targets must be the same.
5020 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5021 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
5022 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
5023 || is_opaque_pointer
5024 || (c_common_unsigned_type (mvl
)
5025 == c_common_unsigned_type (mvr
)))
5028 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5031 && !null_pointer_constant
5032 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
5033 WARN_FOR_ASSIGNMENT (location
, OPT_pedantic
,
5034 G_("ISO C forbids passing argument %d of "
5035 "%qE between function pointer "
5037 G_("ISO C forbids assignment between "
5038 "function pointer and %<void *%>"),
5039 G_("ISO C forbids initialization between "
5040 "function pointer and %<void *%>"),
5041 G_("ISO C forbids return between function "
5042 "pointer and %<void *%>"));
5043 /* Const and volatile mean something different for function types,
5044 so the usual warnings are not appropriate. */
5045 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
5046 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
5048 if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
5050 /* Types differing only by the presence of the 'volatile'
5051 qualifier are acceptable if the 'volatile' has been added
5052 in by the Objective-C EH machinery. */
5053 if (!objc_type_quals_match (ttl
, ttr
))
5054 WARN_FOR_ASSIGNMENT (location
, 0,
5055 G_("passing argument %d of %qE discards "
5056 "qualifiers from pointer target type"),
5057 G_("assignment discards qualifiers "
5058 "from pointer target type"),
5059 G_("initialization discards qualifiers "
5060 "from pointer target type"),
5061 G_("return discards qualifiers from "
5062 "pointer target type"));
5064 /* If this is not a case of ignoring a mismatch in signedness,
5066 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
5069 /* If there is a mismatch, do warn. */
5070 else if (warn_pointer_sign
)
5071 WARN_FOR_ASSIGNMENT (location
, OPT_Wpointer_sign
,
5072 G_("pointer targets in passing argument "
5073 "%d of %qE differ in signedness"),
5074 G_("pointer targets in assignment "
5075 "differ in signedness"),
5076 G_("pointer targets in initialization "
5077 "differ in signedness"),
5078 G_("pointer targets in return differ "
5081 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
5082 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5084 /* Because const and volatile on functions are restrictions
5085 that say the function will not do certain things,
5086 it is okay to use a const or volatile function
5087 where an ordinary one is wanted, but not vice-versa. */
5088 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
5089 WARN_FOR_ASSIGNMENT (location
, 0,
5090 G_("passing argument %d of %qE makes "
5091 "qualified function pointer "
5092 "from unqualified"),
5093 G_("assignment makes qualified function "
5094 "pointer from unqualified"),
5095 G_("initialization makes qualified "
5096 "function pointer from unqualified"),
5097 G_("return makes qualified function "
5098 "pointer from unqualified"));
5102 /* Avoid warning about the volatile ObjC EH puts on decls. */
5104 WARN_FOR_ASSIGNMENT (location
, 0,
5105 G_("passing argument %d of %qE from "
5106 "incompatible pointer type"),
5107 G_("assignment from incompatible pointer type"),
5108 G_("initialization from incompatible "
5110 G_("return from incompatible pointer type"));
5112 return convert (type
, rhs
);
5114 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
5116 /* ??? This should not be an error when inlining calls to
5117 unprototyped functions. */
5118 error_at (location
, "invalid use of non-lvalue array");
5119 return error_mark_node
;
5121 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
5123 /* An explicit constant 0 can convert to a pointer,
5124 or one that results from arithmetic, even including
5125 a cast to integer type. */
5126 if (!null_pointer_constant
)
5127 WARN_FOR_ASSIGNMENT (location
, 0,
5128 G_("passing argument %d of %qE makes "
5129 "pointer from integer without a cast"),
5130 G_("assignment makes pointer from integer "
5132 G_("initialization makes pointer from "
5133 "integer without a cast"),
5134 G_("return makes pointer from integer "
5137 return convert (type
, rhs
);
5139 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
5141 WARN_FOR_ASSIGNMENT (location
, 0,
5142 G_("passing argument %d of %qE makes integer "
5143 "from pointer without a cast"),
5144 G_("assignment makes integer from pointer "
5146 G_("initialization makes integer from pointer "
5148 G_("return makes integer from pointer "
5150 return convert (type
, rhs
);
5152 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
5155 bool save
= in_late_binary_op
;
5156 in_late_binary_op
= true;
5157 ret
= convert (type
, rhs
);
5158 in_late_binary_op
= save
;
5165 error_at (location
, "incompatible type for argument %d of %qE", parmnum
, rname
);
5166 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
5167 ? DECL_SOURCE_LOCATION (fundecl
) : input_location
,
5168 "expected %qT but argument is of type %qT", type
, rhstype
);
5171 error_at (location
, "incompatible types when assigning to type %qT from "
5172 "type %qT", type
, rhstype
);
5176 "incompatible types when initializing type %qT using type %qT",
5181 "incompatible types when returning type %qT but %qT was "
5182 "expected", rhstype
, type
);
5188 return error_mark_node
;
5191 /* If VALUE is a compound expr all of whose expressions are constant, then
5192 return its value. Otherwise, return error_mark_node.
5194 This is for handling COMPOUND_EXPRs as initializer elements
5195 which is allowed with a warning when -pedantic is specified. */
5198 valid_compound_expr_initializer (tree value
, tree endtype
)
5200 if (TREE_CODE (value
) == COMPOUND_EXPR
)
5202 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
5204 return error_mark_node
;
5205 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
5208 else if (!initializer_constant_valid_p (value
, endtype
))
5209 return error_mark_node
;
5214 /* Perform appropriate conversions on the initial value of a variable,
5215 store it in the declaration DECL,
5216 and print any error messages that are appropriate.
5217 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5218 If the init is invalid, store an ERROR_MARK.
5220 INIT_LOC is the location of the initial value. */
5223 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
5228 /* If variable's type was invalidly declared, just ignore it. */
5230 type
= TREE_TYPE (decl
);
5231 if (TREE_CODE (type
) == ERROR_MARK
)
5234 /* Digest the specified initializer into an expression. */
5237 npc
= null_pointer_constant_p (init
);
5238 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
5239 true, TREE_STATIC (decl
));
5241 /* Store the expression if valid; else report error. */
5243 if (!in_system_header
5244 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
5245 warning (OPT_Wtraditional
, "traditional C rejects automatic "
5246 "aggregate initialization");
5248 DECL_INITIAL (decl
) = value
;
5250 /* ANSI wants warnings about out-of-range constant initializers. */
5251 STRIP_TYPE_NOPS (value
);
5252 if (TREE_STATIC (decl
))
5253 constant_expression_warning (value
);
5255 /* Check if we need to set array size from compound literal size. */
5256 if (TREE_CODE (type
) == ARRAY_TYPE
5257 && TYPE_DOMAIN (type
) == 0
5258 && value
!= error_mark_node
)
5260 tree inside_init
= init
;
5262 STRIP_TYPE_NOPS (inside_init
);
5263 inside_init
= fold (inside_init
);
5265 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5267 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
5269 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
5271 /* For int foo[] = (int [3]){1}; we need to set array size
5272 now since later on array initializer will be just the
5273 brace enclosed list of the compound literal. */
5274 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
5275 TREE_TYPE (decl
) = type
;
5276 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
5278 layout_decl (cldecl
, 0);
5284 /* Methods for storing and printing names for error messages. */
5286 /* Implement a spelling stack that allows components of a name to be pushed
5287 and popped. Each element on the stack is this structure. */
5294 unsigned HOST_WIDE_INT i
;
5299 #define SPELLING_STRING 1
5300 #define SPELLING_MEMBER 2
5301 #define SPELLING_BOUNDS 3
5303 static struct spelling
*spelling
; /* Next stack element (unused). */
5304 static struct spelling
*spelling_base
; /* Spelling stack base. */
5305 static int spelling_size
; /* Size of the spelling stack. */
5307 /* Macros to save and restore the spelling stack around push_... functions.
5308 Alternative to SAVE_SPELLING_STACK. */
5310 #define SPELLING_DEPTH() (spelling - spelling_base)
5311 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5313 /* Push an element on the spelling stack with type KIND and assign VALUE
5316 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5318 int depth = SPELLING_DEPTH (); \
5320 if (depth >= spelling_size) \
5322 spelling_size += 10; \
5323 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5325 RESTORE_SPELLING_DEPTH (depth); \
5328 spelling->kind = (KIND); \
5329 spelling->MEMBER = (VALUE); \
5333 /* Push STRING on the stack. Printed literally. */
5336 push_string (const char *string
)
5338 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
5341 /* Push a member name on the stack. Printed as '.' STRING. */
5344 push_member_name (tree decl
)
5346 const char *const string
5348 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
5349 : _("<anonymous>"));
5350 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
5353 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5356 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
5358 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
5361 /* Compute the maximum size in bytes of the printed spelling. */
5364 spelling_length (void)
5369 for (p
= spelling_base
; p
< spelling
; p
++)
5371 if (p
->kind
== SPELLING_BOUNDS
)
5374 size
+= strlen (p
->u
.s
) + 1;
5380 /* Print the spelling to BUFFER and return it. */
5383 print_spelling (char *buffer
)
5388 for (p
= spelling_base
; p
< spelling
; p
++)
5389 if (p
->kind
== SPELLING_BOUNDS
)
5391 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
5397 if (p
->kind
== SPELLING_MEMBER
)
5399 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
5406 /* Issue an error message for a bad initializer component.
5407 MSGID identifies the message.
5408 The component name is taken from the spelling stack. */
5411 error_init (const char *msgid
)
5415 error ("%s", _(msgid
));
5416 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5418 error ("(near initialization for %qs)", ofwhat
);
5421 /* Issue a pedantic warning for a bad initializer component. OPT is
5422 the option OPT_* (from options.h) controlling this warning or 0 if
5423 it is unconditionally given. MSGID identifies the message. The
5424 component name is taken from the spelling stack. */
5427 pedwarn_init (location_t location
, int opt
, const char *msgid
)
5431 pedwarn (location
, opt
, "%s", _(msgid
));
5432 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5434 pedwarn (location
, opt
, "(near initialization for %qs)", ofwhat
);
5437 /* Issue a warning for a bad initializer component.
5439 OPT is the OPT_W* value corresponding to the warning option that
5440 controls this warning. MSGID identifies the message. The
5441 component name is taken from the spelling stack. */
5444 warning_init (int opt
, const char *msgid
)
5448 warning (opt
, "%s", _(msgid
));
5449 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5451 warning (opt
, "(near initialization for %qs)", ofwhat
);
5454 /* If TYPE is an array type and EXPR is a parenthesized string
5455 constant, warn if pedantic that EXPR is being used to initialize an
5456 object of type TYPE. */
5459 maybe_warn_string_init (tree type
, struct c_expr expr
)
5462 && TREE_CODE (type
) == ARRAY_TYPE
5463 && TREE_CODE (expr
.value
) == STRING_CST
5464 && expr
.original_code
!= STRING_CST
)
5465 pedwarn_init (input_location
, OPT_pedantic
,
5466 "array initialized from parenthesized string constant");
5469 /* Digest the parser output INIT as an initializer for type TYPE.
5470 Return a C expression of type TYPE to represent the initial value.
5472 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5474 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5476 If INIT is a string constant, STRICT_STRING is true if it is
5477 unparenthesized or we should not warn here for it being parenthesized.
5478 For other types of INIT, STRICT_STRING is not used.
5480 INIT_LOC is the location of the INIT.
5482 REQUIRE_CONSTANT requests an error if non-constant initializers or
5483 elements are seen. */
5486 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
5487 bool null_pointer_constant
, bool strict_string
,
5488 int require_constant
)
5490 enum tree_code code
= TREE_CODE (type
);
5491 tree inside_init
= init
;
5492 tree semantic_type
= NULL_TREE
;
5493 bool maybe_const
= true;
5495 if (type
== error_mark_node
5497 || init
== error_mark_node
5498 || TREE_TYPE (init
) == error_mark_node
)
5499 return error_mark_node
;
5501 STRIP_TYPE_NOPS (inside_init
);
5503 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
5505 semantic_type
= TREE_TYPE (inside_init
);
5506 inside_init
= TREE_OPERAND (inside_init
, 0);
5508 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
5509 inside_init
= decl_constant_value_for_optimization (inside_init
);
5511 /* Initialization of an array of chars from a string constant
5512 optionally enclosed in braces. */
5514 if (code
== ARRAY_TYPE
&& inside_init
5515 && TREE_CODE (inside_init
) == STRING_CST
)
5517 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
5518 /* Note that an array could be both an array of character type
5519 and an array of wchar_t if wchar_t is signed char or unsigned
5521 bool char_array
= (typ1
== char_type_node
5522 || typ1
== signed_char_type_node
5523 || typ1
== unsigned_char_type_node
);
5524 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
5525 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
5526 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
5528 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
5531 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
5532 expr
.value
= inside_init
;
5533 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
5534 expr
.original_type
= NULL
;
5535 maybe_warn_string_init (type
, expr
);
5537 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
5538 pedwarn_init (init_loc
, OPT_pedantic
,
5539 "initialization of a flexible array member");
5541 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5542 TYPE_MAIN_VARIANT (type
)))
5547 if (typ2
!= char_type_node
)
5549 error_init ("char-array initialized from wide string");
5550 return error_mark_node
;
5555 if (typ2
== char_type_node
)
5557 error_init ("wide character array initialized from non-wide "
5559 return error_mark_node
;
5561 else if (!comptypes(typ1
, typ2
))
5563 error_init ("wide character array initialized from "
5564 "incompatible wide string");
5565 return error_mark_node
;
5569 TREE_TYPE (inside_init
) = type
;
5570 if (TYPE_DOMAIN (type
) != 0
5571 && TYPE_SIZE (type
) != 0
5572 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
5574 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
5576 /* Subtract the size of a single (possibly wide) character
5577 because it's ok to ignore the terminating null char
5578 that is counted in the length of the constant. */
5579 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
5581 - (TYPE_PRECISION (typ1
)
5583 pedwarn_init (init_loc
, 0,
5584 ("initializer-string for array of chars "
5586 else if (warn_cxx_compat
5587 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
5588 warning_at (init_loc
, OPT_Wc___compat
,
5589 ("initializer-string for array chars "
5590 "is too long for C++"));
5595 else if (INTEGRAL_TYPE_P (typ1
))
5597 error_init ("array of inappropriate type initialized "
5598 "from string constant");
5599 return error_mark_node
;
5603 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5604 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5605 below and handle as a constructor. */
5606 if (code
== VECTOR_TYPE
5607 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
5608 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
5609 && TREE_CONSTANT (inside_init
))
5611 if (TREE_CODE (inside_init
) == VECTOR_CST
5612 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5613 TYPE_MAIN_VARIANT (type
)))
5616 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
5618 unsigned HOST_WIDE_INT ix
;
5620 bool constant_p
= true;
5622 /* Iterate through elements and check if all constructor
5623 elements are *_CSTs. */
5624 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
5625 if (!CONSTANT_CLASS_P (value
))
5632 return build_vector_from_ctor (type
,
5633 CONSTRUCTOR_ELTS (inside_init
));
5637 if (warn_sequence_point
)
5638 verify_sequence_points (inside_init
);
5640 /* Any type can be initialized
5641 from an expression of the same type, optionally with braces. */
5643 if (inside_init
&& TREE_TYPE (inside_init
) != 0
5644 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5645 TYPE_MAIN_VARIANT (type
))
5646 || (code
== ARRAY_TYPE
5647 && comptypes (TREE_TYPE (inside_init
), type
))
5648 || (code
== VECTOR_TYPE
5649 && comptypes (TREE_TYPE (inside_init
), type
))
5650 || (code
== POINTER_TYPE
5651 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
5652 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
5653 TREE_TYPE (type
)))))
5655 if (code
== POINTER_TYPE
)
5657 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
5659 if (TREE_CODE (inside_init
) == STRING_CST
5660 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5661 inside_init
= array_to_pointer_conversion
5662 (init_loc
, inside_init
);
5665 error_init ("invalid use of non-lvalue array");
5666 return error_mark_node
;
5671 if (code
== VECTOR_TYPE
)
5672 /* Although the types are compatible, we may require a
5674 inside_init
= convert (type
, inside_init
);
5676 if (require_constant
5677 && (code
== VECTOR_TYPE
|| !flag_isoc99
)
5678 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5680 /* As an extension, allow initializing objects with static storage
5681 duration with compound literals (which are then treated just as
5682 the brace enclosed list they contain). Also allow this for
5683 vectors, as we can only assign them with compound literals. */
5684 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
5685 inside_init
= DECL_INITIAL (decl
);
5688 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
5689 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
5691 error_init ("array initialized from non-constant array expression");
5692 return error_mark_node
;
5695 /* Compound expressions can only occur here if -pedantic or
5696 -pedantic-errors is specified. In the later case, we always want
5697 an error. In the former case, we simply want a warning. */
5698 if (require_constant
&& pedantic
5699 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
5702 = valid_compound_expr_initializer (inside_init
,
5703 TREE_TYPE (inside_init
));
5704 if (inside_init
== error_mark_node
)
5705 error_init ("initializer element is not constant");
5707 pedwarn_init (init_loc
, OPT_pedantic
,
5708 "initializer element is not constant");
5709 if (flag_pedantic_errors
)
5710 inside_init
= error_mark_node
;
5712 else if (require_constant
5713 && !initializer_constant_valid_p (inside_init
,
5714 TREE_TYPE (inside_init
)))
5716 error_init ("initializer element is not constant");
5717 inside_init
= error_mark_node
;
5719 else if (require_constant
&& !maybe_const
)
5720 pedwarn_init (init_loc
, 0,
5721 "initializer element is not a constant expression");
5723 /* Added to enable additional -Wmissing-format-attribute warnings. */
5724 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
5725 inside_init
= convert_for_assignment (init_loc
, type
, inside_init
,
5727 ic_init
, null_pointer_constant
,
5728 NULL_TREE
, NULL_TREE
, 0);
5732 /* Handle scalar types, including conversions. */
5734 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
5735 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
5736 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
5738 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
5739 && (TREE_CODE (init
) == STRING_CST
5740 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
5741 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
5743 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
5746 = convert_for_assignment (init_loc
, type
, inside_init
, origtype
,
5747 ic_init
, null_pointer_constant
,
5748 NULL_TREE
, NULL_TREE
, 0);
5750 /* Check to see if we have already given an error message. */
5751 if (inside_init
== error_mark_node
)
5753 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
5755 error_init ("initializer element is not constant");
5756 inside_init
= error_mark_node
;
5758 else if (require_constant
5759 && !initializer_constant_valid_p (inside_init
,
5760 TREE_TYPE (inside_init
)))
5762 error_init ("initializer element is not computable at load time");
5763 inside_init
= error_mark_node
;
5765 else if (require_constant
&& !maybe_const
)
5766 pedwarn_init (init_loc
, 0,
5767 "initializer element is not a constant expression");
5772 /* Come here only for records and arrays. */
5774 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
5776 error_init ("variable-sized object may not be initialized");
5777 return error_mark_node
;
5780 error_init ("invalid initializer");
5781 return error_mark_node
;
5784 /* Handle initializers that use braces. */
5786 /* Type of object we are accumulating a constructor for.
5787 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5788 static tree constructor_type
;
5790 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5792 static tree constructor_fields
;
5794 /* For an ARRAY_TYPE, this is the specified index
5795 at which to store the next element we get. */
5796 static tree constructor_index
;
5798 /* For an ARRAY_TYPE, this is the maximum index. */
5799 static tree constructor_max_index
;
5801 /* For a RECORD_TYPE, this is the first field not yet written out. */
5802 static tree constructor_unfilled_fields
;
5804 /* For an ARRAY_TYPE, this is the index of the first element
5805 not yet written out. */
5806 static tree constructor_unfilled_index
;
5808 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5809 This is so we can generate gaps between fields, when appropriate. */
5810 static tree constructor_bit_index
;
5812 /* If we are saving up the elements rather than allocating them,
5813 this is the list of elements so far (in reverse order,
5814 most recent first). */
5815 static VEC(constructor_elt
,gc
) *constructor_elements
;
5817 /* 1 if constructor should be incrementally stored into a constructor chain,
5818 0 if all the elements should be kept in AVL tree. */
5819 static int constructor_incremental
;
5821 /* 1 if so far this constructor's elements are all compile-time constants. */
5822 static int constructor_constant
;
5824 /* 1 if so far this constructor's elements are all valid address constants. */
5825 static int constructor_simple
;
5827 /* 1 if this constructor has an element that cannot be part of a
5828 constant expression. */
5829 static int constructor_nonconst
;
5831 /* 1 if this constructor is erroneous so far. */
5832 static int constructor_erroneous
;
5834 /* Structure for managing pending initializer elements, organized as an
5839 struct init_node
*left
, *right
;
5840 struct init_node
*parent
;
5847 /* Tree of pending elements at this constructor level.
5848 These are elements encountered out of order
5849 which belong at places we haven't reached yet in actually
5851 Will never hold tree nodes across GC runs. */
5852 static struct init_node
*constructor_pending_elts
;
5854 /* The SPELLING_DEPTH of this constructor. */
5855 static int constructor_depth
;
5857 /* DECL node for which an initializer is being read.
5858 0 means we are reading a constructor expression
5859 such as (struct foo) {...}. */
5860 static tree constructor_decl
;
5862 /* Nonzero if this is an initializer for a top-level decl. */
5863 static int constructor_top_level
;
5865 /* Nonzero if there were any member designators in this initializer. */
5866 static int constructor_designated
;
5868 /* Nesting depth of designator list. */
5869 static int designator_depth
;
5871 /* Nonzero if there were diagnosed errors in this designator list. */
5872 static int designator_erroneous
;
5875 /* This stack has a level for each implicit or explicit level of
5876 structuring in the initializer, including the outermost one. It
5877 saves the values of most of the variables above. */
5879 struct constructor_range_stack
;
5881 struct constructor_stack
5883 struct constructor_stack
*next
;
5888 tree unfilled_index
;
5889 tree unfilled_fields
;
5891 VEC(constructor_elt
,gc
) *elements
;
5892 struct init_node
*pending_elts
;
5895 /* If value nonzero, this value should replace the entire
5896 constructor at this level. */
5897 struct c_expr replacement_value
;
5898 struct constructor_range_stack
*range_stack
;
5909 static struct constructor_stack
*constructor_stack
;
5911 /* This stack represents designators from some range designator up to
5912 the last designator in the list. */
5914 struct constructor_range_stack
5916 struct constructor_range_stack
*next
, *prev
;
5917 struct constructor_stack
*stack
;
5924 static struct constructor_range_stack
*constructor_range_stack
;
5926 /* This stack records separate initializers that are nested.
5927 Nested initializers can't happen in ANSI C, but GNU C allows them
5928 in cases like { ... (struct foo) { ... } ... }. */
5930 struct initializer_stack
5932 struct initializer_stack
*next
;
5934 struct constructor_stack
*constructor_stack
;
5935 struct constructor_range_stack
*constructor_range_stack
;
5936 VEC(constructor_elt
,gc
) *elements
;
5937 struct spelling
*spelling
;
5938 struct spelling
*spelling_base
;
5941 char require_constant_value
;
5942 char require_constant_elements
;
5945 static struct initializer_stack
*initializer_stack
;
5947 /* Prepare to parse and output the initializer for variable DECL. */
5950 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
5953 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
5955 p
->decl
= constructor_decl
;
5956 p
->require_constant_value
= require_constant_value
;
5957 p
->require_constant_elements
= require_constant_elements
;
5958 p
->constructor_stack
= constructor_stack
;
5959 p
->constructor_range_stack
= constructor_range_stack
;
5960 p
->elements
= constructor_elements
;
5961 p
->spelling
= spelling
;
5962 p
->spelling_base
= spelling_base
;
5963 p
->spelling_size
= spelling_size
;
5964 p
->top_level
= constructor_top_level
;
5965 p
->next
= initializer_stack
;
5966 initializer_stack
= p
;
5968 constructor_decl
= decl
;
5969 constructor_designated
= 0;
5970 constructor_top_level
= top_level
;
5972 if (decl
!= 0 && decl
!= error_mark_node
)
5974 require_constant_value
= TREE_STATIC (decl
);
5975 require_constant_elements
5976 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
5977 /* For a scalar, you can always use any value to initialize,
5978 even within braces. */
5979 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
5980 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
5981 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
5982 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
5983 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
5987 require_constant_value
= 0;
5988 require_constant_elements
= 0;
5989 locus
= _("(anonymous)");
5992 constructor_stack
= 0;
5993 constructor_range_stack
= 0;
5995 missing_braces_mentioned
= 0;
5999 RESTORE_SPELLING_DEPTH (0);
6002 push_string (locus
);
6008 struct initializer_stack
*p
= initializer_stack
;
6010 /* Free the whole constructor stack of this initializer. */
6011 while (constructor_stack
)
6013 struct constructor_stack
*q
= constructor_stack
;
6014 constructor_stack
= q
->next
;
6018 gcc_assert (!constructor_range_stack
);
6020 /* Pop back to the data of the outer initializer (if any). */
6021 free (spelling_base
);
6023 constructor_decl
= p
->decl
;
6024 require_constant_value
= p
->require_constant_value
;
6025 require_constant_elements
= p
->require_constant_elements
;
6026 constructor_stack
= p
->constructor_stack
;
6027 constructor_range_stack
= p
->constructor_range_stack
;
6028 constructor_elements
= p
->elements
;
6029 spelling
= p
->spelling
;
6030 spelling_base
= p
->spelling_base
;
6031 spelling_size
= p
->spelling_size
;
6032 constructor_top_level
= p
->top_level
;
6033 initializer_stack
= p
->next
;
6037 /* Call here when we see the initializer is surrounded by braces.
6038 This is instead of a call to push_init_level;
6039 it is matched by a call to pop_init_level.
6041 TYPE is the type to initialize, for a constructor expression.
6042 For an initializer for a decl, TYPE is zero. */
6045 really_start_incremental_init (tree type
)
6047 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
6050 type
= TREE_TYPE (constructor_decl
);
6052 if (TREE_CODE (type
) == VECTOR_TYPE
6053 && TYPE_VECTOR_OPAQUE (type
))
6054 error ("opaque vector types cannot be initialized");
6056 p
->type
= constructor_type
;
6057 p
->fields
= constructor_fields
;
6058 p
->index
= constructor_index
;
6059 p
->max_index
= constructor_max_index
;
6060 p
->unfilled_index
= constructor_unfilled_index
;
6061 p
->unfilled_fields
= constructor_unfilled_fields
;
6062 p
->bit_index
= constructor_bit_index
;
6063 p
->elements
= constructor_elements
;
6064 p
->constant
= constructor_constant
;
6065 p
->simple
= constructor_simple
;
6066 p
->nonconst
= constructor_nonconst
;
6067 p
->erroneous
= constructor_erroneous
;
6068 p
->pending_elts
= constructor_pending_elts
;
6069 p
->depth
= constructor_depth
;
6070 p
->replacement_value
.value
= 0;
6071 p
->replacement_value
.original_code
= ERROR_MARK
;
6072 p
->replacement_value
.original_type
= NULL
;
6076 p
->incremental
= constructor_incremental
;
6077 p
->designated
= constructor_designated
;
6079 constructor_stack
= p
;
6081 constructor_constant
= 1;
6082 constructor_simple
= 1;
6083 constructor_nonconst
= 0;
6084 constructor_depth
= SPELLING_DEPTH ();
6085 constructor_elements
= 0;
6086 constructor_pending_elts
= 0;
6087 constructor_type
= type
;
6088 constructor_incremental
= 1;
6089 constructor_designated
= 0;
6090 designator_depth
= 0;
6091 designator_erroneous
= 0;
6093 if (TREE_CODE (constructor_type
) == RECORD_TYPE
6094 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6096 constructor_fields
= TYPE_FIELDS (constructor_type
);
6097 /* Skip any nameless bit fields at the beginning. */
6098 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
6099 && DECL_NAME (constructor_fields
) == 0)
6100 constructor_fields
= TREE_CHAIN (constructor_fields
);
6102 constructor_unfilled_fields
= constructor_fields
;
6103 constructor_bit_index
= bitsize_zero_node
;
6105 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6107 if (TYPE_DOMAIN (constructor_type
))
6109 constructor_max_index
6110 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
6112 /* Detect non-empty initializations of zero-length arrays. */
6113 if (constructor_max_index
== NULL_TREE
6114 && TYPE_SIZE (constructor_type
))
6115 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6117 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6118 to initialize VLAs will cause a proper error; avoid tree
6119 checking errors as well by setting a safe value. */
6120 if (constructor_max_index
6121 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
6122 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6125 = convert (bitsizetype
,
6126 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
6130 constructor_index
= bitsize_zero_node
;
6131 constructor_max_index
= NULL_TREE
;
6134 constructor_unfilled_index
= constructor_index
;
6136 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6138 /* Vectors are like simple fixed-size arrays. */
6139 constructor_max_index
=
6140 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
6141 constructor_index
= bitsize_zero_node
;
6142 constructor_unfilled_index
= constructor_index
;
6146 /* Handle the case of int x = {5}; */
6147 constructor_fields
= constructor_type
;
6148 constructor_unfilled_fields
= constructor_type
;
6152 /* Push down into a subobject, for initialization.
6153 If this is for an explicit set of braces, IMPLICIT is 0.
6154 If it is because the next element belongs at a lower level,
6155 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6158 push_init_level (int implicit
)
6160 struct constructor_stack
*p
;
6161 tree value
= NULL_TREE
;
6163 /* If we've exhausted any levels that didn't have braces,
6164 pop them now. If implicit == 1, this will have been done in
6165 process_init_element; do not repeat it here because in the case
6166 of excess initializers for an empty aggregate this leads to an
6167 infinite cycle of popping a level and immediately recreating
6171 while (constructor_stack
->implicit
)
6173 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
6174 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6175 && constructor_fields
== 0)
6176 process_init_element (pop_init_level (1), true);
6177 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
6178 && constructor_max_index
6179 && tree_int_cst_lt (constructor_max_index
,
6181 process_init_element (pop_init_level (1), true);
6187 /* Unless this is an explicit brace, we need to preserve previous
6191 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
6192 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6193 && constructor_fields
)
6194 value
= find_init_member (constructor_fields
);
6195 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6196 value
= find_init_member (constructor_index
);
6199 p
= XNEW (struct constructor_stack
);
6200 p
->type
= constructor_type
;
6201 p
->fields
= constructor_fields
;
6202 p
->index
= constructor_index
;
6203 p
->max_index
= constructor_max_index
;
6204 p
->unfilled_index
= constructor_unfilled_index
;
6205 p
->unfilled_fields
= constructor_unfilled_fields
;
6206 p
->bit_index
= constructor_bit_index
;
6207 p
->elements
= constructor_elements
;
6208 p
->constant
= constructor_constant
;
6209 p
->simple
= constructor_simple
;
6210 p
->nonconst
= constructor_nonconst
;
6211 p
->erroneous
= constructor_erroneous
;
6212 p
->pending_elts
= constructor_pending_elts
;
6213 p
->depth
= constructor_depth
;
6214 p
->replacement_value
.value
= 0;
6215 p
->replacement_value
.original_code
= ERROR_MARK
;
6216 p
->replacement_value
.original_type
= NULL
;
6217 p
->implicit
= implicit
;
6219 p
->incremental
= constructor_incremental
;
6220 p
->designated
= constructor_designated
;
6221 p
->next
= constructor_stack
;
6223 constructor_stack
= p
;
6225 constructor_constant
= 1;
6226 constructor_simple
= 1;
6227 constructor_nonconst
= 0;
6228 constructor_depth
= SPELLING_DEPTH ();
6229 constructor_elements
= 0;
6230 constructor_incremental
= 1;
6231 constructor_designated
= 0;
6232 constructor_pending_elts
= 0;
6235 p
->range_stack
= constructor_range_stack
;
6236 constructor_range_stack
= 0;
6237 designator_depth
= 0;
6238 designator_erroneous
= 0;
6241 /* Don't die if an entire brace-pair level is superfluous
6242 in the containing level. */
6243 if (constructor_type
== 0)
6245 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
6246 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6248 /* Don't die if there are extra init elts at the end. */
6249 if (constructor_fields
== 0)
6250 constructor_type
= 0;
6253 constructor_type
= TREE_TYPE (constructor_fields
);
6254 push_member_name (constructor_fields
);
6255 constructor_depth
++;
6258 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6260 constructor_type
= TREE_TYPE (constructor_type
);
6261 push_array_bounds (tree_low_cst (constructor_index
, 1));
6262 constructor_depth
++;
6265 if (constructor_type
== 0)
6267 error_init ("extra brace group at end of initializer");
6268 constructor_fields
= 0;
6269 constructor_unfilled_fields
= 0;
6273 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
6275 constructor_constant
= TREE_CONSTANT (value
);
6276 constructor_simple
= TREE_STATIC (value
);
6277 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
6278 constructor_elements
= CONSTRUCTOR_ELTS (value
);
6279 if (!VEC_empty (constructor_elt
, constructor_elements
)
6280 && (TREE_CODE (constructor_type
) == RECORD_TYPE
6281 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
6282 set_nonincremental_init ();
6285 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
6287 missing_braces_mentioned
= 1;
6288 warning_init (OPT_Wmissing_braces
, "missing braces around initializer");
6291 if (TREE_CODE (constructor_type
) == RECORD_TYPE
6292 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6294 constructor_fields
= TYPE_FIELDS (constructor_type
);
6295 /* Skip any nameless bit fields at the beginning. */
6296 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
6297 && DECL_NAME (constructor_fields
) == 0)
6298 constructor_fields
= TREE_CHAIN (constructor_fields
);
6300 constructor_unfilled_fields
= constructor_fields
;
6301 constructor_bit_index
= bitsize_zero_node
;
6303 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6305 /* Vectors are like simple fixed-size arrays. */
6306 constructor_max_index
=
6307 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
6308 constructor_index
= convert (bitsizetype
, integer_zero_node
);
6309 constructor_unfilled_index
= constructor_index
;
6311 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6313 if (TYPE_DOMAIN (constructor_type
))
6315 constructor_max_index
6316 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
6318 /* Detect non-empty initializations of zero-length arrays. */
6319 if (constructor_max_index
== NULL_TREE
6320 && TYPE_SIZE (constructor_type
))
6321 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6323 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6324 to initialize VLAs will cause a proper error; avoid tree
6325 checking errors as well by setting a safe value. */
6326 if (constructor_max_index
6327 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
6328 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6331 = convert (bitsizetype
,
6332 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
6335 constructor_index
= bitsize_zero_node
;
6337 constructor_unfilled_index
= constructor_index
;
6338 if (value
&& TREE_CODE (value
) == STRING_CST
)
6340 /* We need to split the char/wchar array into individual
6341 characters, so that we don't have to special case it
6343 set_nonincremental_init_from_string (value
);
6348 if (constructor_type
!= error_mark_node
)
6349 warning_init (0, "braces around scalar initializer");
6350 constructor_fields
= constructor_type
;
6351 constructor_unfilled_fields
= constructor_type
;
6355 /* At the end of an implicit or explicit brace level,
6356 finish up that level of constructor. If a single expression
6357 with redundant braces initialized that level, return the
6358 c_expr structure for that expression. Otherwise, the original_code
6359 element is set to ERROR_MARK.
6360 If we were outputting the elements as they are read, return 0 as the value
6361 from inner levels (process_init_element ignores that),
6362 but return error_mark_node as the value from the outermost level
6363 (that's what we want to put in DECL_INITIAL).
6364 Otherwise, return a CONSTRUCTOR expression as the value. */
6367 pop_init_level (int implicit
)
6369 struct constructor_stack
*p
;
6372 ret
.original_code
= ERROR_MARK
;
6373 ret
.original_type
= NULL
;
6377 /* When we come to an explicit close brace,
6378 pop any inner levels that didn't have explicit braces. */
6379 while (constructor_stack
->implicit
)
6380 process_init_element (pop_init_level (1), true);
6382 gcc_assert (!constructor_range_stack
);
6385 /* Now output all pending elements. */
6386 constructor_incremental
= 1;
6387 output_pending_init_elements (1);
6389 p
= constructor_stack
;
6391 /* Error for initializing a flexible array member, or a zero-length
6392 array member in an inappropriate context. */
6393 if (constructor_type
&& constructor_fields
6394 && TREE_CODE (constructor_type
) == ARRAY_TYPE
6395 && TYPE_DOMAIN (constructor_type
)
6396 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
6398 /* Silently discard empty initializations. The parser will
6399 already have pedwarned for empty brackets. */
6400 if (integer_zerop (constructor_unfilled_index
))
6401 constructor_type
= NULL_TREE
;
6404 gcc_assert (!TYPE_SIZE (constructor_type
));
6406 if (constructor_depth
> 2)
6407 error_init ("initialization of flexible array member in a nested context");
6409 pedwarn_init (input_location
, OPT_pedantic
,
6410 "initialization of a flexible array member");
6412 /* We have already issued an error message for the existence
6413 of a flexible array member not at the end of the structure.
6414 Discard the initializer so that we do not die later. */
6415 if (TREE_CHAIN (constructor_fields
) != NULL_TREE
)
6416 constructor_type
= NULL_TREE
;
6420 /* Warn when some struct elements are implicitly initialized to zero. */
6421 if (warn_missing_field_initializers
6423 && TREE_CODE (constructor_type
) == RECORD_TYPE
6424 && constructor_unfilled_fields
)
6426 /* Do not warn for flexible array members or zero-length arrays. */
6427 while (constructor_unfilled_fields
6428 && (!DECL_SIZE (constructor_unfilled_fields
)
6429 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
6430 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
6432 /* Do not warn if this level of the initializer uses member
6433 designators; it is likely to be deliberate. */
6434 if (constructor_unfilled_fields
&& !constructor_designated
)
6436 push_member_name (constructor_unfilled_fields
);
6437 warning_init (OPT_Wmissing_field_initializers
,
6438 "missing initializer");
6439 RESTORE_SPELLING_DEPTH (constructor_depth
);
6443 /* Pad out the end of the structure. */
6444 if (p
->replacement_value
.value
)
6445 /* If this closes a superfluous brace pair,
6446 just pass out the element between them. */
6447 ret
= p
->replacement_value
;
6448 else if (constructor_type
== 0)
6450 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
6451 && TREE_CODE (constructor_type
) != UNION_TYPE
6452 && TREE_CODE (constructor_type
) != ARRAY_TYPE
6453 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
6455 /* A nonincremental scalar initializer--just return
6456 the element, after verifying there is just one. */
6457 if (VEC_empty (constructor_elt
,constructor_elements
))
6459 if (!constructor_erroneous
)
6460 error_init ("empty scalar initializer");
6461 ret
.value
= error_mark_node
;
6463 else if (VEC_length (constructor_elt
,constructor_elements
) != 1)
6465 error_init ("extra elements in scalar initializer");
6466 ret
.value
= VEC_index (constructor_elt
,constructor_elements
,0)->value
;
6469 ret
.value
= VEC_index (constructor_elt
,constructor_elements
,0)->value
;
6473 if (constructor_erroneous
)
6474 ret
.value
= error_mark_node
;
6477 ret
.value
= build_constructor (constructor_type
,
6478 constructor_elements
);
6479 if (constructor_constant
)
6480 TREE_CONSTANT (ret
.value
) = 1;
6481 if (constructor_constant
&& constructor_simple
)
6482 TREE_STATIC (ret
.value
) = 1;
6483 if (constructor_nonconst
)
6484 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
6488 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
6490 if (constructor_nonconst
)
6491 ret
.original_code
= C_MAYBE_CONST_EXPR
;
6492 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
6493 ret
.original_code
= ERROR_MARK
;
6496 constructor_type
= p
->type
;
6497 constructor_fields
= p
->fields
;
6498 constructor_index
= p
->index
;
6499 constructor_max_index
= p
->max_index
;
6500 constructor_unfilled_index
= p
->unfilled_index
;
6501 constructor_unfilled_fields
= p
->unfilled_fields
;
6502 constructor_bit_index
= p
->bit_index
;
6503 constructor_elements
= p
->elements
;
6504 constructor_constant
= p
->constant
;
6505 constructor_simple
= p
->simple
;
6506 constructor_nonconst
= p
->nonconst
;
6507 constructor_erroneous
= p
->erroneous
;
6508 constructor_incremental
= p
->incremental
;
6509 constructor_designated
= p
->designated
;
6510 constructor_pending_elts
= p
->pending_elts
;
6511 constructor_depth
= p
->depth
;
6513 constructor_range_stack
= p
->range_stack
;
6514 RESTORE_SPELLING_DEPTH (constructor_depth
);
6516 constructor_stack
= p
->next
;
6519 if (ret
.value
== 0 && constructor_stack
== 0)
6520 ret
.value
= error_mark_node
;
6524 /* Common handling for both array range and field name designators.
6525 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6528 set_designator (int array
)
6531 enum tree_code subcode
;
6533 /* Don't die if an entire brace-pair level is superfluous
6534 in the containing level. */
6535 if (constructor_type
== 0)
6538 /* If there were errors in this designator list already, bail out
6540 if (designator_erroneous
)
6543 if (!designator_depth
)
6545 gcc_assert (!constructor_range_stack
);
6547 /* Designator list starts at the level of closest explicit
6549 while (constructor_stack
->implicit
)
6550 process_init_element (pop_init_level (1), true);
6551 constructor_designated
= 1;
6555 switch (TREE_CODE (constructor_type
))
6559 subtype
= TREE_TYPE (constructor_fields
);
6560 if (subtype
!= error_mark_node
)
6561 subtype
= TYPE_MAIN_VARIANT (subtype
);
6564 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6570 subcode
= TREE_CODE (subtype
);
6571 if (array
&& subcode
!= ARRAY_TYPE
)
6573 error_init ("array index in non-array initializer");
6576 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
6578 error_init ("field name not in record or union initializer");
6582 constructor_designated
= 1;
6583 push_init_level (2);
6587 /* If there are range designators in designator list, push a new designator
6588 to constructor_range_stack. RANGE_END is end of such stack range or
6589 NULL_TREE if there is no range designator at this level. */
6592 push_range_stack (tree range_end
)
6594 struct constructor_range_stack
*p
;
6596 p
= GGC_NEW (struct constructor_range_stack
);
6597 p
->prev
= constructor_range_stack
;
6599 p
->fields
= constructor_fields
;
6600 p
->range_start
= constructor_index
;
6601 p
->index
= constructor_index
;
6602 p
->stack
= constructor_stack
;
6603 p
->range_end
= range_end
;
6604 if (constructor_range_stack
)
6605 constructor_range_stack
->next
= p
;
6606 constructor_range_stack
= p
;
6609 /* Within an array initializer, specify the next index to be initialized.
6610 FIRST is that index. If LAST is nonzero, then initialize a range
6611 of indices, running from FIRST through LAST. */
6614 set_init_index (tree first
, tree last
)
6616 if (set_designator (1))
6619 designator_erroneous
= 1;
6621 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
6622 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
6624 error_init ("array index in initializer not of integer type");
6628 if (TREE_CODE (first
) != INTEGER_CST
)
6630 first
= c_fully_fold (first
, false, NULL
);
6631 if (TREE_CODE (first
) == INTEGER_CST
)
6632 pedwarn_init (input_location
, OPT_pedantic
,
6633 "array index in initializer is not "
6634 "an integer constant expression");
6637 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
6639 last
= c_fully_fold (last
, false, NULL
);
6640 if (TREE_CODE (last
) == INTEGER_CST
)
6641 pedwarn_init (input_location
, OPT_pedantic
,
6642 "array index in initializer is not "
6643 "an integer constant expression");
6646 if (TREE_CODE (first
) != INTEGER_CST
)
6647 error_init ("nonconstant array index in initializer");
6648 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
6649 error_init ("nonconstant array index in initializer");
6650 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
6651 error_init ("array index in non-array initializer");
6652 else if (tree_int_cst_sgn (first
) == -1)
6653 error_init ("array index in initializer exceeds array bounds");
6654 else if (constructor_max_index
6655 && tree_int_cst_lt (constructor_max_index
, first
))
6656 error_init ("array index in initializer exceeds array bounds");
6659 constant_expression_warning (first
);
6661 constant_expression_warning (last
);
6662 constructor_index
= convert (bitsizetype
, first
);
6666 if (tree_int_cst_equal (first
, last
))
6668 else if (tree_int_cst_lt (last
, first
))
6670 error_init ("empty index range in initializer");
6675 last
= convert (bitsizetype
, last
);
6676 if (constructor_max_index
!= 0
6677 && tree_int_cst_lt (constructor_max_index
, last
))
6679 error_init ("array index range in initializer exceeds array bounds");
6686 designator_erroneous
= 0;
6687 if (constructor_range_stack
|| last
)
6688 push_range_stack (last
);
6692 /* Within a struct initializer, specify the next field to be initialized. */
6695 set_init_label (tree fieldname
)
6699 if (set_designator (0))
6702 designator_erroneous
= 1;
6704 if (TREE_CODE (constructor_type
) != RECORD_TYPE
6705 && TREE_CODE (constructor_type
) != UNION_TYPE
)
6707 error_init ("field name not in record or union initializer");
6711 for (tail
= TYPE_FIELDS (constructor_type
); tail
;
6712 tail
= TREE_CHAIN (tail
))
6714 if (DECL_NAME (tail
) == fieldname
)
6719 error ("unknown field %qE specified in initializer", fieldname
);
6722 constructor_fields
= tail
;
6724 designator_erroneous
= 0;
6725 if (constructor_range_stack
)
6726 push_range_stack (NULL_TREE
);
6730 /* Add a new initializer to the tree of pending initializers. PURPOSE
6731 identifies the initializer, either array index or field in a structure.
6732 VALUE is the value of that index or field. If ORIGTYPE is not
6733 NULL_TREE, it is the original type of VALUE.
6735 IMPLICIT is true if value comes from pop_init_level (1),
6736 the new initializer has been merged with the existing one
6737 and thus no warnings should be emitted about overriding an
6738 existing initializer. */
6741 add_pending_init (tree purpose
, tree value
, tree origtype
, bool implicit
)
6743 struct init_node
*p
, **q
, *r
;
6745 q
= &constructor_pending_elts
;
6748 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6753 if (tree_int_cst_lt (purpose
, p
->purpose
))
6755 else if (tree_int_cst_lt (p
->purpose
, purpose
))
6761 if (TREE_SIDE_EFFECTS (p
->value
))
6762 warning_init (0, "initialized field with side-effects overwritten");
6763 else if (warn_override_init
)
6764 warning_init (OPT_Woverride_init
, "initialized field overwritten");
6767 p
->origtype
= origtype
;
6776 bitpos
= bit_position (purpose
);
6780 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
6782 else if (p
->purpose
!= purpose
)
6788 if (TREE_SIDE_EFFECTS (p
->value
))
6789 warning_init (0, "initialized field with side-effects overwritten");
6790 else if (warn_override_init
)
6791 warning_init (OPT_Woverride_init
, "initialized field overwritten");
6794 p
->origtype
= origtype
;
6800 r
= GGC_NEW (struct init_node
);
6801 r
->purpose
= purpose
;
6803 r
->origtype
= origtype
;
6813 struct init_node
*s
;
6817 if (p
->balance
== 0)
6819 else if (p
->balance
< 0)
6826 p
->left
->parent
= p
;
6843 constructor_pending_elts
= r
;
6848 struct init_node
*t
= r
->right
;
6852 r
->right
->parent
= r
;
6857 p
->left
->parent
= p
;
6860 p
->balance
= t
->balance
< 0;
6861 r
->balance
= -(t
->balance
> 0);
6876 constructor_pending_elts
= t
;
6882 /* p->balance == +1; growth of left side balances the node. */
6887 else /* r == p->right */
6889 if (p
->balance
== 0)
6890 /* Growth propagation from right side. */
6892 else if (p
->balance
> 0)
6899 p
->right
->parent
= p
;
6916 constructor_pending_elts
= r
;
6918 else /* r->balance == -1 */
6921 struct init_node
*t
= r
->left
;
6925 r
->left
->parent
= r
;
6930 p
->right
->parent
= p
;
6933 r
->balance
= (t
->balance
< 0);
6934 p
->balance
= -(t
->balance
> 0);
6949 constructor_pending_elts
= t
;
6955 /* p->balance == -1; growth of right side balances the node. */
6966 /* Build AVL tree from a sorted chain. */
6969 set_nonincremental_init (void)
6971 unsigned HOST_WIDE_INT ix
;
6974 if (TREE_CODE (constructor_type
) != RECORD_TYPE
6975 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
6978 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
6979 add_pending_init (index
, value
, NULL_TREE
, false);
6980 constructor_elements
= 0;
6981 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
6983 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
6984 /* Skip any nameless bit fields at the beginning. */
6985 while (constructor_unfilled_fields
!= 0
6986 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
6987 && DECL_NAME (constructor_unfilled_fields
) == 0)
6988 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
6991 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6993 if (TYPE_DOMAIN (constructor_type
))
6994 constructor_unfilled_index
6995 = convert (bitsizetype
,
6996 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
6998 constructor_unfilled_index
= bitsize_zero_node
;
7000 constructor_incremental
= 0;
7003 /* Build AVL tree from a string constant. */
7006 set_nonincremental_init_from_string (tree str
)
7008 tree value
, purpose
, type
;
7009 HOST_WIDE_INT val
[2];
7010 const char *p
, *end
;
7011 int byte
, wchar_bytes
, charwidth
, bitpos
;
7013 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
7015 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
7016 charwidth
= TYPE_PRECISION (char_type_node
);
7017 type
= TREE_TYPE (constructor_type
);
7018 p
= TREE_STRING_POINTER (str
);
7019 end
= p
+ TREE_STRING_LENGTH (str
);
7021 for (purpose
= bitsize_zero_node
;
7022 p
< end
&& !tree_int_cst_lt (constructor_max_index
, purpose
);
7023 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
7025 if (wchar_bytes
== 1)
7027 val
[1] = (unsigned char) *p
++;
7034 for (byte
= 0; byte
< wchar_bytes
; byte
++)
7036 if (BYTES_BIG_ENDIAN
)
7037 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
7039 bitpos
= byte
* charwidth
;
7040 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
7041 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
7042 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
7046 if (!TYPE_UNSIGNED (type
))
7048 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
7049 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
7051 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
7053 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
7057 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
7062 else if (val
[0] & (((HOST_WIDE_INT
) 1)
7063 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
7064 val
[0] |= ((HOST_WIDE_INT
) -1)
7065 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
7068 value
= build_int_cst_wide (type
, val
[1], val
[0]);
7069 add_pending_init (purpose
, value
, NULL_TREE
, false);
7072 constructor_incremental
= 0;
7075 /* Return value of FIELD in pending initializer or zero if the field was
7076 not initialized yet. */
7079 find_init_member (tree field
)
7081 struct init_node
*p
;
7083 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7085 if (constructor_incremental
7086 && tree_int_cst_lt (field
, constructor_unfilled_index
))
7087 set_nonincremental_init ();
7089 p
= constructor_pending_elts
;
7092 if (tree_int_cst_lt (field
, p
->purpose
))
7094 else if (tree_int_cst_lt (p
->purpose
, field
))
7100 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7102 tree bitpos
= bit_position (field
);
7104 if (constructor_incremental
7105 && (!constructor_unfilled_fields
7106 || tree_int_cst_lt (bitpos
,
7107 bit_position (constructor_unfilled_fields
))))
7108 set_nonincremental_init ();
7110 p
= constructor_pending_elts
;
7113 if (field
== p
->purpose
)
7115 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7121 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7123 if (!VEC_empty (constructor_elt
, constructor_elements
)
7124 && (VEC_last (constructor_elt
, constructor_elements
)->index
7126 return VEC_last (constructor_elt
, constructor_elements
)->value
;
7131 /* "Output" the next constructor element.
7132 At top level, really output it to assembler code now.
7133 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7134 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7135 TYPE is the data type that the containing data type wants here.
7136 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7137 If VALUE is a string constant, STRICT_STRING is true if it is
7138 unparenthesized or we should not warn here for it being parenthesized.
7139 For other types of VALUE, STRICT_STRING is not used.
7141 PENDING if non-nil means output pending elements that belong
7142 right after this element. (PENDING is normally 1;
7143 it is 0 while outputting pending elements, to avoid recursion.)
7145 IMPLICIT is true if value comes from pop_init_level (1),
7146 the new initializer has been merged with the existing one
7147 and thus no warnings should be emitted about overriding an
7148 existing initializer. */
7151 output_init_element (tree value
, tree origtype
, bool strict_string
, tree type
,
7152 tree field
, int pending
, bool implicit
)
7154 tree semantic_type
= NULL_TREE
;
7155 constructor_elt
*celt
;
7156 bool maybe_const
= true;
7159 if (type
== error_mark_node
|| value
== error_mark_node
)
7161 constructor_erroneous
= 1;
7164 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
7165 && (TREE_CODE (value
) == STRING_CST
7166 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
7167 && !(TREE_CODE (value
) == STRING_CST
7168 && TREE_CODE (type
) == ARRAY_TYPE
7169 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
7170 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
7171 TYPE_MAIN_VARIANT (type
)))
7172 value
= array_to_pointer_conversion (input_location
, value
);
7174 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
7175 && require_constant_value
&& !flag_isoc99
&& pending
)
7177 /* As an extension, allow initializing objects with static storage
7178 duration with compound literals (which are then treated just as
7179 the brace enclosed list they contain). */
7180 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
7181 value
= DECL_INITIAL (decl
);
7184 npc
= null_pointer_constant_p (value
);
7185 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
7187 semantic_type
= TREE_TYPE (value
);
7188 value
= TREE_OPERAND (value
, 0);
7190 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
7192 if (value
== error_mark_node
)
7193 constructor_erroneous
= 1;
7194 else if (!TREE_CONSTANT (value
))
7195 constructor_constant
= 0;
7196 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
7197 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
7198 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7199 && DECL_C_BIT_FIELD (field
)
7200 && TREE_CODE (value
) != INTEGER_CST
))
7201 constructor_simple
= 0;
7203 constructor_nonconst
= 1;
7205 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
7207 if (require_constant_value
)
7209 error_init ("initializer element is not constant");
7210 value
= error_mark_node
;
7212 else if (require_constant_elements
)
7213 pedwarn (input_location
, 0,
7214 "initializer element is not computable at load time");
7216 else if (!maybe_const
7217 && (require_constant_value
|| require_constant_elements
))
7218 pedwarn_init (input_location
, 0,
7219 "initializer element is not a constant expression");
7221 /* Issue -Wc++-compat warnings about initializing a bitfield with
7224 && field
!= NULL_TREE
7225 && TREE_CODE (field
) == FIELD_DECL
7226 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
7227 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
7228 != TYPE_MAIN_VARIANT (type
))
7229 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
7231 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
7232 if (checktype
!= error_mark_node
7233 && (TYPE_MAIN_VARIANT (checktype
)
7234 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
7235 warning_init (OPT_Wc___compat
,
7236 "enum conversion in initialization is invalid in C++");
7239 /* If this field is empty (and not at the end of structure),
7240 don't do anything other than checking the initializer. */
7242 && (TREE_TYPE (field
) == error_mark_node
7243 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
7244 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
7245 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
7246 || TREE_CHAIN (field
)))))
7250 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
7251 value
= digest_init (input_location
, type
, value
, origtype
, npc
,
7252 strict_string
, require_constant_value
);
7253 if (value
== error_mark_node
)
7255 constructor_erroneous
= 1;
7258 if (require_constant_value
|| require_constant_elements
)
7259 constant_expression_warning (value
);
7261 /* If this element doesn't come next in sequence,
7262 put it on constructor_pending_elts. */
7263 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7264 && (!constructor_incremental
7265 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
7267 if (constructor_incremental
7268 && tree_int_cst_lt (field
, constructor_unfilled_index
))
7269 set_nonincremental_init ();
7271 add_pending_init (field
, value
, origtype
, implicit
);
7274 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7275 && (!constructor_incremental
7276 || field
!= constructor_unfilled_fields
))
7278 /* We do this for records but not for unions. In a union,
7279 no matter which field is specified, it can be initialized
7280 right away since it starts at the beginning of the union. */
7281 if (constructor_incremental
)
7283 if (!constructor_unfilled_fields
)
7284 set_nonincremental_init ();
7287 tree bitpos
, unfillpos
;
7289 bitpos
= bit_position (field
);
7290 unfillpos
= bit_position (constructor_unfilled_fields
);
7292 if (tree_int_cst_lt (bitpos
, unfillpos
))
7293 set_nonincremental_init ();
7297 add_pending_init (field
, value
, origtype
, implicit
);
7300 else if (TREE_CODE (constructor_type
) == UNION_TYPE
7301 && !VEC_empty (constructor_elt
, constructor_elements
))
7305 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt
,
7306 constructor_elements
)->value
))
7308 "initialized field with side-effects overwritten");
7309 else if (warn_override_init
)
7310 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7313 /* We can have just one union field set. */
7314 constructor_elements
= 0;
7317 /* Otherwise, output this element either to
7318 constructor_elements or to the assembler file. */
7320 celt
= VEC_safe_push (constructor_elt
, gc
, constructor_elements
, NULL
);
7321 celt
->index
= field
;
7322 celt
->value
= value
;
7324 /* Advance the variable that indicates sequential elements output. */
7325 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7326 constructor_unfilled_index
7327 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
7329 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7331 constructor_unfilled_fields
7332 = TREE_CHAIN (constructor_unfilled_fields
);
7334 /* Skip any nameless bit fields. */
7335 while (constructor_unfilled_fields
!= 0
7336 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7337 && DECL_NAME (constructor_unfilled_fields
) == 0)
7338 constructor_unfilled_fields
=
7339 TREE_CHAIN (constructor_unfilled_fields
);
7341 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7342 constructor_unfilled_fields
= 0;
7344 /* Now output any pending elements which have become next. */
7346 output_pending_init_elements (0);
7349 /* Output any pending elements which have become next.
7350 As we output elements, constructor_unfilled_{fields,index}
7351 advances, which may cause other elements to become next;
7352 if so, they too are output.
7354 If ALL is 0, we return when there are
7355 no more pending elements to output now.
7357 If ALL is 1, we output space as necessary so that
7358 we can output all the pending elements. */
7361 output_pending_init_elements (int all
)
7363 struct init_node
*elt
= constructor_pending_elts
;
7368 /* Look through the whole pending tree.
7369 If we find an element that should be output now,
7370 output it. Otherwise, set NEXT to the element
7371 that comes first among those still pending. */
7376 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7378 if (tree_int_cst_equal (elt
->purpose
,
7379 constructor_unfilled_index
))
7380 output_init_element (elt
->value
, elt
->origtype
, true,
7381 TREE_TYPE (constructor_type
),
7382 constructor_unfilled_index
, 0, false);
7383 else if (tree_int_cst_lt (constructor_unfilled_index
,
7386 /* Advance to the next smaller node. */
7391 /* We have reached the smallest node bigger than the
7392 current unfilled index. Fill the space first. */
7393 next
= elt
->purpose
;
7399 /* Advance to the next bigger node. */
7404 /* We have reached the biggest node in a subtree. Find
7405 the parent of it, which is the next bigger node. */
7406 while (elt
->parent
&& elt
->parent
->right
== elt
)
7409 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
7412 next
= elt
->purpose
;
7418 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7419 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7421 tree ctor_unfilled_bitpos
, elt_bitpos
;
7423 /* If the current record is complete we are done. */
7424 if (constructor_unfilled_fields
== 0)
7427 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
7428 elt_bitpos
= bit_position (elt
->purpose
);
7429 /* We can't compare fields here because there might be empty
7430 fields in between. */
7431 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
7433 constructor_unfilled_fields
= elt
->purpose
;
7434 output_init_element (elt
->value
, elt
->origtype
, true,
7435 TREE_TYPE (elt
->purpose
),
7436 elt
->purpose
, 0, false);
7438 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
7440 /* Advance to the next smaller node. */
7445 /* We have reached the smallest node bigger than the
7446 current unfilled field. Fill the space first. */
7447 next
= elt
->purpose
;
7453 /* Advance to the next bigger node. */
7458 /* We have reached the biggest node in a subtree. Find
7459 the parent of it, which is the next bigger node. */
7460 while (elt
->parent
&& elt
->parent
->right
== elt
)
7464 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
7465 bit_position (elt
->purpose
))))
7467 next
= elt
->purpose
;
7475 /* Ordinarily return, but not if we want to output all
7476 and there are elements left. */
7477 if (!(all
&& next
!= 0))
7480 /* If it's not incremental, just skip over the gap, so that after
7481 jumping to retry we will output the next successive element. */
7482 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7483 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7484 constructor_unfilled_fields
= next
;
7485 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7486 constructor_unfilled_index
= next
;
7488 /* ELT now points to the node in the pending tree with the next
7489 initializer to output. */
7493 /* Add one non-braced element to the current constructor level.
7494 This adjusts the current position within the constructor's type.
7495 This may also start or terminate implicit levels
7496 to handle a partly-braced initializer.
7498 Once this has found the correct level for the new element,
7499 it calls output_init_element.
7501 IMPLICIT is true if value comes from pop_init_level (1),
7502 the new initializer has been merged with the existing one
7503 and thus no warnings should be emitted about overriding an
7504 existing initializer. */
7507 process_init_element (struct c_expr value
, bool implicit
)
7509 tree orig_value
= value
.value
;
7510 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
7511 bool strict_string
= value
.original_code
== STRING_CST
;
7513 designator_depth
= 0;
7514 designator_erroneous
= 0;
7516 /* Handle superfluous braces around string cst as in
7517 char x[] = {"foo"}; */
7520 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7521 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
7522 && integer_zerop (constructor_unfilled_index
))
7524 if (constructor_stack
->replacement_value
.value
)
7525 error_init ("excess elements in char array initializer");
7526 constructor_stack
->replacement_value
= value
;
7530 if (constructor_stack
->replacement_value
.value
!= 0)
7532 error_init ("excess elements in struct initializer");
7536 /* Ignore elements of a brace group if it is entirely superfluous
7537 and has already been diagnosed. */
7538 if (constructor_type
== 0)
7541 /* If we've exhausted any levels that didn't have braces,
7543 while (constructor_stack
->implicit
)
7545 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7546 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7547 && constructor_fields
== 0)
7548 process_init_element (pop_init_level (1), true);
7549 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
7550 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7551 && (constructor_max_index
== 0
7552 || tree_int_cst_lt (constructor_max_index
,
7553 constructor_index
)))
7554 process_init_element (pop_init_level (1), true);
7559 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7560 if (constructor_range_stack
)
7562 /* If value is a compound literal and we'll be just using its
7563 content, don't put it into a SAVE_EXPR. */
7564 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
7565 || !require_constant_value
7568 tree semantic_type
= NULL_TREE
;
7569 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
7571 semantic_type
= TREE_TYPE (value
.value
);
7572 value
.value
= TREE_OPERAND (value
.value
, 0);
7574 value
.value
= c_save_expr (value
.value
);
7576 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7583 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7586 enum tree_code fieldcode
;
7588 if (constructor_fields
== 0)
7590 pedwarn_init (input_location
, 0,
7591 "excess elements in struct initializer");
7595 fieldtype
= TREE_TYPE (constructor_fields
);
7596 if (fieldtype
!= error_mark_node
)
7597 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
7598 fieldcode
= TREE_CODE (fieldtype
);
7600 /* Error for non-static initialization of a flexible array member. */
7601 if (fieldcode
== ARRAY_TYPE
7602 && !require_constant_value
7603 && TYPE_SIZE (fieldtype
) == NULL_TREE
7604 && TREE_CHAIN (constructor_fields
) == NULL_TREE
)
7606 error_init ("non-static initialization of a flexible array member");
7610 /* Accept a string constant to initialize a subarray. */
7611 if (value
.value
!= 0
7612 && fieldcode
== ARRAY_TYPE
7613 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
7615 value
.value
= orig_value
;
7616 /* Otherwise, if we have come to a subaggregate,
7617 and we don't have an element of its type, push into it. */
7618 else if (value
.value
!= 0
7619 && value
.value
!= error_mark_node
7620 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
7621 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
7622 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
7624 push_init_level (1);
7630 push_member_name (constructor_fields
);
7631 output_init_element (value
.value
, value
.original_type
,
7632 strict_string
, fieldtype
,
7633 constructor_fields
, 1, implicit
);
7634 RESTORE_SPELLING_DEPTH (constructor_depth
);
7637 /* Do the bookkeeping for an element that was
7638 directly output as a constructor. */
7640 /* For a record, keep track of end position of last field. */
7641 if (DECL_SIZE (constructor_fields
))
7642 constructor_bit_index
7643 = size_binop_loc (input_location
, PLUS_EXPR
,
7644 bit_position (constructor_fields
),
7645 DECL_SIZE (constructor_fields
));
7647 /* If the current field was the first one not yet written out,
7648 it isn't now, so update. */
7649 if (constructor_unfilled_fields
== constructor_fields
)
7651 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
7652 /* Skip any nameless bit fields. */
7653 while (constructor_unfilled_fields
!= 0
7654 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7655 && DECL_NAME (constructor_unfilled_fields
) == 0)
7656 constructor_unfilled_fields
=
7657 TREE_CHAIN (constructor_unfilled_fields
);
7661 constructor_fields
= TREE_CHAIN (constructor_fields
);
7662 /* Skip any nameless bit fields at the beginning. */
7663 while (constructor_fields
!= 0
7664 && DECL_C_BIT_FIELD (constructor_fields
)
7665 && DECL_NAME (constructor_fields
) == 0)
7666 constructor_fields
= TREE_CHAIN (constructor_fields
);
7668 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7671 enum tree_code fieldcode
;
7673 if (constructor_fields
== 0)
7675 pedwarn_init (input_location
, 0,
7676 "excess elements in union initializer");
7680 fieldtype
= TREE_TYPE (constructor_fields
);
7681 if (fieldtype
!= error_mark_node
)
7682 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
7683 fieldcode
= TREE_CODE (fieldtype
);
7685 /* Warn that traditional C rejects initialization of unions.
7686 We skip the warning if the value is zero. This is done
7687 under the assumption that the zero initializer in user
7688 code appears conditioned on e.g. __STDC__ to avoid
7689 "missing initializer" warnings and relies on default
7690 initialization to zero in the traditional C case.
7691 We also skip the warning if the initializer is designated,
7692 again on the assumption that this must be conditional on
7693 __STDC__ anyway (and we've already complained about the
7694 member-designator already). */
7695 if (!in_system_header
&& !constructor_designated
7696 && !(value
.value
&& (integer_zerop (value
.value
)
7697 || real_zerop (value
.value
))))
7698 warning (OPT_Wtraditional
, "traditional C rejects initialization "
7701 /* Accept a string constant to initialize a subarray. */
7702 if (value
.value
!= 0
7703 && fieldcode
== ARRAY_TYPE
7704 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
7706 value
.value
= orig_value
;
7707 /* Otherwise, if we have come to a subaggregate,
7708 and we don't have an element of its type, push into it. */
7709 else if (value
.value
!= 0
7710 && value
.value
!= error_mark_node
7711 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
7712 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
7713 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
7715 push_init_level (1);
7721 push_member_name (constructor_fields
);
7722 output_init_element (value
.value
, value
.original_type
,
7723 strict_string
, fieldtype
,
7724 constructor_fields
, 1, implicit
);
7725 RESTORE_SPELLING_DEPTH (constructor_depth
);
7728 /* Do the bookkeeping for an element that was
7729 directly output as a constructor. */
7731 constructor_bit_index
= DECL_SIZE (constructor_fields
);
7732 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
7735 constructor_fields
= 0;
7737 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7739 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7740 enum tree_code eltcode
= TREE_CODE (elttype
);
7742 /* Accept a string constant to initialize a subarray. */
7743 if (value
.value
!= 0
7744 && eltcode
== ARRAY_TYPE
7745 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
7747 value
.value
= orig_value
;
7748 /* Otherwise, if we have come to a subaggregate,
7749 and we don't have an element of its type, push into it. */
7750 else if (value
.value
!= 0
7751 && value
.value
!= error_mark_node
7752 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
7753 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
7754 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
7756 push_init_level (1);
7760 if (constructor_max_index
!= 0
7761 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
7762 || integer_all_onesp (constructor_max_index
)))
7764 pedwarn_init (input_location
, 0,
7765 "excess elements in array initializer");
7769 /* Now output the actual element. */
7772 push_array_bounds (tree_low_cst (constructor_index
, 1));
7773 output_init_element (value
.value
, value
.original_type
,
7774 strict_string
, elttype
,
7775 constructor_index
, 1, implicit
);
7776 RESTORE_SPELLING_DEPTH (constructor_depth
);
7780 = size_binop_loc (input_location
, PLUS_EXPR
,
7781 constructor_index
, bitsize_one_node
);
7784 /* If we are doing the bookkeeping for an element that was
7785 directly output as a constructor, we must update
7786 constructor_unfilled_index. */
7787 constructor_unfilled_index
= constructor_index
;
7789 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7791 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7793 /* Do a basic check of initializer size. Note that vectors
7794 always have a fixed size derived from their type. */
7795 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
7797 pedwarn_init (input_location
, 0,
7798 "excess elements in vector initializer");
7802 /* Now output the actual element. */
7805 if (TREE_CODE (value
.value
) == VECTOR_CST
)
7806 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
7807 output_init_element (value
.value
, value
.original_type
,
7808 strict_string
, elttype
,
7809 constructor_index
, 1, implicit
);
7813 = size_binop_loc (input_location
,
7814 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
7817 /* If we are doing the bookkeeping for an element that was
7818 directly output as a constructor, we must update
7819 constructor_unfilled_index. */
7820 constructor_unfilled_index
= constructor_index
;
7823 /* Handle the sole element allowed in a braced initializer
7824 for a scalar variable. */
7825 else if (constructor_type
!= error_mark_node
7826 && constructor_fields
== 0)
7828 pedwarn_init (input_location
, 0,
7829 "excess elements in scalar initializer");
7835 output_init_element (value
.value
, value
.original_type
,
7836 strict_string
, constructor_type
,
7837 NULL_TREE
, 1, implicit
);
7838 constructor_fields
= 0;
7841 /* Handle range initializers either at this level or anywhere higher
7842 in the designator stack. */
7843 if (constructor_range_stack
)
7845 struct constructor_range_stack
*p
, *range_stack
;
7848 range_stack
= constructor_range_stack
;
7849 constructor_range_stack
= 0;
7850 while (constructor_stack
!= range_stack
->stack
)
7852 gcc_assert (constructor_stack
->implicit
);
7853 process_init_element (pop_init_level (1), true);
7855 for (p
= range_stack
;
7856 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
7859 gcc_assert (constructor_stack
->implicit
);
7860 process_init_element (pop_init_level (1), true);
7863 p
->index
= size_binop_loc (input_location
,
7864 PLUS_EXPR
, p
->index
, bitsize_one_node
);
7865 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
7870 constructor_index
= p
->index
;
7871 constructor_fields
= p
->fields
;
7872 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
7880 push_init_level (2);
7881 p
->stack
= constructor_stack
;
7882 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
7883 p
->index
= p
->range_start
;
7887 constructor_range_stack
= range_stack
;
7894 constructor_range_stack
= 0;
7897 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7898 (guaranteed to be 'volatile' or null) and ARGS (represented using
7899 an ASM_EXPR node). */
7901 build_asm_stmt (tree cv_qualifier
, tree args
)
7903 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
7904 ASM_VOLATILE_P (args
) = 1;
7905 return add_stmt (args
);
7908 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7909 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7910 SIMPLE indicates whether there was anything at all after the
7911 string in the asm expression -- asm("blah") and asm("blah" : )
7912 are subtly different. We use a ASM_EXPR node to represent this. */
7914 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
7915 tree clobbers
, tree labels
, bool simple
)
7920 const char *constraint
;
7921 const char **oconstraints
;
7922 bool allows_mem
, allows_reg
, is_inout
;
7923 int ninputs
, noutputs
;
7925 ninputs
= list_length (inputs
);
7926 noutputs
= list_length (outputs
);
7927 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
7929 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
7931 /* Remove output conversions that change the type but not the mode. */
7932 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
7934 tree output
= TREE_VALUE (tail
);
7936 /* ??? Really, this should not be here. Users should be using a
7937 proper lvalue, dammit. But there's a long history of using casts
7938 in the output operands. In cases like longlong.h, this becomes a
7939 primitive form of typechecking -- if the cast can be removed, then
7940 the output operand had a type of the proper width; otherwise we'll
7941 get an error. Gross, but ... */
7942 STRIP_NOPS (output
);
7944 if (!lvalue_or_else (output
, lv_asm
))
7945 output
= error_mark_node
;
7947 if (output
!= error_mark_node
7948 && (TREE_READONLY (output
)
7949 || TYPE_READONLY (TREE_TYPE (output
))
7950 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
7951 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
7952 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
7953 readonly_error (output
, lv_asm
);
7955 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
7956 oconstraints
[i
] = constraint
;
7958 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
7959 &allows_mem
, &allows_reg
, &is_inout
))
7961 /* If the operand is going to end up in memory,
7962 mark it addressable. */
7963 if (!allows_reg
&& !c_mark_addressable (output
))
7964 output
= error_mark_node
;
7967 output
= error_mark_node
;
7969 TREE_VALUE (tail
) = output
;
7972 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
7976 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
7977 input
= TREE_VALUE (tail
);
7979 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
7980 oconstraints
, &allows_mem
, &allows_reg
))
7982 /* If the operand is going to end up in memory,
7983 mark it addressable. */
7984 if (!allows_reg
&& allows_mem
)
7986 /* Strip the nops as we allow this case. FIXME, this really
7987 should be rejected or made deprecated. */
7989 if (!c_mark_addressable (input
))
7990 input
= error_mark_node
;
7994 input
= error_mark_node
;
7996 TREE_VALUE (tail
) = input
;
7999 /* ASMs with labels cannot have outputs. This should have been
8000 enforced by the parser. */
8001 gcc_assert (outputs
== NULL
|| labels
== NULL
);
8003 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
8005 /* asm statements without outputs, including simple ones, are treated
8007 ASM_INPUT_P (args
) = simple
;
8008 ASM_VOLATILE_P (args
) = (noutputs
== 0);
8013 /* Generate a goto statement to LABEL. LOC is the location of the
8017 c_finish_goto_label (location_t loc
, tree label
)
8019 tree decl
= lookup_label_for_goto (loc
, label
);
8022 TREE_USED (decl
) = 1;
8024 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
8025 SET_EXPR_LOCATION (t
, loc
);
8026 return add_stmt (t
);
8030 /* Generate a computed goto statement to EXPR. LOC is the location of
8034 c_finish_goto_ptr (location_t loc
, tree expr
)
8037 pedwarn (loc
, OPT_pedantic
, "ISO C forbids %<goto *expr;%>");
8038 expr
= c_fully_fold (expr
, false, NULL
);
8039 expr
= convert (ptr_type_node
, expr
);
8040 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
8041 SET_EXPR_LOCATION (t
, loc
);
8042 return add_stmt (t
);
8045 /* Generate a C `return' statement. RETVAL is the expression for what
8046 to return, or a null pointer for `return;' with no value. LOC is
8047 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8048 is the original type of RETVAL. */
8051 c_finish_return (location_t loc
, tree retval
, tree origtype
)
8053 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
8054 bool no_warning
= false;
8057 if (TREE_THIS_VOLATILE (current_function_decl
))
8059 "function declared %<noreturn%> has a %<return%> statement");
8063 tree semantic_type
= NULL_TREE
;
8064 npc
= null_pointer_constant_p (retval
);
8065 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
8067 semantic_type
= TREE_TYPE (retval
);
8068 retval
= TREE_OPERAND (retval
, 0);
8070 retval
= c_fully_fold (retval
, false, NULL
);
8072 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
8077 current_function_returns_null
= 1;
8078 if ((warn_return_type
|| flag_isoc99
)
8079 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
8081 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wreturn_type
,
8082 "%<return%> with no value, in "
8083 "function returning non-void");
8087 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
8089 current_function_returns_null
= 1;
8090 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
8092 "%<return%> with a value, in function returning void");
8094 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
8095 "%<return%> with expression, in function returning void");
8099 tree t
= convert_for_assignment (loc
, valtype
, retval
, origtype
,
8101 npc
, NULL_TREE
, NULL_TREE
, 0);
8102 tree res
= DECL_RESULT (current_function_decl
);
8105 current_function_returns_value
= 1;
8106 if (t
== error_mark_node
)
8109 inner
= t
= convert (TREE_TYPE (res
), t
);
8111 /* Strip any conversions, additions, and subtractions, and see if
8112 we are returning the address of a local variable. Warn if so. */
8115 switch (TREE_CODE (inner
))
8118 case NON_LVALUE_EXPR
:
8120 case POINTER_PLUS_EXPR
:
8121 inner
= TREE_OPERAND (inner
, 0);
8125 /* If the second operand of the MINUS_EXPR has a pointer
8126 type (or is converted from it), this may be valid, so
8127 don't give a warning. */
8129 tree op1
= TREE_OPERAND (inner
, 1);
8131 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
8132 && (CONVERT_EXPR_P (op1
)
8133 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
8134 op1
= TREE_OPERAND (op1
, 0);
8136 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
8139 inner
= TREE_OPERAND (inner
, 0);
8144 inner
= TREE_OPERAND (inner
, 0);
8146 while (REFERENCE_CLASS_P (inner
)
8147 && TREE_CODE (inner
) != INDIRECT_REF
)
8148 inner
= TREE_OPERAND (inner
, 0);
8151 && !DECL_EXTERNAL (inner
)
8152 && !TREE_STATIC (inner
)
8153 && DECL_CONTEXT (inner
) == current_function_decl
)
8155 0, "function returns address of local variable");
8165 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
8166 SET_EXPR_LOCATION (retval
, loc
);
8168 if (warn_sequence_point
)
8169 verify_sequence_points (retval
);
8172 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
8173 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
8174 return add_stmt (ret_stmt
);
8178 /* The SWITCH_EXPR being built. */
8181 /* The original type of the testing expression, i.e. before the
8182 default conversion is applied. */
8185 /* A splay-tree mapping the low element of a case range to the high
8186 element, or NULL_TREE if there is no high element. Used to
8187 determine whether or not a new case label duplicates an old case
8188 label. We need a tree, rather than simply a hash table, because
8189 of the GNU case range extension. */
8192 /* The bindings at the point of the switch. This is used for
8193 warnings crossing decls when branching to a case label. */
8194 struct c_spot_bindings
*bindings
;
8196 /* The next node on the stack. */
8197 struct c_switch
*next
;
8200 /* A stack of the currently active switch statements. The innermost
8201 switch statement is on the top of the stack. There is no need to
8202 mark the stack for garbage collection because it is only active
8203 during the processing of the body of a function, and we never
8204 collect at that point. */
8206 struct c_switch
*c_switch_stack
;
8208 /* Start a C switch statement, testing expression EXP. Return the new
8209 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8210 SWITCH_COND_LOC is the location of the switch's condition. */
8213 c_start_case (location_t switch_loc
,
8214 location_t switch_cond_loc
,
8217 tree orig_type
= error_mark_node
;
8218 struct c_switch
*cs
;
8220 if (exp
!= error_mark_node
)
8222 orig_type
= TREE_TYPE (exp
);
8224 if (!INTEGRAL_TYPE_P (orig_type
))
8226 if (orig_type
!= error_mark_node
)
8228 error_at (switch_cond_loc
, "switch quantity not an integer");
8229 orig_type
= error_mark_node
;
8231 exp
= integer_zero_node
;
8235 tree type
= TYPE_MAIN_VARIANT (orig_type
);
8237 if (!in_system_header
8238 && (type
== long_integer_type_node
8239 || type
== long_unsigned_type_node
))
8240 warning_at (switch_cond_loc
,
8241 OPT_Wtraditional
, "%<long%> switch expression not "
8242 "converted to %<int%> in ISO C");
8244 exp
= c_fully_fold (exp
, false, NULL
);
8245 exp
= default_conversion (exp
);
8247 if (warn_sequence_point
)
8248 verify_sequence_points (exp
);
8252 /* Add this new SWITCH_EXPR to the stack. */
8253 cs
= XNEW (struct c_switch
);
8254 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
8255 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
8256 cs
->orig_type
= orig_type
;
8257 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
8258 cs
->bindings
= c_get_switch_bindings ();
8259 cs
->next
= c_switch_stack
;
8260 c_switch_stack
= cs
;
8262 return add_stmt (cs
->switch_expr
);
8265 /* Process a case label at location LOC. */
8268 do_case (location_t loc
, tree low_value
, tree high_value
)
8270 tree label
= NULL_TREE
;
8272 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
8274 low_value
= c_fully_fold (low_value
, false, NULL
);
8275 if (TREE_CODE (low_value
) == INTEGER_CST
)
8276 pedwarn (input_location
, OPT_pedantic
,
8277 "case label is not an integer constant expression");
8280 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
8282 high_value
= c_fully_fold (high_value
, false, NULL
);
8283 if (TREE_CODE (high_value
) == INTEGER_CST
)
8284 pedwarn (input_location
, OPT_pedantic
,
8285 "case label is not an integer constant expression");
8288 if (c_switch_stack
== NULL
)
8291 error_at (loc
, "case label not within a switch statement");
8293 error_at (loc
, "%<default%> label not within a switch statement");
8297 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
8298 EXPR_LOCATION (c_switch_stack
->switch_expr
),
8302 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
8303 SWITCH_COND (c_switch_stack
->switch_expr
),
8304 c_switch_stack
->orig_type
,
8305 low_value
, high_value
);
8306 if (label
== error_mark_node
)
8311 /* Finish the switch statement. */
8314 c_finish_case (tree body
)
8316 struct c_switch
*cs
= c_switch_stack
;
8317 location_t switch_location
;
8319 SWITCH_BODY (cs
->switch_expr
) = body
;
8321 /* Emit warnings as needed. */
8322 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
8323 c_do_switch_warnings (cs
->cases
, switch_location
,
8324 TREE_TYPE (cs
->switch_expr
),
8325 SWITCH_COND (cs
->switch_expr
));
8327 /* Pop the stack. */
8328 c_switch_stack
= cs
->next
;
8329 splay_tree_delete (cs
->cases
);
8330 c_release_switch_bindings (cs
->bindings
);
8334 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8335 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8336 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8337 statement, and was not surrounded with parenthesis. */
8340 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
8341 tree else_block
, bool nested_if
)
8345 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8346 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
8348 tree inner_if
= then_block
;
8350 /* We know from the grammar productions that there is an IF nested
8351 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8352 it might not be exactly THEN_BLOCK, but should be the last
8353 non-container statement within. */
8355 switch (TREE_CODE (inner_if
))
8360 inner_if
= BIND_EXPR_BODY (inner_if
);
8362 case STATEMENT_LIST
:
8363 inner_if
= expr_last (then_block
);
8365 case TRY_FINALLY_EXPR
:
8366 case TRY_CATCH_EXPR
:
8367 inner_if
= TREE_OPERAND (inner_if
, 0);
8374 if (COND_EXPR_ELSE (inner_if
))
8375 warning_at (if_locus
, OPT_Wparentheses
,
8376 "suggest explicit braces to avoid ambiguous %<else%>");
8379 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
8380 SET_EXPR_LOCATION (stmt
, if_locus
);
8384 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8385 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8386 is false for DO loops. INCR is the FOR increment expression. BODY is
8387 the statement controlled by the loop. BLAB is the break label. CLAB is
8388 the continue label. Everything is allowed to be NULL. */
8391 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
8392 tree blab
, tree clab
, bool cond_is_first
)
8394 tree entry
= NULL
, exit
= NULL
, t
;
8396 /* If the condition is zero don't generate a loop construct. */
8397 if (cond
&& integer_zerop (cond
))
8401 t
= build_and_jump (&blab
);
8402 SET_EXPR_LOCATION (t
, start_locus
);
8408 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
8410 /* If we have an exit condition, then we build an IF with gotos either
8411 out of the loop, or to the top of it. If there's no exit condition,
8412 then we just build a jump back to the top. */
8413 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
8415 if (cond
&& !integer_nonzerop (cond
))
8417 /* Canonicalize the loop condition to the end. This means
8418 generating a branch to the loop condition. Reuse the
8419 continue label, if possible. */
8424 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
8425 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
8428 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
8429 SET_EXPR_LOCATION (t
, start_locus
);
8433 t
= build_and_jump (&blab
);
8435 exit
= fold_build3_loc (start_locus
,
8436 COND_EXPR
, void_type_node
, cond
, exit
, t
);
8438 exit
= fold_build3_loc (input_location
,
8439 COND_EXPR
, void_type_node
, cond
, exit
, t
);
8448 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
8456 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
8460 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
8463 tree label
= *label_p
;
8465 /* In switch statements break is sometimes stylistically used after
8466 a return statement. This can lead to spurious warnings about
8467 control reaching the end of a non-void function when it is
8468 inlined. Note that we are calling block_may_fallthru with
8469 language specific tree nodes; this works because
8470 block_may_fallthru returns true when given something it does not
8472 skip
= !block_may_fallthru (cur_stmt_list
);
8477 *label_p
= label
= create_artificial_label (loc
);
8479 else if (TREE_CODE (label
) == LABEL_DECL
)
8481 else switch (TREE_INT_CST_LOW (label
))
8485 error_at (loc
, "break statement not within loop or switch");
8487 error_at (loc
, "continue statement not within a loop");
8491 gcc_assert (is_break
);
8492 error_at (loc
, "break statement used with OpenMP for loop");
8503 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
8505 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
8508 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8511 emit_side_effect_warnings (location_t loc
, tree expr
)
8513 if (expr
== error_mark_node
)
8515 else if (!TREE_SIDE_EFFECTS (expr
))
8517 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
8518 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
8521 warn_if_unused_value (expr
, loc
);
8524 /* Process an expression as if it were a complete statement. Emit
8525 diagnostics, but do not call ADD_STMT. LOC is the location of the
8529 c_process_expr_stmt (location_t loc
, tree expr
)
8534 expr
= c_fully_fold (expr
, false, NULL
);
8536 if (warn_sequence_point
)
8537 verify_sequence_points (expr
);
8539 if (TREE_TYPE (expr
) != error_mark_node
8540 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
8541 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
8542 error_at (loc
, "expression statement has incomplete type");
8544 /* If we're not processing a statement expression, warn about unused values.
8545 Warnings for statement expressions will be emitted later, once we figure
8546 out which is the result. */
8547 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
8548 && warn_unused_value
)
8549 emit_side_effect_warnings (loc
, expr
);
8551 /* If the expression is not of a type to which we cannot assign a line
8552 number, wrap the thing in a no-op NOP_EXPR. */
8553 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
8555 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
8556 SET_EXPR_LOCATION (expr
, loc
);
8562 /* Emit an expression as a statement. LOC is the location of the
8566 c_finish_expr_stmt (location_t loc
, tree expr
)
8569 return add_stmt (c_process_expr_stmt (loc
, expr
));
8574 /* Do the opposite and emit a statement as an expression. To begin,
8575 create a new binding level and return it. */
8578 c_begin_stmt_expr (void)
8582 /* We must force a BLOCK for this level so that, if it is not expanded
8583 later, there is a way to turn off the entire subtree of blocks that
8584 are contained in it. */
8586 ret
= c_begin_compound_stmt (true);
8588 c_bindings_start_stmt_expr (c_switch_stack
== NULL
8590 : c_switch_stack
->bindings
);
8592 /* Mark the current statement list as belonging to a statement list. */
8593 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
8598 /* LOC is the location of the compound statement to which this body
8602 c_finish_stmt_expr (location_t loc
, tree body
)
8604 tree last
, type
, tmp
, val
;
8607 body
= c_end_compound_stmt (loc
, body
, true);
8609 c_bindings_end_stmt_expr (c_switch_stack
== NULL
8611 : c_switch_stack
->bindings
);
8613 /* Locate the last statement in BODY. See c_end_compound_stmt
8614 about always returning a BIND_EXPR. */
8615 last_p
= &BIND_EXPR_BODY (body
);
8616 last
= BIND_EXPR_BODY (body
);
8619 if (TREE_CODE (last
) == STATEMENT_LIST
)
8621 tree_stmt_iterator i
;
8623 /* This can happen with degenerate cases like ({ }). No value. */
8624 if (!TREE_SIDE_EFFECTS (last
))
8627 /* If we're supposed to generate side effects warnings, process
8628 all of the statements except the last. */
8629 if (warn_unused_value
)
8631 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
8634 tree t
= tsi_stmt (i
);
8636 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
8637 emit_side_effect_warnings (tloc
, t
);
8641 i
= tsi_last (last
);
8642 last_p
= tsi_stmt_ptr (i
);
8646 /* If the end of the list is exception related, then the list was split
8647 by a call to push_cleanup. Continue searching. */
8648 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
8649 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
8651 last_p
= &TREE_OPERAND (last
, 0);
8653 goto continue_searching
;
8656 /* In the case that the BIND_EXPR is not necessary, return the
8657 expression out from inside it. */
8658 if (last
== error_mark_node
8659 || (last
== BIND_EXPR_BODY (body
)
8660 && BIND_EXPR_VARS (body
) == NULL
))
8662 /* Even if this looks constant, do not allow it in a constant
8664 last
= c_wrap_maybe_const (last
, true);
8665 /* Do not warn if the return value of a statement expression is
8667 TREE_NO_WARNING (last
) = 1;
8671 /* Extract the type of said expression. */
8672 type
= TREE_TYPE (last
);
8674 /* If we're not returning a value at all, then the BIND_EXPR that
8675 we already have is a fine expression to return. */
8676 if (!type
|| VOID_TYPE_P (type
))
8679 /* Now that we've located the expression containing the value, it seems
8680 silly to make voidify_wrapper_expr repeat the process. Create a
8681 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8682 tmp
= create_tmp_var_raw (type
, NULL
);
8684 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8685 tree_expr_nonnegative_p giving up immediately. */
8687 if (TREE_CODE (val
) == NOP_EXPR
8688 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
8689 val
= TREE_OPERAND (val
, 0);
8691 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
8692 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
8695 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
8696 SET_EXPR_LOCATION (t
, loc
);
8701 /* Begin and end compound statements. This is as simple as pushing
8702 and popping new statement lists from the tree. */
8705 c_begin_compound_stmt (bool do_scope
)
8707 tree stmt
= push_stmt_list ();
8713 /* End a compound statement. STMT is the statement. LOC is the
8714 location of the compound statement-- this is usually the location
8715 of the opening brace. */
8718 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
8724 if (c_dialect_objc ())
8725 objc_clear_super_receiver ();
8726 block
= pop_scope ();
8729 stmt
= pop_stmt_list (stmt
);
8730 stmt
= c_build_bind_expr (loc
, block
, stmt
);
8732 /* If this compound statement is nested immediately inside a statement
8733 expression, then force a BIND_EXPR to be created. Otherwise we'll
8734 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8735 STATEMENT_LISTs merge, and thus we can lose track of what statement
8738 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
8739 && TREE_CODE (stmt
) != BIND_EXPR
)
8741 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
8742 TREE_SIDE_EFFECTS (stmt
) = 1;
8743 SET_EXPR_LOCATION (stmt
, loc
);
8749 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8750 when the current scope is exited. EH_ONLY is true when this is not
8751 meant to apply to normal control flow transfer. */
8754 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
8756 enum tree_code code
;
8760 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
8761 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
8763 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
8764 list
= push_stmt_list ();
8765 TREE_OPERAND (stmt
, 0) = list
;
8766 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
8769 /* Build a binary-operation expression without default conversions.
8770 CODE is the kind of expression to build.
8771 LOCATION is the operator's location.
8772 This function differs from `build' in several ways:
8773 the data type of the result is computed and recorded in it,
8774 warnings are generated if arg data types are invalid,
8775 special handling for addition and subtraction of pointers is known,
8776 and some optimization is done (operations on narrow ints
8777 are done in the narrower type when that gives the same result).
8778 Constant folding is also done before the result is returned.
8780 Note that the operands will never have enumeral types, or function
8781 or array types, because either they will have the default conversions
8782 performed or they have both just been converted to some other type in which
8783 the arithmetic is to be done. */
8786 build_binary_op (location_t location
, enum tree_code code
,
8787 tree orig_op0
, tree orig_op1
, int convert_p
)
8789 tree type0
, type1
, orig_type0
, orig_type1
;
8791 enum tree_code code0
, code1
;
8793 tree ret
= error_mark_node
;
8794 const char *invalid_op_diag
;
8795 bool op0_int_operands
, op1_int_operands
;
8796 bool int_const
, int_const_or_overflow
, int_operands
;
8798 /* Expression code to give to the expression when it is built.
8799 Normally this is CODE, which is what the caller asked for,
8800 but in some special cases we change it. */
8801 enum tree_code resultcode
= code
;
8803 /* Data type in which the computation is to be performed.
8804 In the simplest cases this is the common type of the arguments. */
8805 tree result_type
= NULL
;
8807 /* When the computation is in excess precision, the type of the
8808 final EXCESS_PRECISION_EXPR. */
8809 tree real_result_type
= NULL
;
8811 /* Nonzero means operands have already been type-converted
8812 in whatever way is necessary.
8813 Zero means they need to be converted to RESULT_TYPE. */
8816 /* Nonzero means create the expression with this type, rather than
8818 tree build_type
= 0;
8820 /* Nonzero means after finally constructing the expression
8821 convert it to this type. */
8822 tree final_type
= 0;
8824 /* Nonzero if this is an operation like MIN or MAX which can
8825 safely be computed in short if both args are promoted shorts.
8826 Also implies COMMON.
8827 -1 indicates a bitwise operation; this makes a difference
8828 in the exact conditions for when it is safe to do the operation
8829 in a narrower mode. */
8832 /* Nonzero if this is a comparison operation;
8833 if both args are promoted shorts, compare the original shorts.
8834 Also implies COMMON. */
8835 int short_compare
= 0;
8837 /* Nonzero if this is a right-shift operation, which can be computed on the
8838 original short and then promoted if the operand is a promoted short. */
8839 int short_shift
= 0;
8841 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8844 /* True means types are compatible as far as ObjC is concerned. */
8847 /* True means this is an arithmetic operation that may need excess
8849 bool may_need_excess_precision
;
8851 if (location
== UNKNOWN_LOCATION
)
8852 location
= input_location
;
8857 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
8858 if (op0_int_operands
)
8859 op0
= remove_c_maybe_const_expr (op0
);
8860 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
8861 if (op1_int_operands
)
8862 op1
= remove_c_maybe_const_expr (op1
);
8863 int_operands
= (op0_int_operands
&& op1_int_operands
);
8866 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
8867 && TREE_CODE (orig_op1
) == INTEGER_CST
);
8868 int_const
= (int_const_or_overflow
8869 && !TREE_OVERFLOW (orig_op0
)
8870 && !TREE_OVERFLOW (orig_op1
));
8873 int_const
= int_const_or_overflow
= false;
8877 op0
= default_conversion (op0
);
8878 op1
= default_conversion (op1
);
8881 orig_type0
= type0
= TREE_TYPE (op0
);
8882 orig_type1
= type1
= TREE_TYPE (op1
);
8884 /* The expression codes of the data types of the arguments tell us
8885 whether the arguments are integers, floating, pointers, etc. */
8886 code0
= TREE_CODE (type0
);
8887 code1
= TREE_CODE (type1
);
8889 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8890 STRIP_TYPE_NOPS (op0
);
8891 STRIP_TYPE_NOPS (op1
);
8893 /* If an error was already reported for one of the arguments,
8894 avoid reporting another error. */
8896 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
8897 return error_mark_node
;
8899 if ((invalid_op_diag
8900 = targetm
.invalid_binary_op (code
, type0
, type1
)))
8902 error_at (location
, invalid_op_diag
);
8903 return error_mark_node
;
8911 case TRUNC_DIV_EXPR
:
8913 case FLOOR_DIV_EXPR
:
8914 case ROUND_DIV_EXPR
:
8915 case EXACT_DIV_EXPR
:
8916 may_need_excess_precision
= true;
8919 may_need_excess_precision
= false;
8922 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
8924 op0
= TREE_OPERAND (op0
, 0);
8925 type0
= TREE_TYPE (op0
);
8927 else if (may_need_excess_precision
8928 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
8931 op0
= convert (eptype
, op0
);
8933 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
8935 op1
= TREE_OPERAND (op1
, 0);
8936 type1
= TREE_TYPE (op1
);
8938 else if (may_need_excess_precision
8939 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
8942 op1
= convert (eptype
, op1
);
8945 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
8950 /* Handle the pointer + int case. */
8951 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
8953 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
8954 goto return_build_binary_op
;
8956 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
8958 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
8959 goto return_build_binary_op
;
8966 /* Subtraction of two similar pointers.
8967 We must subtract them as integers, then divide by object size. */
8968 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
8969 && comp_target_types (location
, type0
, type1
))
8971 ret
= pointer_diff (location
, op0
, op1
);
8972 goto return_build_binary_op
;
8974 /* Handle pointer minus int. Just like pointer plus int. */
8975 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
8977 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
8978 goto return_build_binary_op
;
8988 case TRUNC_DIV_EXPR
:
8990 case FLOOR_DIV_EXPR
:
8991 case ROUND_DIV_EXPR
:
8992 case EXACT_DIV_EXPR
:
8993 warn_for_div_by_zero (location
, op1
);
8995 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
8996 || code0
== FIXED_POINT_TYPE
8997 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
8998 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
8999 || code1
== FIXED_POINT_TYPE
9000 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
9002 enum tree_code tcode0
= code0
, tcode1
= code1
;
9004 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
9005 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
9006 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
9007 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
9009 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
9010 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
9011 resultcode
= RDIV_EXPR
;
9013 /* Although it would be tempting to shorten always here, that
9014 loses on some targets, since the modulo instruction is
9015 undefined if the quotient can't be represented in the
9016 computation mode. We shorten only if unsigned or if
9017 dividing by something we know != -1. */
9018 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
9019 || (TREE_CODE (op1
) == INTEGER_CST
9020 && !integer_all_onesp (op1
)));
9028 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
9030 /* Allow vector types which are not floating point types. */
9031 else if (code0
== VECTOR_TYPE
9032 && code1
== VECTOR_TYPE
9033 && !VECTOR_FLOAT_TYPE_P (type0
)
9034 && !VECTOR_FLOAT_TYPE_P (type1
))
9038 case TRUNC_MOD_EXPR
:
9039 case FLOOR_MOD_EXPR
:
9040 warn_for_div_by_zero (location
, op1
);
9042 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
9043 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
9044 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
9046 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
9048 /* Although it would be tempting to shorten always here, that loses
9049 on some targets, since the modulo instruction is undefined if the
9050 quotient can't be represented in the computation mode. We shorten
9051 only if unsigned or if dividing by something we know != -1. */
9052 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
9053 || (TREE_CODE (op1
) == INTEGER_CST
9054 && !integer_all_onesp (op1
)));
9059 case TRUTH_ANDIF_EXPR
:
9060 case TRUTH_ORIF_EXPR
:
9061 case TRUTH_AND_EXPR
:
9063 case TRUTH_XOR_EXPR
:
9064 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
9065 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
9066 || code0
== FIXED_POINT_TYPE
)
9067 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
9068 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
9069 || code1
== FIXED_POINT_TYPE
))
9071 /* Result of these operations is always an int,
9072 but that does not mean the operands should be
9073 converted to ints! */
9074 result_type
= integer_type_node
;
9075 op0
= c_common_truthvalue_conversion (location
, op0
);
9076 op1
= c_common_truthvalue_conversion (location
, op1
);
9079 if (code
== TRUTH_ANDIF_EXPR
)
9081 int_const_or_overflow
= (int_operands
9082 && TREE_CODE (orig_op0
) == INTEGER_CST
9083 && (op0
== truthvalue_false_node
9084 || TREE_CODE (orig_op1
) == INTEGER_CST
));
9085 int_const
= (int_const_or_overflow
9086 && !TREE_OVERFLOW (orig_op0
)
9087 && (op0
== truthvalue_false_node
9088 || !TREE_OVERFLOW (orig_op1
)));
9090 else if (code
== TRUTH_ORIF_EXPR
)
9092 int_const_or_overflow
= (int_operands
9093 && TREE_CODE (orig_op0
) == INTEGER_CST
9094 && (op0
== truthvalue_true_node
9095 || TREE_CODE (orig_op1
) == INTEGER_CST
));
9096 int_const
= (int_const_or_overflow
9097 && !TREE_OVERFLOW (orig_op0
)
9098 && (op0
== truthvalue_true_node
9099 || !TREE_OVERFLOW (orig_op1
)));
9103 /* Shift operations: result has same type as first operand;
9104 always convert second operand to int.
9105 Also set SHORT_SHIFT if shifting rightward. */
9108 if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
9109 && code1
== INTEGER_TYPE
)
9111 if (TREE_CODE (op1
) == INTEGER_CST
)
9113 if (tree_int_cst_sgn (op1
) < 0)
9116 if (c_inhibit_evaluation_warnings
== 0)
9117 warning (0, "right shift count is negative");
9121 if (!integer_zerop (op1
))
9124 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
9127 if (c_inhibit_evaluation_warnings
== 0)
9128 warning (0, "right shift count >= width of type");
9133 /* Use the type of the value to be shifted. */
9134 result_type
= type0
;
9135 /* Convert the shift-count to an integer, regardless of size
9136 of value being shifted. */
9137 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
9138 op1
= convert (integer_type_node
, op1
);
9139 /* Avoid converting op1 to result_type later. */
9145 if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
9146 && code1
== INTEGER_TYPE
)
9148 if (TREE_CODE (op1
) == INTEGER_CST
)
9150 if (tree_int_cst_sgn (op1
) < 0)
9153 if (c_inhibit_evaluation_warnings
== 0)
9154 warning (0, "left shift count is negative");
9157 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
9160 if (c_inhibit_evaluation_warnings
== 0)
9161 warning (0, "left shift count >= width of type");
9165 /* Use the type of the value to be shifted. */
9166 result_type
= type0
;
9167 /* Convert the shift-count to an integer, regardless of size
9168 of value being shifted. */
9169 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
9170 op1
= convert (integer_type_node
, op1
);
9171 /* Avoid converting op1 to result_type later. */
9178 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
9179 warning_at (location
,
9181 "comparing floating point with == or != is unsafe");
9182 /* Result of comparison is always int,
9183 but don't convert the args to int! */
9184 build_type
= integer_type_node
;
9185 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9186 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
9187 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9188 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
9190 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
9192 tree tt0
= TREE_TYPE (type0
);
9193 tree tt1
= TREE_TYPE (type1
);
9194 /* Anything compares with void *. void * compares with anything.
9195 Otherwise, the targets must be compatible
9196 and both must be object or both incomplete. */
9197 if (comp_target_types (location
, type0
, type1
))
9198 result_type
= common_pointer_type (type0
, type1
);
9199 else if (VOID_TYPE_P (tt0
))
9201 /* op0 != orig_op0 detects the case of something
9202 whose value is 0 but which isn't a valid null ptr const. */
9203 if (pedantic
&& !null_pointer_constant_p (orig_op0
)
9204 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
9205 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9206 "comparison of %<void *%> with function pointer");
9208 else if (VOID_TYPE_P (tt1
))
9210 if (pedantic
&& !null_pointer_constant_p (orig_op1
)
9211 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
9212 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9213 "comparison of %<void *%> with function pointer");
9216 /* Avoid warning about the volatile ObjC EH puts on decls. */
9218 pedwarn (location
, 0,
9219 "comparison of distinct pointer types lacks a cast");
9221 if (result_type
== NULL_TREE
)
9222 result_type
= ptr_type_node
;
9224 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
9226 if (TREE_CODE (op0
) == ADDR_EXPR
9227 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
9228 warning_at (location
,
9229 OPT_Waddress
, "the address of %qD will never be NULL",
9230 TREE_OPERAND (op0
, 0));
9231 result_type
= type0
;
9233 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
9235 if (TREE_CODE (op1
) == ADDR_EXPR
9236 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
9237 warning_at (location
,
9238 OPT_Waddress
, "the address of %qD will never be NULL",
9239 TREE_OPERAND (op1
, 0));
9240 result_type
= type1
;
9242 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9244 result_type
= type0
;
9245 pedwarn (location
, 0, "comparison between pointer and integer");
9247 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
9249 result_type
= type1
;
9250 pedwarn (location
, 0, "comparison between pointer and integer");
9258 build_type
= integer_type_node
;
9259 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9260 || code0
== FIXED_POINT_TYPE
)
9261 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9262 || code1
== FIXED_POINT_TYPE
))
9264 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
9266 if (comp_target_types (location
, type0
, type1
))
9268 result_type
= common_pointer_type (type0
, type1
);
9269 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
9270 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
9271 pedwarn (location
, 0,
9272 "comparison of complete and incomplete pointers");
9273 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
9274 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9275 "ordered comparisons of pointers to functions");
9279 result_type
= ptr_type_node
;
9280 pedwarn (location
, 0,
9281 "comparison of distinct pointer types lacks a cast");
9284 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
9286 result_type
= type0
;
9288 pedwarn (location
, OPT_pedantic
,
9289 "ordered comparison of pointer with integer zero");
9290 else if (extra_warnings
)
9291 warning_at (location
, OPT_Wextra
,
9292 "ordered comparison of pointer with integer zero");
9294 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
9296 result_type
= type1
;
9297 pedwarn (location
, OPT_pedantic
,
9298 "ordered comparison of pointer with integer zero");
9300 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9302 result_type
= type0
;
9303 pedwarn (location
, 0, "comparison between pointer and integer");
9305 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
9307 result_type
= type1
;
9308 pedwarn (location
, 0, "comparison between pointer and integer");
9316 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
9317 return error_mark_node
;
9319 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
9320 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
9321 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0
),
9322 TREE_TYPE (type1
))))
9324 binary_op_error (location
, code
, type0
, type1
);
9325 return error_mark_node
;
9328 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
9329 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
9331 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
9332 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
9334 bool first_complex
= (code0
== COMPLEX_TYPE
);
9335 bool second_complex
= (code1
== COMPLEX_TYPE
);
9336 int none_complex
= (!first_complex
&& !second_complex
);
9338 if (shorten
|| common
|| short_compare
)
9340 result_type
= c_common_type (type0
, type1
);
9341 if (result_type
== error_mark_node
)
9342 return error_mark_node
;
9345 if (first_complex
!= second_complex
9346 && (code
== PLUS_EXPR
9347 || code
== MINUS_EXPR
9348 || code
== MULT_EXPR
9349 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
9350 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
9351 && flag_signed_zeros
)
9353 /* An operation on mixed real/complex operands must be
9354 handled specially, but the language-independent code can
9355 more easily optimize the plain complex arithmetic if
9356 -fno-signed-zeros. */
9357 tree real_type
= TREE_TYPE (result_type
);
9359 if (type0
!= orig_type0
|| type1
!= orig_type1
)
9361 gcc_assert (may_need_excess_precision
&& common
);
9362 real_result_type
= c_common_type (orig_type0
, orig_type1
);
9366 if (TREE_TYPE (op0
) != result_type
)
9367 op0
= convert_and_check (result_type
, op0
);
9368 if (TREE_TYPE (op1
) != real_type
)
9369 op1
= convert_and_check (real_type
, op1
);
9373 if (TREE_TYPE (op0
) != real_type
)
9374 op0
= convert_and_check (real_type
, op0
);
9375 if (TREE_TYPE (op1
) != result_type
)
9376 op1
= convert_and_check (result_type
, op1
);
9378 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
9379 return error_mark_node
;
9382 op0
= c_save_expr (op0
);
9383 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
9385 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
9390 case TRUNC_DIV_EXPR
:
9391 imag
= build2 (resultcode
, real_type
, imag
, op1
);
9395 real
= build2 (resultcode
, real_type
, real
, op1
);
9403 op1
= c_save_expr (op1
);
9404 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
9406 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
9411 imag
= build2 (resultcode
, real_type
, op0
, imag
);
9414 real
= build2 (resultcode
, real_type
, op0
, real
);
9417 real
= build2 (resultcode
, real_type
, op0
, real
);
9418 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
9424 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
9425 goto return_build_binary_op
;
9428 /* For certain operations (which identify themselves by shorten != 0)
9429 if both args were extended from the same smaller type,
9430 do the arithmetic in that type and then extend.
9432 shorten !=0 and !=1 indicates a bitwise operation.
9433 For them, this optimization is safe only if
9434 both args are zero-extended or both are sign-extended.
9435 Otherwise, we might change the result.
9436 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9437 but calculated in (unsigned short) it would be (unsigned short)-1. */
9439 if (shorten
&& none_complex
)
9441 final_type
= result_type
;
9442 result_type
= shorten_binary_op (result_type
, op0
, op1
,
9446 /* Shifts can be shortened if shifting right. */
9451 tree arg0
= get_narrower (op0
, &unsigned_arg
);
9453 final_type
= result_type
;
9455 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
9456 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
9458 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
9459 && tree_int_cst_sgn (op1
) > 0
9460 /* We can shorten only if the shift count is less than the
9461 number of bits in the smaller type size. */
9462 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
9463 /* We cannot drop an unsigned shift after sign-extension. */
9464 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
9466 /* Do an unsigned shift if the operand was zero-extended. */
9468 = c_common_signed_or_unsigned_type (unsigned_arg
,
9470 /* Convert value-to-be-shifted to that type. */
9471 if (TREE_TYPE (op0
) != result_type
)
9472 op0
= convert (result_type
, op0
);
9477 /* Comparison operations are shortened too but differently.
9478 They identify themselves by setting short_compare = 1. */
9482 /* Don't write &op0, etc., because that would prevent op0
9483 from being kept in a register.
9484 Instead, make copies of the our local variables and
9485 pass the copies by reference, then copy them back afterward. */
9486 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
9487 enum tree_code xresultcode
= resultcode
;
9489 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
9494 goto return_build_binary_op
;
9497 op0
= xop0
, op1
= xop1
;
9499 resultcode
= xresultcode
;
9501 if (c_inhibit_evaluation_warnings
== 0)
9503 bool op0_maybe_const
= true;
9504 bool op1_maybe_const
= true;
9505 tree orig_op0_folded
, orig_op1_folded
;
9507 if (in_late_binary_op
)
9509 orig_op0_folded
= orig_op0
;
9510 orig_op1_folded
= orig_op1
;
9514 /* Fold for the sake of possible warnings, as in
9515 build_conditional_expr. This requires the
9516 "original" values to be folded, not just op0 and
9518 c_inhibit_evaluation_warnings
++;
9519 op0
= c_fully_fold (op0
, require_constant_value
,
9521 op1
= c_fully_fold (op1
, require_constant_value
,
9523 c_inhibit_evaluation_warnings
--;
9524 orig_op0_folded
= c_fully_fold (orig_op0
,
9525 require_constant_value
,
9527 orig_op1_folded
= c_fully_fold (orig_op1
,
9528 require_constant_value
,
9532 if (warn_sign_compare
)
9533 warn_for_sign_compare (location
, orig_op0_folded
,
9534 orig_op1_folded
, op0
, op1
,
9535 result_type
, resultcode
);
9536 if (!in_late_binary_op
)
9538 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
9539 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
9540 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
9541 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
9547 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9548 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9549 Then the expression will be built.
9550 It will be given type FINAL_TYPE if that is nonzero;
9551 otherwise, it will be given type RESULT_TYPE. */
9555 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
9556 return error_mark_node
;
9561 if (TREE_TYPE (op0
) != result_type
)
9562 op0
= convert_and_check (result_type
, op0
);
9563 if (TREE_TYPE (op1
) != result_type
)
9564 op1
= convert_and_check (result_type
, op1
);
9566 /* This can happen if one operand has a vector type, and the other
9567 has a different type. */
9568 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
9569 return error_mark_node
;
9572 if (build_type
== NULL_TREE
)
9574 build_type
= result_type
;
9575 if (type0
!= orig_type0
|| type1
!= orig_type1
)
9577 gcc_assert (may_need_excess_precision
&& common
);
9578 real_result_type
= c_common_type (orig_type0
, orig_type1
);
9582 /* Treat expressions in initializers specially as they can't trap. */
9583 if (int_const_or_overflow
)
9584 ret
= (require_constant_value
9585 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
9587 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
9589 ret
= build2 (resultcode
, build_type
, op0
, op1
);
9590 if (final_type
!= 0)
9591 ret
= convert (final_type
, ret
);
9593 return_build_binary_op
:
9594 gcc_assert (ret
!= error_mark_node
);
9595 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
9597 ? note_integer_operands (ret
)
9598 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
9599 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
9600 && !in_late_binary_op
)
9601 ret
= note_integer_operands (ret
);
9602 if (real_result_type
)
9603 ret
= build1 (EXCESS_PRECISION_EXPR
, real_result_type
, ret
);
9604 protected_set_expr_location (ret
, location
);
9609 /* Convert EXPR to be a truth-value, validating its type for this
9610 purpose. LOCATION is the source location for the expression. */
9613 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
9615 bool int_const
, int_operands
;
9617 switch (TREE_CODE (TREE_TYPE (expr
)))
9620 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
9621 return error_mark_node
;
9624 error_at (location
, "used struct type value where scalar is required");
9625 return error_mark_node
;
9628 error_at (location
, "used union type value where scalar is required");
9629 return error_mark_node
;
9638 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
9639 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
9641 expr
= remove_c_maybe_const_expr (expr
);
9643 /* ??? Should we also give an error for void and vectors rather than
9644 leaving those to give errors later? */
9645 expr
= c_common_truthvalue_conversion (location
, expr
);
9647 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
9649 if (TREE_OVERFLOW (expr
))
9652 return note_integer_operands (expr
);
9654 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
9655 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9660 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9664 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
9666 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
9668 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
9669 /* Executing a compound literal inside a function reinitializes
9671 if (!TREE_STATIC (decl
))
9679 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9682 c_begin_omp_parallel (void)
9687 block
= c_begin_compound_stmt (true);
9692 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
9693 statement. LOC is the location of the OMP_PARALLEL. */
9696 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
9700 block
= c_end_compound_stmt (loc
, block
, true);
9702 stmt
= make_node (OMP_PARALLEL
);
9703 TREE_TYPE (stmt
) = void_type_node
;
9704 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
9705 OMP_PARALLEL_BODY (stmt
) = block
;
9706 SET_EXPR_LOCATION (stmt
, loc
);
9708 return add_stmt (stmt
);
9711 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9714 c_begin_omp_task (void)
9719 block
= c_begin_compound_stmt (true);
9724 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
9725 statement. LOC is the location of the #pragma. */
9728 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
9732 block
= c_end_compound_stmt (loc
, block
, true);
9734 stmt
= make_node (OMP_TASK
);
9735 TREE_TYPE (stmt
) = void_type_node
;
9736 OMP_TASK_CLAUSES (stmt
) = clauses
;
9737 OMP_TASK_BODY (stmt
) = block
;
9738 SET_EXPR_LOCATION (stmt
, loc
);
9740 return add_stmt (stmt
);
9743 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9744 Remove any elements from the list that are invalid. */
9747 c_finish_omp_clauses (tree clauses
)
9749 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
9750 tree c
, t
, *pc
= &clauses
;
9753 bitmap_obstack_initialize (NULL
);
9754 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
9755 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
9756 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
9758 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
9760 bool remove
= false;
9761 bool need_complete
= false;
9762 bool need_implicitly_determined
= false;
9764 switch (OMP_CLAUSE_CODE (c
))
9766 case OMP_CLAUSE_SHARED
:
9768 need_implicitly_determined
= true;
9769 goto check_dup_generic
;
9771 case OMP_CLAUSE_PRIVATE
:
9773 need_complete
= true;
9774 need_implicitly_determined
= true;
9775 goto check_dup_generic
;
9777 case OMP_CLAUSE_REDUCTION
:
9779 need_implicitly_determined
= true;
9780 t
= OMP_CLAUSE_DECL (c
);
9781 if (AGGREGATE_TYPE_P (TREE_TYPE (t
))
9782 || POINTER_TYPE_P (TREE_TYPE (t
)))
9784 error_at (OMP_CLAUSE_LOCATION (c
),
9785 "%qE has invalid type for %<reduction%>", t
);
9788 else if (FLOAT_TYPE_P (TREE_TYPE (t
)))
9790 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
9791 const char *r_name
= NULL
;
9808 case TRUTH_ANDIF_EXPR
:
9811 case TRUTH_ORIF_EXPR
:
9819 error_at (OMP_CLAUSE_LOCATION (c
),
9820 "%qE has invalid type for %<reduction(%s)%>",
9825 goto check_dup_generic
;
9827 case OMP_CLAUSE_COPYPRIVATE
:
9828 name
= "copyprivate";
9829 goto check_dup_generic
;
9831 case OMP_CLAUSE_COPYIN
:
9833 t
= OMP_CLAUSE_DECL (c
);
9834 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
9836 error_at (OMP_CLAUSE_LOCATION (c
),
9837 "%qE must be %<threadprivate%> for %<copyin%>", t
);
9840 goto check_dup_generic
;
9843 t
= OMP_CLAUSE_DECL (c
);
9844 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
9846 error_at (OMP_CLAUSE_LOCATION (c
),
9847 "%qE is not a variable in clause %qs", t
, name
);
9850 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
9851 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
9852 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
9854 error_at (OMP_CLAUSE_LOCATION (c
),
9855 "%qE appears more than once in data clauses", t
);
9859 bitmap_set_bit (&generic_head
, DECL_UID (t
));
9862 case OMP_CLAUSE_FIRSTPRIVATE
:
9863 name
= "firstprivate";
9864 t
= OMP_CLAUSE_DECL (c
);
9865 need_complete
= true;
9866 need_implicitly_determined
= true;
9867 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
9869 error_at (OMP_CLAUSE_LOCATION (c
),
9870 "%qE is not a variable in clause %<firstprivate%>", t
);
9873 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
9874 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
9876 error_at (OMP_CLAUSE_LOCATION (c
),
9877 "%qE appears more than once in data clauses", t
);
9881 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
9884 case OMP_CLAUSE_LASTPRIVATE
:
9885 name
= "lastprivate";
9886 t
= OMP_CLAUSE_DECL (c
);
9887 need_complete
= true;
9888 need_implicitly_determined
= true;
9889 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
9891 error_at (OMP_CLAUSE_LOCATION (c
),
9892 "%qE is not a variable in clause %<lastprivate%>", t
);
9895 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
9896 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
9898 error_at (OMP_CLAUSE_LOCATION (c
),
9899 "%qE appears more than once in data clauses", t
);
9903 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
9907 case OMP_CLAUSE_NUM_THREADS
:
9908 case OMP_CLAUSE_SCHEDULE
:
9909 case OMP_CLAUSE_NOWAIT
:
9910 case OMP_CLAUSE_ORDERED
:
9911 case OMP_CLAUSE_DEFAULT
:
9912 case OMP_CLAUSE_UNTIED
:
9913 case OMP_CLAUSE_COLLAPSE
:
9914 pc
= &OMP_CLAUSE_CHAIN (c
);
9923 t
= OMP_CLAUSE_DECL (c
);
9927 t
= require_complete_type (t
);
9928 if (t
== error_mark_node
)
9932 if (need_implicitly_determined
)
9934 const char *share_name
= NULL
;
9936 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
9937 share_name
= "threadprivate";
9938 else switch (c_omp_predetermined_sharing (t
))
9940 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
9942 case OMP_CLAUSE_DEFAULT_SHARED
:
9943 share_name
= "shared";
9945 case OMP_CLAUSE_DEFAULT_PRIVATE
:
9946 share_name
= "private";
9953 error_at (OMP_CLAUSE_LOCATION (c
),
9954 "%qE is predetermined %qs for %qs",
9955 t
, share_name
, name
);
9962 *pc
= OMP_CLAUSE_CHAIN (c
);
9964 pc
= &OMP_CLAUSE_CHAIN (c
);
9967 bitmap_obstack_release (NULL
);
9971 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9972 down to the element type of an array. */
9975 c_build_qualified_type (tree type
, int type_quals
)
9977 if (type
== error_mark_node
)
9980 if (TREE_CODE (type
) == ARRAY_TYPE
)
9983 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
9986 /* See if we already have an identically qualified type. */
9987 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
9989 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
9990 && TYPE_NAME (t
) == TYPE_NAME (type
)
9991 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
9992 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
9993 TYPE_ATTRIBUTES (type
)))
9998 tree domain
= TYPE_DOMAIN (type
);
10000 t
= build_variant_type_copy (type
);
10001 TREE_TYPE (t
) = element_type
;
10003 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
10004 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
10005 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10006 else if (TYPE_CANONICAL (element_type
) != element_type
10007 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
10009 tree unqualified_canon
10010 = build_array_type (TYPE_CANONICAL (element_type
),
10011 domain
? TYPE_CANONICAL (domain
)
10014 = c_build_qualified_type (unqualified_canon
, type_quals
);
10017 TYPE_CANONICAL (t
) = t
;
10022 /* A restrict-qualified pointer type must be a pointer to object or
10023 incomplete type. Note that the use of POINTER_TYPE_P also allows
10024 REFERENCE_TYPEs, which is appropriate for C++. */
10025 if ((type_quals
& TYPE_QUAL_RESTRICT
)
10026 && (!POINTER_TYPE_P (type
)
10027 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
10029 error ("invalid use of %<restrict%>");
10030 type_quals
&= ~TYPE_QUAL_RESTRICT
;
10033 return build_qualified_type (type
, type_quals
);
10036 /* Build a VA_ARG_EXPR for the C parser. */
10039 c_build_va_arg (location_t loc
, tree expr
, tree type
)
10041 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
10042 warning_at (loc
, OPT_Wc___compat
,
10043 "C++ requires promoted type, not enum type, in %<va_arg%>");
10044 return build_va_arg (loc
, expr
, type
);