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
)
3944 op1
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (op1
),
3946 C_MAYBE_CONST_EXPR_NON_CONST (op1
) = !op1_maybe_const
;
3948 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
3950 op2
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (op2
),
3952 C_MAYBE_CONST_EXPR_NON_CONST (op2
) = !op2_maybe_const
;
3958 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
3960 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
3961 pedwarn (colon_loc
, OPT_pedantic
,
3962 "ISO C forbids conditional expr with only one void side");
3963 result_type
= void_type_node
;
3965 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
3967 if (comp_target_types (colon_loc
, type1
, type2
))
3968 result_type
= common_pointer_type (type1
, type2
);
3969 else if (null_pointer_constant_p (orig_op1
))
3970 result_type
= qualify_type (type2
, type1
);
3971 else if (null_pointer_constant_p (orig_op2
))
3972 result_type
= qualify_type (type1
, type2
);
3973 else if (VOID_TYPE_P (TREE_TYPE (type1
)))
3975 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
3976 pedwarn (colon_loc
, OPT_pedantic
,
3977 "ISO C forbids conditional expr between "
3978 "%<void *%> and function pointer");
3979 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
3980 TREE_TYPE (type2
)));
3982 else if (VOID_TYPE_P (TREE_TYPE (type2
)))
3984 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
3985 pedwarn (colon_loc
, OPT_pedantic
,
3986 "ISO C forbids conditional expr between "
3987 "%<void *%> and function pointer");
3988 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
3989 TREE_TYPE (type1
)));
3994 pedwarn (colon_loc
, 0,
3995 "pointer type mismatch in conditional expression");
3996 result_type
= build_pointer_type (void_type_node
);
3999 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4001 if (!null_pointer_constant_p (orig_op2
))
4002 pedwarn (colon_loc
, 0,
4003 "pointer/integer type mismatch in conditional expression");
4006 op2
= null_pointer_node
;
4008 result_type
= type1
;
4010 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4012 if (!null_pointer_constant_p (orig_op1
))
4013 pedwarn (colon_loc
, 0,
4014 "pointer/integer type mismatch in conditional expression");
4017 op1
= null_pointer_node
;
4019 result_type
= type2
;
4024 if (flag_cond_mismatch
)
4025 result_type
= void_type_node
;
4028 error_at (colon_loc
, "type mismatch in conditional expression");
4029 return error_mark_node
;
4033 /* Merge const and volatile flags of the incoming types. */
4035 = build_type_variant (result_type
,
4036 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4037 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4039 if (result_type
!= type1
)
4040 op1
= convert_and_check (result_type
, op1
);
4041 if (result_type
!= type2
)
4042 op2
= convert_and_check (result_type
, op2
);
4044 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4046 op2_int_operands
= true;
4047 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4049 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4051 op1_int_operands
= true;
4052 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4054 int_const
= int_operands
= (ifexp_int_operands
4056 && op2_int_operands
);
4059 int_const
= ((ifexp
== truthvalue_true_node
4060 && TREE_CODE (orig_op1
) == INTEGER_CST
4061 && !TREE_OVERFLOW (orig_op1
))
4062 || (ifexp
== truthvalue_false_node
4063 && TREE_CODE (orig_op2
) == INTEGER_CST
4064 && !TREE_OVERFLOW (orig_op2
)));
4066 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4067 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4070 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4072 ret
= note_integer_operands (ret
);
4075 ret
= build1 (EXCESS_PRECISION_EXPR
, ep_result_type
, ret
);
4077 protected_set_expr_location (ret
, colon_loc
);
4081 /* Return a compound expression that performs two expressions and
4082 returns the value of the second of them.
4084 LOC is the location of the COMPOUND_EXPR. */
4087 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4089 bool expr1_int_operands
, expr2_int_operands
;
4090 tree eptype
= NULL_TREE
;
4093 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4094 if (expr1_int_operands
)
4095 expr1
= remove_c_maybe_const_expr (expr1
);
4096 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4097 if (expr2_int_operands
)
4098 expr2
= remove_c_maybe_const_expr (expr2
);
4100 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4101 expr1
= TREE_OPERAND (expr1
, 0);
4102 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4104 eptype
= TREE_TYPE (expr2
);
4105 expr2
= TREE_OPERAND (expr2
, 0);
4108 if (!TREE_SIDE_EFFECTS (expr1
))
4110 /* The left-hand operand of a comma expression is like an expression
4111 statement: with -Wunused, we should warn if it doesn't have
4112 any side-effects, unless it was explicitly cast to (void). */
4113 if (warn_unused_value
)
4115 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4116 && CONVERT_EXPR_P (expr1
))
4118 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4119 && TREE_CODE (expr1
) == COMPOUND_EXPR
4120 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4121 ; /* (void) a, (void) b, c */
4123 warning_at (loc
, OPT_Wunused_value
,
4124 "left-hand operand of comma expression has no effect");
4128 /* With -Wunused, we should also warn if the left-hand operand does have
4129 side-effects, but computes a value which is not used. For example, in
4130 `foo() + bar(), baz()' the result of the `+' operator is not used,
4131 so we should issue a warning. */
4132 else if (warn_unused_value
)
4133 warn_if_unused_value (expr1
, loc
);
4135 if (expr2
== error_mark_node
)
4136 return error_mark_node
;
4138 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4141 && expr1_int_operands
4142 && expr2_int_operands
)
4143 ret
= note_integer_operands (ret
);
4146 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4148 protected_set_expr_location (ret
, loc
);
4152 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4153 which we are casting. OTYPE is the type of the expression being
4154 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4155 on the command line. */
4158 handle_warn_cast_qual (tree type
, tree otype
)
4160 tree in_type
= type
;
4161 tree in_otype
= otype
;
4166 /* Check that the qualifiers on IN_TYPE are a superset of the
4167 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4168 nodes is uninteresting and we stop as soon as we hit a
4169 non-POINTER_TYPE node on either type. */
4172 in_otype
= TREE_TYPE (in_otype
);
4173 in_type
= TREE_TYPE (in_type
);
4175 /* GNU C allows cv-qualified function types. 'const' means the
4176 function is very pure, 'volatile' means it can't return. We
4177 need to warn when such qualifiers are added, not when they're
4179 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4180 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4181 added
|= (TYPE_QUALS (in_type
) & ~TYPE_QUALS (in_otype
));
4183 discarded
|= (TYPE_QUALS (in_otype
) & ~TYPE_QUALS (in_type
));
4185 while (TREE_CODE (in_type
) == POINTER_TYPE
4186 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4189 warning (OPT_Wcast_qual
, "cast adds new qualifiers to function type");
4192 /* There are qualifiers present in IN_OTYPE that are not present
4194 warning (OPT_Wcast_qual
,
4195 "cast discards qualifiers from pointer target type");
4197 if (added
|| discarded
)
4200 /* A cast from **T to const **T is unsafe, because it can cause a
4201 const value to be changed with no additional warning. We only
4202 issue this warning if T is the same on both sides, and we only
4203 issue the warning if there are the same number of pointers on
4204 both sides, as otherwise the cast is clearly unsafe anyhow. A
4205 cast is unsafe when a qualifier is added at one level and const
4206 is not present at all outer levels.
4208 To issue this warning, we check at each level whether the cast
4209 adds new qualifiers not already seen. We don't need to special
4210 case function types, as they won't have the same
4211 TYPE_MAIN_VARIANT. */
4213 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4215 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4220 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4223 in_type
= TREE_TYPE (in_type
);
4224 in_otype
= TREE_TYPE (in_otype
);
4225 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4228 warning (OPT_Wcast_qual
,
4229 ("new qualifiers in middle of multi-level non-const cast "
4234 is_const
= TYPE_READONLY (in_type
);
4236 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4239 /* Build an expression representing a cast to type TYPE of expression EXPR.
4240 LOC is the location of the cast-- typically the open paren of the cast. */
4243 build_c_cast (location_t loc
, tree type
, tree expr
)
4247 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4248 expr
= TREE_OPERAND (expr
, 0);
4252 if (type
== error_mark_node
|| expr
== error_mark_node
)
4253 return error_mark_node
;
4255 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4256 only in <protocol> qualifications. But when constructing cast expressions,
4257 the protocols do matter and must be kept around. */
4258 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4259 return build1 (NOP_EXPR
, type
, expr
);
4261 type
= TYPE_MAIN_VARIANT (type
);
4263 if (TREE_CODE (type
) == ARRAY_TYPE
)
4265 error_at (loc
, "cast specifies array type");
4266 return error_mark_node
;
4269 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4271 error_at (loc
, "cast specifies function type");
4272 return error_mark_node
;
4275 if (!VOID_TYPE_P (type
))
4277 value
= require_complete_type (value
);
4278 if (value
== error_mark_node
)
4279 return error_mark_node
;
4282 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4284 if (TREE_CODE (type
) == RECORD_TYPE
4285 || TREE_CODE (type
) == UNION_TYPE
)
4286 pedwarn (loc
, OPT_pedantic
,
4287 "ISO C forbids casting nonscalar to the same type");
4289 else if (TREE_CODE (type
) == UNION_TYPE
)
4293 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
4294 if (TREE_TYPE (field
) != error_mark_node
4295 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4296 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4303 pedwarn (loc
, OPT_pedantic
, "ISO C forbids casts to union type");
4304 t
= digest_init (loc
, type
,
4305 build_constructor_single (type
, field
, value
),
4306 NULL_TREE
, false, true, 0);
4307 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4310 error_at (loc
, "cast to union type from type not present in union");
4311 return error_mark_node
;
4317 if (type
== void_type_node
)
4319 tree t
= build1 (CONVERT_EXPR
, type
, value
);
4320 SET_EXPR_LOCATION (t
, loc
);
4324 otype
= TREE_TYPE (value
);
4326 /* Optionally warn about potentially worrisome casts. */
4328 && TREE_CODE (type
) == POINTER_TYPE
4329 && TREE_CODE (otype
) == POINTER_TYPE
)
4330 handle_warn_cast_qual (type
, otype
);
4332 /* Warn about possible alignment problems. */
4333 if (STRICT_ALIGNMENT
4334 && TREE_CODE (type
) == POINTER_TYPE
4335 && TREE_CODE (otype
) == POINTER_TYPE
4336 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
4337 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
4338 /* Don't warn about opaque types, where the actual alignment
4339 restriction is unknown. */
4340 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
4341 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
4342 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
4343 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
4344 warning_at (loc
, OPT_Wcast_align
,
4345 "cast increases required alignment of target type");
4347 if (TREE_CODE (type
) == INTEGER_TYPE
4348 && TREE_CODE (otype
) == POINTER_TYPE
4349 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
4350 /* Unlike conversion of integers to pointers, where the
4351 warning is disabled for converting constants because
4352 of cases such as SIG_*, warn about converting constant
4353 pointers to integers. In some cases it may cause unwanted
4354 sign extension, and a warning is appropriate. */
4355 warning_at (loc
, OPT_Wpointer_to_int_cast
,
4356 "cast from pointer to integer of different size");
4358 if (TREE_CODE (value
) == CALL_EXPR
4359 && TREE_CODE (type
) != TREE_CODE (otype
))
4360 warning_at (loc
, OPT_Wbad_function_cast
,
4361 "cast from function call of type %qT "
4362 "to non-matching type %qT", otype
, type
);
4364 if (TREE_CODE (type
) == POINTER_TYPE
4365 && TREE_CODE (otype
) == INTEGER_TYPE
4366 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
4367 /* Don't warn about converting any constant. */
4368 && !TREE_CONSTANT (value
))
4370 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
4371 "of different size");
4373 if (warn_strict_aliasing
<= 2)
4374 strict_aliasing_warning (otype
, type
, expr
);
4376 /* If pedantic, warn for conversions between function and object
4377 pointer types, except for converting a null pointer constant
4378 to function pointer type. */
4380 && TREE_CODE (type
) == POINTER_TYPE
4381 && TREE_CODE (otype
) == POINTER_TYPE
4382 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
4383 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
4384 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
4385 "conversion of function pointer to object pointer type");
4388 && TREE_CODE (type
) == POINTER_TYPE
4389 && TREE_CODE (otype
) == POINTER_TYPE
4390 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
4391 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
4392 && !null_pointer_constant_p (value
))
4393 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
4394 "conversion of object pointer to function pointer type");
4397 value
= convert (type
, value
);
4399 /* Ignore any integer overflow caused by the cast. */
4400 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
4402 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
4404 if (!TREE_OVERFLOW (value
))
4406 /* Avoid clobbering a shared constant. */
4407 value
= copy_node (value
);
4408 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
4411 else if (TREE_OVERFLOW (value
))
4412 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4413 value
= build_int_cst_wide (TREE_TYPE (value
),
4414 TREE_INT_CST_LOW (value
),
4415 TREE_INT_CST_HIGH (value
));
4419 /* Don't let a cast be an lvalue. */
4421 value
= non_lvalue_loc (loc
, value
);
4423 /* Don't allow the results of casting to floating-point or complex
4424 types be confused with actual constants, or casts involving
4425 integer and pointer types other than direct integer-to-integer
4426 and integer-to-pointer be confused with integer constant
4427 expressions and null pointer constants. */
4428 if (TREE_CODE (value
) == REAL_CST
4429 || TREE_CODE (value
) == COMPLEX_CST
4430 || (TREE_CODE (value
) == INTEGER_CST
4431 && !((TREE_CODE (expr
) == INTEGER_CST
4432 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
4433 || TREE_CODE (expr
) == REAL_CST
4434 || TREE_CODE (expr
) == COMPLEX_CST
)))
4435 value
= build1 (NOP_EXPR
, type
, value
);
4437 if (CAN_HAVE_LOCATION_P (value
))
4438 SET_EXPR_LOCATION (value
, loc
);
4442 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4443 location of the open paren of the cast, or the position of the cast
4446 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
4449 tree type_expr
= NULL_TREE
;
4450 bool type_expr_const
= true;
4452 int saved_wsp
= warn_strict_prototypes
;
4454 /* This avoids warnings about unprototyped casts on
4455 integers. E.g. "#define SIG_DFL (void(*)())0". */
4456 if (TREE_CODE (expr
) == INTEGER_CST
)
4457 warn_strict_prototypes
= 0;
4458 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
4459 warn_strict_prototypes
= saved_wsp
;
4461 ret
= build_c_cast (loc
, type
, expr
);
4464 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
4465 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !type_expr_const
;
4466 SET_EXPR_LOCATION (ret
, loc
);
4469 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
4470 SET_EXPR_LOCATION (ret
, loc
);
4472 /* C++ does not permits types to be defined in a cast. */
4473 if (warn_cxx_compat
&& type_name
->specs
->tag_defined_p
)
4474 warning_at (loc
, OPT_Wc___compat
,
4475 "defining a type in a cast is invalid in C++");
4480 /* Build an assignment expression of lvalue LHS from value RHS.
4481 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4482 may differ from TREE_TYPE (LHS) for an enum bitfield.
4483 MODIFYCODE is the code for a binary operator that we use
4484 to combine the old value of LHS with RHS to get the new value.
4485 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4486 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4487 which may differ from TREE_TYPE (RHS) for an enum value.
4489 LOCATION is the location of the MODIFYCODE operator.
4490 RHS_LOC is the location of the RHS. */
4493 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
4494 enum tree_code modifycode
,
4495 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
4499 tree rhs_semantic_type
= NULL_TREE
;
4500 tree lhstype
= TREE_TYPE (lhs
);
4501 tree olhstype
= lhstype
;
4504 /* Types that aren't fully specified cannot be used in assignments. */
4505 lhs
= require_complete_type (lhs
);
4507 /* Avoid duplicate error messages from operands that had errors. */
4508 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
4509 return error_mark_node
;
4511 if (!lvalue_or_else (lhs
, lv_assign
))
4512 return error_mark_node
;
4514 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4516 rhs_semantic_type
= TREE_TYPE (rhs
);
4517 rhs
= TREE_OPERAND (rhs
, 0);
4522 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
4524 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
4525 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
4527 if (inner
== error_mark_node
)
4528 return error_mark_node
;
4529 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4530 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
4531 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
4532 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
4533 protected_set_expr_location (result
, location
);
4537 /* If a binary op has been requested, combine the old LHS value with the RHS
4538 producing the value we should actually store into the LHS. */
4540 if (modifycode
!= NOP_EXPR
)
4542 lhs
= c_fully_fold (lhs
, false, NULL
);
4543 lhs
= stabilize_reference (lhs
);
4544 newrhs
= build_binary_op (location
,
4545 modifycode
, lhs
, rhs
, 1);
4547 /* The original type of the right hand side is no longer
4549 rhs_origtype
= NULL_TREE
;
4552 /* Give an error for storing in something that is 'const'. */
4554 if (TYPE_READONLY (lhstype
)
4555 || ((TREE_CODE (lhstype
) == RECORD_TYPE
4556 || TREE_CODE (lhstype
) == UNION_TYPE
)
4557 && C_TYPE_FIELDS_READONLY (lhstype
)))
4559 readonly_error (lhs
, lv_assign
);
4560 return error_mark_node
;
4562 else if (TREE_READONLY (lhs
))
4563 readonly_warning (lhs
, lv_assign
);
4565 /* If storing into a structure or union member,
4566 it has probably been given type `int'.
4567 Compute the type that would go with
4568 the actual amount of storage the member occupies. */
4570 if (TREE_CODE (lhs
) == COMPONENT_REF
4571 && (TREE_CODE (lhstype
) == INTEGER_TYPE
4572 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
4573 || TREE_CODE (lhstype
) == REAL_TYPE
4574 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
4575 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
4577 /* If storing in a field that is in actuality a short or narrower than one,
4578 we must store in the field in its actual type. */
4580 if (lhstype
!= TREE_TYPE (lhs
))
4582 lhs
= copy_node (lhs
);
4583 TREE_TYPE (lhs
) = lhstype
;
4586 /* Issue -Wc++-compat warnings about an assignment to an enum type
4587 when LHS does not have its original type. This happens for,
4588 e.g., an enum bitfield in a struct. */
4590 && lhs_origtype
!= NULL_TREE
4591 && lhs_origtype
!= lhstype
4592 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
4594 tree checktype
= (rhs_origtype
!= NULL_TREE
4597 if (checktype
!= error_mark_node
4598 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
))
4599 warning_at (location
, OPT_Wc___compat
,
4600 "enum conversion in assignment is invalid in C++");
4603 /* Convert new value to destination type. Fold it first, then
4604 restore any excess precision information, for the sake of
4605 conversion warnings. */
4607 npc
= null_pointer_constant_p (newrhs
);
4608 newrhs
= c_fully_fold (newrhs
, false, NULL
);
4609 if (rhs_semantic_type
)
4610 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
4611 newrhs
= convert_for_assignment (location
, lhstype
, newrhs
, rhs_origtype
,
4612 ic_assign
, npc
, NULL_TREE
, NULL_TREE
, 0);
4613 if (TREE_CODE (newrhs
) == ERROR_MARK
)
4614 return error_mark_node
;
4616 /* Emit ObjC write barrier, if necessary. */
4617 if (c_dialect_objc () && flag_objc_gc
)
4619 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
4622 protected_set_expr_location (result
, location
);
4627 /* Scan operands. */
4629 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
4630 TREE_SIDE_EFFECTS (result
) = 1;
4631 protected_set_expr_location (result
, location
);
4633 /* If we got the LHS in a different type for storing in,
4634 convert the result back to the nominal type of LHS
4635 so that the value we return always has the same type
4636 as the LHS argument. */
4638 if (olhstype
== TREE_TYPE (result
))
4641 result
= convert_for_assignment (location
, olhstype
, result
, rhs_origtype
,
4642 ic_assign
, false, NULL_TREE
, NULL_TREE
, 0);
4643 protected_set_expr_location (result
, location
);
4647 /* Convert value RHS to type TYPE as preparation for an assignment to
4648 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4649 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4650 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4651 constant before any folding.
4652 The real work of conversion is done by `convert'.
4653 The purpose of this function is to generate error messages
4654 for assignments that are not allowed in C.
4655 ERRTYPE says whether it is argument passing, assignment,
4656 initialization or return.
4658 LOCATION is the location of the RHS.
4659 FUNCTION is a tree for the function being called.
4660 PARMNUM is the number of the argument, for printing in error messages. */
4663 convert_for_assignment (location_t location
, tree type
, tree rhs
,
4664 tree origtype
, enum impl_conv errtype
,
4665 bool null_pointer_constant
, tree fundecl
,
4666 tree function
, int parmnum
)
4668 enum tree_code codel
= TREE_CODE (type
);
4669 tree orig_rhs
= rhs
;
4671 enum tree_code coder
;
4672 tree rname
= NULL_TREE
;
4673 bool objc_ok
= false;
4675 if (errtype
== ic_argpass
)
4678 /* Change pointer to function to the function itself for
4680 if (TREE_CODE (function
) == ADDR_EXPR
4681 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
4682 function
= TREE_OPERAND (function
, 0);
4684 /* Handle an ObjC selector specially for diagnostics. */
4685 selector
= objc_message_selector ();
4687 if (selector
&& parmnum
> 2)
4694 /* This macro is used to emit diagnostics to ensure that all format
4695 strings are complete sentences, visible to gettext and checked at
4697 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4702 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4703 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4704 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4705 "expected %qT but argument is of type %qT", \
4709 pedwarn (LOCATION, OPT, AS); \
4712 pedwarn (LOCATION, OPT, IN); \
4715 pedwarn (LOCATION, OPT, RE); \
4718 gcc_unreachable (); \
4722 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4723 rhs
= TREE_OPERAND (rhs
, 0);
4725 rhstype
= TREE_TYPE (rhs
);
4726 coder
= TREE_CODE (rhstype
);
4728 if (coder
== ERROR_MARK
)
4729 return error_mark_node
;
4731 if (c_dialect_objc ())
4754 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
4757 if (warn_cxx_compat
)
4759 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
4760 if (checktype
!= error_mark_node
4761 && TREE_CODE (type
) == ENUMERAL_TYPE
4762 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
4764 WARN_FOR_ASSIGNMENT (input_location
, OPT_Wc___compat
,
4765 G_("enum conversion when passing argument "
4766 "%d of %qE is invalid in C++"),
4767 G_("enum conversion in assignment is "
4769 G_("enum conversion in initialization is "
4771 G_("enum conversion in return is "
4776 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
4779 if (coder
== VOID_TYPE
)
4781 /* Except for passing an argument to an unprototyped function,
4782 this is a constraint violation. When passing an argument to
4783 an unprototyped function, it is compile-time undefined;
4784 making it a constraint in that case was rejected in
4786 error_at (location
, "void value not ignored as it ought to be");
4787 return error_mark_node
;
4789 rhs
= require_complete_type (rhs
);
4790 if (rhs
== error_mark_node
)
4791 return error_mark_node
;
4792 /* A type converts to a reference to it.
4793 This code doesn't fully support references, it's just for the
4794 special case of va_start and va_copy. */
4795 if (codel
== REFERENCE_TYPE
4796 && comptypes (TREE_TYPE (type
), TREE_TYPE (rhs
)) == 1)
4798 if (!lvalue_p (rhs
))
4800 error_at (location
, "cannot pass rvalue to reference parameter");
4801 return error_mark_node
;
4803 if (!c_mark_addressable (rhs
))
4804 return error_mark_node
;
4805 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
4806 SET_EXPR_LOCATION (rhs
, location
);
4808 /* We already know that these two types are compatible, but they
4809 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4810 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4811 likely to be va_list, a typedef to __builtin_va_list, which
4812 is different enough that it will cause problems later. */
4813 if (TREE_TYPE (TREE_TYPE (rhs
)) != TREE_TYPE (type
))
4815 rhs
= build1 (NOP_EXPR
, build_pointer_type (TREE_TYPE (type
)), rhs
);
4816 SET_EXPR_LOCATION (rhs
, location
);
4819 rhs
= build1 (NOP_EXPR
, type
, rhs
);
4820 SET_EXPR_LOCATION (rhs
, location
);
4823 /* Some types can interconvert without explicit casts. */
4824 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
4825 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
4826 return convert (type
, rhs
);
4827 /* Arithmetic types all interconvert, and enum is treated like int. */
4828 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
4829 || codel
== FIXED_POINT_TYPE
4830 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
4831 || codel
== BOOLEAN_TYPE
)
4832 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
4833 || coder
== FIXED_POINT_TYPE
4834 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
4835 || coder
== BOOLEAN_TYPE
))
4838 bool save
= in_late_binary_op
;
4839 if (codel
== BOOLEAN_TYPE
)
4840 in_late_binary_op
= true;
4841 ret
= convert_and_check (type
, orig_rhs
);
4842 if (codel
== BOOLEAN_TYPE
)
4843 in_late_binary_op
= save
;
4847 /* Aggregates in different TUs might need conversion. */
4848 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
4850 && comptypes (type
, rhstype
))
4851 return convert_and_check (type
, rhs
);
4853 /* Conversion to a transparent union from its member types.
4854 This applies only to function arguments. */
4855 if (codel
== UNION_TYPE
&& TYPE_TRANSPARENT_UNION (type
)
4856 && errtype
== ic_argpass
)
4858 tree memb
, marginal_memb
= NULL_TREE
;
4860 for (memb
= TYPE_FIELDS (type
); memb
; memb
= TREE_CHAIN (memb
))
4862 tree memb_type
= TREE_TYPE (memb
);
4864 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
4865 TYPE_MAIN_VARIANT (rhstype
)))
4868 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
4871 if (coder
== POINTER_TYPE
)
4873 tree ttl
= TREE_TYPE (memb_type
);
4874 tree ttr
= TREE_TYPE (rhstype
);
4876 /* Any non-function converts to a [const][volatile] void *
4877 and vice versa; otherwise, targets must be the same.
4878 Meanwhile, the lhs target must have all the qualifiers of
4880 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
4881 || comp_target_types (location
, memb_type
, rhstype
))
4883 /* If this type won't generate any warnings, use it. */
4884 if (TYPE_QUALS (ttl
) == TYPE_QUALS (ttr
)
4885 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
4886 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
4887 ? ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
4888 == TYPE_QUALS (ttr
))
4889 : ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
4890 == TYPE_QUALS (ttl
))))
4893 /* Keep looking for a better type, but remember this one. */
4895 marginal_memb
= memb
;
4899 /* Can convert integer zero to any pointer type. */
4900 if (null_pointer_constant
)
4902 rhs
= null_pointer_node
;
4907 if (memb
|| marginal_memb
)
4911 /* We have only a marginally acceptable member type;
4912 it needs a warning. */
4913 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
4914 tree ttr
= TREE_TYPE (rhstype
);
4916 /* Const and volatile mean something different for function
4917 types, so the usual warnings are not appropriate. */
4918 if (TREE_CODE (ttr
) == FUNCTION_TYPE
4919 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
4921 /* Because const and volatile on functions are
4922 restrictions that say the function will not do
4923 certain things, it is okay to use a const or volatile
4924 function where an ordinary one is wanted, but not
4926 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
4927 WARN_FOR_ASSIGNMENT (location
, 0,
4928 G_("passing argument %d of %qE "
4929 "makes qualified function "
4930 "pointer from unqualified"),
4931 G_("assignment makes qualified "
4932 "function pointer from "
4934 G_("initialization makes qualified "
4935 "function pointer from "
4937 G_("return makes qualified function "
4938 "pointer from unqualified"));
4940 else if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
4941 WARN_FOR_ASSIGNMENT (location
, 0,
4942 G_("passing argument %d of %qE discards "
4943 "qualifiers from pointer target type"),
4944 G_("assignment discards qualifiers "
4945 "from pointer target type"),
4946 G_("initialization discards qualifiers "
4947 "from pointer target type"),
4948 G_("return discards qualifiers from "
4949 "pointer target type"));
4951 memb
= marginal_memb
;
4954 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
4955 pedwarn (location
, OPT_pedantic
,
4956 "ISO C prohibits argument conversion to union type");
4958 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
4959 return build_constructor_single (type
, memb
, rhs
);
4963 /* Conversions among pointers */
4964 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
4965 && (coder
== codel
))
4967 tree ttl
= TREE_TYPE (type
);
4968 tree ttr
= TREE_TYPE (rhstype
);
4971 bool is_opaque_pointer
;
4972 int target_cmp
= 0; /* Cache comp_target_types () result. */
4974 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
4975 mvl
= TYPE_MAIN_VARIANT (mvl
);
4976 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
4977 mvr
= TYPE_MAIN_VARIANT (mvr
);
4978 /* Opaque pointers are treated like void pointers. */
4979 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
4981 /* C++ does not allow the implicit conversion void* -> T*. However,
4982 for the purpose of reducing the number of false positives, we
4983 tolerate the special case of
4987 where NULL is typically defined in C to be '(void *) 0'. */
4988 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
4989 warning_at (location
, OPT_Wc___compat
,
4990 "request for implicit conversion "
4991 "from %qT to %qT not permitted in C++", rhstype
, type
);
4993 /* Check if the right-hand side has a format attribute but the
4994 left-hand side doesn't. */
4995 if (warn_missing_format_attribute
4996 && check_missing_format_attribute (type
, rhstype
))
5001 warning_at (location
, OPT_Wmissing_format_attribute
,
5002 "argument %d of %qE might be "
5003 "a candidate for a format attribute",
5007 warning_at (location
, OPT_Wmissing_format_attribute
,
5008 "assignment left-hand side might be "
5009 "a candidate for a format attribute");
5012 warning_at (location
, OPT_Wmissing_format_attribute
,
5013 "initialization left-hand side might be "
5014 "a candidate for a format attribute");
5017 warning_at (location
, OPT_Wmissing_format_attribute
,
5018 "return type might be "
5019 "a candidate for a format attribute");
5026 /* Any non-function converts to a [const][volatile] void *
5027 and vice versa; otherwise, targets must be the same.
5028 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5029 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
5030 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
5031 || is_opaque_pointer
5032 || (c_common_unsigned_type (mvl
)
5033 == c_common_unsigned_type (mvr
)))
5036 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5039 && !null_pointer_constant
5040 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
5041 WARN_FOR_ASSIGNMENT (location
, OPT_pedantic
,
5042 G_("ISO C forbids passing argument %d of "
5043 "%qE between function pointer "
5045 G_("ISO C forbids assignment between "
5046 "function pointer and %<void *%>"),
5047 G_("ISO C forbids initialization between "
5048 "function pointer and %<void *%>"),
5049 G_("ISO C forbids return between function "
5050 "pointer and %<void *%>"));
5051 /* Const and volatile mean something different for function types,
5052 so the usual warnings are not appropriate. */
5053 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
5054 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
5056 if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
5058 /* Types differing only by the presence of the 'volatile'
5059 qualifier are acceptable if the 'volatile' has been added
5060 in by the Objective-C EH machinery. */
5061 if (!objc_type_quals_match (ttl
, ttr
))
5062 WARN_FOR_ASSIGNMENT (location
, 0,
5063 G_("passing argument %d of %qE discards "
5064 "qualifiers from pointer target type"),
5065 G_("assignment discards qualifiers "
5066 "from pointer target type"),
5067 G_("initialization discards qualifiers "
5068 "from pointer target type"),
5069 G_("return discards qualifiers from "
5070 "pointer target type"));
5072 /* If this is not a case of ignoring a mismatch in signedness,
5074 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
5077 /* If there is a mismatch, do warn. */
5078 else if (warn_pointer_sign
)
5079 WARN_FOR_ASSIGNMENT (location
, OPT_Wpointer_sign
,
5080 G_("pointer targets in passing argument "
5081 "%d of %qE differ in signedness"),
5082 G_("pointer targets in assignment "
5083 "differ in signedness"),
5084 G_("pointer targets in initialization "
5085 "differ in signedness"),
5086 G_("pointer targets in return differ "
5089 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
5090 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5092 /* Because const and volatile on functions are restrictions
5093 that say the function will not do certain things,
5094 it is okay to use a const or volatile function
5095 where an ordinary one is wanted, but not vice-versa. */
5096 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
5097 WARN_FOR_ASSIGNMENT (location
, 0,
5098 G_("passing argument %d of %qE makes "
5099 "qualified function pointer "
5100 "from unqualified"),
5101 G_("assignment makes qualified function "
5102 "pointer from unqualified"),
5103 G_("initialization makes qualified "
5104 "function pointer from unqualified"),
5105 G_("return makes qualified function "
5106 "pointer from unqualified"));
5110 /* Avoid warning about the volatile ObjC EH puts on decls. */
5112 WARN_FOR_ASSIGNMENT (location
, 0,
5113 G_("passing argument %d of %qE from "
5114 "incompatible pointer type"),
5115 G_("assignment from incompatible pointer type"),
5116 G_("initialization from incompatible "
5118 G_("return from incompatible pointer type"));
5120 return convert (type
, rhs
);
5122 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
5124 /* ??? This should not be an error when inlining calls to
5125 unprototyped functions. */
5126 error_at (location
, "invalid use of non-lvalue array");
5127 return error_mark_node
;
5129 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
5131 /* An explicit constant 0 can convert to a pointer,
5132 or one that results from arithmetic, even including
5133 a cast to integer type. */
5134 if (!null_pointer_constant
)
5135 WARN_FOR_ASSIGNMENT (location
, 0,
5136 G_("passing argument %d of %qE makes "
5137 "pointer from integer without a cast"),
5138 G_("assignment makes pointer from integer "
5140 G_("initialization makes pointer from "
5141 "integer without a cast"),
5142 G_("return makes pointer from integer "
5145 return convert (type
, rhs
);
5147 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
5149 WARN_FOR_ASSIGNMENT (location
, 0,
5150 G_("passing argument %d of %qE makes integer "
5151 "from pointer without a cast"),
5152 G_("assignment makes integer from pointer "
5154 G_("initialization makes integer from pointer "
5156 G_("return makes integer from pointer "
5158 return convert (type
, rhs
);
5160 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
5163 bool save
= in_late_binary_op
;
5164 in_late_binary_op
= true;
5165 ret
= convert (type
, rhs
);
5166 in_late_binary_op
= save
;
5173 error_at (location
, "incompatible type for argument %d of %qE", parmnum
, rname
);
5174 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
5175 ? DECL_SOURCE_LOCATION (fundecl
) : input_location
,
5176 "expected %qT but argument is of type %qT", type
, rhstype
);
5179 error_at (location
, "incompatible types when assigning to type %qT from "
5180 "type %qT", type
, rhstype
);
5184 "incompatible types when initializing type %qT using type %qT",
5189 "incompatible types when returning type %qT but %qT was "
5190 "expected", rhstype
, type
);
5196 return error_mark_node
;
5199 /* If VALUE is a compound expr all of whose expressions are constant, then
5200 return its value. Otherwise, return error_mark_node.
5202 This is for handling COMPOUND_EXPRs as initializer elements
5203 which is allowed with a warning when -pedantic is specified. */
5206 valid_compound_expr_initializer (tree value
, tree endtype
)
5208 if (TREE_CODE (value
) == COMPOUND_EXPR
)
5210 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
5212 return error_mark_node
;
5213 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
5216 else if (!initializer_constant_valid_p (value
, endtype
))
5217 return error_mark_node
;
5222 /* Perform appropriate conversions on the initial value of a variable,
5223 store it in the declaration DECL,
5224 and print any error messages that are appropriate.
5225 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5226 If the init is invalid, store an ERROR_MARK.
5228 INIT_LOC is the location of the initial value. */
5231 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
5236 /* If variable's type was invalidly declared, just ignore it. */
5238 type
= TREE_TYPE (decl
);
5239 if (TREE_CODE (type
) == ERROR_MARK
)
5242 /* Digest the specified initializer into an expression. */
5245 npc
= null_pointer_constant_p (init
);
5246 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
5247 true, TREE_STATIC (decl
));
5249 /* Store the expression if valid; else report error. */
5251 if (!in_system_header
5252 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
5253 warning (OPT_Wtraditional
, "traditional C rejects automatic "
5254 "aggregate initialization");
5256 DECL_INITIAL (decl
) = value
;
5258 /* ANSI wants warnings about out-of-range constant initializers. */
5259 STRIP_TYPE_NOPS (value
);
5260 if (TREE_STATIC (decl
))
5261 constant_expression_warning (value
);
5263 /* Check if we need to set array size from compound literal size. */
5264 if (TREE_CODE (type
) == ARRAY_TYPE
5265 && TYPE_DOMAIN (type
) == 0
5266 && value
!= error_mark_node
)
5268 tree inside_init
= init
;
5270 STRIP_TYPE_NOPS (inside_init
);
5271 inside_init
= fold (inside_init
);
5273 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5275 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
5277 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
5279 /* For int foo[] = (int [3]){1}; we need to set array size
5280 now since later on array initializer will be just the
5281 brace enclosed list of the compound literal. */
5282 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
5283 TREE_TYPE (decl
) = type
;
5284 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
5286 layout_decl (cldecl
, 0);
5292 /* Methods for storing and printing names for error messages. */
5294 /* Implement a spelling stack that allows components of a name to be pushed
5295 and popped. Each element on the stack is this structure. */
5302 unsigned HOST_WIDE_INT i
;
5307 #define SPELLING_STRING 1
5308 #define SPELLING_MEMBER 2
5309 #define SPELLING_BOUNDS 3
5311 static struct spelling
*spelling
; /* Next stack element (unused). */
5312 static struct spelling
*spelling_base
; /* Spelling stack base. */
5313 static int spelling_size
; /* Size of the spelling stack. */
5315 /* Macros to save and restore the spelling stack around push_... functions.
5316 Alternative to SAVE_SPELLING_STACK. */
5318 #define SPELLING_DEPTH() (spelling - spelling_base)
5319 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5321 /* Push an element on the spelling stack with type KIND and assign VALUE
5324 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5326 int depth = SPELLING_DEPTH (); \
5328 if (depth >= spelling_size) \
5330 spelling_size += 10; \
5331 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5333 RESTORE_SPELLING_DEPTH (depth); \
5336 spelling->kind = (KIND); \
5337 spelling->MEMBER = (VALUE); \
5341 /* Push STRING on the stack. Printed literally. */
5344 push_string (const char *string
)
5346 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
5349 /* Push a member name on the stack. Printed as '.' STRING. */
5352 push_member_name (tree decl
)
5354 const char *const string
5356 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
5357 : _("<anonymous>"));
5358 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
5361 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5364 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
5366 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
5369 /* Compute the maximum size in bytes of the printed spelling. */
5372 spelling_length (void)
5377 for (p
= spelling_base
; p
< spelling
; p
++)
5379 if (p
->kind
== SPELLING_BOUNDS
)
5382 size
+= strlen (p
->u
.s
) + 1;
5388 /* Print the spelling to BUFFER and return it. */
5391 print_spelling (char *buffer
)
5396 for (p
= spelling_base
; p
< spelling
; p
++)
5397 if (p
->kind
== SPELLING_BOUNDS
)
5399 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
5405 if (p
->kind
== SPELLING_MEMBER
)
5407 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
5414 /* Issue an error message for a bad initializer component.
5415 MSGID identifies the message.
5416 The component name is taken from the spelling stack. */
5419 error_init (const char *msgid
)
5423 error ("%s", _(msgid
));
5424 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5426 error ("(near initialization for %qs)", ofwhat
);
5429 /* Issue a pedantic warning for a bad initializer component. OPT is
5430 the option OPT_* (from options.h) controlling this warning or 0 if
5431 it is unconditionally given. MSGID identifies the message. The
5432 component name is taken from the spelling stack. */
5435 pedwarn_init (location_t location
, int opt
, const char *msgid
)
5439 pedwarn (location
, opt
, "%s", _(msgid
));
5440 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5442 pedwarn (location
, opt
, "(near initialization for %qs)", ofwhat
);
5445 /* Issue a warning for a bad initializer component.
5447 OPT is the OPT_W* value corresponding to the warning option that
5448 controls this warning. MSGID identifies the message. The
5449 component name is taken from the spelling stack. */
5452 warning_init (int opt
, const char *msgid
)
5456 warning (opt
, "%s", _(msgid
));
5457 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5459 warning (opt
, "(near initialization for %qs)", ofwhat
);
5462 /* If TYPE is an array type and EXPR is a parenthesized string
5463 constant, warn if pedantic that EXPR is being used to initialize an
5464 object of type TYPE. */
5467 maybe_warn_string_init (tree type
, struct c_expr expr
)
5470 && TREE_CODE (type
) == ARRAY_TYPE
5471 && TREE_CODE (expr
.value
) == STRING_CST
5472 && expr
.original_code
!= STRING_CST
)
5473 pedwarn_init (input_location
, OPT_pedantic
,
5474 "array initialized from parenthesized string constant");
5477 /* Digest the parser output INIT as an initializer for type TYPE.
5478 Return a C expression of type TYPE to represent the initial value.
5480 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5482 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5484 If INIT is a string constant, STRICT_STRING is true if it is
5485 unparenthesized or we should not warn here for it being parenthesized.
5486 For other types of INIT, STRICT_STRING is not used.
5488 INIT_LOC is the location of the INIT.
5490 REQUIRE_CONSTANT requests an error if non-constant initializers or
5491 elements are seen. */
5494 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
5495 bool null_pointer_constant
, bool strict_string
,
5496 int require_constant
)
5498 enum tree_code code
= TREE_CODE (type
);
5499 tree inside_init
= init
;
5500 tree semantic_type
= NULL_TREE
;
5501 bool maybe_const
= true;
5503 if (type
== error_mark_node
5505 || init
== error_mark_node
5506 || TREE_TYPE (init
) == error_mark_node
)
5507 return error_mark_node
;
5509 STRIP_TYPE_NOPS (inside_init
);
5511 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
5513 semantic_type
= TREE_TYPE (inside_init
);
5514 inside_init
= TREE_OPERAND (inside_init
, 0);
5516 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
5517 inside_init
= decl_constant_value_for_optimization (inside_init
);
5519 /* Initialization of an array of chars from a string constant
5520 optionally enclosed in braces. */
5522 if (code
== ARRAY_TYPE
&& inside_init
5523 && TREE_CODE (inside_init
) == STRING_CST
)
5525 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
5526 /* Note that an array could be both an array of character type
5527 and an array of wchar_t if wchar_t is signed char or unsigned
5529 bool char_array
= (typ1
== char_type_node
5530 || typ1
== signed_char_type_node
5531 || typ1
== unsigned_char_type_node
);
5532 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
5533 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
5534 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
5536 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
5539 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
5540 expr
.value
= inside_init
;
5541 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
5542 expr
.original_type
= NULL
;
5543 maybe_warn_string_init (type
, expr
);
5545 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
5546 pedwarn_init (init_loc
, OPT_pedantic
,
5547 "initialization of a flexible array member");
5549 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5550 TYPE_MAIN_VARIANT (type
)))
5555 if (typ2
!= char_type_node
)
5557 error_init ("char-array initialized from wide string");
5558 return error_mark_node
;
5563 if (typ2
== char_type_node
)
5565 error_init ("wide character array initialized from non-wide "
5567 return error_mark_node
;
5569 else if (!comptypes(typ1
, typ2
))
5571 error_init ("wide character array initialized from "
5572 "incompatible wide string");
5573 return error_mark_node
;
5577 TREE_TYPE (inside_init
) = type
;
5578 if (TYPE_DOMAIN (type
) != 0
5579 && TYPE_SIZE (type
) != 0
5580 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
5582 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
5584 /* Subtract the size of a single (possibly wide) character
5585 because it's ok to ignore the terminating null char
5586 that is counted in the length of the constant. */
5587 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
5589 - (TYPE_PRECISION (typ1
)
5591 pedwarn_init (init_loc
, 0,
5592 ("initializer-string for array of chars "
5594 else if (warn_cxx_compat
5595 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
5596 warning_at (init_loc
, OPT_Wc___compat
,
5597 ("initializer-string for array chars "
5598 "is too long for C++"));
5603 else if (INTEGRAL_TYPE_P (typ1
))
5605 error_init ("array of inappropriate type initialized "
5606 "from string constant");
5607 return error_mark_node
;
5611 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5612 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5613 below and handle as a constructor. */
5614 if (code
== VECTOR_TYPE
5615 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
5616 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
5617 && TREE_CONSTANT (inside_init
))
5619 if (TREE_CODE (inside_init
) == VECTOR_CST
5620 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5621 TYPE_MAIN_VARIANT (type
)))
5624 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
5626 unsigned HOST_WIDE_INT ix
;
5628 bool constant_p
= true;
5630 /* Iterate through elements and check if all constructor
5631 elements are *_CSTs. */
5632 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
5633 if (!CONSTANT_CLASS_P (value
))
5640 return build_vector_from_ctor (type
,
5641 CONSTRUCTOR_ELTS (inside_init
));
5645 if (warn_sequence_point
)
5646 verify_sequence_points (inside_init
);
5648 /* Any type can be initialized
5649 from an expression of the same type, optionally with braces. */
5651 if (inside_init
&& TREE_TYPE (inside_init
) != 0
5652 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
5653 TYPE_MAIN_VARIANT (type
))
5654 || (code
== ARRAY_TYPE
5655 && comptypes (TREE_TYPE (inside_init
), type
))
5656 || (code
== VECTOR_TYPE
5657 && comptypes (TREE_TYPE (inside_init
), type
))
5658 || (code
== POINTER_TYPE
5659 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
5660 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
5661 TREE_TYPE (type
)))))
5663 if (code
== POINTER_TYPE
)
5665 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
5667 if (TREE_CODE (inside_init
) == STRING_CST
5668 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5669 inside_init
= array_to_pointer_conversion
5670 (init_loc
, inside_init
);
5673 error_init ("invalid use of non-lvalue array");
5674 return error_mark_node
;
5679 if (code
== VECTOR_TYPE
)
5680 /* Although the types are compatible, we may require a
5682 inside_init
= convert (type
, inside_init
);
5684 if (require_constant
5685 && (code
== VECTOR_TYPE
|| !flag_isoc99
)
5686 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
5688 /* As an extension, allow initializing objects with static storage
5689 duration with compound literals (which are then treated just as
5690 the brace enclosed list they contain). Also allow this for
5691 vectors, as we can only assign them with compound literals. */
5692 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
5693 inside_init
= DECL_INITIAL (decl
);
5696 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
5697 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
5699 error_init ("array initialized from non-constant array expression");
5700 return error_mark_node
;
5703 /* Compound expressions can only occur here if -pedantic or
5704 -pedantic-errors is specified. In the later case, we always want
5705 an error. In the former case, we simply want a warning. */
5706 if (require_constant
&& pedantic
5707 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
5710 = valid_compound_expr_initializer (inside_init
,
5711 TREE_TYPE (inside_init
));
5712 if (inside_init
== error_mark_node
)
5713 error_init ("initializer element is not constant");
5715 pedwarn_init (init_loc
, OPT_pedantic
,
5716 "initializer element is not constant");
5717 if (flag_pedantic_errors
)
5718 inside_init
= error_mark_node
;
5720 else if (require_constant
5721 && !initializer_constant_valid_p (inside_init
,
5722 TREE_TYPE (inside_init
)))
5724 error_init ("initializer element is not constant");
5725 inside_init
= error_mark_node
;
5727 else if (require_constant
&& !maybe_const
)
5728 pedwarn_init (init_loc
, 0,
5729 "initializer element is not a constant expression");
5731 /* Added to enable additional -Wmissing-format-attribute warnings. */
5732 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
5733 inside_init
= convert_for_assignment (init_loc
, type
, inside_init
,
5735 ic_init
, null_pointer_constant
,
5736 NULL_TREE
, NULL_TREE
, 0);
5740 /* Handle scalar types, including conversions. */
5742 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
5743 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
5744 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
5746 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
5747 && (TREE_CODE (init
) == STRING_CST
5748 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
5749 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
5751 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
5754 = convert_for_assignment (init_loc
, type
, inside_init
, origtype
,
5755 ic_init
, null_pointer_constant
,
5756 NULL_TREE
, NULL_TREE
, 0);
5758 /* Check to see if we have already given an error message. */
5759 if (inside_init
== error_mark_node
)
5761 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
5763 error_init ("initializer element is not constant");
5764 inside_init
= error_mark_node
;
5766 else if (require_constant
5767 && !initializer_constant_valid_p (inside_init
,
5768 TREE_TYPE (inside_init
)))
5770 error_init ("initializer element is not computable at load time");
5771 inside_init
= error_mark_node
;
5773 else if (require_constant
&& !maybe_const
)
5774 pedwarn_init (init_loc
, 0,
5775 "initializer element is not a constant expression");
5780 /* Come here only for records and arrays. */
5782 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
5784 error_init ("variable-sized object may not be initialized");
5785 return error_mark_node
;
5788 error_init ("invalid initializer");
5789 return error_mark_node
;
5792 /* Handle initializers that use braces. */
5794 /* Type of object we are accumulating a constructor for.
5795 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5796 static tree constructor_type
;
5798 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5800 static tree constructor_fields
;
5802 /* For an ARRAY_TYPE, this is the specified index
5803 at which to store the next element we get. */
5804 static tree constructor_index
;
5806 /* For an ARRAY_TYPE, this is the maximum index. */
5807 static tree constructor_max_index
;
5809 /* For a RECORD_TYPE, this is the first field not yet written out. */
5810 static tree constructor_unfilled_fields
;
5812 /* For an ARRAY_TYPE, this is the index of the first element
5813 not yet written out. */
5814 static tree constructor_unfilled_index
;
5816 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5817 This is so we can generate gaps between fields, when appropriate. */
5818 static tree constructor_bit_index
;
5820 /* If we are saving up the elements rather than allocating them,
5821 this is the list of elements so far (in reverse order,
5822 most recent first). */
5823 static VEC(constructor_elt
,gc
) *constructor_elements
;
5825 /* 1 if constructor should be incrementally stored into a constructor chain,
5826 0 if all the elements should be kept in AVL tree. */
5827 static int constructor_incremental
;
5829 /* 1 if so far this constructor's elements are all compile-time constants. */
5830 static int constructor_constant
;
5832 /* 1 if so far this constructor's elements are all valid address constants. */
5833 static int constructor_simple
;
5835 /* 1 if this constructor has an element that cannot be part of a
5836 constant expression. */
5837 static int constructor_nonconst
;
5839 /* 1 if this constructor is erroneous so far. */
5840 static int constructor_erroneous
;
5842 /* Structure for managing pending initializer elements, organized as an
5847 struct init_node
*left
, *right
;
5848 struct init_node
*parent
;
5855 /* Tree of pending elements at this constructor level.
5856 These are elements encountered out of order
5857 which belong at places we haven't reached yet in actually
5859 Will never hold tree nodes across GC runs. */
5860 static struct init_node
*constructor_pending_elts
;
5862 /* The SPELLING_DEPTH of this constructor. */
5863 static int constructor_depth
;
5865 /* DECL node for which an initializer is being read.
5866 0 means we are reading a constructor expression
5867 such as (struct foo) {...}. */
5868 static tree constructor_decl
;
5870 /* Nonzero if this is an initializer for a top-level decl. */
5871 static int constructor_top_level
;
5873 /* Nonzero if there were any member designators in this initializer. */
5874 static int constructor_designated
;
5876 /* Nesting depth of designator list. */
5877 static int designator_depth
;
5879 /* Nonzero if there were diagnosed errors in this designator list. */
5880 static int designator_erroneous
;
5883 /* This stack has a level for each implicit or explicit level of
5884 structuring in the initializer, including the outermost one. It
5885 saves the values of most of the variables above. */
5887 struct constructor_range_stack
;
5889 struct constructor_stack
5891 struct constructor_stack
*next
;
5896 tree unfilled_index
;
5897 tree unfilled_fields
;
5899 VEC(constructor_elt
,gc
) *elements
;
5900 struct init_node
*pending_elts
;
5903 /* If value nonzero, this value should replace the entire
5904 constructor at this level. */
5905 struct c_expr replacement_value
;
5906 struct constructor_range_stack
*range_stack
;
5917 static struct constructor_stack
*constructor_stack
;
5919 /* This stack represents designators from some range designator up to
5920 the last designator in the list. */
5922 struct constructor_range_stack
5924 struct constructor_range_stack
*next
, *prev
;
5925 struct constructor_stack
*stack
;
5932 static struct constructor_range_stack
*constructor_range_stack
;
5934 /* This stack records separate initializers that are nested.
5935 Nested initializers can't happen in ANSI C, but GNU C allows them
5936 in cases like { ... (struct foo) { ... } ... }. */
5938 struct initializer_stack
5940 struct initializer_stack
*next
;
5942 struct constructor_stack
*constructor_stack
;
5943 struct constructor_range_stack
*constructor_range_stack
;
5944 VEC(constructor_elt
,gc
) *elements
;
5945 struct spelling
*spelling
;
5946 struct spelling
*spelling_base
;
5949 char require_constant_value
;
5950 char require_constant_elements
;
5953 static struct initializer_stack
*initializer_stack
;
5955 /* Prepare to parse and output the initializer for variable DECL. */
5958 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
5961 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
5963 p
->decl
= constructor_decl
;
5964 p
->require_constant_value
= require_constant_value
;
5965 p
->require_constant_elements
= require_constant_elements
;
5966 p
->constructor_stack
= constructor_stack
;
5967 p
->constructor_range_stack
= constructor_range_stack
;
5968 p
->elements
= constructor_elements
;
5969 p
->spelling
= spelling
;
5970 p
->spelling_base
= spelling_base
;
5971 p
->spelling_size
= spelling_size
;
5972 p
->top_level
= constructor_top_level
;
5973 p
->next
= initializer_stack
;
5974 initializer_stack
= p
;
5976 constructor_decl
= decl
;
5977 constructor_designated
= 0;
5978 constructor_top_level
= top_level
;
5980 if (decl
!= 0 && decl
!= error_mark_node
)
5982 require_constant_value
= TREE_STATIC (decl
);
5983 require_constant_elements
5984 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
5985 /* For a scalar, you can always use any value to initialize,
5986 even within braces. */
5987 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
5988 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
5989 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
5990 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
5991 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
5995 require_constant_value
= 0;
5996 require_constant_elements
= 0;
5997 locus
= _("(anonymous)");
6000 constructor_stack
= 0;
6001 constructor_range_stack
= 0;
6003 missing_braces_mentioned
= 0;
6007 RESTORE_SPELLING_DEPTH (0);
6010 push_string (locus
);
6016 struct initializer_stack
*p
= initializer_stack
;
6018 /* Free the whole constructor stack of this initializer. */
6019 while (constructor_stack
)
6021 struct constructor_stack
*q
= constructor_stack
;
6022 constructor_stack
= q
->next
;
6026 gcc_assert (!constructor_range_stack
);
6028 /* Pop back to the data of the outer initializer (if any). */
6029 free (spelling_base
);
6031 constructor_decl
= p
->decl
;
6032 require_constant_value
= p
->require_constant_value
;
6033 require_constant_elements
= p
->require_constant_elements
;
6034 constructor_stack
= p
->constructor_stack
;
6035 constructor_range_stack
= p
->constructor_range_stack
;
6036 constructor_elements
= p
->elements
;
6037 spelling
= p
->spelling
;
6038 spelling_base
= p
->spelling_base
;
6039 spelling_size
= p
->spelling_size
;
6040 constructor_top_level
= p
->top_level
;
6041 initializer_stack
= p
->next
;
6045 /* Call here when we see the initializer is surrounded by braces.
6046 This is instead of a call to push_init_level;
6047 it is matched by a call to pop_init_level.
6049 TYPE is the type to initialize, for a constructor expression.
6050 For an initializer for a decl, TYPE is zero. */
6053 really_start_incremental_init (tree type
)
6055 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
6058 type
= TREE_TYPE (constructor_decl
);
6060 if (TREE_CODE (type
) == VECTOR_TYPE
6061 && TYPE_VECTOR_OPAQUE (type
))
6062 error ("opaque vector types cannot be initialized");
6064 p
->type
= constructor_type
;
6065 p
->fields
= constructor_fields
;
6066 p
->index
= constructor_index
;
6067 p
->max_index
= constructor_max_index
;
6068 p
->unfilled_index
= constructor_unfilled_index
;
6069 p
->unfilled_fields
= constructor_unfilled_fields
;
6070 p
->bit_index
= constructor_bit_index
;
6071 p
->elements
= constructor_elements
;
6072 p
->constant
= constructor_constant
;
6073 p
->simple
= constructor_simple
;
6074 p
->nonconst
= constructor_nonconst
;
6075 p
->erroneous
= constructor_erroneous
;
6076 p
->pending_elts
= constructor_pending_elts
;
6077 p
->depth
= constructor_depth
;
6078 p
->replacement_value
.value
= 0;
6079 p
->replacement_value
.original_code
= ERROR_MARK
;
6080 p
->replacement_value
.original_type
= NULL
;
6084 p
->incremental
= constructor_incremental
;
6085 p
->designated
= constructor_designated
;
6087 constructor_stack
= p
;
6089 constructor_constant
= 1;
6090 constructor_simple
= 1;
6091 constructor_nonconst
= 0;
6092 constructor_depth
= SPELLING_DEPTH ();
6093 constructor_elements
= 0;
6094 constructor_pending_elts
= 0;
6095 constructor_type
= type
;
6096 constructor_incremental
= 1;
6097 constructor_designated
= 0;
6098 designator_depth
= 0;
6099 designator_erroneous
= 0;
6101 if (TREE_CODE (constructor_type
) == RECORD_TYPE
6102 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6104 constructor_fields
= TYPE_FIELDS (constructor_type
);
6105 /* Skip any nameless bit fields at the beginning. */
6106 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
6107 && DECL_NAME (constructor_fields
) == 0)
6108 constructor_fields
= TREE_CHAIN (constructor_fields
);
6110 constructor_unfilled_fields
= constructor_fields
;
6111 constructor_bit_index
= bitsize_zero_node
;
6113 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6115 if (TYPE_DOMAIN (constructor_type
))
6117 constructor_max_index
6118 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
6120 /* Detect non-empty initializations of zero-length arrays. */
6121 if (constructor_max_index
== NULL_TREE
6122 && TYPE_SIZE (constructor_type
))
6123 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6125 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6126 to initialize VLAs will cause a proper error; avoid tree
6127 checking errors as well by setting a safe value. */
6128 if (constructor_max_index
6129 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
6130 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6133 = convert (bitsizetype
,
6134 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
6138 constructor_index
= bitsize_zero_node
;
6139 constructor_max_index
= NULL_TREE
;
6142 constructor_unfilled_index
= constructor_index
;
6144 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6146 /* Vectors are like simple fixed-size arrays. */
6147 constructor_max_index
=
6148 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
6149 constructor_index
= bitsize_zero_node
;
6150 constructor_unfilled_index
= constructor_index
;
6154 /* Handle the case of int x = {5}; */
6155 constructor_fields
= constructor_type
;
6156 constructor_unfilled_fields
= constructor_type
;
6160 /* Push down into a subobject, for initialization.
6161 If this is for an explicit set of braces, IMPLICIT is 0.
6162 If it is because the next element belongs at a lower level,
6163 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6166 push_init_level (int implicit
)
6168 struct constructor_stack
*p
;
6169 tree value
= NULL_TREE
;
6171 /* If we've exhausted any levels that didn't have braces,
6172 pop them now. If implicit == 1, this will have been done in
6173 process_init_element; do not repeat it here because in the case
6174 of excess initializers for an empty aggregate this leads to an
6175 infinite cycle of popping a level and immediately recreating
6179 while (constructor_stack
->implicit
)
6181 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
6182 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6183 && constructor_fields
== 0)
6184 process_init_element (pop_init_level (1), true);
6185 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
6186 && constructor_max_index
6187 && tree_int_cst_lt (constructor_max_index
,
6189 process_init_element (pop_init_level (1), true);
6195 /* Unless this is an explicit brace, we need to preserve previous
6199 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
6200 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6201 && constructor_fields
)
6202 value
= find_init_member (constructor_fields
);
6203 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6204 value
= find_init_member (constructor_index
);
6207 p
= XNEW (struct constructor_stack
);
6208 p
->type
= constructor_type
;
6209 p
->fields
= constructor_fields
;
6210 p
->index
= constructor_index
;
6211 p
->max_index
= constructor_max_index
;
6212 p
->unfilled_index
= constructor_unfilled_index
;
6213 p
->unfilled_fields
= constructor_unfilled_fields
;
6214 p
->bit_index
= constructor_bit_index
;
6215 p
->elements
= constructor_elements
;
6216 p
->constant
= constructor_constant
;
6217 p
->simple
= constructor_simple
;
6218 p
->nonconst
= constructor_nonconst
;
6219 p
->erroneous
= constructor_erroneous
;
6220 p
->pending_elts
= constructor_pending_elts
;
6221 p
->depth
= constructor_depth
;
6222 p
->replacement_value
.value
= 0;
6223 p
->replacement_value
.original_code
= ERROR_MARK
;
6224 p
->replacement_value
.original_type
= NULL
;
6225 p
->implicit
= implicit
;
6227 p
->incremental
= constructor_incremental
;
6228 p
->designated
= constructor_designated
;
6229 p
->next
= constructor_stack
;
6231 constructor_stack
= p
;
6233 constructor_constant
= 1;
6234 constructor_simple
= 1;
6235 constructor_nonconst
= 0;
6236 constructor_depth
= SPELLING_DEPTH ();
6237 constructor_elements
= 0;
6238 constructor_incremental
= 1;
6239 constructor_designated
= 0;
6240 constructor_pending_elts
= 0;
6243 p
->range_stack
= constructor_range_stack
;
6244 constructor_range_stack
= 0;
6245 designator_depth
= 0;
6246 designator_erroneous
= 0;
6249 /* Don't die if an entire brace-pair level is superfluous
6250 in the containing level. */
6251 if (constructor_type
== 0)
6253 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
6254 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6256 /* Don't die if there are extra init elts at the end. */
6257 if (constructor_fields
== 0)
6258 constructor_type
= 0;
6261 constructor_type
= TREE_TYPE (constructor_fields
);
6262 push_member_name (constructor_fields
);
6263 constructor_depth
++;
6266 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6268 constructor_type
= TREE_TYPE (constructor_type
);
6269 push_array_bounds (tree_low_cst (constructor_index
, 1));
6270 constructor_depth
++;
6273 if (constructor_type
== 0)
6275 error_init ("extra brace group at end of initializer");
6276 constructor_fields
= 0;
6277 constructor_unfilled_fields
= 0;
6281 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
6283 constructor_constant
= TREE_CONSTANT (value
);
6284 constructor_simple
= TREE_STATIC (value
);
6285 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
6286 constructor_elements
= CONSTRUCTOR_ELTS (value
);
6287 if (!VEC_empty (constructor_elt
, constructor_elements
)
6288 && (TREE_CODE (constructor_type
) == RECORD_TYPE
6289 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
6290 set_nonincremental_init ();
6293 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
6295 missing_braces_mentioned
= 1;
6296 warning_init (OPT_Wmissing_braces
, "missing braces around initializer");
6299 if (TREE_CODE (constructor_type
) == RECORD_TYPE
6300 || TREE_CODE (constructor_type
) == UNION_TYPE
)
6302 constructor_fields
= TYPE_FIELDS (constructor_type
);
6303 /* Skip any nameless bit fields at the beginning. */
6304 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
6305 && DECL_NAME (constructor_fields
) == 0)
6306 constructor_fields
= TREE_CHAIN (constructor_fields
);
6308 constructor_unfilled_fields
= constructor_fields
;
6309 constructor_bit_index
= bitsize_zero_node
;
6311 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6313 /* Vectors are like simple fixed-size arrays. */
6314 constructor_max_index
=
6315 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
6316 constructor_index
= convert (bitsizetype
, integer_zero_node
);
6317 constructor_unfilled_index
= constructor_index
;
6319 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6321 if (TYPE_DOMAIN (constructor_type
))
6323 constructor_max_index
6324 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
6326 /* Detect non-empty initializations of zero-length arrays. */
6327 if (constructor_max_index
== NULL_TREE
6328 && TYPE_SIZE (constructor_type
))
6329 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6331 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6332 to initialize VLAs will cause a proper error; avoid tree
6333 checking errors as well by setting a safe value. */
6334 if (constructor_max_index
6335 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
6336 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
6339 = convert (bitsizetype
,
6340 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
6343 constructor_index
= bitsize_zero_node
;
6345 constructor_unfilled_index
= constructor_index
;
6346 if (value
&& TREE_CODE (value
) == STRING_CST
)
6348 /* We need to split the char/wchar array into individual
6349 characters, so that we don't have to special case it
6351 set_nonincremental_init_from_string (value
);
6356 if (constructor_type
!= error_mark_node
)
6357 warning_init (0, "braces around scalar initializer");
6358 constructor_fields
= constructor_type
;
6359 constructor_unfilled_fields
= constructor_type
;
6363 /* At the end of an implicit or explicit brace level,
6364 finish up that level of constructor. If a single expression
6365 with redundant braces initialized that level, return the
6366 c_expr structure for that expression. Otherwise, the original_code
6367 element is set to ERROR_MARK.
6368 If we were outputting the elements as they are read, return 0 as the value
6369 from inner levels (process_init_element ignores that),
6370 but return error_mark_node as the value from the outermost level
6371 (that's what we want to put in DECL_INITIAL).
6372 Otherwise, return a CONSTRUCTOR expression as the value. */
6375 pop_init_level (int implicit
)
6377 struct constructor_stack
*p
;
6380 ret
.original_code
= ERROR_MARK
;
6381 ret
.original_type
= NULL
;
6385 /* When we come to an explicit close brace,
6386 pop any inner levels that didn't have explicit braces. */
6387 while (constructor_stack
->implicit
)
6388 process_init_element (pop_init_level (1), true);
6390 gcc_assert (!constructor_range_stack
);
6393 /* Now output all pending elements. */
6394 constructor_incremental
= 1;
6395 output_pending_init_elements (1);
6397 p
= constructor_stack
;
6399 /* Error for initializing a flexible array member, or a zero-length
6400 array member in an inappropriate context. */
6401 if (constructor_type
&& constructor_fields
6402 && TREE_CODE (constructor_type
) == ARRAY_TYPE
6403 && TYPE_DOMAIN (constructor_type
)
6404 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
6406 /* Silently discard empty initializations. The parser will
6407 already have pedwarned for empty brackets. */
6408 if (integer_zerop (constructor_unfilled_index
))
6409 constructor_type
= NULL_TREE
;
6412 gcc_assert (!TYPE_SIZE (constructor_type
));
6414 if (constructor_depth
> 2)
6415 error_init ("initialization of flexible array member in a nested context");
6417 pedwarn_init (input_location
, OPT_pedantic
,
6418 "initialization of a flexible array member");
6420 /* We have already issued an error message for the existence
6421 of a flexible array member not at the end of the structure.
6422 Discard the initializer so that we do not die later. */
6423 if (TREE_CHAIN (constructor_fields
) != NULL_TREE
)
6424 constructor_type
= NULL_TREE
;
6428 /* Warn when some struct elements are implicitly initialized to zero. */
6429 if (warn_missing_field_initializers
6431 && TREE_CODE (constructor_type
) == RECORD_TYPE
6432 && constructor_unfilled_fields
)
6434 /* Do not warn for flexible array members or zero-length arrays. */
6435 while (constructor_unfilled_fields
6436 && (!DECL_SIZE (constructor_unfilled_fields
)
6437 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
6438 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
6440 /* Do not warn if this level of the initializer uses member
6441 designators; it is likely to be deliberate. */
6442 if (constructor_unfilled_fields
&& !constructor_designated
)
6444 push_member_name (constructor_unfilled_fields
);
6445 warning_init (OPT_Wmissing_field_initializers
,
6446 "missing initializer");
6447 RESTORE_SPELLING_DEPTH (constructor_depth
);
6451 /* Pad out the end of the structure. */
6452 if (p
->replacement_value
.value
)
6453 /* If this closes a superfluous brace pair,
6454 just pass out the element between them. */
6455 ret
= p
->replacement_value
;
6456 else if (constructor_type
== 0)
6458 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
6459 && TREE_CODE (constructor_type
) != UNION_TYPE
6460 && TREE_CODE (constructor_type
) != ARRAY_TYPE
6461 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
6463 /* A nonincremental scalar initializer--just return
6464 the element, after verifying there is just one. */
6465 if (VEC_empty (constructor_elt
,constructor_elements
))
6467 if (!constructor_erroneous
)
6468 error_init ("empty scalar initializer");
6469 ret
.value
= error_mark_node
;
6471 else if (VEC_length (constructor_elt
,constructor_elements
) != 1)
6473 error_init ("extra elements in scalar initializer");
6474 ret
.value
= VEC_index (constructor_elt
,constructor_elements
,0)->value
;
6477 ret
.value
= VEC_index (constructor_elt
,constructor_elements
,0)->value
;
6481 if (constructor_erroneous
)
6482 ret
.value
= error_mark_node
;
6485 ret
.value
= build_constructor (constructor_type
,
6486 constructor_elements
);
6487 if (constructor_constant
)
6488 TREE_CONSTANT (ret
.value
) = 1;
6489 if (constructor_constant
&& constructor_simple
)
6490 TREE_STATIC (ret
.value
) = 1;
6491 if (constructor_nonconst
)
6492 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
6496 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
6498 if (constructor_nonconst
)
6499 ret
.original_code
= C_MAYBE_CONST_EXPR
;
6500 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
6501 ret
.original_code
= ERROR_MARK
;
6504 constructor_type
= p
->type
;
6505 constructor_fields
= p
->fields
;
6506 constructor_index
= p
->index
;
6507 constructor_max_index
= p
->max_index
;
6508 constructor_unfilled_index
= p
->unfilled_index
;
6509 constructor_unfilled_fields
= p
->unfilled_fields
;
6510 constructor_bit_index
= p
->bit_index
;
6511 constructor_elements
= p
->elements
;
6512 constructor_constant
= p
->constant
;
6513 constructor_simple
= p
->simple
;
6514 constructor_nonconst
= p
->nonconst
;
6515 constructor_erroneous
= p
->erroneous
;
6516 constructor_incremental
= p
->incremental
;
6517 constructor_designated
= p
->designated
;
6518 constructor_pending_elts
= p
->pending_elts
;
6519 constructor_depth
= p
->depth
;
6521 constructor_range_stack
= p
->range_stack
;
6522 RESTORE_SPELLING_DEPTH (constructor_depth
);
6524 constructor_stack
= p
->next
;
6527 if (ret
.value
== 0 && constructor_stack
== 0)
6528 ret
.value
= error_mark_node
;
6532 /* Common handling for both array range and field name designators.
6533 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6536 set_designator (int array
)
6539 enum tree_code subcode
;
6541 /* Don't die if an entire brace-pair level is superfluous
6542 in the containing level. */
6543 if (constructor_type
== 0)
6546 /* If there were errors in this designator list already, bail out
6548 if (designator_erroneous
)
6551 if (!designator_depth
)
6553 gcc_assert (!constructor_range_stack
);
6555 /* Designator list starts at the level of closest explicit
6557 while (constructor_stack
->implicit
)
6558 process_init_element (pop_init_level (1), true);
6559 constructor_designated
= 1;
6563 switch (TREE_CODE (constructor_type
))
6567 subtype
= TREE_TYPE (constructor_fields
);
6568 if (subtype
!= error_mark_node
)
6569 subtype
= TYPE_MAIN_VARIANT (subtype
);
6572 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6578 subcode
= TREE_CODE (subtype
);
6579 if (array
&& subcode
!= ARRAY_TYPE
)
6581 error_init ("array index in non-array initializer");
6584 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
6586 error_init ("field name not in record or union initializer");
6590 constructor_designated
= 1;
6591 push_init_level (2);
6595 /* If there are range designators in designator list, push a new designator
6596 to constructor_range_stack. RANGE_END is end of such stack range or
6597 NULL_TREE if there is no range designator at this level. */
6600 push_range_stack (tree range_end
)
6602 struct constructor_range_stack
*p
;
6604 p
= GGC_NEW (struct constructor_range_stack
);
6605 p
->prev
= constructor_range_stack
;
6607 p
->fields
= constructor_fields
;
6608 p
->range_start
= constructor_index
;
6609 p
->index
= constructor_index
;
6610 p
->stack
= constructor_stack
;
6611 p
->range_end
= range_end
;
6612 if (constructor_range_stack
)
6613 constructor_range_stack
->next
= p
;
6614 constructor_range_stack
= p
;
6617 /* Within an array initializer, specify the next index to be initialized.
6618 FIRST is that index. If LAST is nonzero, then initialize a range
6619 of indices, running from FIRST through LAST. */
6622 set_init_index (tree first
, tree last
)
6624 if (set_designator (1))
6627 designator_erroneous
= 1;
6629 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
6630 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
6632 error_init ("array index in initializer not of integer type");
6636 if (TREE_CODE (first
) != INTEGER_CST
)
6638 first
= c_fully_fold (first
, false, NULL
);
6639 if (TREE_CODE (first
) == INTEGER_CST
)
6640 pedwarn_init (input_location
, OPT_pedantic
,
6641 "array index in initializer is not "
6642 "an integer constant expression");
6645 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
6647 last
= c_fully_fold (last
, false, NULL
);
6648 if (TREE_CODE (last
) == INTEGER_CST
)
6649 pedwarn_init (input_location
, OPT_pedantic
,
6650 "array index in initializer is not "
6651 "an integer constant expression");
6654 if (TREE_CODE (first
) != INTEGER_CST
)
6655 error_init ("nonconstant array index in initializer");
6656 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
6657 error_init ("nonconstant array index in initializer");
6658 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
6659 error_init ("array index in non-array initializer");
6660 else if (tree_int_cst_sgn (first
) == -1)
6661 error_init ("array index in initializer exceeds array bounds");
6662 else if (constructor_max_index
6663 && tree_int_cst_lt (constructor_max_index
, first
))
6664 error_init ("array index in initializer exceeds array bounds");
6667 constant_expression_warning (first
);
6669 constant_expression_warning (last
);
6670 constructor_index
= convert (bitsizetype
, first
);
6674 if (tree_int_cst_equal (first
, last
))
6676 else if (tree_int_cst_lt (last
, first
))
6678 error_init ("empty index range in initializer");
6683 last
= convert (bitsizetype
, last
);
6684 if (constructor_max_index
!= 0
6685 && tree_int_cst_lt (constructor_max_index
, last
))
6687 error_init ("array index range in initializer exceeds array bounds");
6694 designator_erroneous
= 0;
6695 if (constructor_range_stack
|| last
)
6696 push_range_stack (last
);
6700 /* Within a struct initializer, specify the next field to be initialized. */
6703 set_init_label (tree fieldname
)
6707 if (set_designator (0))
6710 designator_erroneous
= 1;
6712 if (TREE_CODE (constructor_type
) != RECORD_TYPE
6713 && TREE_CODE (constructor_type
) != UNION_TYPE
)
6715 error_init ("field name not in record or union initializer");
6719 for (tail
= TYPE_FIELDS (constructor_type
); tail
;
6720 tail
= TREE_CHAIN (tail
))
6722 if (DECL_NAME (tail
) == fieldname
)
6727 error ("unknown field %qE specified in initializer", fieldname
);
6730 constructor_fields
= tail
;
6732 designator_erroneous
= 0;
6733 if (constructor_range_stack
)
6734 push_range_stack (NULL_TREE
);
6738 /* Add a new initializer to the tree of pending initializers. PURPOSE
6739 identifies the initializer, either array index or field in a structure.
6740 VALUE is the value of that index or field. If ORIGTYPE is not
6741 NULL_TREE, it is the original type of VALUE.
6743 IMPLICIT is true if value comes from pop_init_level (1),
6744 the new initializer has been merged with the existing one
6745 and thus no warnings should be emitted about overriding an
6746 existing initializer. */
6749 add_pending_init (tree purpose
, tree value
, tree origtype
, bool implicit
)
6751 struct init_node
*p
, **q
, *r
;
6753 q
= &constructor_pending_elts
;
6756 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6761 if (tree_int_cst_lt (purpose
, p
->purpose
))
6763 else if (tree_int_cst_lt (p
->purpose
, purpose
))
6769 if (TREE_SIDE_EFFECTS (p
->value
))
6770 warning_init (0, "initialized field with side-effects overwritten");
6771 else if (warn_override_init
)
6772 warning_init (OPT_Woverride_init
, "initialized field overwritten");
6775 p
->origtype
= origtype
;
6784 bitpos
= bit_position (purpose
);
6788 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
6790 else if (p
->purpose
!= purpose
)
6796 if (TREE_SIDE_EFFECTS (p
->value
))
6797 warning_init (0, "initialized field with side-effects overwritten");
6798 else if (warn_override_init
)
6799 warning_init (OPT_Woverride_init
, "initialized field overwritten");
6802 p
->origtype
= origtype
;
6808 r
= GGC_NEW (struct init_node
);
6809 r
->purpose
= purpose
;
6811 r
->origtype
= origtype
;
6821 struct init_node
*s
;
6825 if (p
->balance
== 0)
6827 else if (p
->balance
< 0)
6834 p
->left
->parent
= p
;
6851 constructor_pending_elts
= r
;
6856 struct init_node
*t
= r
->right
;
6860 r
->right
->parent
= r
;
6865 p
->left
->parent
= p
;
6868 p
->balance
= t
->balance
< 0;
6869 r
->balance
= -(t
->balance
> 0);
6884 constructor_pending_elts
= t
;
6890 /* p->balance == +1; growth of left side balances the node. */
6895 else /* r == p->right */
6897 if (p
->balance
== 0)
6898 /* Growth propagation from right side. */
6900 else if (p
->balance
> 0)
6907 p
->right
->parent
= p
;
6924 constructor_pending_elts
= r
;
6926 else /* r->balance == -1 */
6929 struct init_node
*t
= r
->left
;
6933 r
->left
->parent
= r
;
6938 p
->right
->parent
= p
;
6941 r
->balance
= (t
->balance
< 0);
6942 p
->balance
= -(t
->balance
> 0);
6957 constructor_pending_elts
= t
;
6963 /* p->balance == -1; growth of right side balances the node. */
6974 /* Build AVL tree from a sorted chain. */
6977 set_nonincremental_init (void)
6979 unsigned HOST_WIDE_INT ix
;
6982 if (TREE_CODE (constructor_type
) != RECORD_TYPE
6983 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
6986 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
6987 add_pending_init (index
, value
, NULL_TREE
, false);
6988 constructor_elements
= 0;
6989 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
6991 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
6992 /* Skip any nameless bit fields at the beginning. */
6993 while (constructor_unfilled_fields
!= 0
6994 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
6995 && DECL_NAME (constructor_unfilled_fields
) == 0)
6996 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
6999 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7001 if (TYPE_DOMAIN (constructor_type
))
7002 constructor_unfilled_index
7003 = convert (bitsizetype
,
7004 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7006 constructor_unfilled_index
= bitsize_zero_node
;
7008 constructor_incremental
= 0;
7011 /* Build AVL tree from a string constant. */
7014 set_nonincremental_init_from_string (tree str
)
7016 tree value
, purpose
, type
;
7017 HOST_WIDE_INT val
[2];
7018 const char *p
, *end
;
7019 int byte
, wchar_bytes
, charwidth
, bitpos
;
7021 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
7023 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
7024 charwidth
= TYPE_PRECISION (char_type_node
);
7025 type
= TREE_TYPE (constructor_type
);
7026 p
= TREE_STRING_POINTER (str
);
7027 end
= p
+ TREE_STRING_LENGTH (str
);
7029 for (purpose
= bitsize_zero_node
;
7030 p
< end
&& !tree_int_cst_lt (constructor_max_index
, purpose
);
7031 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
7033 if (wchar_bytes
== 1)
7035 val
[1] = (unsigned char) *p
++;
7042 for (byte
= 0; byte
< wchar_bytes
; byte
++)
7044 if (BYTES_BIG_ENDIAN
)
7045 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
7047 bitpos
= byte
* charwidth
;
7048 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
7049 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
7050 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
7054 if (!TYPE_UNSIGNED (type
))
7056 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
7057 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
7059 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
7061 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
7065 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
7070 else if (val
[0] & (((HOST_WIDE_INT
) 1)
7071 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
7072 val
[0] |= ((HOST_WIDE_INT
) -1)
7073 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
7076 value
= build_int_cst_wide (type
, val
[1], val
[0]);
7077 add_pending_init (purpose
, value
, NULL_TREE
, false);
7080 constructor_incremental
= 0;
7083 /* Return value of FIELD in pending initializer or zero if the field was
7084 not initialized yet. */
7087 find_init_member (tree field
)
7089 struct init_node
*p
;
7091 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7093 if (constructor_incremental
7094 && tree_int_cst_lt (field
, constructor_unfilled_index
))
7095 set_nonincremental_init ();
7097 p
= constructor_pending_elts
;
7100 if (tree_int_cst_lt (field
, p
->purpose
))
7102 else if (tree_int_cst_lt (p
->purpose
, field
))
7108 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7110 tree bitpos
= bit_position (field
);
7112 if (constructor_incremental
7113 && (!constructor_unfilled_fields
7114 || tree_int_cst_lt (bitpos
,
7115 bit_position (constructor_unfilled_fields
))))
7116 set_nonincremental_init ();
7118 p
= constructor_pending_elts
;
7121 if (field
== p
->purpose
)
7123 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7129 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7131 if (!VEC_empty (constructor_elt
, constructor_elements
)
7132 && (VEC_last (constructor_elt
, constructor_elements
)->index
7134 return VEC_last (constructor_elt
, constructor_elements
)->value
;
7139 /* "Output" the next constructor element.
7140 At top level, really output it to assembler code now.
7141 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7142 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7143 TYPE is the data type that the containing data type wants here.
7144 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7145 If VALUE is a string constant, STRICT_STRING is true if it is
7146 unparenthesized or we should not warn here for it being parenthesized.
7147 For other types of VALUE, STRICT_STRING is not used.
7149 PENDING if non-nil means output pending elements that belong
7150 right after this element. (PENDING is normally 1;
7151 it is 0 while outputting pending elements, to avoid recursion.)
7153 IMPLICIT is true if value comes from pop_init_level (1),
7154 the new initializer has been merged with the existing one
7155 and thus no warnings should be emitted about overriding an
7156 existing initializer. */
7159 output_init_element (tree value
, tree origtype
, bool strict_string
, tree type
,
7160 tree field
, int pending
, bool implicit
)
7162 tree semantic_type
= NULL_TREE
;
7163 constructor_elt
*celt
;
7164 bool maybe_const
= true;
7167 if (type
== error_mark_node
|| value
== error_mark_node
)
7169 constructor_erroneous
= 1;
7172 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
7173 && (TREE_CODE (value
) == STRING_CST
7174 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
7175 && !(TREE_CODE (value
) == STRING_CST
7176 && TREE_CODE (type
) == ARRAY_TYPE
7177 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
7178 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
7179 TYPE_MAIN_VARIANT (type
)))
7180 value
= array_to_pointer_conversion (input_location
, value
);
7182 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
7183 && require_constant_value
&& !flag_isoc99
&& pending
)
7185 /* As an extension, allow initializing objects with static storage
7186 duration with compound literals (which are then treated just as
7187 the brace enclosed list they contain). */
7188 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
7189 value
= DECL_INITIAL (decl
);
7192 npc
= null_pointer_constant_p (value
);
7193 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
7195 semantic_type
= TREE_TYPE (value
);
7196 value
= TREE_OPERAND (value
, 0);
7198 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
7200 if (value
== error_mark_node
)
7201 constructor_erroneous
= 1;
7202 else if (!TREE_CONSTANT (value
))
7203 constructor_constant
= 0;
7204 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
7205 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
7206 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7207 && DECL_C_BIT_FIELD (field
)
7208 && TREE_CODE (value
) != INTEGER_CST
))
7209 constructor_simple
= 0;
7211 constructor_nonconst
= 1;
7213 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
7215 if (require_constant_value
)
7217 error_init ("initializer element is not constant");
7218 value
= error_mark_node
;
7220 else if (require_constant_elements
)
7221 pedwarn (input_location
, 0,
7222 "initializer element is not computable at load time");
7224 else if (!maybe_const
7225 && (require_constant_value
|| require_constant_elements
))
7226 pedwarn_init (input_location
, 0,
7227 "initializer element is not a constant expression");
7229 /* Issue -Wc++-compat warnings about initializing a bitfield with
7232 && field
!= NULL_TREE
7233 && TREE_CODE (field
) == FIELD_DECL
7234 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
7235 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
7236 != TYPE_MAIN_VARIANT (type
))
7237 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
7239 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
7240 if (checktype
!= error_mark_node
7241 && (TYPE_MAIN_VARIANT (checktype
)
7242 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
7243 warning_init (OPT_Wc___compat
,
7244 "enum conversion in initialization is invalid in C++");
7247 /* If this field is empty (and not at the end of structure),
7248 don't do anything other than checking the initializer. */
7250 && (TREE_TYPE (field
) == error_mark_node
7251 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
7252 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
7253 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
7254 || TREE_CHAIN (field
)))))
7258 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
7259 value
= digest_init (input_location
, type
, value
, origtype
, npc
,
7260 strict_string
, require_constant_value
);
7261 if (value
== error_mark_node
)
7263 constructor_erroneous
= 1;
7266 if (require_constant_value
|| require_constant_elements
)
7267 constant_expression_warning (value
);
7269 /* If this element doesn't come next in sequence,
7270 put it on constructor_pending_elts. */
7271 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7272 && (!constructor_incremental
7273 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
7275 if (constructor_incremental
7276 && tree_int_cst_lt (field
, constructor_unfilled_index
))
7277 set_nonincremental_init ();
7279 add_pending_init (field
, value
, origtype
, implicit
);
7282 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7283 && (!constructor_incremental
7284 || field
!= constructor_unfilled_fields
))
7286 /* We do this for records but not for unions. In a union,
7287 no matter which field is specified, it can be initialized
7288 right away since it starts at the beginning of the union. */
7289 if (constructor_incremental
)
7291 if (!constructor_unfilled_fields
)
7292 set_nonincremental_init ();
7295 tree bitpos
, unfillpos
;
7297 bitpos
= bit_position (field
);
7298 unfillpos
= bit_position (constructor_unfilled_fields
);
7300 if (tree_int_cst_lt (bitpos
, unfillpos
))
7301 set_nonincremental_init ();
7305 add_pending_init (field
, value
, origtype
, implicit
);
7308 else if (TREE_CODE (constructor_type
) == UNION_TYPE
7309 && !VEC_empty (constructor_elt
, constructor_elements
))
7313 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt
,
7314 constructor_elements
)->value
))
7316 "initialized field with side-effects overwritten");
7317 else if (warn_override_init
)
7318 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7321 /* We can have just one union field set. */
7322 constructor_elements
= 0;
7325 /* Otherwise, output this element either to
7326 constructor_elements or to the assembler file. */
7328 celt
= VEC_safe_push (constructor_elt
, gc
, constructor_elements
, NULL
);
7329 celt
->index
= field
;
7330 celt
->value
= value
;
7332 /* Advance the variable that indicates sequential elements output. */
7333 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7334 constructor_unfilled_index
7335 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
7337 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7339 constructor_unfilled_fields
7340 = TREE_CHAIN (constructor_unfilled_fields
);
7342 /* Skip any nameless bit fields. */
7343 while (constructor_unfilled_fields
!= 0
7344 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7345 && DECL_NAME (constructor_unfilled_fields
) == 0)
7346 constructor_unfilled_fields
=
7347 TREE_CHAIN (constructor_unfilled_fields
);
7349 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7350 constructor_unfilled_fields
= 0;
7352 /* Now output any pending elements which have become next. */
7354 output_pending_init_elements (0);
7357 /* Output any pending elements which have become next.
7358 As we output elements, constructor_unfilled_{fields,index}
7359 advances, which may cause other elements to become next;
7360 if so, they too are output.
7362 If ALL is 0, we return when there are
7363 no more pending elements to output now.
7365 If ALL is 1, we output space as necessary so that
7366 we can output all the pending elements. */
7369 output_pending_init_elements (int all
)
7371 struct init_node
*elt
= constructor_pending_elts
;
7376 /* Look through the whole pending tree.
7377 If we find an element that should be output now,
7378 output it. Otherwise, set NEXT to the element
7379 that comes first among those still pending. */
7384 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7386 if (tree_int_cst_equal (elt
->purpose
,
7387 constructor_unfilled_index
))
7388 output_init_element (elt
->value
, elt
->origtype
, true,
7389 TREE_TYPE (constructor_type
),
7390 constructor_unfilled_index
, 0, false);
7391 else if (tree_int_cst_lt (constructor_unfilled_index
,
7394 /* Advance to the next smaller node. */
7399 /* We have reached the smallest node bigger than the
7400 current unfilled index. Fill the space first. */
7401 next
= elt
->purpose
;
7407 /* Advance to the next bigger node. */
7412 /* We have reached the biggest node in a subtree. Find
7413 the parent of it, which is the next bigger node. */
7414 while (elt
->parent
&& elt
->parent
->right
== elt
)
7417 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
7420 next
= elt
->purpose
;
7426 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7427 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7429 tree ctor_unfilled_bitpos
, elt_bitpos
;
7431 /* If the current record is complete we are done. */
7432 if (constructor_unfilled_fields
== 0)
7435 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
7436 elt_bitpos
= bit_position (elt
->purpose
);
7437 /* We can't compare fields here because there might be empty
7438 fields in between. */
7439 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
7441 constructor_unfilled_fields
= elt
->purpose
;
7442 output_init_element (elt
->value
, elt
->origtype
, true,
7443 TREE_TYPE (elt
->purpose
),
7444 elt
->purpose
, 0, false);
7446 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
7448 /* Advance to the next smaller node. */
7453 /* We have reached the smallest node bigger than the
7454 current unfilled field. Fill the space first. */
7455 next
= elt
->purpose
;
7461 /* Advance to the next bigger node. */
7466 /* We have reached the biggest node in a subtree. Find
7467 the parent of it, which is the next bigger node. */
7468 while (elt
->parent
&& elt
->parent
->right
== elt
)
7472 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
7473 bit_position (elt
->purpose
))))
7475 next
= elt
->purpose
;
7483 /* Ordinarily return, but not if we want to output all
7484 and there are elements left. */
7485 if (!(all
&& next
!= 0))
7488 /* If it's not incremental, just skip over the gap, so that after
7489 jumping to retry we will output the next successive element. */
7490 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7491 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7492 constructor_unfilled_fields
= next
;
7493 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7494 constructor_unfilled_index
= next
;
7496 /* ELT now points to the node in the pending tree with the next
7497 initializer to output. */
7501 /* Add one non-braced element to the current constructor level.
7502 This adjusts the current position within the constructor's type.
7503 This may also start or terminate implicit levels
7504 to handle a partly-braced initializer.
7506 Once this has found the correct level for the new element,
7507 it calls output_init_element.
7509 IMPLICIT is true if value comes from pop_init_level (1),
7510 the new initializer has been merged with the existing one
7511 and thus no warnings should be emitted about overriding an
7512 existing initializer. */
7515 process_init_element (struct c_expr value
, bool implicit
)
7517 tree orig_value
= value
.value
;
7518 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
7519 bool strict_string
= value
.original_code
== STRING_CST
;
7521 designator_depth
= 0;
7522 designator_erroneous
= 0;
7524 /* Handle superfluous braces around string cst as in
7525 char x[] = {"foo"}; */
7528 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7529 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
7530 && integer_zerop (constructor_unfilled_index
))
7532 if (constructor_stack
->replacement_value
.value
)
7533 error_init ("excess elements in char array initializer");
7534 constructor_stack
->replacement_value
= value
;
7538 if (constructor_stack
->replacement_value
.value
!= 0)
7540 error_init ("excess elements in struct initializer");
7544 /* Ignore elements of a brace group if it is entirely superfluous
7545 and has already been diagnosed. */
7546 if (constructor_type
== 0)
7549 /* If we've exhausted any levels that didn't have braces,
7551 while (constructor_stack
->implicit
)
7553 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7554 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7555 && constructor_fields
== 0)
7556 process_init_element (pop_init_level (1), true);
7557 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
7558 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7559 && (constructor_max_index
== 0
7560 || tree_int_cst_lt (constructor_max_index
,
7561 constructor_index
)))
7562 process_init_element (pop_init_level (1), true);
7567 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7568 if (constructor_range_stack
)
7570 /* If value is a compound literal and we'll be just using its
7571 content, don't put it into a SAVE_EXPR. */
7572 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
7573 || !require_constant_value
7576 tree semantic_type
= NULL_TREE
;
7577 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
7579 semantic_type
= TREE_TYPE (value
.value
);
7580 value
.value
= TREE_OPERAND (value
.value
, 0);
7582 value
.value
= c_save_expr (value
.value
);
7584 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7591 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7594 enum tree_code fieldcode
;
7596 if (constructor_fields
== 0)
7598 pedwarn_init (input_location
, 0,
7599 "excess elements in struct initializer");
7603 fieldtype
= TREE_TYPE (constructor_fields
);
7604 if (fieldtype
!= error_mark_node
)
7605 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
7606 fieldcode
= TREE_CODE (fieldtype
);
7608 /* Error for non-static initialization of a flexible array member. */
7609 if (fieldcode
== ARRAY_TYPE
7610 && !require_constant_value
7611 && TYPE_SIZE (fieldtype
) == NULL_TREE
7612 && TREE_CHAIN (constructor_fields
) == NULL_TREE
)
7614 error_init ("non-static initialization of a flexible array member");
7618 /* Accept a string constant to initialize a subarray. */
7619 if (value
.value
!= 0
7620 && fieldcode
== ARRAY_TYPE
7621 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
7623 value
.value
= orig_value
;
7624 /* Otherwise, if we have come to a subaggregate,
7625 and we don't have an element of its type, push into it. */
7626 else if (value
.value
!= 0
7627 && value
.value
!= error_mark_node
7628 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
7629 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
7630 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
7632 push_init_level (1);
7638 push_member_name (constructor_fields
);
7639 output_init_element (value
.value
, value
.original_type
,
7640 strict_string
, fieldtype
,
7641 constructor_fields
, 1, implicit
);
7642 RESTORE_SPELLING_DEPTH (constructor_depth
);
7645 /* Do the bookkeeping for an element that was
7646 directly output as a constructor. */
7648 /* For a record, keep track of end position of last field. */
7649 if (DECL_SIZE (constructor_fields
))
7650 constructor_bit_index
7651 = size_binop_loc (input_location
, PLUS_EXPR
,
7652 bit_position (constructor_fields
),
7653 DECL_SIZE (constructor_fields
));
7655 /* If the current field was the first one not yet written out,
7656 it isn't now, so update. */
7657 if (constructor_unfilled_fields
== constructor_fields
)
7659 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
7660 /* Skip any nameless bit fields. */
7661 while (constructor_unfilled_fields
!= 0
7662 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7663 && DECL_NAME (constructor_unfilled_fields
) == 0)
7664 constructor_unfilled_fields
=
7665 TREE_CHAIN (constructor_unfilled_fields
);
7669 constructor_fields
= TREE_CHAIN (constructor_fields
);
7670 /* Skip any nameless bit fields at the beginning. */
7671 while (constructor_fields
!= 0
7672 && DECL_C_BIT_FIELD (constructor_fields
)
7673 && DECL_NAME (constructor_fields
) == 0)
7674 constructor_fields
= TREE_CHAIN (constructor_fields
);
7676 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
7679 enum tree_code fieldcode
;
7681 if (constructor_fields
== 0)
7683 pedwarn_init (input_location
, 0,
7684 "excess elements in union initializer");
7688 fieldtype
= TREE_TYPE (constructor_fields
);
7689 if (fieldtype
!= error_mark_node
)
7690 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
7691 fieldcode
= TREE_CODE (fieldtype
);
7693 /* Warn that traditional C rejects initialization of unions.
7694 We skip the warning if the value is zero. This is done
7695 under the assumption that the zero initializer in user
7696 code appears conditioned on e.g. __STDC__ to avoid
7697 "missing initializer" warnings and relies on default
7698 initialization to zero in the traditional C case.
7699 We also skip the warning if the initializer is designated,
7700 again on the assumption that this must be conditional on
7701 __STDC__ anyway (and we've already complained about the
7702 member-designator already). */
7703 if (!in_system_header
&& !constructor_designated
7704 && !(value
.value
&& (integer_zerop (value
.value
)
7705 || real_zerop (value
.value
))))
7706 warning (OPT_Wtraditional
, "traditional C rejects initialization "
7709 /* Accept a string constant to initialize a subarray. */
7710 if (value
.value
!= 0
7711 && fieldcode
== ARRAY_TYPE
7712 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
7714 value
.value
= orig_value
;
7715 /* Otherwise, if we have come to a subaggregate,
7716 and we don't have an element of its type, push into it. */
7717 else if (value
.value
!= 0
7718 && value
.value
!= error_mark_node
7719 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
7720 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
7721 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
7723 push_init_level (1);
7729 push_member_name (constructor_fields
);
7730 output_init_element (value
.value
, value
.original_type
,
7731 strict_string
, fieldtype
,
7732 constructor_fields
, 1, implicit
);
7733 RESTORE_SPELLING_DEPTH (constructor_depth
);
7736 /* Do the bookkeeping for an element that was
7737 directly output as a constructor. */
7739 constructor_bit_index
= DECL_SIZE (constructor_fields
);
7740 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
7743 constructor_fields
= 0;
7745 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7747 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7748 enum tree_code eltcode
= TREE_CODE (elttype
);
7750 /* Accept a string constant to initialize a subarray. */
7751 if (value
.value
!= 0
7752 && eltcode
== ARRAY_TYPE
7753 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
7755 value
.value
= orig_value
;
7756 /* Otherwise, if we have come to a subaggregate,
7757 and we don't have an element of its type, push into it. */
7758 else if (value
.value
!= 0
7759 && value
.value
!= error_mark_node
7760 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
7761 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
7762 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
7764 push_init_level (1);
7768 if (constructor_max_index
!= 0
7769 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
7770 || integer_all_onesp (constructor_max_index
)))
7772 pedwarn_init (input_location
, 0,
7773 "excess elements in array initializer");
7777 /* Now output the actual element. */
7780 push_array_bounds (tree_low_cst (constructor_index
, 1));
7781 output_init_element (value
.value
, value
.original_type
,
7782 strict_string
, elttype
,
7783 constructor_index
, 1, implicit
);
7784 RESTORE_SPELLING_DEPTH (constructor_depth
);
7788 = size_binop_loc (input_location
, PLUS_EXPR
,
7789 constructor_index
, bitsize_one_node
);
7792 /* If we are doing the bookkeeping for an element that was
7793 directly output as a constructor, we must update
7794 constructor_unfilled_index. */
7795 constructor_unfilled_index
= constructor_index
;
7797 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7799 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7801 /* Do a basic check of initializer size. Note that vectors
7802 always have a fixed size derived from their type. */
7803 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
7805 pedwarn_init (input_location
, 0,
7806 "excess elements in vector initializer");
7810 /* Now output the actual element. */
7813 if (TREE_CODE (value
.value
) == VECTOR_CST
)
7814 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
7815 output_init_element (value
.value
, value
.original_type
,
7816 strict_string
, elttype
,
7817 constructor_index
, 1, implicit
);
7821 = size_binop_loc (input_location
,
7822 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
7825 /* If we are doing the bookkeeping for an element that was
7826 directly output as a constructor, we must update
7827 constructor_unfilled_index. */
7828 constructor_unfilled_index
= constructor_index
;
7831 /* Handle the sole element allowed in a braced initializer
7832 for a scalar variable. */
7833 else if (constructor_type
!= error_mark_node
7834 && constructor_fields
== 0)
7836 pedwarn_init (input_location
, 0,
7837 "excess elements in scalar initializer");
7843 output_init_element (value
.value
, value
.original_type
,
7844 strict_string
, constructor_type
,
7845 NULL_TREE
, 1, implicit
);
7846 constructor_fields
= 0;
7849 /* Handle range initializers either at this level or anywhere higher
7850 in the designator stack. */
7851 if (constructor_range_stack
)
7853 struct constructor_range_stack
*p
, *range_stack
;
7856 range_stack
= constructor_range_stack
;
7857 constructor_range_stack
= 0;
7858 while (constructor_stack
!= range_stack
->stack
)
7860 gcc_assert (constructor_stack
->implicit
);
7861 process_init_element (pop_init_level (1), true);
7863 for (p
= range_stack
;
7864 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
7867 gcc_assert (constructor_stack
->implicit
);
7868 process_init_element (pop_init_level (1), true);
7871 p
->index
= size_binop_loc (input_location
,
7872 PLUS_EXPR
, p
->index
, bitsize_one_node
);
7873 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
7878 constructor_index
= p
->index
;
7879 constructor_fields
= p
->fields
;
7880 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
7888 push_init_level (2);
7889 p
->stack
= constructor_stack
;
7890 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
7891 p
->index
= p
->range_start
;
7895 constructor_range_stack
= range_stack
;
7902 constructor_range_stack
= 0;
7905 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7906 (guaranteed to be 'volatile' or null) and ARGS (represented using
7907 an ASM_EXPR node). */
7909 build_asm_stmt (tree cv_qualifier
, tree args
)
7911 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
7912 ASM_VOLATILE_P (args
) = 1;
7913 return add_stmt (args
);
7916 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7917 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7918 SIMPLE indicates whether there was anything at all after the
7919 string in the asm expression -- asm("blah") and asm("blah" : )
7920 are subtly different. We use a ASM_EXPR node to represent this. */
7922 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
7923 tree clobbers
, tree labels
, bool simple
)
7928 const char *constraint
;
7929 const char **oconstraints
;
7930 bool allows_mem
, allows_reg
, is_inout
;
7931 int ninputs
, noutputs
;
7933 ninputs
= list_length (inputs
);
7934 noutputs
= list_length (outputs
);
7935 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
7937 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
7939 /* Remove output conversions that change the type but not the mode. */
7940 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
7942 tree output
= TREE_VALUE (tail
);
7944 /* ??? Really, this should not be here. Users should be using a
7945 proper lvalue, dammit. But there's a long history of using casts
7946 in the output operands. In cases like longlong.h, this becomes a
7947 primitive form of typechecking -- if the cast can be removed, then
7948 the output operand had a type of the proper width; otherwise we'll
7949 get an error. Gross, but ... */
7950 STRIP_NOPS (output
);
7952 if (!lvalue_or_else (output
, lv_asm
))
7953 output
= error_mark_node
;
7955 if (output
!= error_mark_node
7956 && (TREE_READONLY (output
)
7957 || TYPE_READONLY (TREE_TYPE (output
))
7958 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
7959 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
7960 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
7961 readonly_error (output
, lv_asm
);
7963 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
7964 oconstraints
[i
] = constraint
;
7966 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
7967 &allows_mem
, &allows_reg
, &is_inout
))
7969 /* If the operand is going to end up in memory,
7970 mark it addressable. */
7971 if (!allows_reg
&& !c_mark_addressable (output
))
7972 output
= error_mark_node
;
7975 output
= error_mark_node
;
7977 TREE_VALUE (tail
) = output
;
7980 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
7984 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
7985 input
= TREE_VALUE (tail
);
7987 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
7988 oconstraints
, &allows_mem
, &allows_reg
))
7990 /* If the operand is going to end up in memory,
7991 mark it addressable. */
7992 if (!allows_reg
&& allows_mem
)
7994 /* Strip the nops as we allow this case. FIXME, this really
7995 should be rejected or made deprecated. */
7997 if (!c_mark_addressable (input
))
7998 input
= error_mark_node
;
8002 input
= error_mark_node
;
8004 TREE_VALUE (tail
) = input
;
8007 /* ASMs with labels cannot have outputs. This should have been
8008 enforced by the parser. */
8009 gcc_assert (outputs
== NULL
|| labels
== NULL
);
8011 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
8013 /* asm statements without outputs, including simple ones, are treated
8015 ASM_INPUT_P (args
) = simple
;
8016 ASM_VOLATILE_P (args
) = (noutputs
== 0);
8021 /* Generate a goto statement to LABEL. LOC is the location of the
8025 c_finish_goto_label (location_t loc
, tree label
)
8027 tree decl
= lookup_label_for_goto (loc
, label
);
8030 TREE_USED (decl
) = 1;
8032 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
8033 SET_EXPR_LOCATION (t
, loc
);
8034 return add_stmt (t
);
8038 /* Generate a computed goto statement to EXPR. LOC is the location of
8042 c_finish_goto_ptr (location_t loc
, tree expr
)
8045 pedwarn (loc
, OPT_pedantic
, "ISO C forbids %<goto *expr;%>");
8046 expr
= c_fully_fold (expr
, false, NULL
);
8047 expr
= convert (ptr_type_node
, expr
);
8048 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
8049 SET_EXPR_LOCATION (t
, loc
);
8050 return add_stmt (t
);
8053 /* Generate a C `return' statement. RETVAL is the expression for what
8054 to return, or a null pointer for `return;' with no value. LOC is
8055 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8056 is the original type of RETVAL. */
8059 c_finish_return (location_t loc
, tree retval
, tree origtype
)
8061 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
8062 bool no_warning
= false;
8065 if (TREE_THIS_VOLATILE (current_function_decl
))
8067 "function declared %<noreturn%> has a %<return%> statement");
8071 tree semantic_type
= NULL_TREE
;
8072 npc
= null_pointer_constant_p (retval
);
8073 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
8075 semantic_type
= TREE_TYPE (retval
);
8076 retval
= TREE_OPERAND (retval
, 0);
8078 retval
= c_fully_fold (retval
, false, NULL
);
8080 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
8085 current_function_returns_null
= 1;
8086 if ((warn_return_type
|| flag_isoc99
)
8087 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
8089 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wreturn_type
,
8090 "%<return%> with no value, in "
8091 "function returning non-void");
8095 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
8097 current_function_returns_null
= 1;
8098 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
8100 "%<return%> with a value, in function returning void");
8102 pedwarn (loc
, OPT_pedantic
, "ISO C forbids "
8103 "%<return%> with expression, in function returning void");
8107 tree t
= convert_for_assignment (loc
, valtype
, retval
, origtype
,
8109 npc
, NULL_TREE
, NULL_TREE
, 0);
8110 tree res
= DECL_RESULT (current_function_decl
);
8113 current_function_returns_value
= 1;
8114 if (t
== error_mark_node
)
8117 inner
= t
= convert (TREE_TYPE (res
), t
);
8119 /* Strip any conversions, additions, and subtractions, and see if
8120 we are returning the address of a local variable. Warn if so. */
8123 switch (TREE_CODE (inner
))
8126 case NON_LVALUE_EXPR
:
8128 case POINTER_PLUS_EXPR
:
8129 inner
= TREE_OPERAND (inner
, 0);
8133 /* If the second operand of the MINUS_EXPR has a pointer
8134 type (or is converted from it), this may be valid, so
8135 don't give a warning. */
8137 tree op1
= TREE_OPERAND (inner
, 1);
8139 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
8140 && (CONVERT_EXPR_P (op1
)
8141 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
8142 op1
= TREE_OPERAND (op1
, 0);
8144 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
8147 inner
= TREE_OPERAND (inner
, 0);
8152 inner
= TREE_OPERAND (inner
, 0);
8154 while (REFERENCE_CLASS_P (inner
)
8155 && TREE_CODE (inner
) != INDIRECT_REF
)
8156 inner
= TREE_OPERAND (inner
, 0);
8159 && !DECL_EXTERNAL (inner
)
8160 && !TREE_STATIC (inner
)
8161 && DECL_CONTEXT (inner
) == current_function_decl
)
8163 0, "function returns address of local variable");
8173 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
8174 SET_EXPR_LOCATION (retval
, loc
);
8176 if (warn_sequence_point
)
8177 verify_sequence_points (retval
);
8180 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
8181 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
8182 return add_stmt (ret_stmt
);
8186 /* The SWITCH_EXPR being built. */
8189 /* The original type of the testing expression, i.e. before the
8190 default conversion is applied. */
8193 /* A splay-tree mapping the low element of a case range to the high
8194 element, or NULL_TREE if there is no high element. Used to
8195 determine whether or not a new case label duplicates an old case
8196 label. We need a tree, rather than simply a hash table, because
8197 of the GNU case range extension. */
8200 /* The bindings at the point of the switch. This is used for
8201 warnings crossing decls when branching to a case label. */
8202 struct c_spot_bindings
*bindings
;
8204 /* The next node on the stack. */
8205 struct c_switch
*next
;
8208 /* A stack of the currently active switch statements. The innermost
8209 switch statement is on the top of the stack. There is no need to
8210 mark the stack for garbage collection because it is only active
8211 during the processing of the body of a function, and we never
8212 collect at that point. */
8214 struct c_switch
*c_switch_stack
;
8216 /* Start a C switch statement, testing expression EXP. Return the new
8217 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8218 SWITCH_COND_LOC is the location of the switch's condition. */
8221 c_start_case (location_t switch_loc
,
8222 location_t switch_cond_loc
,
8225 tree orig_type
= error_mark_node
;
8226 struct c_switch
*cs
;
8228 if (exp
!= error_mark_node
)
8230 orig_type
= TREE_TYPE (exp
);
8232 if (!INTEGRAL_TYPE_P (orig_type
))
8234 if (orig_type
!= error_mark_node
)
8236 error_at (switch_cond_loc
, "switch quantity not an integer");
8237 orig_type
= error_mark_node
;
8239 exp
= integer_zero_node
;
8243 tree type
= TYPE_MAIN_VARIANT (orig_type
);
8245 if (!in_system_header
8246 && (type
== long_integer_type_node
8247 || type
== long_unsigned_type_node
))
8248 warning_at (switch_cond_loc
,
8249 OPT_Wtraditional
, "%<long%> switch expression not "
8250 "converted to %<int%> in ISO C");
8252 exp
= c_fully_fold (exp
, false, NULL
);
8253 exp
= default_conversion (exp
);
8255 if (warn_sequence_point
)
8256 verify_sequence_points (exp
);
8260 /* Add this new SWITCH_EXPR to the stack. */
8261 cs
= XNEW (struct c_switch
);
8262 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
8263 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
8264 cs
->orig_type
= orig_type
;
8265 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
8266 cs
->bindings
= c_get_switch_bindings ();
8267 cs
->next
= c_switch_stack
;
8268 c_switch_stack
= cs
;
8270 return add_stmt (cs
->switch_expr
);
8273 /* Process a case label at location LOC. */
8276 do_case (location_t loc
, tree low_value
, tree high_value
)
8278 tree label
= NULL_TREE
;
8280 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
8282 low_value
= c_fully_fold (low_value
, false, NULL
);
8283 if (TREE_CODE (low_value
) == INTEGER_CST
)
8284 pedwarn (input_location
, OPT_pedantic
,
8285 "case label is not an integer constant expression");
8288 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
8290 high_value
= c_fully_fold (high_value
, false, NULL
);
8291 if (TREE_CODE (high_value
) == INTEGER_CST
)
8292 pedwarn (input_location
, OPT_pedantic
,
8293 "case label is not an integer constant expression");
8296 if (c_switch_stack
== NULL
)
8299 error_at (loc
, "case label not within a switch statement");
8301 error_at (loc
, "%<default%> label not within a switch statement");
8305 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
8306 EXPR_LOCATION (c_switch_stack
->switch_expr
),
8310 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
8311 SWITCH_COND (c_switch_stack
->switch_expr
),
8312 c_switch_stack
->orig_type
,
8313 low_value
, high_value
);
8314 if (label
== error_mark_node
)
8319 /* Finish the switch statement. */
8322 c_finish_case (tree body
)
8324 struct c_switch
*cs
= c_switch_stack
;
8325 location_t switch_location
;
8327 SWITCH_BODY (cs
->switch_expr
) = body
;
8329 /* Emit warnings as needed. */
8330 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
8331 c_do_switch_warnings (cs
->cases
, switch_location
,
8332 TREE_TYPE (cs
->switch_expr
),
8333 SWITCH_COND (cs
->switch_expr
));
8335 /* Pop the stack. */
8336 c_switch_stack
= cs
->next
;
8337 splay_tree_delete (cs
->cases
);
8338 c_release_switch_bindings (cs
->bindings
);
8342 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8343 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8344 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8345 statement, and was not surrounded with parenthesis. */
8348 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
8349 tree else_block
, bool nested_if
)
8353 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8354 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
8356 tree inner_if
= then_block
;
8358 /* We know from the grammar productions that there is an IF nested
8359 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8360 it might not be exactly THEN_BLOCK, but should be the last
8361 non-container statement within. */
8363 switch (TREE_CODE (inner_if
))
8368 inner_if
= BIND_EXPR_BODY (inner_if
);
8370 case STATEMENT_LIST
:
8371 inner_if
= expr_last (then_block
);
8373 case TRY_FINALLY_EXPR
:
8374 case TRY_CATCH_EXPR
:
8375 inner_if
= TREE_OPERAND (inner_if
, 0);
8382 if (COND_EXPR_ELSE (inner_if
))
8383 warning_at (if_locus
, OPT_Wparentheses
,
8384 "suggest explicit braces to avoid ambiguous %<else%>");
8387 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
8388 SET_EXPR_LOCATION (stmt
, if_locus
);
8392 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8393 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8394 is false for DO loops. INCR is the FOR increment expression. BODY is
8395 the statement controlled by the loop. BLAB is the break label. CLAB is
8396 the continue label. Everything is allowed to be NULL. */
8399 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
8400 tree blab
, tree clab
, bool cond_is_first
)
8402 tree entry
= NULL
, exit
= NULL
, t
;
8404 /* If the condition is zero don't generate a loop construct. */
8405 if (cond
&& integer_zerop (cond
))
8409 t
= build_and_jump (&blab
);
8410 SET_EXPR_LOCATION (t
, start_locus
);
8416 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
8418 /* If we have an exit condition, then we build an IF with gotos either
8419 out of the loop, or to the top of it. If there's no exit condition,
8420 then we just build a jump back to the top. */
8421 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
8423 if (cond
&& !integer_nonzerop (cond
))
8425 /* Canonicalize the loop condition to the end. This means
8426 generating a branch to the loop condition. Reuse the
8427 continue label, if possible. */
8432 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
8433 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
8436 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
8437 SET_EXPR_LOCATION (t
, start_locus
);
8441 t
= build_and_jump (&blab
);
8443 exit
= fold_build3_loc (start_locus
,
8444 COND_EXPR
, void_type_node
, cond
, exit
, t
);
8446 exit
= fold_build3_loc (input_location
,
8447 COND_EXPR
, void_type_node
, cond
, exit
, t
);
8456 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
8464 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
8468 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
8471 tree label
= *label_p
;
8473 /* In switch statements break is sometimes stylistically used after
8474 a return statement. This can lead to spurious warnings about
8475 control reaching the end of a non-void function when it is
8476 inlined. Note that we are calling block_may_fallthru with
8477 language specific tree nodes; this works because
8478 block_may_fallthru returns true when given something it does not
8480 skip
= !block_may_fallthru (cur_stmt_list
);
8485 *label_p
= label
= create_artificial_label (loc
);
8487 else if (TREE_CODE (label
) == LABEL_DECL
)
8489 else switch (TREE_INT_CST_LOW (label
))
8493 error_at (loc
, "break statement not within loop or switch");
8495 error_at (loc
, "continue statement not within a loop");
8499 gcc_assert (is_break
);
8500 error_at (loc
, "break statement used with OpenMP for loop");
8511 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
8513 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
8516 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8519 emit_side_effect_warnings (location_t loc
, tree expr
)
8521 if (expr
== error_mark_node
)
8523 else if (!TREE_SIDE_EFFECTS (expr
))
8525 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
8526 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
8529 warn_if_unused_value (expr
, loc
);
8532 /* Process an expression as if it were a complete statement. Emit
8533 diagnostics, but do not call ADD_STMT. LOC is the location of the
8537 c_process_expr_stmt (location_t loc
, tree expr
)
8542 expr
= c_fully_fold (expr
, false, NULL
);
8544 if (warn_sequence_point
)
8545 verify_sequence_points (expr
);
8547 if (TREE_TYPE (expr
) != error_mark_node
8548 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
8549 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
8550 error_at (loc
, "expression statement has incomplete type");
8552 /* If we're not processing a statement expression, warn about unused values.
8553 Warnings for statement expressions will be emitted later, once we figure
8554 out which is the result. */
8555 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
8556 && warn_unused_value
)
8557 emit_side_effect_warnings (loc
, expr
);
8559 /* If the expression is not of a type to which we cannot assign a line
8560 number, wrap the thing in a no-op NOP_EXPR. */
8561 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
8563 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
8564 SET_EXPR_LOCATION (expr
, loc
);
8570 /* Emit an expression as a statement. LOC is the location of the
8574 c_finish_expr_stmt (location_t loc
, tree expr
)
8577 return add_stmt (c_process_expr_stmt (loc
, expr
));
8582 /* Do the opposite and emit a statement as an expression. To begin,
8583 create a new binding level and return it. */
8586 c_begin_stmt_expr (void)
8590 /* We must force a BLOCK for this level so that, if it is not expanded
8591 later, there is a way to turn off the entire subtree of blocks that
8592 are contained in it. */
8594 ret
= c_begin_compound_stmt (true);
8596 c_bindings_start_stmt_expr (c_switch_stack
== NULL
8598 : c_switch_stack
->bindings
);
8600 /* Mark the current statement list as belonging to a statement list. */
8601 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
8606 /* LOC is the location of the compound statement to which this body
8610 c_finish_stmt_expr (location_t loc
, tree body
)
8612 tree last
, type
, tmp
, val
;
8615 body
= c_end_compound_stmt (loc
, body
, true);
8617 c_bindings_end_stmt_expr (c_switch_stack
== NULL
8619 : c_switch_stack
->bindings
);
8621 /* Locate the last statement in BODY. See c_end_compound_stmt
8622 about always returning a BIND_EXPR. */
8623 last_p
= &BIND_EXPR_BODY (body
);
8624 last
= BIND_EXPR_BODY (body
);
8627 if (TREE_CODE (last
) == STATEMENT_LIST
)
8629 tree_stmt_iterator i
;
8631 /* This can happen with degenerate cases like ({ }). No value. */
8632 if (!TREE_SIDE_EFFECTS (last
))
8635 /* If we're supposed to generate side effects warnings, process
8636 all of the statements except the last. */
8637 if (warn_unused_value
)
8639 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
8642 tree t
= tsi_stmt (i
);
8644 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
8645 emit_side_effect_warnings (tloc
, t
);
8649 i
= tsi_last (last
);
8650 last_p
= tsi_stmt_ptr (i
);
8654 /* If the end of the list is exception related, then the list was split
8655 by a call to push_cleanup. Continue searching. */
8656 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
8657 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
8659 last_p
= &TREE_OPERAND (last
, 0);
8661 goto continue_searching
;
8664 /* In the case that the BIND_EXPR is not necessary, return the
8665 expression out from inside it. */
8666 if (last
== error_mark_node
8667 || (last
== BIND_EXPR_BODY (body
)
8668 && BIND_EXPR_VARS (body
) == NULL
))
8670 /* Even if this looks constant, do not allow it in a constant
8672 last
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (last
), NULL_TREE
, last
);
8673 C_MAYBE_CONST_EXPR_NON_CONST (last
) = 1;
8674 /* Do not warn if the return value of a statement expression is
8676 TREE_NO_WARNING (last
) = 1;
8680 /* Extract the type of said expression. */
8681 type
= TREE_TYPE (last
);
8683 /* If we're not returning a value at all, then the BIND_EXPR that
8684 we already have is a fine expression to return. */
8685 if (!type
|| VOID_TYPE_P (type
))
8688 /* Now that we've located the expression containing the value, it seems
8689 silly to make voidify_wrapper_expr repeat the process. Create a
8690 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8691 tmp
= create_tmp_var_raw (type
, NULL
);
8693 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8694 tree_expr_nonnegative_p giving up immediately. */
8696 if (TREE_CODE (val
) == NOP_EXPR
8697 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
8698 val
= TREE_OPERAND (val
, 0);
8700 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
8701 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
8704 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
8705 SET_EXPR_LOCATION (t
, loc
);
8710 /* Begin and end compound statements. This is as simple as pushing
8711 and popping new statement lists from the tree. */
8714 c_begin_compound_stmt (bool do_scope
)
8716 tree stmt
= push_stmt_list ();
8722 /* End a compound statement. STMT is the statement. LOC is the
8723 location of the compound statement-- this is usually the location
8724 of the opening brace. */
8727 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
8733 if (c_dialect_objc ())
8734 objc_clear_super_receiver ();
8735 block
= pop_scope ();
8738 stmt
= pop_stmt_list (stmt
);
8739 stmt
= c_build_bind_expr (loc
, block
, stmt
);
8741 /* If this compound statement is nested immediately inside a statement
8742 expression, then force a BIND_EXPR to be created. Otherwise we'll
8743 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8744 STATEMENT_LISTs merge, and thus we can lose track of what statement
8747 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
8748 && TREE_CODE (stmt
) != BIND_EXPR
)
8750 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
8751 TREE_SIDE_EFFECTS (stmt
) = 1;
8752 SET_EXPR_LOCATION (stmt
, loc
);
8758 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8759 when the current scope is exited. EH_ONLY is true when this is not
8760 meant to apply to normal control flow transfer. */
8763 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
8765 enum tree_code code
;
8769 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
8770 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
8772 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
8773 list
= push_stmt_list ();
8774 TREE_OPERAND (stmt
, 0) = list
;
8775 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
8778 /* Build a binary-operation expression without default conversions.
8779 CODE is the kind of expression to build.
8780 LOCATION is the operator's location.
8781 This function differs from `build' in several ways:
8782 the data type of the result is computed and recorded in it,
8783 warnings are generated if arg data types are invalid,
8784 special handling for addition and subtraction of pointers is known,
8785 and some optimization is done (operations on narrow ints
8786 are done in the narrower type when that gives the same result).
8787 Constant folding is also done before the result is returned.
8789 Note that the operands will never have enumeral types, or function
8790 or array types, because either they will have the default conversions
8791 performed or they have both just been converted to some other type in which
8792 the arithmetic is to be done. */
8795 build_binary_op (location_t location
, enum tree_code code
,
8796 tree orig_op0
, tree orig_op1
, int convert_p
)
8798 tree type0
, type1
, orig_type0
, orig_type1
;
8800 enum tree_code code0
, code1
;
8802 tree ret
= error_mark_node
;
8803 const char *invalid_op_diag
;
8804 bool op0_int_operands
, op1_int_operands
;
8805 bool int_const
, int_const_or_overflow
, int_operands
;
8807 /* Expression code to give to the expression when it is built.
8808 Normally this is CODE, which is what the caller asked for,
8809 but in some special cases we change it. */
8810 enum tree_code resultcode
= code
;
8812 /* Data type in which the computation is to be performed.
8813 In the simplest cases this is the common type of the arguments. */
8814 tree result_type
= NULL
;
8816 /* When the computation is in excess precision, the type of the
8817 final EXCESS_PRECISION_EXPR. */
8818 tree real_result_type
= NULL
;
8820 /* Nonzero means operands have already been type-converted
8821 in whatever way is necessary.
8822 Zero means they need to be converted to RESULT_TYPE. */
8825 /* Nonzero means create the expression with this type, rather than
8827 tree build_type
= 0;
8829 /* Nonzero means after finally constructing the expression
8830 convert it to this type. */
8831 tree final_type
= 0;
8833 /* Nonzero if this is an operation like MIN or MAX which can
8834 safely be computed in short if both args are promoted shorts.
8835 Also implies COMMON.
8836 -1 indicates a bitwise operation; this makes a difference
8837 in the exact conditions for when it is safe to do the operation
8838 in a narrower mode. */
8841 /* Nonzero if this is a comparison operation;
8842 if both args are promoted shorts, compare the original shorts.
8843 Also implies COMMON. */
8844 int short_compare
= 0;
8846 /* Nonzero if this is a right-shift operation, which can be computed on the
8847 original short and then promoted if the operand is a promoted short. */
8848 int short_shift
= 0;
8850 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8853 /* True means types are compatible as far as ObjC is concerned. */
8856 /* True means this is an arithmetic operation that may need excess
8858 bool may_need_excess_precision
;
8860 if (location
== UNKNOWN_LOCATION
)
8861 location
= input_location
;
8866 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
8867 if (op0_int_operands
)
8868 op0
= remove_c_maybe_const_expr (op0
);
8869 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
8870 if (op1_int_operands
)
8871 op1
= remove_c_maybe_const_expr (op1
);
8872 int_operands
= (op0_int_operands
&& op1_int_operands
);
8875 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
8876 && TREE_CODE (orig_op1
) == INTEGER_CST
);
8877 int_const
= (int_const_or_overflow
8878 && !TREE_OVERFLOW (orig_op0
)
8879 && !TREE_OVERFLOW (orig_op1
));
8882 int_const
= int_const_or_overflow
= false;
8886 op0
= default_conversion (op0
);
8887 op1
= default_conversion (op1
);
8890 orig_type0
= type0
= TREE_TYPE (op0
);
8891 orig_type1
= type1
= TREE_TYPE (op1
);
8893 /* The expression codes of the data types of the arguments tell us
8894 whether the arguments are integers, floating, pointers, etc. */
8895 code0
= TREE_CODE (type0
);
8896 code1
= TREE_CODE (type1
);
8898 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8899 STRIP_TYPE_NOPS (op0
);
8900 STRIP_TYPE_NOPS (op1
);
8902 /* If an error was already reported for one of the arguments,
8903 avoid reporting another error. */
8905 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
8906 return error_mark_node
;
8908 if ((invalid_op_diag
8909 = targetm
.invalid_binary_op (code
, type0
, type1
)))
8911 error_at (location
, invalid_op_diag
);
8912 return error_mark_node
;
8920 case TRUNC_DIV_EXPR
:
8922 case FLOOR_DIV_EXPR
:
8923 case ROUND_DIV_EXPR
:
8924 case EXACT_DIV_EXPR
:
8925 may_need_excess_precision
= true;
8928 may_need_excess_precision
= false;
8931 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
8933 op0
= TREE_OPERAND (op0
, 0);
8934 type0
= TREE_TYPE (op0
);
8936 else if (may_need_excess_precision
8937 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
8940 op0
= convert (eptype
, op0
);
8942 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
8944 op1
= TREE_OPERAND (op1
, 0);
8945 type1
= TREE_TYPE (op1
);
8947 else if (may_need_excess_precision
8948 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
8951 op1
= convert (eptype
, op1
);
8954 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
8959 /* Handle the pointer + int case. */
8960 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
8962 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
8963 goto return_build_binary_op
;
8965 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
8967 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
8968 goto return_build_binary_op
;
8975 /* Subtraction of two similar pointers.
8976 We must subtract them as integers, then divide by object size. */
8977 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
8978 && comp_target_types (location
, type0
, type1
))
8980 ret
= pointer_diff (location
, op0
, op1
);
8981 goto return_build_binary_op
;
8983 /* Handle pointer minus int. Just like pointer plus int. */
8984 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
8986 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
8987 goto return_build_binary_op
;
8997 case TRUNC_DIV_EXPR
:
8999 case FLOOR_DIV_EXPR
:
9000 case ROUND_DIV_EXPR
:
9001 case EXACT_DIV_EXPR
:
9002 warn_for_div_by_zero (location
, op1
);
9004 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9005 || code0
== FIXED_POINT_TYPE
9006 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
9007 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9008 || code1
== FIXED_POINT_TYPE
9009 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
9011 enum tree_code tcode0
= code0
, tcode1
= code1
;
9013 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
9014 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
9015 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
9016 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
9018 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
9019 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
9020 resultcode
= RDIV_EXPR
;
9022 /* Although it would be tempting to shorten always here, that
9023 loses on some targets, since the modulo instruction is
9024 undefined if the quotient can't be represented in the
9025 computation mode. We shorten only if unsigned or if
9026 dividing by something we know != -1. */
9027 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
9028 || (TREE_CODE (op1
) == INTEGER_CST
9029 && !integer_all_onesp (op1
)));
9037 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
9039 /* Allow vector types which are not floating point types. */
9040 else if (code0
== VECTOR_TYPE
9041 && code1
== VECTOR_TYPE
9042 && !VECTOR_FLOAT_TYPE_P (type0
)
9043 && !VECTOR_FLOAT_TYPE_P (type1
))
9047 case TRUNC_MOD_EXPR
:
9048 case FLOOR_MOD_EXPR
:
9049 warn_for_div_by_zero (location
, op1
);
9051 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
9052 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
9053 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
9055 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
9057 /* Although it would be tempting to shorten always here, that loses
9058 on some targets, since the modulo instruction is undefined if the
9059 quotient can't be represented in the computation mode. We shorten
9060 only if unsigned or if dividing by something we know != -1. */
9061 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
9062 || (TREE_CODE (op1
) == INTEGER_CST
9063 && !integer_all_onesp (op1
)));
9068 case TRUTH_ANDIF_EXPR
:
9069 case TRUTH_ORIF_EXPR
:
9070 case TRUTH_AND_EXPR
:
9072 case TRUTH_XOR_EXPR
:
9073 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
9074 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
9075 || code0
== FIXED_POINT_TYPE
)
9076 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
9077 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
9078 || code1
== FIXED_POINT_TYPE
))
9080 /* Result of these operations is always an int,
9081 but that does not mean the operands should be
9082 converted to ints! */
9083 result_type
= integer_type_node
;
9084 op0
= c_common_truthvalue_conversion (location
, op0
);
9085 op1
= c_common_truthvalue_conversion (location
, op1
);
9088 if (code
== TRUTH_ANDIF_EXPR
)
9090 int_const_or_overflow
= (int_operands
9091 && TREE_CODE (orig_op0
) == INTEGER_CST
9092 && (op0
== truthvalue_false_node
9093 || TREE_CODE (orig_op1
) == INTEGER_CST
));
9094 int_const
= (int_const_or_overflow
9095 && !TREE_OVERFLOW (orig_op0
)
9096 && (op0
== truthvalue_false_node
9097 || !TREE_OVERFLOW (orig_op1
)));
9099 else if (code
== TRUTH_ORIF_EXPR
)
9101 int_const_or_overflow
= (int_operands
9102 && TREE_CODE (orig_op0
) == INTEGER_CST
9103 && (op0
== truthvalue_true_node
9104 || TREE_CODE (orig_op1
) == INTEGER_CST
));
9105 int_const
= (int_const_or_overflow
9106 && !TREE_OVERFLOW (orig_op0
)
9107 && (op0
== truthvalue_true_node
9108 || !TREE_OVERFLOW (orig_op1
)));
9112 /* Shift operations: result has same type as first operand;
9113 always convert second operand to int.
9114 Also set SHORT_SHIFT if shifting rightward. */
9117 if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
9118 && code1
== INTEGER_TYPE
)
9120 if (TREE_CODE (op1
) == INTEGER_CST
)
9122 if (tree_int_cst_sgn (op1
) < 0)
9125 if (c_inhibit_evaluation_warnings
== 0)
9126 warning (0, "right shift count is negative");
9130 if (!integer_zerop (op1
))
9133 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
9136 if (c_inhibit_evaluation_warnings
== 0)
9137 warning (0, "right shift count >= width of type");
9142 /* Use the type of the value to be shifted. */
9143 result_type
= type0
;
9144 /* Convert the shift-count to an integer, regardless of size
9145 of value being shifted. */
9146 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
9147 op1
= convert (integer_type_node
, op1
);
9148 /* Avoid converting op1 to result_type later. */
9154 if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
9155 && code1
== INTEGER_TYPE
)
9157 if (TREE_CODE (op1
) == INTEGER_CST
)
9159 if (tree_int_cst_sgn (op1
) < 0)
9162 if (c_inhibit_evaluation_warnings
== 0)
9163 warning (0, "left shift count is negative");
9166 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
9169 if (c_inhibit_evaluation_warnings
== 0)
9170 warning (0, "left shift count >= width of type");
9174 /* Use the type of the value to be shifted. */
9175 result_type
= type0
;
9176 /* Convert the shift-count to an integer, regardless of size
9177 of value being shifted. */
9178 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
9179 op1
= convert (integer_type_node
, op1
);
9180 /* Avoid converting op1 to result_type later. */
9187 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
9188 warning_at (location
,
9190 "comparing floating point with == or != is unsafe");
9191 /* Result of comparison is always int,
9192 but don't convert the args to int! */
9193 build_type
= integer_type_node
;
9194 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9195 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
9196 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9197 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
9199 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
9201 tree tt0
= TREE_TYPE (type0
);
9202 tree tt1
= TREE_TYPE (type1
);
9203 /* Anything compares with void *. void * compares with anything.
9204 Otherwise, the targets must be compatible
9205 and both must be object or both incomplete. */
9206 if (comp_target_types (location
, type0
, type1
))
9207 result_type
= common_pointer_type (type0
, type1
);
9208 else if (VOID_TYPE_P (tt0
))
9210 /* op0 != orig_op0 detects the case of something
9211 whose value is 0 but which isn't a valid null ptr const. */
9212 if (pedantic
&& !null_pointer_constant_p (orig_op0
)
9213 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
9214 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9215 "comparison of %<void *%> with function pointer");
9217 else if (VOID_TYPE_P (tt1
))
9219 if (pedantic
&& !null_pointer_constant_p (orig_op1
)
9220 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
9221 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9222 "comparison of %<void *%> with function pointer");
9225 /* Avoid warning about the volatile ObjC EH puts on decls. */
9227 pedwarn (location
, 0,
9228 "comparison of distinct pointer types lacks a cast");
9230 if (result_type
== NULL_TREE
)
9231 result_type
= ptr_type_node
;
9233 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
9235 if (TREE_CODE (op0
) == ADDR_EXPR
9236 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
9237 warning_at (location
,
9238 OPT_Waddress
, "the address of %qD will never be NULL",
9239 TREE_OPERAND (op0
, 0));
9240 result_type
= type0
;
9242 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
9244 if (TREE_CODE (op1
) == ADDR_EXPR
9245 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
9246 warning_at (location
,
9247 OPT_Waddress
, "the address of %qD will never be NULL",
9248 TREE_OPERAND (op1
, 0));
9249 result_type
= type1
;
9251 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9253 result_type
= type0
;
9254 pedwarn (location
, 0, "comparison between pointer and integer");
9256 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
9258 result_type
= type1
;
9259 pedwarn (location
, 0, "comparison between pointer and integer");
9267 build_type
= integer_type_node
;
9268 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
9269 || code0
== FIXED_POINT_TYPE
)
9270 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
9271 || code1
== FIXED_POINT_TYPE
))
9273 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
9275 if (comp_target_types (location
, type0
, type1
))
9277 result_type
= common_pointer_type (type0
, type1
);
9278 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
9279 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
9280 pedwarn (location
, 0,
9281 "comparison of complete and incomplete pointers");
9282 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
9283 pedwarn (location
, OPT_pedantic
, "ISO C forbids "
9284 "ordered comparisons of pointers to functions");
9288 result_type
= ptr_type_node
;
9289 pedwarn (location
, 0,
9290 "comparison of distinct pointer types lacks a cast");
9293 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
9295 result_type
= type0
;
9297 pedwarn (location
, OPT_pedantic
,
9298 "ordered comparison of pointer with integer zero");
9299 else if (extra_warnings
)
9300 warning_at (location
, OPT_Wextra
,
9301 "ordered comparison of pointer with integer zero");
9303 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
9305 result_type
= type1
;
9306 pedwarn (location
, OPT_pedantic
,
9307 "ordered comparison of pointer with integer zero");
9309 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
9311 result_type
= type0
;
9312 pedwarn (location
, 0, "comparison between pointer and integer");
9314 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
9316 result_type
= type1
;
9317 pedwarn (location
, 0, "comparison between pointer and integer");
9325 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
9326 return error_mark_node
;
9328 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
9329 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
9330 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0
),
9331 TREE_TYPE (type1
))))
9333 binary_op_error (location
, code
, type0
, type1
);
9334 return error_mark_node
;
9337 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
9338 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
9340 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
9341 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
9343 bool first_complex
= (code0
== COMPLEX_TYPE
);
9344 bool second_complex
= (code1
== COMPLEX_TYPE
);
9345 int none_complex
= (!first_complex
&& !second_complex
);
9347 if (shorten
|| common
|| short_compare
)
9349 result_type
= c_common_type (type0
, type1
);
9350 if (result_type
== error_mark_node
)
9351 return error_mark_node
;
9354 if (first_complex
!= second_complex
9355 && (code
== PLUS_EXPR
9356 || code
== MINUS_EXPR
9357 || code
== MULT_EXPR
9358 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
9359 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
9360 && flag_signed_zeros
)
9362 /* An operation on mixed real/complex operands must be
9363 handled specially, but the language-independent code can
9364 more easily optimize the plain complex arithmetic if
9365 -fno-signed-zeros. */
9366 tree real_type
= TREE_TYPE (result_type
);
9368 if (type0
!= orig_type0
|| type1
!= orig_type1
)
9370 gcc_assert (may_need_excess_precision
&& common
);
9371 real_result_type
= c_common_type (orig_type0
, orig_type1
);
9375 if (TREE_TYPE (op0
) != result_type
)
9376 op0
= convert_and_check (result_type
, op0
);
9377 if (TREE_TYPE (op1
) != real_type
)
9378 op1
= convert_and_check (real_type
, op1
);
9382 if (TREE_TYPE (op0
) != real_type
)
9383 op0
= convert_and_check (real_type
, op0
);
9384 if (TREE_TYPE (op1
) != result_type
)
9385 op1
= convert_and_check (result_type
, op1
);
9387 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
9388 return error_mark_node
;
9391 op0
= c_save_expr (op0
);
9392 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
9394 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
9399 case TRUNC_DIV_EXPR
:
9400 imag
= build2 (resultcode
, real_type
, imag
, op1
);
9404 real
= build2 (resultcode
, real_type
, real
, op1
);
9412 op1
= c_save_expr (op1
);
9413 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
9415 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
9420 imag
= build2 (resultcode
, real_type
, op0
, imag
);
9423 real
= build2 (resultcode
, real_type
, op0
, real
);
9426 real
= build2 (resultcode
, real_type
, op0
, real
);
9427 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
9433 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
9434 goto return_build_binary_op
;
9437 /* For certain operations (which identify themselves by shorten != 0)
9438 if both args were extended from the same smaller type,
9439 do the arithmetic in that type and then extend.
9441 shorten !=0 and !=1 indicates a bitwise operation.
9442 For them, this optimization is safe only if
9443 both args are zero-extended or both are sign-extended.
9444 Otherwise, we might change the result.
9445 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9446 but calculated in (unsigned short) it would be (unsigned short)-1. */
9448 if (shorten
&& none_complex
)
9450 final_type
= result_type
;
9451 result_type
= shorten_binary_op (result_type
, op0
, op1
,
9455 /* Shifts can be shortened if shifting right. */
9460 tree arg0
= get_narrower (op0
, &unsigned_arg
);
9462 final_type
= result_type
;
9464 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
9465 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
9467 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
9468 && tree_int_cst_sgn (op1
) > 0
9469 /* We can shorten only if the shift count is less than the
9470 number of bits in the smaller type size. */
9471 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
9472 /* We cannot drop an unsigned shift after sign-extension. */
9473 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
9475 /* Do an unsigned shift if the operand was zero-extended. */
9477 = c_common_signed_or_unsigned_type (unsigned_arg
,
9479 /* Convert value-to-be-shifted to that type. */
9480 if (TREE_TYPE (op0
) != result_type
)
9481 op0
= convert (result_type
, op0
);
9486 /* Comparison operations are shortened too but differently.
9487 They identify themselves by setting short_compare = 1. */
9491 /* Don't write &op0, etc., because that would prevent op0
9492 from being kept in a register.
9493 Instead, make copies of the our local variables and
9494 pass the copies by reference, then copy them back afterward. */
9495 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
9496 enum tree_code xresultcode
= resultcode
;
9498 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
9503 goto return_build_binary_op
;
9506 op0
= xop0
, op1
= xop1
;
9508 resultcode
= xresultcode
;
9510 if (c_inhibit_evaluation_warnings
== 0)
9512 bool op0_maybe_const
= true;
9513 bool op1_maybe_const
= true;
9514 tree orig_op0_folded
, orig_op1_folded
;
9516 if (in_late_binary_op
)
9518 orig_op0_folded
= orig_op0
;
9519 orig_op1_folded
= orig_op1
;
9523 /* Fold for the sake of possible warnings, as in
9524 build_conditional_expr. This requires the
9525 "original" values to be folded, not just op0 and
9527 c_inhibit_evaluation_warnings
++;
9528 op0
= c_fully_fold (op0
, require_constant_value
,
9530 op1
= c_fully_fold (op1
, require_constant_value
,
9532 c_inhibit_evaluation_warnings
--;
9533 orig_op0_folded
= c_fully_fold (orig_op0
,
9534 require_constant_value
,
9536 orig_op1_folded
= c_fully_fold (orig_op1
,
9537 require_constant_value
,
9541 if (warn_sign_compare
)
9542 warn_for_sign_compare (location
, orig_op0_folded
,
9543 orig_op1_folded
, op0
, op1
,
9544 result_type
, resultcode
);
9545 if (!in_late_binary_op
)
9547 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
9549 op0
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (op0
),
9551 C_MAYBE_CONST_EXPR_NON_CONST (op0
) = !op0_maybe_const
;
9553 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
9555 op1
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (op1
),
9557 C_MAYBE_CONST_EXPR_NON_CONST (op1
) = !op1_maybe_const
;
9564 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9565 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9566 Then the expression will be built.
9567 It will be given type FINAL_TYPE if that is nonzero;
9568 otherwise, it will be given type RESULT_TYPE. */
9572 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
9573 return error_mark_node
;
9578 if (TREE_TYPE (op0
) != result_type
)
9579 op0
= convert_and_check (result_type
, op0
);
9580 if (TREE_TYPE (op1
) != result_type
)
9581 op1
= convert_and_check (result_type
, op1
);
9583 /* This can happen if one operand has a vector type, and the other
9584 has a different type. */
9585 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
9586 return error_mark_node
;
9589 if (build_type
== NULL_TREE
)
9591 build_type
= result_type
;
9592 if (type0
!= orig_type0
|| type1
!= orig_type1
)
9594 gcc_assert (may_need_excess_precision
&& common
);
9595 real_result_type
= c_common_type (orig_type0
, orig_type1
);
9599 /* Treat expressions in initializers specially as they can't trap. */
9600 if (int_const_or_overflow
)
9601 ret
= (require_constant_value
9602 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
9604 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
9606 ret
= build2 (resultcode
, build_type
, op0
, op1
);
9607 if (final_type
!= 0)
9608 ret
= convert (final_type
, ret
);
9610 return_build_binary_op
:
9611 gcc_assert (ret
!= error_mark_node
);
9612 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
9614 ? note_integer_operands (ret
)
9615 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
9616 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
9617 && !in_late_binary_op
)
9618 ret
= note_integer_operands (ret
);
9619 if (real_result_type
)
9620 ret
= build1 (EXCESS_PRECISION_EXPR
, real_result_type
, ret
);
9621 protected_set_expr_location (ret
, location
);
9626 /* Convert EXPR to be a truth-value, validating its type for this
9627 purpose. LOCATION is the source location for the expression. */
9630 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
9632 bool int_const
, int_operands
;
9634 switch (TREE_CODE (TREE_TYPE (expr
)))
9637 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
9638 return error_mark_node
;
9641 error_at (location
, "used struct type value where scalar is required");
9642 return error_mark_node
;
9645 error_at (location
, "used union type value where scalar is required");
9646 return error_mark_node
;
9655 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
9656 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
9658 expr
= remove_c_maybe_const_expr (expr
);
9660 /* ??? Should we also give an error for void and vectors rather than
9661 leaving those to give errors later? */
9662 expr
= c_common_truthvalue_conversion (location
, expr
);
9664 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
9666 if (TREE_OVERFLOW (expr
))
9669 return note_integer_operands (expr
);
9671 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
9672 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9677 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9681 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
9683 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
9685 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
9686 /* Executing a compound literal inside a function reinitializes
9688 if (!TREE_STATIC (decl
))
9696 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9699 c_begin_omp_parallel (void)
9704 block
= c_begin_compound_stmt (true);
9709 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
9710 statement. LOC is the location of the OMP_PARALLEL. */
9713 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
9717 block
= c_end_compound_stmt (loc
, block
, true);
9719 stmt
= make_node (OMP_PARALLEL
);
9720 TREE_TYPE (stmt
) = void_type_node
;
9721 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
9722 OMP_PARALLEL_BODY (stmt
) = block
;
9723 SET_EXPR_LOCATION (stmt
, loc
);
9725 return add_stmt (stmt
);
9728 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9731 c_begin_omp_task (void)
9736 block
= c_begin_compound_stmt (true);
9741 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
9742 statement. LOC is the location of the #pragma. */
9745 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
9749 block
= c_end_compound_stmt (loc
, block
, true);
9751 stmt
= make_node (OMP_TASK
);
9752 TREE_TYPE (stmt
) = void_type_node
;
9753 OMP_TASK_CLAUSES (stmt
) = clauses
;
9754 OMP_TASK_BODY (stmt
) = block
;
9755 SET_EXPR_LOCATION (stmt
, loc
);
9757 return add_stmt (stmt
);
9760 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9761 Remove any elements from the list that are invalid. */
9764 c_finish_omp_clauses (tree clauses
)
9766 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
9767 tree c
, t
, *pc
= &clauses
;
9770 bitmap_obstack_initialize (NULL
);
9771 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
9772 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
9773 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
9775 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
9777 bool remove
= false;
9778 bool need_complete
= false;
9779 bool need_implicitly_determined
= false;
9781 switch (OMP_CLAUSE_CODE (c
))
9783 case OMP_CLAUSE_SHARED
:
9785 need_implicitly_determined
= true;
9786 goto check_dup_generic
;
9788 case OMP_CLAUSE_PRIVATE
:
9790 need_complete
= true;
9791 need_implicitly_determined
= true;
9792 goto check_dup_generic
;
9794 case OMP_CLAUSE_REDUCTION
:
9796 need_implicitly_determined
= true;
9797 t
= OMP_CLAUSE_DECL (c
);
9798 if (AGGREGATE_TYPE_P (TREE_TYPE (t
))
9799 || POINTER_TYPE_P (TREE_TYPE (t
)))
9801 error_at (OMP_CLAUSE_LOCATION (c
),
9802 "%qE has invalid type for %<reduction%>", t
);
9805 else if (FLOAT_TYPE_P (TREE_TYPE (t
)))
9807 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
9808 const char *r_name
= NULL
;
9825 case TRUTH_ANDIF_EXPR
:
9828 case TRUTH_ORIF_EXPR
:
9836 error_at (OMP_CLAUSE_LOCATION (c
),
9837 "%qE has invalid type for %<reduction(%s)%>",
9842 goto check_dup_generic
;
9844 case OMP_CLAUSE_COPYPRIVATE
:
9845 name
= "copyprivate";
9846 goto check_dup_generic
;
9848 case OMP_CLAUSE_COPYIN
:
9850 t
= OMP_CLAUSE_DECL (c
);
9851 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
9853 error_at (OMP_CLAUSE_LOCATION (c
),
9854 "%qE must be %<threadprivate%> for %<copyin%>", t
);
9857 goto check_dup_generic
;
9860 t
= OMP_CLAUSE_DECL (c
);
9861 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
9863 error_at (OMP_CLAUSE_LOCATION (c
),
9864 "%qE is not a variable in clause %qs", t
, name
);
9867 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
9868 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
9869 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
9871 error_at (OMP_CLAUSE_LOCATION (c
),
9872 "%qE appears more than once in data clauses", t
);
9876 bitmap_set_bit (&generic_head
, DECL_UID (t
));
9879 case OMP_CLAUSE_FIRSTPRIVATE
:
9880 name
= "firstprivate";
9881 t
= OMP_CLAUSE_DECL (c
);
9882 need_complete
= true;
9883 need_implicitly_determined
= true;
9884 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
9886 error_at (OMP_CLAUSE_LOCATION (c
),
9887 "%qE is not a variable in clause %<firstprivate%>", t
);
9890 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
9891 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
9893 error_at (OMP_CLAUSE_LOCATION (c
),
9894 "%qE appears more than once in data clauses", t
);
9898 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
9901 case OMP_CLAUSE_LASTPRIVATE
:
9902 name
= "lastprivate";
9903 t
= OMP_CLAUSE_DECL (c
);
9904 need_complete
= true;
9905 need_implicitly_determined
= true;
9906 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
9908 error_at (OMP_CLAUSE_LOCATION (c
),
9909 "%qE is not a variable in clause %<lastprivate%>", t
);
9912 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
9913 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
9915 error_at (OMP_CLAUSE_LOCATION (c
),
9916 "%qE appears more than once in data clauses", t
);
9920 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
9924 case OMP_CLAUSE_NUM_THREADS
:
9925 case OMP_CLAUSE_SCHEDULE
:
9926 case OMP_CLAUSE_NOWAIT
:
9927 case OMP_CLAUSE_ORDERED
:
9928 case OMP_CLAUSE_DEFAULT
:
9929 case OMP_CLAUSE_UNTIED
:
9930 case OMP_CLAUSE_COLLAPSE
:
9931 pc
= &OMP_CLAUSE_CHAIN (c
);
9940 t
= OMP_CLAUSE_DECL (c
);
9944 t
= require_complete_type (t
);
9945 if (t
== error_mark_node
)
9949 if (need_implicitly_determined
)
9951 const char *share_name
= NULL
;
9953 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
9954 share_name
= "threadprivate";
9955 else switch (c_omp_predetermined_sharing (t
))
9957 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
9959 case OMP_CLAUSE_DEFAULT_SHARED
:
9960 share_name
= "shared";
9962 case OMP_CLAUSE_DEFAULT_PRIVATE
:
9963 share_name
= "private";
9970 error_at (OMP_CLAUSE_LOCATION (c
),
9971 "%qE is predetermined %qs for %qs",
9972 t
, share_name
, name
);
9979 *pc
= OMP_CLAUSE_CHAIN (c
);
9981 pc
= &OMP_CLAUSE_CHAIN (c
);
9984 bitmap_obstack_release (NULL
);
9988 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9989 down to the element type of an array. */
9992 c_build_qualified_type (tree type
, int type_quals
)
9994 if (type
== error_mark_node
)
9997 if (TREE_CODE (type
) == ARRAY_TYPE
)
10000 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
10003 /* See if we already have an identically qualified type. */
10004 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
10006 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
10007 && TYPE_NAME (t
) == TYPE_NAME (type
)
10008 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
10009 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
10010 TYPE_ATTRIBUTES (type
)))
10015 tree domain
= TYPE_DOMAIN (type
);
10017 t
= build_variant_type_copy (type
);
10018 TREE_TYPE (t
) = element_type
;
10020 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
10021 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
10022 SET_TYPE_STRUCTURAL_EQUALITY (t
);
10023 else if (TYPE_CANONICAL (element_type
) != element_type
10024 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
10026 tree unqualified_canon
10027 = build_array_type (TYPE_CANONICAL (element_type
),
10028 domain
? TYPE_CANONICAL (domain
)
10031 = c_build_qualified_type (unqualified_canon
, type_quals
);
10034 TYPE_CANONICAL (t
) = t
;
10039 /* A restrict-qualified pointer type must be a pointer to object or
10040 incomplete type. Note that the use of POINTER_TYPE_P also allows
10041 REFERENCE_TYPEs, which is appropriate for C++. */
10042 if ((type_quals
& TYPE_QUAL_RESTRICT
)
10043 && (!POINTER_TYPE_P (type
)
10044 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
10046 error ("invalid use of %<restrict%>");
10047 type_quals
&= ~TYPE_QUAL_RESTRICT
;
10050 return build_qualified_type (type
, type_quals
);
10053 /* Build a VA_ARG_EXPR for the C parser. */
10056 c_build_va_arg (location_t loc
, tree expr
, tree type
)
10058 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
10059 warning_at (loc
, OPT_Wc___compat
,
10060 "C++ requires promoted type, not enum type, in %<va_arg%>");
10061 return build_va_arg (loc
, expr
, type
);