gcc/c-family:
[official-gcc.git] / gcc / c / c-typeck.c
blobe52533ecd6d3b35054e0df9b0571d8d3e28f9c59
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "langhooks.h"
32 #include "c-tree.h"
33 #include "c-lang.h"
34 #include "flags.h"
35 #include "intl.h"
36 #include "target.h"
37 #include "tree-iterator.h"
38 #include "bitmap.h"
39 #include "gimple.h"
40 #include "c-family/c-objc.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-ubsan.h"
44 /* Possible cases of implicit bad conversions. Used to select
45 diagnostic messages in convert_for_assignment. */
46 enum impl_conv {
47 ic_argpass,
48 ic_assign,
49 ic_init,
50 ic_return
53 /* The level of nesting inside "__alignof__". */
54 int in_alignof;
56 /* The level of nesting inside "sizeof". */
57 int in_sizeof;
59 /* The level of nesting inside "typeof". */
60 int in_typeof;
62 /* The argument of last parsed sizeof expression, only to be tested
63 if expr.original_code == SIZEOF_EXPR. */
64 tree c_last_sizeof_arg;
66 /* Nonzero if we've already printed a "missing braces around initializer"
67 message within this initializer. */
68 static int missing_braces_mentioned;
70 static int require_constant_value;
71 static int require_constant_elements;
73 static bool null_pointer_constant_p (const_tree);
74 static tree qualify_type (tree, tree);
75 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
76 bool *);
77 static int comp_target_types (location_t, tree, tree);
78 static int function_types_compatible_p (const_tree, const_tree, bool *,
79 bool *);
80 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
81 static tree lookup_field (tree, tree);
82 static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
83 tree, tree);
84 static tree pointer_diff (location_t, tree, tree);
85 static tree convert_for_assignment (location_t, tree, tree, tree,
86 enum impl_conv, bool, tree, tree, int);
87 static tree valid_compound_expr_initializer (tree, tree);
88 static void push_string (const char *);
89 static void push_member_name (tree);
90 static int spelling_length (void);
91 static char *print_spelling (char *);
92 static void warning_init (int, const char *);
93 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
94 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
95 struct obstack *);
96 static void output_pending_init_elements (int, struct obstack *);
97 static int set_designator (int, struct obstack *);
98 static void push_range_stack (tree, struct obstack *);
99 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
100 static void set_nonincremental_init (struct obstack *);
101 static void set_nonincremental_init_from_string (tree, struct obstack *);
102 static tree find_init_member (tree, struct obstack *);
103 static void readonly_warning (tree, enum lvalue_use);
104 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
105 static void record_maybe_used_decl (tree);
106 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
108 /* Return true if EXP is a null pointer constant, false otherwise. */
110 static bool
111 null_pointer_constant_p (const_tree expr)
113 /* This should really operate on c_expr structures, but they aren't
114 yet available everywhere required. */
115 tree type = TREE_TYPE (expr);
116 return (TREE_CODE (expr) == INTEGER_CST
117 && !TREE_OVERFLOW (expr)
118 && integer_zerop (expr)
119 && (INTEGRAL_TYPE_P (type)
120 || (TREE_CODE (type) == POINTER_TYPE
121 && VOID_TYPE_P (TREE_TYPE (type))
122 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
125 /* EXPR may appear in an unevaluated part of an integer constant
126 expression, but not in an evaluated part. Wrap it in a
127 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
128 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
130 static tree
131 note_integer_operands (tree expr)
133 tree ret;
134 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
136 ret = copy_node (expr);
137 TREE_OVERFLOW (ret) = 1;
139 else
141 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
142 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
144 return ret;
147 /* Having checked whether EXPR may appear in an unevaluated part of an
148 integer constant expression and found that it may, remove any
149 C_MAYBE_CONST_EXPR noting this fact and return the resulting
150 expression. */
152 static inline tree
153 remove_c_maybe_const_expr (tree expr)
155 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
156 return C_MAYBE_CONST_EXPR_EXPR (expr);
157 else
158 return expr;
161 \f/* This is a cache to hold if two types are compatible or not. */
163 struct tagged_tu_seen_cache {
164 const struct tagged_tu_seen_cache * next;
165 const_tree t1;
166 const_tree t2;
167 /* The return value of tagged_types_tu_compatible_p if we had seen
168 these two types already. */
169 int val;
172 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
173 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
175 /* Do `exp = require_complete_type (exp);' to make sure exp
176 does not have an incomplete type. (That includes void types.) */
178 tree
179 require_complete_type (tree value)
181 tree type = TREE_TYPE (value);
183 if (value == error_mark_node || type == error_mark_node)
184 return error_mark_node;
186 /* First, detect a valid value with a complete type. */
187 if (COMPLETE_TYPE_P (type))
188 return value;
190 c_incomplete_type_error (value, type);
191 return error_mark_node;
194 /* Print an error message for invalid use of an incomplete type.
195 VALUE is the expression that was used (or 0 if that isn't known)
196 and TYPE is the type that was invalid. */
198 void
199 c_incomplete_type_error (const_tree value, const_tree type)
201 const char *type_code_string;
203 /* Avoid duplicate error message. */
204 if (TREE_CODE (type) == ERROR_MARK)
205 return;
207 if (value != 0 && (TREE_CODE (value) == VAR_DECL
208 || TREE_CODE (value) == PARM_DECL))
209 error ("%qD has an incomplete type", value);
210 else
212 retry:
213 /* We must print an error message. Be clever about what it says. */
215 switch (TREE_CODE (type))
217 case RECORD_TYPE:
218 type_code_string = "struct";
219 break;
221 case UNION_TYPE:
222 type_code_string = "union";
223 break;
225 case ENUMERAL_TYPE:
226 type_code_string = "enum";
227 break;
229 case VOID_TYPE:
230 error ("invalid use of void expression");
231 return;
233 case ARRAY_TYPE:
234 if (TYPE_DOMAIN (type))
236 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
238 error ("invalid use of flexible array member");
239 return;
241 type = TREE_TYPE (type);
242 goto retry;
244 error ("invalid use of array with unspecified bounds");
245 return;
247 default:
248 gcc_unreachable ();
251 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
252 error ("invalid use of undefined type %<%s %E%>",
253 type_code_string, TYPE_NAME (type));
254 else
255 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
256 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
260 /* Given a type, apply default promotions wrt unnamed function
261 arguments and return the new type. */
263 tree
264 c_type_promotes_to (tree type)
266 if (TYPE_MAIN_VARIANT (type) == float_type_node)
267 return double_type_node;
269 if (c_promoting_integer_type_p (type))
271 /* Preserve unsignedness if not really getting any wider. */
272 if (TYPE_UNSIGNED (type)
273 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
274 return unsigned_type_node;
275 return integer_type_node;
278 return type;
281 /* Return true if between two named address spaces, whether there is a superset
282 named address space that encompasses both address spaces. If there is a
283 superset, return which address space is the superset. */
285 static bool
286 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
288 if (as1 == as2)
290 *common = as1;
291 return true;
293 else if (targetm.addr_space.subset_p (as1, as2))
295 *common = as2;
296 return true;
298 else if (targetm.addr_space.subset_p (as2, as1))
300 *common = as1;
301 return true;
303 else
304 return false;
307 /* Return a variant of TYPE which has all the type qualifiers of LIKE
308 as well as those of TYPE. */
310 static tree
311 qualify_type (tree type, tree like)
313 addr_space_t as_type = TYPE_ADDR_SPACE (type);
314 addr_space_t as_like = TYPE_ADDR_SPACE (like);
315 addr_space_t as_common;
317 /* If the two named address spaces are different, determine the common
318 superset address space. If there isn't one, raise an error. */
319 if (!addr_space_superset (as_type, as_like, &as_common))
321 as_common = as_type;
322 error ("%qT and %qT are in disjoint named address spaces",
323 type, like);
326 return c_build_qualified_type (type,
327 TYPE_QUALS_NO_ADDR_SPACE (type)
328 | TYPE_QUALS_NO_ADDR_SPACE (like)
329 | ENCODE_QUAL_ADDR_SPACE (as_common));
332 /* Return true iff the given tree T is a variable length array. */
334 bool
335 c_vla_type_p (const_tree t)
337 if (TREE_CODE (t) == ARRAY_TYPE
338 && C_TYPE_VARIABLE_SIZE (t))
339 return true;
340 return false;
343 /* Return the composite type of two compatible types.
345 We assume that comptypes has already been done and returned
346 nonzero; if that isn't so, this may crash. In particular, we
347 assume that qualifiers match. */
349 tree
350 composite_type (tree t1, tree t2)
352 enum tree_code code1;
353 enum tree_code code2;
354 tree attributes;
356 /* Save time if the two types are the same. */
358 if (t1 == t2) return t1;
360 /* If one type is nonsense, use the other. */
361 if (t1 == error_mark_node)
362 return t2;
363 if (t2 == error_mark_node)
364 return t1;
366 code1 = TREE_CODE (t1);
367 code2 = TREE_CODE (t2);
369 /* Merge the attributes. */
370 attributes = targetm.merge_type_attributes (t1, t2);
372 /* If one is an enumerated type and the other is the compatible
373 integer type, the composite type might be either of the two
374 (DR#013 question 3). For consistency, use the enumerated type as
375 the composite type. */
377 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
378 return t1;
379 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
380 return t2;
382 gcc_assert (code1 == code2);
384 switch (code1)
386 case POINTER_TYPE:
387 /* For two pointers, do this recursively on the target type. */
389 tree pointed_to_1 = TREE_TYPE (t1);
390 tree pointed_to_2 = TREE_TYPE (t2);
391 tree target = composite_type (pointed_to_1, pointed_to_2);
392 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
393 t1 = build_type_attribute_variant (t1, attributes);
394 return qualify_type (t1, t2);
397 case ARRAY_TYPE:
399 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400 int quals;
401 tree unqual_elt;
402 tree d1 = TYPE_DOMAIN (t1);
403 tree d2 = TYPE_DOMAIN (t2);
404 bool d1_variable, d2_variable;
405 bool d1_zero, d2_zero;
406 bool t1_complete, t2_complete;
408 /* We should not have any type quals on arrays at all. */
409 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
410 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
412 t1_complete = COMPLETE_TYPE_P (t1);
413 t2_complete = COMPLETE_TYPE_P (t2);
415 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
416 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
418 d1_variable = (!d1_zero
419 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
420 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
421 d2_variable = (!d2_zero
422 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
423 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
424 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
425 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
427 /* Save space: see if the result is identical to one of the args. */
428 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
429 && (d2_variable || d2_zero || !d1_variable))
430 return build_type_attribute_variant (t1, attributes);
431 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
432 && (d1_variable || d1_zero || !d2_variable))
433 return build_type_attribute_variant (t2, attributes);
435 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
436 return build_type_attribute_variant (t1, attributes);
437 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
438 return build_type_attribute_variant (t2, attributes);
440 /* Merge the element types, and have a size if either arg has
441 one. We may have qualifiers on the element types. To set
442 up TYPE_MAIN_VARIANT correctly, we need to form the
443 composite of the unqualified types and add the qualifiers
444 back at the end. */
445 quals = TYPE_QUALS (strip_array_types (elt));
446 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
447 t1 = build_array_type (unqual_elt,
448 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
449 && (d2_variable
450 || d2_zero
451 || !d1_variable))
452 ? t1
453 : t2));
454 /* Ensure a composite type involving a zero-length array type
455 is a zero-length type not an incomplete type. */
456 if (d1_zero && d2_zero
457 && (t1_complete || t2_complete)
458 && !COMPLETE_TYPE_P (t1))
460 TYPE_SIZE (t1) = bitsize_zero_node;
461 TYPE_SIZE_UNIT (t1) = size_zero_node;
463 t1 = c_build_qualified_type (t1, quals);
464 return build_type_attribute_variant (t1, attributes);
467 case ENUMERAL_TYPE:
468 case RECORD_TYPE:
469 case UNION_TYPE:
470 if (attributes != NULL)
472 /* Try harder not to create a new aggregate type. */
473 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
474 return t1;
475 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
476 return t2;
478 return build_type_attribute_variant (t1, attributes);
480 case FUNCTION_TYPE:
481 /* Function types: prefer the one that specified arg types.
482 If both do, merge the arg types. Also merge the return types. */
484 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
485 tree p1 = TYPE_ARG_TYPES (t1);
486 tree p2 = TYPE_ARG_TYPES (t2);
487 int len;
488 tree newargs, n;
489 int i;
491 /* Save space: see if the result is identical to one of the args. */
492 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
493 return build_type_attribute_variant (t1, attributes);
494 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
495 return build_type_attribute_variant (t2, attributes);
497 /* Simple way if one arg fails to specify argument types. */
498 if (TYPE_ARG_TYPES (t1) == 0)
500 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
501 t1 = build_type_attribute_variant (t1, attributes);
502 return qualify_type (t1, t2);
504 if (TYPE_ARG_TYPES (t2) == 0)
506 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
507 t1 = build_type_attribute_variant (t1, attributes);
508 return qualify_type (t1, t2);
511 /* If both args specify argument types, we must merge the two
512 lists, argument by argument. */
514 len = list_length (p1);
515 newargs = 0;
517 for (i = 0; i < len; i++)
518 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
520 n = newargs;
522 for (; p1;
523 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
525 /* A null type means arg type is not specified.
526 Take whatever the other function type has. */
527 if (TREE_VALUE (p1) == 0)
529 TREE_VALUE (n) = TREE_VALUE (p2);
530 goto parm_done;
532 if (TREE_VALUE (p2) == 0)
534 TREE_VALUE (n) = TREE_VALUE (p1);
535 goto parm_done;
538 /* Given wait (union {union wait *u; int *i} *)
539 and wait (union wait *),
540 prefer union wait * as type of parm. */
541 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
542 && TREE_VALUE (p1) != TREE_VALUE (p2))
544 tree memb;
545 tree mv2 = TREE_VALUE (p2);
546 if (mv2 && mv2 != error_mark_node
547 && TREE_CODE (mv2) != ARRAY_TYPE)
548 mv2 = TYPE_MAIN_VARIANT (mv2);
549 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
550 memb; memb = DECL_CHAIN (memb))
552 tree mv3 = TREE_TYPE (memb);
553 if (mv3 && mv3 != error_mark_node
554 && TREE_CODE (mv3) != ARRAY_TYPE)
555 mv3 = TYPE_MAIN_VARIANT (mv3);
556 if (comptypes (mv3, mv2))
558 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
559 TREE_VALUE (p2));
560 pedwarn (input_location, OPT_Wpedantic,
561 "function types not truly compatible in ISO C");
562 goto parm_done;
566 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
567 && TREE_VALUE (p2) != TREE_VALUE (p1))
569 tree memb;
570 tree mv1 = TREE_VALUE (p1);
571 if (mv1 && mv1 != error_mark_node
572 && TREE_CODE (mv1) != ARRAY_TYPE)
573 mv1 = TYPE_MAIN_VARIANT (mv1);
574 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
575 memb; memb = DECL_CHAIN (memb))
577 tree mv3 = TREE_TYPE (memb);
578 if (mv3 && mv3 != error_mark_node
579 && TREE_CODE (mv3) != ARRAY_TYPE)
580 mv3 = TYPE_MAIN_VARIANT (mv3);
581 if (comptypes (mv3, mv1))
583 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
584 TREE_VALUE (p1));
585 pedwarn (input_location, OPT_Wpedantic,
586 "function types not truly compatible in ISO C");
587 goto parm_done;
591 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
592 parm_done: ;
595 t1 = build_function_type (valtype, newargs);
596 t1 = qualify_type (t1, t2);
597 /* ... falls through ... */
600 default:
601 return build_type_attribute_variant (t1, attributes);
606 /* Return the type of a conditional expression between pointers to
607 possibly differently qualified versions of compatible types.
609 We assume that comp_target_types has already been done and returned
610 nonzero; if that isn't so, this may crash. */
612 static tree
613 common_pointer_type (tree t1, tree t2)
615 tree attributes;
616 tree pointed_to_1, mv1;
617 tree pointed_to_2, mv2;
618 tree target;
619 unsigned target_quals;
620 addr_space_t as1, as2, as_common;
621 int quals1, quals2;
623 /* Save time if the two types are the same. */
625 if (t1 == t2) return t1;
627 /* If one type is nonsense, use the other. */
628 if (t1 == error_mark_node)
629 return t2;
630 if (t2 == error_mark_node)
631 return t1;
633 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
634 && TREE_CODE (t2) == POINTER_TYPE);
636 /* Merge the attributes. */
637 attributes = targetm.merge_type_attributes (t1, t2);
639 /* Find the composite type of the target types, and combine the
640 qualifiers of the two types' targets. Do not lose qualifiers on
641 array element types by taking the TYPE_MAIN_VARIANT. */
642 mv1 = pointed_to_1 = TREE_TYPE (t1);
643 mv2 = pointed_to_2 = TREE_TYPE (t2);
644 if (TREE_CODE (mv1) != ARRAY_TYPE)
645 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
646 if (TREE_CODE (mv2) != ARRAY_TYPE)
647 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
648 target = composite_type (mv1, mv2);
650 /* For function types do not merge const qualifiers, but drop them
651 if used inconsistently. The middle-end uses these to mark const
652 and noreturn functions. */
653 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
654 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
656 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
657 target_quals = (quals1 & quals2);
658 else
659 target_quals = (quals1 | quals2);
661 /* If the two named address spaces are different, determine the common
662 superset address space. This is guaranteed to exist due to the
663 assumption that comp_target_type returned non-zero. */
664 as1 = TYPE_ADDR_SPACE (pointed_to_1);
665 as2 = TYPE_ADDR_SPACE (pointed_to_2);
666 if (!addr_space_superset (as1, as2, &as_common))
667 gcc_unreachable ();
669 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
671 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
672 return build_type_attribute_variant (t1, attributes);
675 /* Return the common type for two arithmetic types under the usual
676 arithmetic conversions. The default conversions have already been
677 applied, and enumerated types converted to their compatible integer
678 types. The resulting type is unqualified and has no attributes.
680 This is the type for the result of most arithmetic operations
681 if the operands have the given two types. */
683 static tree
684 c_common_type (tree t1, tree t2)
686 enum tree_code code1;
687 enum tree_code code2;
689 /* If one type is nonsense, use the other. */
690 if (t1 == error_mark_node)
691 return t2;
692 if (t2 == error_mark_node)
693 return t1;
695 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
696 t1 = TYPE_MAIN_VARIANT (t1);
698 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
699 t2 = TYPE_MAIN_VARIANT (t2);
701 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
702 t1 = build_type_attribute_variant (t1, NULL_TREE);
704 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
705 t2 = build_type_attribute_variant (t2, NULL_TREE);
707 /* Save time if the two types are the same. */
709 if (t1 == t2) return t1;
711 code1 = TREE_CODE (t1);
712 code2 = TREE_CODE (t2);
714 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
715 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
716 || code1 == INTEGER_TYPE);
717 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
718 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
719 || code2 == INTEGER_TYPE);
721 /* When one operand is a decimal float type, the other operand cannot be
722 a generic float type or a complex type. We also disallow vector types
723 here. */
724 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
725 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
727 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
729 error ("can%'t mix operands of decimal float and vector types");
730 return error_mark_node;
732 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
734 error ("can%'t mix operands of decimal float and complex types");
735 return error_mark_node;
737 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
739 error ("can%'t mix operands of decimal float and other float types");
740 return error_mark_node;
744 /* If one type is a vector type, return that type. (How the usual
745 arithmetic conversions apply to the vector types extension is not
746 precisely specified.) */
747 if (code1 == VECTOR_TYPE)
748 return t1;
750 if (code2 == VECTOR_TYPE)
751 return t2;
753 /* If one type is complex, form the common type of the non-complex
754 components, then make that complex. Use T1 or T2 if it is the
755 required type. */
756 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
758 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
759 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
760 tree subtype = c_common_type (subtype1, subtype2);
762 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
763 return t1;
764 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
765 return t2;
766 else
767 return build_complex_type (subtype);
770 /* If only one is real, use it as the result. */
772 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
773 return t1;
775 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
776 return t2;
778 /* If both are real and either are decimal floating point types, use
779 the decimal floating point type with the greater precision. */
781 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
783 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
784 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
785 return dfloat128_type_node;
786 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
787 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
788 return dfloat64_type_node;
789 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
790 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
791 return dfloat32_type_node;
794 /* Deal with fixed-point types. */
795 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
797 unsigned int unsignedp = 0, satp = 0;
798 enum machine_mode m1, m2;
799 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
801 m1 = TYPE_MODE (t1);
802 m2 = TYPE_MODE (t2);
804 /* If one input type is saturating, the result type is saturating. */
805 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
806 satp = 1;
808 /* If both fixed-point types are unsigned, the result type is unsigned.
809 When mixing fixed-point and integer types, follow the sign of the
810 fixed-point type.
811 Otherwise, the result type is signed. */
812 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
813 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
814 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
815 && TYPE_UNSIGNED (t1))
816 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
817 && TYPE_UNSIGNED (t2)))
818 unsignedp = 1;
820 /* The result type is signed. */
821 if (unsignedp == 0)
823 /* If the input type is unsigned, we need to convert to the
824 signed type. */
825 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
827 enum mode_class mclass = (enum mode_class) 0;
828 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
829 mclass = MODE_FRACT;
830 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
831 mclass = MODE_ACCUM;
832 else
833 gcc_unreachable ();
834 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
836 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
838 enum mode_class mclass = (enum mode_class) 0;
839 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
840 mclass = MODE_FRACT;
841 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
842 mclass = MODE_ACCUM;
843 else
844 gcc_unreachable ();
845 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
849 if (code1 == FIXED_POINT_TYPE)
851 fbit1 = GET_MODE_FBIT (m1);
852 ibit1 = GET_MODE_IBIT (m1);
854 else
856 fbit1 = 0;
857 /* Signed integers need to subtract one sign bit. */
858 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
861 if (code2 == FIXED_POINT_TYPE)
863 fbit2 = GET_MODE_FBIT (m2);
864 ibit2 = GET_MODE_IBIT (m2);
866 else
868 fbit2 = 0;
869 /* Signed integers need to subtract one sign bit. */
870 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
873 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
874 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
875 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
876 satp);
879 /* Both real or both integers; use the one with greater precision. */
881 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
882 return t1;
883 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
884 return t2;
886 /* Same precision. Prefer long longs to longs to ints when the
887 same precision, following the C99 rules on integer type rank
888 (which are equivalent to the C90 rules for C90 types). */
890 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
891 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
892 return long_long_unsigned_type_node;
894 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
895 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
897 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
898 return long_long_unsigned_type_node;
899 else
900 return long_long_integer_type_node;
903 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
905 return long_unsigned_type_node;
907 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
910 /* But preserve unsignedness from the other type,
911 since long cannot hold all the values of an unsigned int. */
912 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
913 return long_unsigned_type_node;
914 else
915 return long_integer_type_node;
918 /* Likewise, prefer long double to double even if same size. */
919 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
920 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
921 return long_double_type_node;
923 /* Likewise, prefer double to float even if same size.
924 We got a couple of embedded targets with 32 bit doubles, and the
925 pdp11 might have 64 bit floats. */
926 if (TYPE_MAIN_VARIANT (t1) == double_type_node
927 || TYPE_MAIN_VARIANT (t2) == double_type_node)
928 return double_type_node;
930 /* Otherwise prefer the unsigned one. */
932 if (TYPE_UNSIGNED (t1))
933 return t1;
934 else
935 return t2;
938 /* Wrapper around c_common_type that is used by c-common.c and other
939 front end optimizations that remove promotions. ENUMERAL_TYPEs
940 are allowed here and are converted to their compatible integer types.
941 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
942 preferably a non-Boolean type as the common type. */
943 tree
944 common_type (tree t1, tree t2)
946 if (TREE_CODE (t1) == ENUMERAL_TYPE)
947 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
948 if (TREE_CODE (t2) == ENUMERAL_TYPE)
949 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
951 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
952 if (TREE_CODE (t1) == BOOLEAN_TYPE
953 && TREE_CODE (t2) == BOOLEAN_TYPE)
954 return boolean_type_node;
956 /* If either type is BOOLEAN_TYPE, then return the other. */
957 if (TREE_CODE (t1) == BOOLEAN_TYPE)
958 return t2;
959 if (TREE_CODE (t2) == BOOLEAN_TYPE)
960 return t1;
962 return c_common_type (t1, t2);
965 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
966 or various other operations. Return 2 if they are compatible
967 but a warning may be needed if you use them together. */
970 comptypes (tree type1, tree type2)
972 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
973 int val;
975 val = comptypes_internal (type1, type2, NULL, NULL);
976 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
978 return val;
981 /* Like comptypes, but if it returns non-zero because enum and int are
982 compatible, it sets *ENUM_AND_INT_P to true. */
984 static int
985 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
987 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
988 int val;
990 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
991 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
993 return val;
996 /* Like comptypes, but if it returns nonzero for different types, it
997 sets *DIFFERENT_TYPES_P to true. */
1000 comptypes_check_different_types (tree type1, tree type2,
1001 bool *different_types_p)
1003 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1004 int val;
1006 val = comptypes_internal (type1, type2, NULL, different_types_p);
1007 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1009 return val;
1012 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1013 or various other operations. Return 2 if they are compatible
1014 but a warning may be needed if you use them together. If
1015 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1016 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1017 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1018 NULL, and the types are compatible but different enough not to be
1019 permitted in C11 typedef redeclarations, then this sets
1020 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1021 false, but may or may not be set if the types are incompatible.
1022 This differs from comptypes, in that we don't free the seen
1023 types. */
1025 static int
1026 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1027 bool *different_types_p)
1029 const_tree t1 = type1;
1030 const_tree t2 = type2;
1031 int attrval, val;
1033 /* Suppress errors caused by previously reported errors. */
1035 if (t1 == t2 || !t1 || !t2
1036 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1037 return 1;
1039 /* Enumerated types are compatible with integer types, but this is
1040 not transitive: two enumerated types in the same translation unit
1041 are compatible with each other only if they are the same type. */
1043 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1045 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1046 if (TREE_CODE (t2) != VOID_TYPE)
1048 if (enum_and_int_p != NULL)
1049 *enum_and_int_p = true;
1050 if (different_types_p != NULL)
1051 *different_types_p = true;
1054 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1056 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1057 if (TREE_CODE (t1) != VOID_TYPE)
1059 if (enum_and_int_p != NULL)
1060 *enum_and_int_p = true;
1061 if (different_types_p != NULL)
1062 *different_types_p = true;
1066 if (t1 == t2)
1067 return 1;
1069 /* Different classes of types can't be compatible. */
1071 if (TREE_CODE (t1) != TREE_CODE (t2))
1072 return 0;
1074 /* Qualifiers must match. C99 6.7.3p9 */
1076 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1077 return 0;
1079 /* Allow for two different type nodes which have essentially the same
1080 definition. Note that we already checked for equality of the type
1081 qualifiers (just above). */
1083 if (TREE_CODE (t1) != ARRAY_TYPE
1084 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1085 return 1;
1087 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1088 if (!(attrval = comp_type_attributes (t1, t2)))
1089 return 0;
1091 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1092 val = 0;
1094 switch (TREE_CODE (t1))
1096 case POINTER_TYPE:
1097 /* Do not remove mode or aliasing information. */
1098 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1099 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1100 break;
1101 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1102 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1103 enum_and_int_p, different_types_p));
1104 break;
1106 case FUNCTION_TYPE:
1107 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1108 different_types_p);
1109 break;
1111 case ARRAY_TYPE:
1113 tree d1 = TYPE_DOMAIN (t1);
1114 tree d2 = TYPE_DOMAIN (t2);
1115 bool d1_variable, d2_variable;
1116 bool d1_zero, d2_zero;
1117 val = 1;
1119 /* Target types must match incl. qualifiers. */
1120 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1121 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1122 enum_and_int_p,
1123 different_types_p)))
1124 return 0;
1126 if (different_types_p != NULL
1127 && (d1 == 0) != (d2 == 0))
1128 *different_types_p = true;
1129 /* Sizes must match unless one is missing or variable. */
1130 if (d1 == 0 || d2 == 0 || d1 == d2)
1131 break;
1133 d1_zero = !TYPE_MAX_VALUE (d1);
1134 d2_zero = !TYPE_MAX_VALUE (d2);
1136 d1_variable = (!d1_zero
1137 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1138 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1139 d2_variable = (!d2_zero
1140 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1141 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1142 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1143 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1145 if (different_types_p != NULL
1146 && d1_variable != d2_variable)
1147 *different_types_p = true;
1148 if (d1_variable || d2_variable)
1149 break;
1150 if (d1_zero && d2_zero)
1151 break;
1152 if (d1_zero || d2_zero
1153 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1154 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1155 val = 0;
1157 break;
1160 case ENUMERAL_TYPE:
1161 case RECORD_TYPE:
1162 case UNION_TYPE:
1163 if (val != 1 && !same_translation_unit_p (t1, t2))
1165 tree a1 = TYPE_ATTRIBUTES (t1);
1166 tree a2 = TYPE_ATTRIBUTES (t2);
1168 if (! attribute_list_contained (a1, a2)
1169 && ! attribute_list_contained (a2, a1))
1170 break;
1172 if (attrval != 2)
1173 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1174 different_types_p);
1175 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1176 different_types_p);
1178 break;
1180 case VECTOR_TYPE:
1181 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1182 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1183 enum_and_int_p, different_types_p));
1184 break;
1186 default:
1187 break;
1189 return attrval == 2 && val == 1 ? 2 : val;
1192 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1193 their qualifiers, except for named address spaces. If the pointers point to
1194 different named addresses, then we must determine if one address space is a
1195 subset of the other. */
1197 static int
1198 comp_target_types (location_t location, tree ttl, tree ttr)
1200 int val;
1201 tree mvl = TREE_TYPE (ttl);
1202 tree mvr = TREE_TYPE (ttr);
1203 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1204 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1205 addr_space_t as_common;
1206 bool enum_and_int_p;
1208 /* Fail if pointers point to incompatible address spaces. */
1209 if (!addr_space_superset (asl, asr, &as_common))
1210 return 0;
1212 /* Do not lose qualifiers on element types of array types that are
1213 pointer targets by taking their TYPE_MAIN_VARIANT. */
1214 if (TREE_CODE (mvl) != ARRAY_TYPE)
1215 mvl = TYPE_MAIN_VARIANT (mvl);
1216 if (TREE_CODE (mvr) != ARRAY_TYPE)
1217 mvr = TYPE_MAIN_VARIANT (mvr);
1218 enum_and_int_p = false;
1219 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1221 if (val == 2)
1222 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1224 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1225 warning_at (location, OPT_Wc___compat,
1226 "pointer target types incompatible in C++");
1228 return val;
1231 /* Subroutines of `comptypes'. */
1233 /* Determine whether two trees derive from the same translation unit.
1234 If the CONTEXT chain ends in a null, that tree's context is still
1235 being parsed, so if two trees have context chains ending in null,
1236 they're in the same translation unit. */
1238 same_translation_unit_p (const_tree t1, const_tree t2)
1240 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1241 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1243 case tcc_declaration:
1244 t1 = DECL_CONTEXT (t1); break;
1245 case tcc_type:
1246 t1 = TYPE_CONTEXT (t1); break;
1247 case tcc_exceptional:
1248 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1249 default: gcc_unreachable ();
1252 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1253 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1255 case tcc_declaration:
1256 t2 = DECL_CONTEXT (t2); break;
1257 case tcc_type:
1258 t2 = TYPE_CONTEXT (t2); break;
1259 case tcc_exceptional:
1260 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1261 default: gcc_unreachable ();
1264 return t1 == t2;
1267 /* Allocate the seen two types, assuming that they are compatible. */
1269 static struct tagged_tu_seen_cache *
1270 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1272 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1273 tu->next = tagged_tu_seen_base;
1274 tu->t1 = t1;
1275 tu->t2 = t2;
1277 tagged_tu_seen_base = tu;
1279 /* The C standard says that two structures in different translation
1280 units are compatible with each other only if the types of their
1281 fields are compatible (among other things). We assume that they
1282 are compatible until proven otherwise when building the cache.
1283 An example where this can occur is:
1284 struct a
1286 struct a *next;
1288 If we are comparing this against a similar struct in another TU,
1289 and did not assume they were compatible, we end up with an infinite
1290 loop. */
1291 tu->val = 1;
1292 return tu;
1295 /* Free the seen types until we get to TU_TIL. */
1297 static void
1298 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1300 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1301 while (tu != tu_til)
1303 const struct tagged_tu_seen_cache *const tu1
1304 = (const struct tagged_tu_seen_cache *) tu;
1305 tu = tu1->next;
1306 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1308 tagged_tu_seen_base = tu_til;
1311 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1312 compatible. If the two types are not the same (which has been
1313 checked earlier), this can only happen when multiple translation
1314 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1315 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1316 comptypes_internal. */
1318 static int
1319 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1320 bool *enum_and_int_p, bool *different_types_p)
1322 tree s1, s2;
1323 bool needs_warning = false;
1325 /* We have to verify that the tags of the types are the same. This
1326 is harder than it looks because this may be a typedef, so we have
1327 to go look at the original type. It may even be a typedef of a
1328 typedef...
1329 In the case of compiler-created builtin structs the TYPE_DECL
1330 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1331 while (TYPE_NAME (t1)
1332 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1333 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1334 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1336 while (TYPE_NAME (t2)
1337 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1338 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1339 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1341 /* C90 didn't have the requirement that the two tags be the same. */
1342 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1343 return 0;
1345 /* C90 didn't say what happened if one or both of the types were
1346 incomplete; we choose to follow C99 rules here, which is that they
1347 are compatible. */
1348 if (TYPE_SIZE (t1) == NULL
1349 || TYPE_SIZE (t2) == NULL)
1350 return 1;
1353 const struct tagged_tu_seen_cache * tts_i;
1354 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1355 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1356 return tts_i->val;
1359 switch (TREE_CODE (t1))
1361 case ENUMERAL_TYPE:
1363 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1364 /* Speed up the case where the type values are in the same order. */
1365 tree tv1 = TYPE_VALUES (t1);
1366 tree tv2 = TYPE_VALUES (t2);
1368 if (tv1 == tv2)
1370 return 1;
1373 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1375 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1376 break;
1377 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1379 tu->val = 0;
1380 return 0;
1384 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1386 return 1;
1388 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1390 tu->val = 0;
1391 return 0;
1394 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1396 tu->val = 0;
1397 return 0;
1400 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1402 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1403 if (s2 == NULL
1404 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1406 tu->val = 0;
1407 return 0;
1410 return 1;
1413 case UNION_TYPE:
1415 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1416 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1418 tu->val = 0;
1419 return 0;
1422 /* Speed up the common case where the fields are in the same order. */
1423 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1424 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1426 int result;
1428 if (DECL_NAME (s1) != DECL_NAME (s2))
1429 break;
1430 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1431 enum_and_int_p, different_types_p);
1433 if (result != 1 && !DECL_NAME (s1))
1434 break;
1435 if (result == 0)
1437 tu->val = 0;
1438 return 0;
1440 if (result == 2)
1441 needs_warning = true;
1443 if (TREE_CODE (s1) == FIELD_DECL
1444 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1445 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1447 tu->val = 0;
1448 return 0;
1451 if (!s1 && !s2)
1453 tu->val = needs_warning ? 2 : 1;
1454 return tu->val;
1457 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1459 bool ok = false;
1461 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1462 if (DECL_NAME (s1) == DECL_NAME (s2))
1464 int result;
1466 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1467 enum_and_int_p,
1468 different_types_p);
1470 if (result != 1 && !DECL_NAME (s1))
1471 continue;
1472 if (result == 0)
1474 tu->val = 0;
1475 return 0;
1477 if (result == 2)
1478 needs_warning = true;
1480 if (TREE_CODE (s1) == FIELD_DECL
1481 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1482 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1483 break;
1485 ok = true;
1486 break;
1488 if (!ok)
1490 tu->val = 0;
1491 return 0;
1494 tu->val = needs_warning ? 2 : 10;
1495 return tu->val;
1498 case RECORD_TYPE:
1500 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1502 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1503 s1 && s2;
1504 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1506 int result;
1507 if (TREE_CODE (s1) != TREE_CODE (s2)
1508 || DECL_NAME (s1) != DECL_NAME (s2))
1509 break;
1510 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1511 enum_and_int_p, different_types_p);
1512 if (result == 0)
1513 break;
1514 if (result == 2)
1515 needs_warning = true;
1517 if (TREE_CODE (s1) == FIELD_DECL
1518 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1519 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1520 break;
1522 if (s1 && s2)
1523 tu->val = 0;
1524 else
1525 tu->val = needs_warning ? 2 : 1;
1526 return tu->val;
1529 default:
1530 gcc_unreachable ();
1534 /* Return 1 if two function types F1 and F2 are compatible.
1535 If either type specifies no argument types,
1536 the other must specify a fixed number of self-promoting arg types.
1537 Otherwise, if one type specifies only the number of arguments,
1538 the other must specify that number of self-promoting arg types.
1539 Otherwise, the argument types must match.
1540 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1542 static int
1543 function_types_compatible_p (const_tree f1, const_tree f2,
1544 bool *enum_and_int_p, bool *different_types_p)
1546 tree args1, args2;
1547 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1548 int val = 1;
1549 int val1;
1550 tree ret1, ret2;
1552 ret1 = TREE_TYPE (f1);
1553 ret2 = TREE_TYPE (f2);
1555 /* 'volatile' qualifiers on a function's return type used to mean
1556 the function is noreturn. */
1557 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1558 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1559 if (TYPE_VOLATILE (ret1))
1560 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1561 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1562 if (TYPE_VOLATILE (ret2))
1563 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1564 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1565 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1566 if (val == 0)
1567 return 0;
1569 args1 = TYPE_ARG_TYPES (f1);
1570 args2 = TYPE_ARG_TYPES (f2);
1572 if (different_types_p != NULL
1573 && (args1 == 0) != (args2 == 0))
1574 *different_types_p = true;
1576 /* An unspecified parmlist matches any specified parmlist
1577 whose argument types don't need default promotions. */
1579 if (args1 == 0)
1581 if (!self_promoting_args_p (args2))
1582 return 0;
1583 /* If one of these types comes from a non-prototype fn definition,
1584 compare that with the other type's arglist.
1585 If they don't match, ask for a warning (but no error). */
1586 if (TYPE_ACTUAL_ARG_TYPES (f1)
1587 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1588 enum_and_int_p, different_types_p))
1589 val = 2;
1590 return val;
1592 if (args2 == 0)
1594 if (!self_promoting_args_p (args1))
1595 return 0;
1596 if (TYPE_ACTUAL_ARG_TYPES (f2)
1597 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1598 enum_and_int_p, different_types_p))
1599 val = 2;
1600 return val;
1603 /* Both types have argument lists: compare them and propagate results. */
1604 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1605 different_types_p);
1606 return val1 != 1 ? val1 : val;
1609 /* Check two lists of types for compatibility, returning 0 for
1610 incompatible, 1 for compatible, or 2 for compatible with
1611 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1612 comptypes_internal. */
1614 static int
1615 type_lists_compatible_p (const_tree args1, const_tree args2,
1616 bool *enum_and_int_p, bool *different_types_p)
1618 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1619 int val = 1;
1620 int newval = 0;
1622 while (1)
1624 tree a1, mv1, a2, mv2;
1625 if (args1 == 0 && args2 == 0)
1626 return val;
1627 /* If one list is shorter than the other,
1628 they fail to match. */
1629 if (args1 == 0 || args2 == 0)
1630 return 0;
1631 mv1 = a1 = TREE_VALUE (args1);
1632 mv2 = a2 = TREE_VALUE (args2);
1633 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1634 mv1 = TYPE_MAIN_VARIANT (mv1);
1635 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1636 mv2 = TYPE_MAIN_VARIANT (mv2);
1637 /* A null pointer instead of a type
1638 means there is supposed to be an argument
1639 but nothing is specified about what type it has.
1640 So match anything that self-promotes. */
1641 if (different_types_p != NULL
1642 && (a1 == 0) != (a2 == 0))
1643 *different_types_p = true;
1644 if (a1 == 0)
1646 if (c_type_promotes_to (a2) != a2)
1647 return 0;
1649 else if (a2 == 0)
1651 if (c_type_promotes_to (a1) != a1)
1652 return 0;
1654 /* If one of the lists has an error marker, ignore this arg. */
1655 else if (TREE_CODE (a1) == ERROR_MARK
1656 || TREE_CODE (a2) == ERROR_MARK)
1658 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1659 different_types_p)))
1661 if (different_types_p != NULL)
1662 *different_types_p = true;
1663 /* Allow wait (union {union wait *u; int *i} *)
1664 and wait (union wait *) to be compatible. */
1665 if (TREE_CODE (a1) == UNION_TYPE
1666 && (TYPE_NAME (a1) == 0
1667 || TYPE_TRANSPARENT_AGGR (a1))
1668 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1669 && tree_int_cst_equal (TYPE_SIZE (a1),
1670 TYPE_SIZE (a2)))
1672 tree memb;
1673 for (memb = TYPE_FIELDS (a1);
1674 memb; memb = DECL_CHAIN (memb))
1676 tree mv3 = TREE_TYPE (memb);
1677 if (mv3 && mv3 != error_mark_node
1678 && TREE_CODE (mv3) != ARRAY_TYPE)
1679 mv3 = TYPE_MAIN_VARIANT (mv3);
1680 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1681 different_types_p))
1682 break;
1684 if (memb == 0)
1685 return 0;
1687 else if (TREE_CODE (a2) == UNION_TYPE
1688 && (TYPE_NAME (a2) == 0
1689 || TYPE_TRANSPARENT_AGGR (a2))
1690 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1691 && tree_int_cst_equal (TYPE_SIZE (a2),
1692 TYPE_SIZE (a1)))
1694 tree memb;
1695 for (memb = TYPE_FIELDS (a2);
1696 memb; memb = DECL_CHAIN (memb))
1698 tree mv3 = TREE_TYPE (memb);
1699 if (mv3 && mv3 != error_mark_node
1700 && TREE_CODE (mv3) != ARRAY_TYPE)
1701 mv3 = TYPE_MAIN_VARIANT (mv3);
1702 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1703 different_types_p))
1704 break;
1706 if (memb == 0)
1707 return 0;
1709 else
1710 return 0;
1713 /* comptypes said ok, but record if it said to warn. */
1714 if (newval > val)
1715 val = newval;
1717 args1 = TREE_CHAIN (args1);
1718 args2 = TREE_CHAIN (args2);
1722 /* Compute the size to increment a pointer by. */
1724 static tree
1725 c_size_in_bytes (const_tree type)
1727 enum tree_code code = TREE_CODE (type);
1729 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1730 return size_one_node;
1732 if (!COMPLETE_OR_VOID_TYPE_P (type))
1734 error ("arithmetic on pointer to an incomplete type");
1735 return size_one_node;
1738 /* Convert in case a char is more than one unit. */
1739 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1740 size_int (TYPE_PRECISION (char_type_node)
1741 / BITS_PER_UNIT));
1744 /* Return either DECL or its known constant value (if it has one). */
1746 tree
1747 decl_constant_value (tree decl)
1749 if (/* Don't change a variable array bound or initial value to a constant
1750 in a place where a variable is invalid. Note that DECL_INITIAL
1751 isn't valid for a PARM_DECL. */
1752 current_function_decl != 0
1753 && TREE_CODE (decl) != PARM_DECL
1754 && !TREE_THIS_VOLATILE (decl)
1755 && TREE_READONLY (decl)
1756 && DECL_INITIAL (decl) != 0
1757 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1758 /* This is invalid if initial value is not constant.
1759 If it has either a function call, a memory reference,
1760 or a variable, then re-evaluating it could give different results. */
1761 && TREE_CONSTANT (DECL_INITIAL (decl))
1762 /* Check for cases where this is sub-optimal, even though valid. */
1763 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1764 return DECL_INITIAL (decl);
1765 return decl;
1768 /* Convert the array expression EXP to a pointer. */
1769 static tree
1770 array_to_pointer_conversion (location_t loc, tree exp)
1772 tree orig_exp = exp;
1773 tree type = TREE_TYPE (exp);
1774 tree adr;
1775 tree restype = TREE_TYPE (type);
1776 tree ptrtype;
1778 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1780 STRIP_TYPE_NOPS (exp);
1782 if (TREE_NO_WARNING (orig_exp))
1783 TREE_NO_WARNING (exp) = 1;
1785 ptrtype = build_pointer_type (restype);
1787 if (TREE_CODE (exp) == INDIRECT_REF)
1788 return convert (ptrtype, TREE_OPERAND (exp, 0));
1790 /* In C++ array compound literals are temporary objects unless they are
1791 const or appear in namespace scope, so they are destroyed too soon
1792 to use them for much of anything (c++/53220). */
1793 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1795 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1796 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1797 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1798 "converting an array compound literal to a pointer "
1799 "is ill-formed in C++");
1802 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1803 return convert (ptrtype, adr);
1806 /* Convert the function expression EXP to a pointer. */
1807 static tree
1808 function_to_pointer_conversion (location_t loc, tree exp)
1810 tree orig_exp = exp;
1812 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1814 STRIP_TYPE_NOPS (exp);
1816 if (TREE_NO_WARNING (orig_exp))
1817 TREE_NO_WARNING (exp) = 1;
1819 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1822 /* Mark EXP as read, not just set, for set but not used -Wunused
1823 warning purposes. */
1825 void
1826 mark_exp_read (tree exp)
1828 switch (TREE_CODE (exp))
1830 case VAR_DECL:
1831 case PARM_DECL:
1832 DECL_READ_P (exp) = 1;
1833 break;
1834 case ARRAY_REF:
1835 case COMPONENT_REF:
1836 case MODIFY_EXPR:
1837 case REALPART_EXPR:
1838 case IMAGPART_EXPR:
1839 CASE_CONVERT:
1840 case ADDR_EXPR:
1841 mark_exp_read (TREE_OPERAND (exp, 0));
1842 break;
1843 case COMPOUND_EXPR:
1844 case C_MAYBE_CONST_EXPR:
1845 mark_exp_read (TREE_OPERAND (exp, 1));
1846 break;
1847 default:
1848 break;
1852 /* Perform the default conversion of arrays and functions to pointers.
1853 Return the result of converting EXP. For any other expression, just
1854 return EXP.
1856 LOC is the location of the expression. */
1858 struct c_expr
1859 default_function_array_conversion (location_t loc, struct c_expr exp)
1861 tree orig_exp = exp.value;
1862 tree type = TREE_TYPE (exp.value);
1863 enum tree_code code = TREE_CODE (type);
1865 switch (code)
1867 case ARRAY_TYPE:
1869 bool not_lvalue = false;
1870 bool lvalue_array_p;
1872 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1873 || CONVERT_EXPR_P (exp.value))
1874 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1876 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1877 not_lvalue = true;
1878 exp.value = TREE_OPERAND (exp.value, 0);
1881 if (TREE_NO_WARNING (orig_exp))
1882 TREE_NO_WARNING (exp.value) = 1;
1884 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1885 if (!flag_isoc99 && !lvalue_array_p)
1887 /* Before C99, non-lvalue arrays do not decay to pointers.
1888 Normally, using such an array would be invalid; but it can
1889 be used correctly inside sizeof or as a statement expression.
1890 Thus, do not give an error here; an error will result later. */
1891 return exp;
1894 exp.value = array_to_pointer_conversion (loc, exp.value);
1896 break;
1897 case FUNCTION_TYPE:
1898 exp.value = function_to_pointer_conversion (loc, exp.value);
1899 break;
1900 default:
1901 break;
1904 return exp;
1907 struct c_expr
1908 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1910 mark_exp_read (exp.value);
1911 return default_function_array_conversion (loc, exp);
1914 /* EXP is an expression of integer type. Apply the integer promotions
1915 to it and return the promoted value. */
1917 tree
1918 perform_integral_promotions (tree exp)
1920 tree type = TREE_TYPE (exp);
1921 enum tree_code code = TREE_CODE (type);
1923 gcc_assert (INTEGRAL_TYPE_P (type));
1925 /* Normally convert enums to int,
1926 but convert wide enums to something wider. */
1927 if (code == ENUMERAL_TYPE)
1929 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1930 TYPE_PRECISION (integer_type_node)),
1931 ((TYPE_PRECISION (type)
1932 >= TYPE_PRECISION (integer_type_node))
1933 && TYPE_UNSIGNED (type)));
1935 return convert (type, exp);
1938 /* ??? This should no longer be needed now bit-fields have their
1939 proper types. */
1940 if (TREE_CODE (exp) == COMPONENT_REF
1941 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1942 /* If it's thinner than an int, promote it like a
1943 c_promoting_integer_type_p, otherwise leave it alone. */
1944 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1945 TYPE_PRECISION (integer_type_node)))
1946 return convert (integer_type_node, exp);
1948 if (c_promoting_integer_type_p (type))
1950 /* Preserve unsignedness if not really getting any wider. */
1951 if (TYPE_UNSIGNED (type)
1952 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1953 return convert (unsigned_type_node, exp);
1955 return convert (integer_type_node, exp);
1958 return exp;
1962 /* Perform default promotions for C data used in expressions.
1963 Enumeral types or short or char are converted to int.
1964 In addition, manifest constants symbols are replaced by their values. */
1966 tree
1967 default_conversion (tree exp)
1969 tree orig_exp;
1970 tree type = TREE_TYPE (exp);
1971 enum tree_code code = TREE_CODE (type);
1972 tree promoted_type;
1974 mark_exp_read (exp);
1976 /* Functions and arrays have been converted during parsing. */
1977 gcc_assert (code != FUNCTION_TYPE);
1978 if (code == ARRAY_TYPE)
1979 return exp;
1981 /* Constants can be used directly unless they're not loadable. */
1982 if (TREE_CODE (exp) == CONST_DECL)
1983 exp = DECL_INITIAL (exp);
1985 /* Strip no-op conversions. */
1986 orig_exp = exp;
1987 STRIP_TYPE_NOPS (exp);
1989 if (TREE_NO_WARNING (orig_exp))
1990 TREE_NO_WARNING (exp) = 1;
1992 if (code == VOID_TYPE)
1994 error ("void value not ignored as it ought to be");
1995 return error_mark_node;
1998 exp = require_complete_type (exp);
1999 if (exp == error_mark_node)
2000 return error_mark_node;
2002 promoted_type = targetm.promoted_type (type);
2003 if (promoted_type)
2004 return convert (promoted_type, exp);
2006 if (INTEGRAL_TYPE_P (type))
2007 return perform_integral_promotions (exp);
2009 return exp;
2012 /* Look up COMPONENT in a structure or union TYPE.
2014 If the component name is not found, returns NULL_TREE. Otherwise,
2015 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2016 stepping down the chain to the component, which is in the last
2017 TREE_VALUE of the list. Normally the list is of length one, but if
2018 the component is embedded within (nested) anonymous structures or
2019 unions, the list steps down the chain to the component. */
2021 static tree
2022 lookup_field (tree type, tree component)
2024 tree field;
2026 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2027 to the field elements. Use a binary search on this array to quickly
2028 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2029 will always be set for structures which have many elements. */
2031 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2033 int bot, top, half;
2034 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2036 field = TYPE_FIELDS (type);
2037 bot = 0;
2038 top = TYPE_LANG_SPECIFIC (type)->s->len;
2039 while (top - bot > 1)
2041 half = (top - bot + 1) >> 1;
2042 field = field_array[bot+half];
2044 if (DECL_NAME (field) == NULL_TREE)
2046 /* Step through all anon unions in linear fashion. */
2047 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2049 field = field_array[bot++];
2050 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2051 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2053 tree anon = lookup_field (TREE_TYPE (field), component);
2055 if (anon)
2056 return tree_cons (NULL_TREE, field, anon);
2058 /* The Plan 9 compiler permits referring
2059 directly to an anonymous struct/union field
2060 using a typedef name. */
2061 if (flag_plan9_extensions
2062 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2063 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2064 == TYPE_DECL)
2065 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2066 == component))
2067 break;
2071 /* Entire record is only anon unions. */
2072 if (bot > top)
2073 return NULL_TREE;
2075 /* Restart the binary search, with new lower bound. */
2076 continue;
2079 if (DECL_NAME (field) == component)
2080 break;
2081 if (DECL_NAME (field) < component)
2082 bot += half;
2083 else
2084 top = bot + half;
2087 if (DECL_NAME (field_array[bot]) == component)
2088 field = field_array[bot];
2089 else if (DECL_NAME (field) != component)
2090 return NULL_TREE;
2092 else
2094 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2096 if (DECL_NAME (field) == NULL_TREE
2097 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2098 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2100 tree anon = lookup_field (TREE_TYPE (field), component);
2102 if (anon)
2103 return tree_cons (NULL_TREE, field, anon);
2105 /* The Plan 9 compiler permits referring directly to an
2106 anonymous struct/union field using a typedef
2107 name. */
2108 if (flag_plan9_extensions
2109 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2110 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2111 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2112 == component))
2113 break;
2116 if (DECL_NAME (field) == component)
2117 break;
2120 if (field == NULL_TREE)
2121 return NULL_TREE;
2124 return tree_cons (NULL_TREE, field, NULL_TREE);
2127 /* Make an expression to refer to the COMPONENT field of structure or
2128 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2129 location of the COMPONENT_REF. */
2131 tree
2132 build_component_ref (location_t loc, tree datum, tree component)
2134 tree type = TREE_TYPE (datum);
2135 enum tree_code code = TREE_CODE (type);
2136 tree field = NULL;
2137 tree ref;
2138 bool datum_lvalue = lvalue_p (datum);
2140 if (!objc_is_public (datum, component))
2141 return error_mark_node;
2143 /* Detect Objective-C property syntax object.property. */
2144 if (c_dialect_objc ()
2145 && (ref = objc_maybe_build_component_ref (datum, component)))
2146 return ref;
2148 /* See if there is a field or component with name COMPONENT. */
2150 if (code == RECORD_TYPE || code == UNION_TYPE)
2152 if (!COMPLETE_TYPE_P (type))
2154 c_incomplete_type_error (NULL_TREE, type);
2155 return error_mark_node;
2158 field = lookup_field (type, component);
2160 if (!field)
2162 error_at (loc, "%qT has no member named %qE", type, component);
2163 return error_mark_node;
2166 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2167 This might be better solved in future the way the C++ front
2168 end does it - by giving the anonymous entities each a
2169 separate name and type, and then have build_component_ref
2170 recursively call itself. We can't do that here. */
2173 tree subdatum = TREE_VALUE (field);
2174 int quals;
2175 tree subtype;
2176 bool use_datum_quals;
2178 if (TREE_TYPE (subdatum) == error_mark_node)
2179 return error_mark_node;
2181 /* If this is an rvalue, it does not have qualifiers in C
2182 standard terms and we must avoid propagating such
2183 qualifiers down to a non-lvalue array that is then
2184 converted to a pointer. */
2185 use_datum_quals = (datum_lvalue
2186 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2188 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2189 if (use_datum_quals)
2190 quals |= TYPE_QUALS (TREE_TYPE (datum));
2191 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2193 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2194 NULL_TREE);
2195 SET_EXPR_LOCATION (ref, loc);
2196 if (TREE_READONLY (subdatum)
2197 || (use_datum_quals && TREE_READONLY (datum)))
2198 TREE_READONLY (ref) = 1;
2199 if (TREE_THIS_VOLATILE (subdatum)
2200 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2201 TREE_THIS_VOLATILE (ref) = 1;
2203 if (TREE_DEPRECATED (subdatum))
2204 warn_deprecated_use (subdatum, NULL_TREE);
2206 datum = ref;
2208 field = TREE_CHAIN (field);
2210 while (field);
2212 return ref;
2214 else if (code != ERROR_MARK)
2215 error_at (loc,
2216 "request for member %qE in something not a structure or union",
2217 component);
2219 return error_mark_node;
2222 /* Given an expression PTR for a pointer, return an expression
2223 for the value pointed to.
2224 ERRORSTRING is the name of the operator to appear in error messages.
2226 LOC is the location to use for the generated tree. */
2228 tree
2229 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2231 tree pointer = default_conversion (ptr);
2232 tree type = TREE_TYPE (pointer);
2233 tree ref;
2235 if (TREE_CODE (type) == POINTER_TYPE)
2237 if (CONVERT_EXPR_P (pointer)
2238 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2240 /* If a warning is issued, mark it to avoid duplicates from
2241 the backend. This only needs to be done at
2242 warn_strict_aliasing > 2. */
2243 if (warn_strict_aliasing > 2)
2244 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2245 type, TREE_OPERAND (pointer, 0)))
2246 TREE_NO_WARNING (pointer) = 1;
2249 if (TREE_CODE (pointer) == ADDR_EXPR
2250 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2251 == TREE_TYPE (type)))
2253 ref = TREE_OPERAND (pointer, 0);
2254 protected_set_expr_location (ref, loc);
2255 return ref;
2257 else
2259 tree t = TREE_TYPE (type);
2261 ref = build1 (INDIRECT_REF, t, pointer);
2263 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2265 error_at (loc, "dereferencing pointer to incomplete type");
2266 return error_mark_node;
2268 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2269 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2271 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2272 so that we get the proper error message if the result is used
2273 to assign to. Also, &* is supposed to be a no-op.
2274 And ANSI C seems to specify that the type of the result
2275 should be the const type. */
2276 /* A de-reference of a pointer to const is not a const. It is valid
2277 to change it via some other pointer. */
2278 TREE_READONLY (ref) = TYPE_READONLY (t);
2279 TREE_SIDE_EFFECTS (ref)
2280 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2281 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2282 protected_set_expr_location (ref, loc);
2283 return ref;
2286 else if (TREE_CODE (pointer) != ERROR_MARK)
2287 invalid_indirection_error (loc, type, errstring);
2289 return error_mark_node;
2292 /* This handles expressions of the form "a[i]", which denotes
2293 an array reference.
2295 This is logically equivalent in C to *(a+i), but we may do it differently.
2296 If A is a variable or a member, we generate a primitive ARRAY_REF.
2297 This avoids forcing the array out of registers, and can work on
2298 arrays that are not lvalues (for example, members of structures returned
2299 by functions).
2301 For vector types, allow vector[i] but not i[vector], and create
2302 *(((type*)&vectortype) + i) for the expression.
2304 LOC is the location to use for the returned expression. */
2306 tree
2307 build_array_ref (location_t loc, tree array, tree index)
2309 tree ret;
2310 bool swapped = false;
2311 if (TREE_TYPE (array) == error_mark_node
2312 || TREE_TYPE (index) == error_mark_node)
2313 return error_mark_node;
2315 if (flag_enable_cilkplus && contains_array_notation_expr (index))
2317 size_t rank = 0;
2318 if (!find_rank (loc, index, index, true, &rank))
2319 return error_mark_node;
2320 if (rank > 1)
2322 error_at (loc, "rank of the array's index is greater than 1");
2323 return error_mark_node;
2326 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2327 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2328 /* Allow vector[index] but not index[vector]. */
2329 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2331 tree temp;
2332 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2333 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2335 error_at (loc,
2336 "subscripted value is neither array nor pointer nor vector");
2338 return error_mark_node;
2340 temp = array;
2341 array = index;
2342 index = temp;
2343 swapped = true;
2346 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2348 error_at (loc, "array subscript is not an integer");
2349 return error_mark_node;
2352 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2354 error_at (loc, "subscripted value is pointer to function");
2355 return error_mark_node;
2358 /* ??? Existing practice has been to warn only when the char
2359 index is syntactically the index, not for char[array]. */
2360 if (!swapped)
2361 warn_array_subscript_with_type_char (index);
2363 /* Apply default promotions *after* noticing character types. */
2364 index = default_conversion (index);
2366 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2368 convert_vector_to_pointer_for_subscript (loc, &array, index);
2370 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2372 tree rval, type;
2374 /* An array that is indexed by a non-constant
2375 cannot be stored in a register; we must be able to do
2376 address arithmetic on its address.
2377 Likewise an array of elements of variable size. */
2378 if (TREE_CODE (index) != INTEGER_CST
2379 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2380 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2382 if (!c_mark_addressable (array))
2383 return error_mark_node;
2385 /* An array that is indexed by a constant value which is not within
2386 the array bounds cannot be stored in a register either; because we
2387 would get a crash in store_bit_field/extract_bit_field when trying
2388 to access a non-existent part of the register. */
2389 if (TREE_CODE (index) == INTEGER_CST
2390 && TYPE_DOMAIN (TREE_TYPE (array))
2391 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2393 if (!c_mark_addressable (array))
2394 return error_mark_node;
2397 if (pedantic)
2399 tree foo = array;
2400 while (TREE_CODE (foo) == COMPONENT_REF)
2401 foo = TREE_OPERAND (foo, 0);
2402 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2403 pedwarn (loc, OPT_Wpedantic,
2404 "ISO C forbids subscripting %<register%> array");
2405 else if (!flag_isoc99 && !lvalue_p (foo))
2406 pedwarn (loc, OPT_Wpedantic,
2407 "ISO C90 forbids subscripting non-lvalue array");
2410 type = TREE_TYPE (TREE_TYPE (array));
2411 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2412 /* Array ref is const/volatile if the array elements are
2413 or if the array is. */
2414 TREE_READONLY (rval)
2415 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2416 | TREE_READONLY (array));
2417 TREE_SIDE_EFFECTS (rval)
2418 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2419 | TREE_SIDE_EFFECTS (array));
2420 TREE_THIS_VOLATILE (rval)
2421 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2422 /* This was added by rms on 16 Nov 91.
2423 It fixes vol struct foo *a; a->elts[1]
2424 in an inline function.
2425 Hope it doesn't break something else. */
2426 | TREE_THIS_VOLATILE (array));
2427 ret = require_complete_type (rval);
2428 protected_set_expr_location (ret, loc);
2429 return ret;
2431 else
2433 tree ar = default_conversion (array);
2435 if (ar == error_mark_node)
2436 return ar;
2438 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2439 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2441 return build_indirect_ref
2442 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2443 RO_ARRAY_INDEXING);
2447 /* Build an external reference to identifier ID. FUN indicates
2448 whether this will be used for a function call. LOC is the source
2449 location of the identifier. This sets *TYPE to the type of the
2450 identifier, which is not the same as the type of the returned value
2451 for CONST_DECLs defined as enum constants. If the type of the
2452 identifier is not available, *TYPE is set to NULL. */
2453 tree
2454 build_external_ref (location_t loc, tree id, int fun, tree *type)
2456 tree ref;
2457 tree decl = lookup_name (id);
2459 /* In Objective-C, an instance variable (ivar) may be preferred to
2460 whatever lookup_name() found. */
2461 decl = objc_lookup_ivar (decl, id);
2463 *type = NULL;
2464 if (decl && decl != error_mark_node)
2466 ref = decl;
2467 *type = TREE_TYPE (ref);
2469 else if (fun)
2470 /* Implicit function declaration. */
2471 ref = implicitly_declare (loc, id);
2472 else if (decl == error_mark_node)
2473 /* Don't complain about something that's already been
2474 complained about. */
2475 return error_mark_node;
2476 else
2478 undeclared_variable (loc, id);
2479 return error_mark_node;
2482 if (TREE_TYPE (ref) == error_mark_node)
2483 return error_mark_node;
2485 if (TREE_DEPRECATED (ref))
2486 warn_deprecated_use (ref, NULL_TREE);
2488 /* Recursive call does not count as usage. */
2489 if (ref != current_function_decl)
2491 TREE_USED (ref) = 1;
2494 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2496 if (!in_sizeof && !in_typeof)
2497 C_DECL_USED (ref) = 1;
2498 else if (DECL_INITIAL (ref) == 0
2499 && DECL_EXTERNAL (ref)
2500 && !TREE_PUBLIC (ref))
2501 record_maybe_used_decl (ref);
2504 if (TREE_CODE (ref) == CONST_DECL)
2506 used_types_insert (TREE_TYPE (ref));
2508 if (warn_cxx_compat
2509 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2510 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2512 warning_at (loc, OPT_Wc___compat,
2513 ("enum constant defined in struct or union "
2514 "is not visible in C++"));
2515 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2518 ref = DECL_INITIAL (ref);
2519 TREE_CONSTANT (ref) = 1;
2521 else if (current_function_decl != 0
2522 && !DECL_FILE_SCOPE_P (current_function_decl)
2523 && (TREE_CODE (ref) == VAR_DECL
2524 || TREE_CODE (ref) == PARM_DECL
2525 || TREE_CODE (ref) == FUNCTION_DECL))
2527 tree context = decl_function_context (ref);
2529 if (context != 0 && context != current_function_decl)
2530 DECL_NONLOCAL (ref) = 1;
2532 /* C99 6.7.4p3: An inline definition of a function with external
2533 linkage ... shall not contain a reference to an identifier with
2534 internal linkage. */
2535 else if (current_function_decl != 0
2536 && DECL_DECLARED_INLINE_P (current_function_decl)
2537 && DECL_EXTERNAL (current_function_decl)
2538 && VAR_OR_FUNCTION_DECL_P (ref)
2539 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2540 && ! TREE_PUBLIC (ref)
2541 && DECL_CONTEXT (ref) != current_function_decl)
2542 record_inline_static (loc, current_function_decl, ref,
2543 csi_internal);
2545 return ref;
2548 /* Record details of decls possibly used inside sizeof or typeof. */
2549 struct maybe_used_decl
2551 /* The decl. */
2552 tree decl;
2553 /* The level seen at (in_sizeof + in_typeof). */
2554 int level;
2555 /* The next one at this level or above, or NULL. */
2556 struct maybe_used_decl *next;
2559 static struct maybe_used_decl *maybe_used_decls;
2561 /* Record that DECL, an undefined static function reference seen
2562 inside sizeof or typeof, might be used if the operand of sizeof is
2563 a VLA type or the operand of typeof is a variably modified
2564 type. */
2566 static void
2567 record_maybe_used_decl (tree decl)
2569 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2570 t->decl = decl;
2571 t->level = in_sizeof + in_typeof;
2572 t->next = maybe_used_decls;
2573 maybe_used_decls = t;
2576 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2577 USED is false, just discard them. If it is true, mark them used
2578 (if no longer inside sizeof or typeof) or move them to the next
2579 level up (if still inside sizeof or typeof). */
2581 void
2582 pop_maybe_used (bool used)
2584 struct maybe_used_decl *p = maybe_used_decls;
2585 int cur_level = in_sizeof + in_typeof;
2586 while (p && p->level > cur_level)
2588 if (used)
2590 if (cur_level == 0)
2591 C_DECL_USED (p->decl) = 1;
2592 else
2593 p->level = cur_level;
2595 p = p->next;
2597 if (!used || cur_level == 0)
2598 maybe_used_decls = p;
2601 /* Return the result of sizeof applied to EXPR. */
2603 struct c_expr
2604 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2606 struct c_expr ret;
2607 if (expr.value == error_mark_node)
2609 ret.value = error_mark_node;
2610 ret.original_code = ERROR_MARK;
2611 ret.original_type = NULL;
2612 pop_maybe_used (false);
2614 else
2616 bool expr_const_operands = true;
2617 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2618 &expr_const_operands);
2619 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2620 c_last_sizeof_arg = expr.value;
2621 ret.original_code = SIZEOF_EXPR;
2622 ret.original_type = NULL;
2623 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2625 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2626 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2627 folded_expr, ret.value);
2628 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2629 SET_EXPR_LOCATION (ret.value, loc);
2631 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2633 return ret;
2636 /* Return the result of sizeof applied to T, a structure for the type
2637 name passed to sizeof (rather than the type itself). LOC is the
2638 location of the original expression. */
2640 struct c_expr
2641 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2643 tree type;
2644 struct c_expr ret;
2645 tree type_expr = NULL_TREE;
2646 bool type_expr_const = true;
2647 type = groktypename (t, &type_expr, &type_expr_const);
2648 ret.value = c_sizeof (loc, type);
2649 c_last_sizeof_arg = type;
2650 ret.original_code = SIZEOF_EXPR;
2651 ret.original_type = NULL;
2652 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2653 && c_vla_type_p (type))
2655 /* If the type is a [*] array, it is a VLA but is represented as
2656 having a size of zero. In such a case we must ensure that
2657 the result of sizeof does not get folded to a constant by
2658 c_fully_fold, because if the size is evaluated the result is
2659 not constant and so constraints on zero or negative size
2660 arrays must not be applied when this sizeof call is inside
2661 another array declarator. */
2662 if (!type_expr)
2663 type_expr = integer_zero_node;
2664 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2665 type_expr, ret.value);
2666 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2668 pop_maybe_used (type != error_mark_node
2669 ? C_TYPE_VARIABLE_SIZE (type) : false);
2670 return ret;
2673 /* Build a function call to function FUNCTION with parameters PARAMS.
2674 The function call is at LOC.
2675 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2676 TREE_VALUE of each node is a parameter-expression.
2677 FUNCTION's data type may be a function type or a pointer-to-function. */
2679 tree
2680 build_function_call (location_t loc, tree function, tree params)
2682 vec<tree, va_gc> *v;
2683 tree ret;
2685 vec_alloc (v, list_length (params));
2686 for (; params; params = TREE_CHAIN (params))
2687 v->quick_push (TREE_VALUE (params));
2688 ret = build_function_call_vec (loc, function, v, NULL);
2689 vec_free (v);
2690 return ret;
2693 /* Give a note about the location of the declaration of DECL. */
2695 static void inform_declaration (tree decl)
2697 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2698 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2701 /* Build a function call to function FUNCTION with parameters PARAMS.
2702 ORIGTYPES, if not NULL, is a vector of types; each element is
2703 either NULL or the original type of the corresponding element in
2704 PARAMS. The original type may differ from TREE_TYPE of the
2705 parameter for enums. FUNCTION's data type may be a function type
2706 or pointer-to-function. This function changes the elements of
2707 PARAMS. */
2709 tree
2710 build_function_call_vec (location_t loc, tree function,
2711 vec<tree, va_gc> *params,
2712 vec<tree, va_gc> *origtypes)
2714 tree fntype, fundecl = 0;
2715 tree name = NULL_TREE, result;
2716 tree tem;
2717 int nargs;
2718 tree *argarray;
2721 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2722 STRIP_TYPE_NOPS (function);
2724 /* Convert anything with function type to a pointer-to-function. */
2725 if (TREE_CODE (function) == FUNCTION_DECL)
2727 /* Implement type-directed function overloading for builtins.
2728 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2729 handle all the type checking. The result is a complete expression
2730 that implements this function call. */
2731 tem = resolve_overloaded_builtin (loc, function, params);
2732 if (tem)
2733 return tem;
2735 name = DECL_NAME (function);
2737 if (flag_tm)
2738 tm_malloc_replacement (function);
2739 fundecl = function;
2740 /* Atomic functions have type checking/casting already done. They are
2741 often rewritten and don't match the original parameter list. */
2742 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2743 origtypes = NULL;
2745 if (flag_enable_cilkplus
2746 && is_cilkplus_reduce_builtin (function))
2747 origtypes = NULL;
2749 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2750 function = function_to_pointer_conversion (loc, function);
2752 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2753 expressions, like those used for ObjC messenger dispatches. */
2754 if (params && !params->is_empty ())
2755 function = objc_rewrite_function_call (function, (*params)[0]);
2757 function = c_fully_fold (function, false, NULL);
2759 fntype = TREE_TYPE (function);
2761 if (TREE_CODE (fntype) == ERROR_MARK)
2762 return error_mark_node;
2764 if (!(TREE_CODE (fntype) == POINTER_TYPE
2765 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2767 if (!flag_diagnostics_show_caret)
2768 error_at (loc,
2769 "called object %qE is not a function or function pointer",
2770 function);
2771 else if (DECL_P (function))
2773 error_at (loc,
2774 "called object %qD is not a function or function pointer",
2775 function);
2776 inform_declaration (function);
2778 else
2779 error_at (loc,
2780 "called object is not a function or function pointer");
2781 return error_mark_node;
2784 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2785 current_function_returns_abnormally = 1;
2787 /* fntype now gets the type of function pointed to. */
2788 fntype = TREE_TYPE (fntype);
2790 /* Convert the parameters to the types declared in the
2791 function prototype, or apply default promotions. */
2793 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2794 function, fundecl);
2795 if (nargs < 0)
2796 return error_mark_node;
2798 /* Check that the function is called through a compatible prototype.
2799 If it is not, replace the call by a trap, wrapped up in a compound
2800 expression if necessary. This has the nice side-effect to prevent
2801 the tree-inliner from generating invalid assignment trees which may
2802 blow up in the RTL expander later. */
2803 if (CONVERT_EXPR_P (function)
2804 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2805 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2806 && !comptypes (fntype, TREE_TYPE (tem)))
2808 tree return_type = TREE_TYPE (fntype);
2809 tree trap = build_function_call (loc,
2810 builtin_decl_explicit (BUILT_IN_TRAP),
2811 NULL_TREE);
2812 int i;
2814 /* This situation leads to run-time undefined behavior. We can't,
2815 therefore, simply error unless we can prove that all possible
2816 executions of the program must execute the code. */
2817 if (warning_at (loc, 0, "function called through a non-compatible type"))
2818 /* We can, however, treat "undefined" any way we please.
2819 Call abort to encourage the user to fix the program. */
2820 inform (loc, "if this code is reached, the program will abort");
2821 /* Before the abort, allow the function arguments to exit or
2822 call longjmp. */
2823 for (i = 0; i < nargs; i++)
2824 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
2826 if (VOID_TYPE_P (return_type))
2828 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2829 pedwarn (loc, 0,
2830 "function with qualified void return type called");
2831 return trap;
2833 else
2835 tree rhs;
2837 if (AGGREGATE_TYPE_P (return_type))
2838 rhs = build_compound_literal (loc, return_type,
2839 build_constructor (return_type,
2840 NULL),
2841 false);
2842 else
2843 rhs = build_zero_cst (return_type);
2845 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2846 trap, rhs));
2850 argarray = vec_safe_address (params);
2852 /* Check that arguments to builtin functions match the expectations. */
2853 if (fundecl
2854 && DECL_BUILT_IN (fundecl)
2855 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2856 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2857 return error_mark_node;
2859 /* Check that the arguments to the function are valid. */
2860 check_function_arguments (fntype, nargs, argarray);
2862 if (name != NULL_TREE
2863 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2865 if (require_constant_value)
2866 result =
2867 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2868 function, nargs, argarray);
2869 else
2870 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2871 function, nargs, argarray);
2872 if (TREE_CODE (result) == NOP_EXPR
2873 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2874 STRIP_TYPE_NOPS (result);
2876 else
2877 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2878 function, nargs, argarray);
2880 if (VOID_TYPE_P (TREE_TYPE (result)))
2882 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2883 pedwarn (loc, 0,
2884 "function with qualified void return type called");
2885 return result;
2887 return require_complete_type (result);
2890 /* Convert the argument expressions in the vector VALUES
2891 to the types in the list TYPELIST.
2893 If TYPELIST is exhausted, or when an element has NULL as its type,
2894 perform the default conversions.
2896 ORIGTYPES is the original types of the expressions in VALUES. This
2897 holds the type of enum values which have been converted to integral
2898 types. It may be NULL.
2900 FUNCTION is a tree for the called function. It is used only for
2901 error messages, where it is formatted with %qE.
2903 This is also where warnings about wrong number of args are generated.
2905 Returns the actual number of arguments processed (which may be less
2906 than the length of VALUES in some error situations), or -1 on
2907 failure. */
2909 static int
2910 convert_arguments (tree typelist, vec<tree, va_gc> *values,
2911 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
2913 tree typetail, val;
2914 unsigned int parmnum;
2915 bool error_args = false;
2916 const bool type_generic = fundecl
2917 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2918 bool type_generic_remove_excess_precision = false;
2919 tree selector;
2921 /* Change pointer to function to the function itself for
2922 diagnostics. */
2923 if (TREE_CODE (function) == ADDR_EXPR
2924 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2925 function = TREE_OPERAND (function, 0);
2927 /* Handle an ObjC selector specially for diagnostics. */
2928 selector = objc_message_selector ();
2930 /* For type-generic built-in functions, determine whether excess
2931 precision should be removed (classification) or not
2932 (comparison). */
2933 if (type_generic
2934 && DECL_BUILT_IN (fundecl)
2935 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2937 switch (DECL_FUNCTION_CODE (fundecl))
2939 case BUILT_IN_ISFINITE:
2940 case BUILT_IN_ISINF:
2941 case BUILT_IN_ISINF_SIGN:
2942 case BUILT_IN_ISNAN:
2943 case BUILT_IN_ISNORMAL:
2944 case BUILT_IN_FPCLASSIFY:
2945 type_generic_remove_excess_precision = true;
2946 break;
2948 default:
2949 type_generic_remove_excess_precision = false;
2950 break;
2953 if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
2954 return vec_safe_length (values);
2956 /* Scan the given expressions and types, producing individual
2957 converted arguments. */
2959 for (typetail = typelist, parmnum = 0;
2960 values && values->iterate (parmnum, &val);
2961 ++parmnum)
2963 tree type = typetail ? TREE_VALUE (typetail) : 0;
2964 tree valtype = TREE_TYPE (val);
2965 tree rname = function;
2966 int argnum = parmnum + 1;
2967 const char *invalid_func_diag;
2968 bool excess_precision = false;
2969 bool npc;
2970 tree parmval;
2972 if (type == void_type_node)
2974 if (selector)
2975 error_at (input_location,
2976 "too many arguments to method %qE", selector);
2977 else
2978 error_at (input_location,
2979 "too many arguments to function %qE", function);
2980 inform_declaration (fundecl);
2981 return parmnum;
2984 if (selector && argnum > 2)
2986 rname = selector;
2987 argnum -= 2;
2990 npc = null_pointer_constant_p (val);
2992 /* If there is excess precision and a prototype, convert once to
2993 the required type rather than converting via the semantic
2994 type. Likewise without a prototype a float value represented
2995 as long double should be converted once to double. But for
2996 type-generic classification functions excess precision must
2997 be removed here. */
2998 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2999 && (type || !type_generic || !type_generic_remove_excess_precision))
3001 val = TREE_OPERAND (val, 0);
3002 excess_precision = true;
3004 val = c_fully_fold (val, false, NULL);
3005 STRIP_TYPE_NOPS (val);
3007 val = require_complete_type (val);
3009 if (type != 0)
3011 /* Formal parm type is specified by a function prototype. */
3013 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3015 error ("type of formal parameter %d is incomplete", parmnum + 1);
3016 parmval = val;
3018 else
3020 tree origtype;
3022 /* Optionally warn about conversions that
3023 differ from the default conversions. */
3024 if (warn_traditional_conversion || warn_traditional)
3026 unsigned int formal_prec = TYPE_PRECISION (type);
3028 if (INTEGRAL_TYPE_P (type)
3029 && TREE_CODE (valtype) == REAL_TYPE)
3030 warning (0, "passing argument %d of %qE as integer "
3031 "rather than floating due to prototype",
3032 argnum, rname);
3033 if (INTEGRAL_TYPE_P (type)
3034 && TREE_CODE (valtype) == COMPLEX_TYPE)
3035 warning (0, "passing argument %d of %qE as integer "
3036 "rather than complex due to prototype",
3037 argnum, rname);
3038 else if (TREE_CODE (type) == COMPLEX_TYPE
3039 && TREE_CODE (valtype) == REAL_TYPE)
3040 warning (0, "passing argument %d of %qE as complex "
3041 "rather than floating due to prototype",
3042 argnum, rname);
3043 else if (TREE_CODE (type) == REAL_TYPE
3044 && INTEGRAL_TYPE_P (valtype))
3045 warning (0, "passing argument %d of %qE as floating "
3046 "rather than integer due to prototype",
3047 argnum, rname);
3048 else if (TREE_CODE (type) == COMPLEX_TYPE
3049 && INTEGRAL_TYPE_P (valtype))
3050 warning (0, "passing argument %d of %qE as complex "
3051 "rather than integer due to prototype",
3052 argnum, rname);
3053 else if (TREE_CODE (type) == REAL_TYPE
3054 && TREE_CODE (valtype) == COMPLEX_TYPE)
3055 warning (0, "passing argument %d of %qE as floating "
3056 "rather than complex due to prototype",
3057 argnum, rname);
3058 /* ??? At some point, messages should be written about
3059 conversions between complex types, but that's too messy
3060 to do now. */
3061 else if (TREE_CODE (type) == REAL_TYPE
3062 && TREE_CODE (valtype) == REAL_TYPE)
3064 /* Warn if any argument is passed as `float',
3065 since without a prototype it would be `double'. */
3066 if (formal_prec == TYPE_PRECISION (float_type_node)
3067 && type != dfloat32_type_node)
3068 warning (0, "passing argument %d of %qE as %<float%> "
3069 "rather than %<double%> due to prototype",
3070 argnum, rname);
3072 /* Warn if mismatch between argument and prototype
3073 for decimal float types. Warn of conversions with
3074 binary float types and of precision narrowing due to
3075 prototype. */
3076 else if (type != valtype
3077 && (type == dfloat32_type_node
3078 || type == dfloat64_type_node
3079 || type == dfloat128_type_node
3080 || valtype == dfloat32_type_node
3081 || valtype == dfloat64_type_node
3082 || valtype == dfloat128_type_node)
3083 && (formal_prec
3084 <= TYPE_PRECISION (valtype)
3085 || (type == dfloat128_type_node
3086 && (valtype
3087 != dfloat64_type_node
3088 && (valtype
3089 != dfloat32_type_node)))
3090 || (type == dfloat64_type_node
3091 && (valtype
3092 != dfloat32_type_node))))
3093 warning (0, "passing argument %d of %qE as %qT "
3094 "rather than %qT due to prototype",
3095 argnum, rname, type, valtype);
3098 /* Detect integer changing in width or signedness.
3099 These warnings are only activated with
3100 -Wtraditional-conversion, not with -Wtraditional. */
3101 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3102 && INTEGRAL_TYPE_P (valtype))
3104 tree would_have_been = default_conversion (val);
3105 tree type1 = TREE_TYPE (would_have_been);
3107 if (TREE_CODE (type) == ENUMERAL_TYPE
3108 && (TYPE_MAIN_VARIANT (type)
3109 == TYPE_MAIN_VARIANT (valtype)))
3110 /* No warning if function asks for enum
3111 and the actual arg is that enum type. */
3113 else if (formal_prec != TYPE_PRECISION (type1))
3114 warning (OPT_Wtraditional_conversion,
3115 "passing argument %d of %qE "
3116 "with different width due to prototype",
3117 argnum, rname);
3118 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3120 /* Don't complain if the formal parameter type
3121 is an enum, because we can't tell now whether
3122 the value was an enum--even the same enum. */
3123 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3125 else if (TREE_CODE (val) == INTEGER_CST
3126 && int_fits_type_p (val, type))
3127 /* Change in signedness doesn't matter
3128 if a constant value is unaffected. */
3130 /* If the value is extended from a narrower
3131 unsigned type, it doesn't matter whether we
3132 pass it as signed or unsigned; the value
3133 certainly is the same either way. */
3134 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3135 && TYPE_UNSIGNED (valtype))
3137 else if (TYPE_UNSIGNED (type))
3138 warning (OPT_Wtraditional_conversion,
3139 "passing argument %d of %qE "
3140 "as unsigned due to prototype",
3141 argnum, rname);
3142 else
3143 warning (OPT_Wtraditional_conversion,
3144 "passing argument %d of %qE "
3145 "as signed due to prototype", argnum, rname);
3149 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3150 sake of better warnings from convert_and_check. */
3151 if (excess_precision)
3152 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3153 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3154 parmval = convert_for_assignment (input_location, type, val,
3155 origtype, ic_argpass, npc,
3156 fundecl, function,
3157 parmnum + 1);
3159 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3160 && INTEGRAL_TYPE_P (type)
3161 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3162 parmval = default_conversion (parmval);
3165 else if (TREE_CODE (valtype) == REAL_TYPE
3166 && (TYPE_PRECISION (valtype)
3167 <= TYPE_PRECISION (double_type_node))
3168 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3169 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3170 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3172 if (type_generic)
3173 parmval = val;
3174 else
3176 /* Convert `float' to `double'. */
3177 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3178 warning (OPT_Wdouble_promotion,
3179 "implicit conversion from %qT to %qT when passing "
3180 "argument to function",
3181 valtype, double_type_node);
3182 parmval = convert (double_type_node, val);
3185 else if (excess_precision && !type_generic)
3186 /* A "double" argument with excess precision being passed
3187 without a prototype or in variable arguments. */
3188 parmval = convert (valtype, val);
3189 else if ((invalid_func_diag =
3190 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3192 error (invalid_func_diag);
3193 return -1;
3195 else
3196 /* Convert `short' and `char' to full-size `int'. */
3197 parmval = default_conversion (val);
3199 (*values)[parmnum] = parmval;
3200 if (parmval == error_mark_node)
3201 error_args = true;
3203 if (typetail)
3204 typetail = TREE_CHAIN (typetail);
3207 gcc_assert (parmnum == vec_safe_length (values));
3209 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3211 error_at (input_location,
3212 "too few arguments to function %qE", function);
3213 inform_declaration (fundecl);
3214 return -1;
3217 return error_args ? -1 : (int) parmnum;
3220 /* This is the entry point used by the parser to build unary operators
3221 in the input. CODE, a tree_code, specifies the unary operator, and
3222 ARG is the operand. For unary plus, the C parser currently uses
3223 CONVERT_EXPR for code.
3225 LOC is the location to use for the tree generated.
3228 struct c_expr
3229 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3231 struct c_expr result;
3233 result.value = build_unary_op (loc, code, arg.value, 0);
3234 result.original_code = code;
3235 result.original_type = NULL;
3237 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3238 overflow_warning (loc, result.value);
3240 return result;
3243 /* This is the entry point used by the parser to build binary operators
3244 in the input. CODE, a tree_code, specifies the binary operator, and
3245 ARG1 and ARG2 are the operands. In addition to constructing the
3246 expression, we check for operands that were written with other binary
3247 operators in a way that is likely to confuse the user.
3249 LOCATION is the location of the binary operator. */
3251 struct c_expr
3252 parser_build_binary_op (location_t location, enum tree_code code,
3253 struct c_expr arg1, struct c_expr arg2)
3255 struct c_expr result;
3257 enum tree_code code1 = arg1.original_code;
3258 enum tree_code code2 = arg2.original_code;
3259 tree type1 = (arg1.original_type
3260 ? arg1.original_type
3261 : TREE_TYPE (arg1.value));
3262 tree type2 = (arg2.original_type
3263 ? arg2.original_type
3264 : TREE_TYPE (arg2.value));
3266 result.value = build_binary_op (location, code,
3267 arg1.value, arg2.value, 1);
3268 result.original_code = code;
3269 result.original_type = NULL;
3271 if (TREE_CODE (result.value) == ERROR_MARK)
3272 return result;
3274 if (location != UNKNOWN_LOCATION)
3275 protected_set_expr_location (result.value, location);
3277 /* Check for cases such as x+y<<z which users are likely
3278 to misinterpret. */
3279 if (warn_parentheses)
3280 warn_about_parentheses (input_location, code,
3281 code1, arg1.value, code2, arg2.value);
3283 if (warn_logical_op)
3284 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3285 code1, arg1.value, code2, arg2.value);
3287 /* Warn about comparisons against string literals, with the exception
3288 of testing for equality or inequality of a string literal with NULL. */
3289 if (code == EQ_EXPR || code == NE_EXPR)
3291 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3292 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3293 warning_at (location, OPT_Waddress,
3294 "comparison with string literal results in unspecified behavior");
3296 else if (TREE_CODE_CLASS (code) == tcc_comparison
3297 && (code1 == STRING_CST || code2 == STRING_CST))
3298 warning_at (location, OPT_Waddress,
3299 "comparison with string literal results in unspecified behavior");
3301 if (TREE_OVERFLOW_P (result.value)
3302 && !TREE_OVERFLOW_P (arg1.value)
3303 && !TREE_OVERFLOW_P (arg2.value))
3304 overflow_warning (location, result.value);
3306 /* Warn about comparisons of different enum types. */
3307 if (warn_enum_compare
3308 && TREE_CODE_CLASS (code) == tcc_comparison
3309 && TREE_CODE (type1) == ENUMERAL_TYPE
3310 && TREE_CODE (type2) == ENUMERAL_TYPE
3311 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3312 warning_at (location, OPT_Wenum_compare,
3313 "comparison between %qT and %qT",
3314 type1, type2);
3316 return result;
3319 /* Return a tree for the difference of pointers OP0 and OP1.
3320 The resulting tree has type int. */
3322 static tree
3323 pointer_diff (location_t loc, tree op0, tree op1)
3325 tree restype = ptrdiff_type_node;
3326 tree result, inttype;
3328 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3329 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3330 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3331 tree con0, con1, lit0, lit1;
3332 tree orig_op1 = op1;
3334 /* If the operands point into different address spaces, we need to
3335 explicitly convert them to pointers into the common address space
3336 before we can subtract the numerical address values. */
3337 if (as0 != as1)
3339 addr_space_t as_common;
3340 tree common_type;
3342 /* Determine the common superset address space. This is guaranteed
3343 to exist because the caller verified that comp_target_types
3344 returned non-zero. */
3345 if (!addr_space_superset (as0, as1, &as_common))
3346 gcc_unreachable ();
3348 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3349 op0 = convert (common_type, op0);
3350 op1 = convert (common_type, op1);
3353 /* Determine integer type to perform computations in. This will usually
3354 be the same as the result type (ptrdiff_t), but may need to be a wider
3355 type if pointers for the address space are wider than ptrdiff_t. */
3356 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3357 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3358 else
3359 inttype = restype;
3362 if (TREE_CODE (target_type) == VOID_TYPE)
3363 pedwarn (loc, OPT_Wpointer_arith,
3364 "pointer of type %<void *%> used in subtraction");
3365 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3366 pedwarn (loc, OPT_Wpointer_arith,
3367 "pointer to a function used in subtraction");
3369 /* If the conversion to ptrdiff_type does anything like widening or
3370 converting a partial to an integral mode, we get a convert_expression
3371 that is in the way to do any simplifications.
3372 (fold-const.c doesn't know that the extra bits won't be needed.
3373 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3374 different mode in place.)
3375 So first try to find a common term here 'by hand'; we want to cover
3376 at least the cases that occur in legal static initializers. */
3377 if (CONVERT_EXPR_P (op0)
3378 && (TYPE_PRECISION (TREE_TYPE (op0))
3379 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3380 con0 = TREE_OPERAND (op0, 0);
3381 else
3382 con0 = op0;
3383 if (CONVERT_EXPR_P (op1)
3384 && (TYPE_PRECISION (TREE_TYPE (op1))
3385 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3386 con1 = TREE_OPERAND (op1, 0);
3387 else
3388 con1 = op1;
3390 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3392 lit0 = TREE_OPERAND (con0, 1);
3393 con0 = TREE_OPERAND (con0, 0);
3395 else
3396 lit0 = integer_zero_node;
3398 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3400 lit1 = TREE_OPERAND (con1, 1);
3401 con1 = TREE_OPERAND (con1, 0);
3403 else
3404 lit1 = integer_zero_node;
3406 if (operand_equal_p (con0, con1, 0))
3408 op0 = lit0;
3409 op1 = lit1;
3413 /* First do the subtraction as integers;
3414 then drop through to build the divide operator.
3415 Do not do default conversions on the minus operator
3416 in case restype is a short type. */
3418 op0 = build_binary_op (loc,
3419 MINUS_EXPR, convert (inttype, op0),
3420 convert (inttype, op1), 0);
3421 /* This generates an error if op1 is pointer to incomplete type. */
3422 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3423 error_at (loc, "arithmetic on pointer to an incomplete type");
3425 /* This generates an error if op0 is pointer to incomplete type. */
3426 op1 = c_size_in_bytes (target_type);
3428 /* Divide by the size, in easiest possible way. */
3429 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3430 op0, convert (inttype, op1));
3432 /* Convert to final result type if necessary. */
3433 return convert (restype, result);
3436 /* Construct and perhaps optimize a tree representation
3437 for a unary operation. CODE, a tree_code, specifies the operation
3438 and XARG is the operand.
3439 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3440 the default promotions (such as from short to int).
3441 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3442 allows non-lvalues; this is only used to handle conversion of non-lvalue
3443 arrays to pointers in C99.
3445 LOCATION is the location of the operator. */
3447 tree
3448 build_unary_op (location_t location,
3449 enum tree_code code, tree xarg, int flag)
3451 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3452 tree arg = xarg;
3453 tree argtype = 0;
3454 enum tree_code typecode;
3455 tree val;
3456 tree ret = error_mark_node;
3457 tree eptype = NULL_TREE;
3458 int noconvert = flag;
3459 const char *invalid_op_diag;
3460 bool int_operands;
3462 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3463 if (int_operands)
3464 arg = remove_c_maybe_const_expr (arg);
3466 if (code != ADDR_EXPR)
3467 arg = require_complete_type (arg);
3469 typecode = TREE_CODE (TREE_TYPE (arg));
3470 if (typecode == ERROR_MARK)
3471 return error_mark_node;
3472 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3473 typecode = INTEGER_TYPE;
3475 if ((invalid_op_diag
3476 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3478 error_at (location, invalid_op_diag);
3479 return error_mark_node;
3482 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3484 eptype = TREE_TYPE (arg);
3485 arg = TREE_OPERAND (arg, 0);
3488 switch (code)
3490 case CONVERT_EXPR:
3491 /* This is used for unary plus, because a CONVERT_EXPR
3492 is enough to prevent anybody from looking inside for
3493 associativity, but won't generate any code. */
3494 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3495 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3496 || typecode == VECTOR_TYPE))
3498 error_at (location, "wrong type argument to unary plus");
3499 return error_mark_node;
3501 else if (!noconvert)
3502 arg = default_conversion (arg);
3503 arg = non_lvalue_loc (location, arg);
3504 break;
3506 case NEGATE_EXPR:
3507 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3508 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3509 || typecode == VECTOR_TYPE))
3511 error_at (location, "wrong type argument to unary minus");
3512 return error_mark_node;
3514 else if (!noconvert)
3515 arg = default_conversion (arg);
3516 break;
3518 case BIT_NOT_EXPR:
3519 /* ~ works on integer types and non float vectors. */
3520 if (typecode == INTEGER_TYPE
3521 || (typecode == VECTOR_TYPE
3522 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3524 if (!noconvert)
3525 arg = default_conversion (arg);
3527 else if (typecode == COMPLEX_TYPE)
3529 code = CONJ_EXPR;
3530 pedwarn (location, OPT_Wpedantic,
3531 "ISO C does not support %<~%> for complex conjugation");
3532 if (!noconvert)
3533 arg = default_conversion (arg);
3535 else
3537 error_at (location, "wrong type argument to bit-complement");
3538 return error_mark_node;
3540 break;
3542 case ABS_EXPR:
3543 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3545 error_at (location, "wrong type argument to abs");
3546 return error_mark_node;
3548 else if (!noconvert)
3549 arg = default_conversion (arg);
3550 break;
3552 case CONJ_EXPR:
3553 /* Conjugating a real value is a no-op, but allow it anyway. */
3554 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3555 || typecode == COMPLEX_TYPE))
3557 error_at (location, "wrong type argument to conjugation");
3558 return error_mark_node;
3560 else if (!noconvert)
3561 arg = default_conversion (arg);
3562 break;
3564 case TRUTH_NOT_EXPR:
3565 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3566 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3567 && typecode != COMPLEX_TYPE)
3569 error_at (location,
3570 "wrong type argument to unary exclamation mark");
3571 return error_mark_node;
3573 if (int_operands)
3575 arg = c_objc_common_truthvalue_conversion (location, xarg);
3576 arg = remove_c_maybe_const_expr (arg);
3578 else
3579 arg = c_objc_common_truthvalue_conversion (location, arg);
3580 ret = invert_truthvalue_loc (location, arg);
3581 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3582 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3583 location = EXPR_LOCATION (ret);
3584 goto return_build_unary_op;
3586 case REALPART_EXPR:
3587 case IMAGPART_EXPR:
3588 ret = build_real_imag_expr (location, code, arg);
3589 if (ret == error_mark_node)
3590 return error_mark_node;
3591 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3592 eptype = TREE_TYPE (eptype);
3593 goto return_build_unary_op;
3595 case PREINCREMENT_EXPR:
3596 case POSTINCREMENT_EXPR:
3597 case PREDECREMENT_EXPR:
3598 case POSTDECREMENT_EXPR:
3600 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3602 tree inner = build_unary_op (location, code,
3603 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3604 if (inner == error_mark_node)
3605 return error_mark_node;
3606 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3607 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3608 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3609 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3610 goto return_build_unary_op;
3613 /* Complain about anything that is not a true lvalue. In
3614 Objective-C, skip this check for property_refs. */
3615 if (!objc_is_property_ref (arg)
3616 && !lvalue_or_else (location,
3617 arg, ((code == PREINCREMENT_EXPR
3618 || code == POSTINCREMENT_EXPR)
3619 ? lv_increment
3620 : lv_decrement)))
3621 return error_mark_node;
3623 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3625 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3626 warning_at (location, OPT_Wc___compat,
3627 "increment of enumeration value is invalid in C++");
3628 else
3629 warning_at (location, OPT_Wc___compat,
3630 "decrement of enumeration value is invalid in C++");
3633 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3634 arg = c_fully_fold (arg, false, NULL);
3636 /* Increment or decrement the real part of the value,
3637 and don't change the imaginary part. */
3638 if (typecode == COMPLEX_TYPE)
3640 tree real, imag;
3642 pedwarn (location, OPT_Wpedantic,
3643 "ISO C does not support %<++%> and %<--%> on complex types");
3645 arg = stabilize_reference (arg);
3646 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3647 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3648 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3649 if (real == error_mark_node || imag == error_mark_node)
3650 return error_mark_node;
3651 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3652 real, imag);
3653 goto return_build_unary_op;
3656 /* Report invalid types. */
3658 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3659 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3661 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3662 error_at (location, "wrong type argument to increment");
3663 else
3664 error_at (location, "wrong type argument to decrement");
3666 return error_mark_node;
3670 tree inc;
3672 argtype = TREE_TYPE (arg);
3674 /* Compute the increment. */
3676 if (typecode == POINTER_TYPE)
3678 /* If pointer target is an undefined struct,
3679 we just cannot know how to do the arithmetic. */
3680 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3682 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3683 error_at (location,
3684 "increment of pointer to unknown structure");
3685 else
3686 error_at (location,
3687 "decrement of pointer to unknown structure");
3689 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3690 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3692 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3693 pedwarn (location, OPT_Wpointer_arith,
3694 "wrong type argument to increment");
3695 else
3696 pedwarn (location, OPT_Wpointer_arith,
3697 "wrong type argument to decrement");
3700 inc = c_size_in_bytes (TREE_TYPE (argtype));
3701 inc = convert_to_ptrofftype_loc (location, inc);
3703 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3705 /* For signed fract types, we invert ++ to -- or
3706 -- to ++, and change inc from 1 to -1, because
3707 it is not possible to represent 1 in signed fract constants.
3708 For unsigned fract types, the result always overflows and
3709 we get an undefined (original) or the maximum value. */
3710 if (code == PREINCREMENT_EXPR)
3711 code = PREDECREMENT_EXPR;
3712 else if (code == PREDECREMENT_EXPR)
3713 code = PREINCREMENT_EXPR;
3714 else if (code == POSTINCREMENT_EXPR)
3715 code = POSTDECREMENT_EXPR;
3716 else /* code == POSTDECREMENT_EXPR */
3717 code = POSTINCREMENT_EXPR;
3719 inc = integer_minus_one_node;
3720 inc = convert (argtype, inc);
3722 else
3724 inc = integer_one_node;
3725 inc = convert (argtype, inc);
3728 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3729 need to ask Objective-C to build the increment or decrement
3730 expression for it. */
3731 if (objc_is_property_ref (arg))
3732 return objc_build_incr_expr_for_property_ref (location, code,
3733 arg, inc);
3735 /* Report a read-only lvalue. */
3736 if (TYPE_READONLY (argtype))
3738 readonly_error (arg,
3739 ((code == PREINCREMENT_EXPR
3740 || code == POSTINCREMENT_EXPR)
3741 ? lv_increment : lv_decrement));
3742 return error_mark_node;
3744 else if (TREE_READONLY (arg))
3745 readonly_warning (arg,
3746 ((code == PREINCREMENT_EXPR
3747 || code == POSTINCREMENT_EXPR)
3748 ? lv_increment : lv_decrement));
3750 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3751 val = boolean_increment (code, arg);
3752 else
3753 val = build2 (code, TREE_TYPE (arg), arg, inc);
3754 TREE_SIDE_EFFECTS (val) = 1;
3755 if (TREE_CODE (val) != code)
3756 TREE_NO_WARNING (val) = 1;
3757 ret = val;
3758 goto return_build_unary_op;
3761 case ADDR_EXPR:
3762 /* Note that this operation never does default_conversion. */
3764 /* The operand of unary '&' must be an lvalue (which excludes
3765 expressions of type void), or, in C99, the result of a [] or
3766 unary '*' operator. */
3767 if (VOID_TYPE_P (TREE_TYPE (arg))
3768 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3769 && (TREE_CODE (arg) != INDIRECT_REF
3770 || !flag_isoc99))
3771 pedwarn (location, 0, "taking address of expression of type %<void%>");
3773 /* Let &* cancel out to simplify resulting code. */
3774 if (TREE_CODE (arg) == INDIRECT_REF)
3776 /* Don't let this be an lvalue. */
3777 if (lvalue_p (TREE_OPERAND (arg, 0)))
3778 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3779 ret = TREE_OPERAND (arg, 0);
3780 goto return_build_unary_op;
3783 /* For &x[y], return x+y */
3784 if (TREE_CODE (arg) == ARRAY_REF)
3786 tree op0 = TREE_OPERAND (arg, 0);
3787 if (!c_mark_addressable (op0))
3788 return error_mark_node;
3791 /* Anything not already handled and not a true memory reference
3792 or a non-lvalue array is an error. */
3793 else if (typecode != FUNCTION_TYPE && !flag
3794 && !lvalue_or_else (location, arg, lv_addressof))
3795 return error_mark_node;
3797 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3798 folding later. */
3799 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3801 tree inner = build_unary_op (location, code,
3802 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3803 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3804 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3805 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3806 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3807 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3808 goto return_build_unary_op;
3811 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3812 argtype = TREE_TYPE (arg);
3814 /* If the lvalue is const or volatile, merge that into the type
3815 to which the address will point. This is only needed
3816 for function types. */
3817 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3818 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3819 && TREE_CODE (argtype) == FUNCTION_TYPE)
3821 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3822 int quals = orig_quals;
3824 if (TREE_READONLY (arg))
3825 quals |= TYPE_QUAL_CONST;
3826 if (TREE_THIS_VOLATILE (arg))
3827 quals |= TYPE_QUAL_VOLATILE;
3829 argtype = c_build_qualified_type (argtype, quals);
3832 if (!c_mark_addressable (arg))
3833 return error_mark_node;
3835 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3836 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3838 argtype = build_pointer_type (argtype);
3840 /* ??? Cope with user tricks that amount to offsetof. Delete this
3841 when we have proper support for integer constant expressions. */
3842 val = get_base_address (arg);
3843 if (val && TREE_CODE (val) == INDIRECT_REF
3844 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3846 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3847 goto return_build_unary_op;
3850 val = build1 (ADDR_EXPR, argtype, arg);
3852 ret = val;
3853 goto return_build_unary_op;
3855 default:
3856 gcc_unreachable ();
3859 if (argtype == 0)
3860 argtype = TREE_TYPE (arg);
3861 if (TREE_CODE (arg) == INTEGER_CST)
3862 ret = (require_constant_value
3863 ? fold_build1_initializer_loc (location, code, argtype, arg)
3864 : fold_build1_loc (location, code, argtype, arg));
3865 else
3866 ret = build1 (code, argtype, arg);
3867 return_build_unary_op:
3868 gcc_assert (ret != error_mark_node);
3869 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3870 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3871 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3872 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3873 ret = note_integer_operands (ret);
3874 if (eptype)
3875 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3876 protected_set_expr_location (ret, location);
3877 return ret;
3880 /* Return nonzero if REF is an lvalue valid for this language.
3881 Lvalues can be assigned, unless their type has TYPE_READONLY.
3882 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3884 bool
3885 lvalue_p (const_tree ref)
3887 const enum tree_code code = TREE_CODE (ref);
3889 switch (code)
3891 case REALPART_EXPR:
3892 case IMAGPART_EXPR:
3893 case COMPONENT_REF:
3894 return lvalue_p (TREE_OPERAND (ref, 0));
3896 case C_MAYBE_CONST_EXPR:
3897 return lvalue_p (TREE_OPERAND (ref, 1));
3899 case COMPOUND_LITERAL_EXPR:
3900 case STRING_CST:
3901 return 1;
3903 case INDIRECT_REF:
3904 case ARRAY_REF:
3905 case ARRAY_NOTATION_REF:
3906 case VAR_DECL:
3907 case PARM_DECL:
3908 case RESULT_DECL:
3909 case ERROR_MARK:
3910 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3911 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3913 case BIND_EXPR:
3914 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3916 default:
3917 return 0;
3921 /* Give a warning for storing in something that is read-only in GCC
3922 terms but not const in ISO C terms. */
3924 static void
3925 readonly_warning (tree arg, enum lvalue_use use)
3927 switch (use)
3929 case lv_assign:
3930 warning (0, "assignment of read-only location %qE", arg);
3931 break;
3932 case lv_increment:
3933 warning (0, "increment of read-only location %qE", arg);
3934 break;
3935 case lv_decrement:
3936 warning (0, "decrement of read-only location %qE", arg);
3937 break;
3938 default:
3939 gcc_unreachable ();
3941 return;
3945 /* Return nonzero if REF is an lvalue valid for this language;
3946 otherwise, print an error message and return zero. USE says
3947 how the lvalue is being used and so selects the error message.
3948 LOCATION is the location at which any error should be reported. */
3950 static int
3951 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
3953 int win = lvalue_p (ref);
3955 if (!win)
3956 lvalue_error (loc, use);
3958 return win;
3961 /* Mark EXP saying that we need to be able to take the
3962 address of it; it should not be allocated in a register.
3963 Returns true if successful. */
3965 bool
3966 c_mark_addressable (tree exp)
3968 tree x = exp;
3970 while (1)
3971 switch (TREE_CODE (x))
3973 case COMPONENT_REF:
3974 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3976 error
3977 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3978 return false;
3981 /* ... fall through ... */
3983 case ADDR_EXPR:
3984 case ARRAY_REF:
3985 case REALPART_EXPR:
3986 case IMAGPART_EXPR:
3987 x = TREE_OPERAND (x, 0);
3988 break;
3990 case COMPOUND_LITERAL_EXPR:
3991 case CONSTRUCTOR:
3992 TREE_ADDRESSABLE (x) = 1;
3993 return true;
3995 case VAR_DECL:
3996 case CONST_DECL:
3997 case PARM_DECL:
3998 case RESULT_DECL:
3999 if (C_DECL_REGISTER (x)
4000 && DECL_NONLOCAL (x))
4002 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4004 error
4005 ("global register variable %qD used in nested function", x);
4006 return false;
4008 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4010 else if (C_DECL_REGISTER (x))
4012 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4013 error ("address of global register variable %qD requested", x);
4014 else
4015 error ("address of register variable %qD requested", x);
4016 return false;
4019 /* drops in */
4020 case FUNCTION_DECL:
4021 TREE_ADDRESSABLE (x) = 1;
4022 /* drops out */
4023 default:
4024 return true;
4028 /* Convert EXPR to TYPE, warning about conversion problems with
4029 constants. SEMANTIC_TYPE is the type this conversion would use
4030 without excess precision. If SEMANTIC_TYPE is NULL, this function
4031 is equivalent to convert_and_check. This function is a wrapper that
4032 handles conversions that may be different than
4033 the usual ones because of excess precision. */
4035 static tree
4036 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4038 if (TREE_TYPE (expr) == type)
4039 return expr;
4041 if (!semantic_type)
4042 return convert_and_check (type, expr);
4044 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4045 && TREE_TYPE (expr) != semantic_type)
4047 /* For integers, we need to check the real conversion, not
4048 the conversion to the excess precision type. */
4049 expr = convert_and_check (semantic_type, expr);
4051 /* Result type is the excess precision type, which should be
4052 large enough, so do not check. */
4053 return convert (type, expr);
4056 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4057 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4058 if folded to an integer constant then the unselected half may
4059 contain arbitrary operations not normally permitted in constant
4060 expressions. Set the location of the expression to LOC. */
4062 tree
4063 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4064 tree op1, tree op1_original_type, tree op2,
4065 tree op2_original_type)
4067 tree type1;
4068 tree type2;
4069 enum tree_code code1;
4070 enum tree_code code2;
4071 tree result_type = NULL;
4072 tree semantic_result_type = NULL;
4073 tree orig_op1 = op1, orig_op2 = op2;
4074 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4075 bool ifexp_int_operands;
4076 tree ret;
4078 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4079 if (op1_int_operands)
4080 op1 = remove_c_maybe_const_expr (op1);
4081 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4082 if (op2_int_operands)
4083 op2 = remove_c_maybe_const_expr (op2);
4084 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4085 if (ifexp_int_operands)
4086 ifexp = remove_c_maybe_const_expr (ifexp);
4088 /* Promote both alternatives. */
4090 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4091 op1 = default_conversion (op1);
4092 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4093 op2 = default_conversion (op2);
4095 if (TREE_CODE (ifexp) == ERROR_MARK
4096 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4097 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4098 return error_mark_node;
4100 type1 = TREE_TYPE (op1);
4101 code1 = TREE_CODE (type1);
4102 type2 = TREE_TYPE (op2);
4103 code2 = TREE_CODE (type2);
4105 /* C90 does not permit non-lvalue arrays in conditional expressions.
4106 In C99 they will be pointers by now. */
4107 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4109 error_at (colon_loc, "non-lvalue array in conditional expression");
4110 return error_mark_node;
4113 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4114 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4115 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4116 || code1 == COMPLEX_TYPE)
4117 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4118 || code2 == COMPLEX_TYPE))
4120 semantic_result_type = c_common_type (type1, type2);
4121 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4123 op1 = TREE_OPERAND (op1, 0);
4124 type1 = TREE_TYPE (op1);
4125 gcc_assert (TREE_CODE (type1) == code1);
4127 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4129 op2 = TREE_OPERAND (op2, 0);
4130 type2 = TREE_TYPE (op2);
4131 gcc_assert (TREE_CODE (type2) == code2);
4135 if (warn_cxx_compat)
4137 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4138 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4140 if (TREE_CODE (t1) == ENUMERAL_TYPE
4141 && TREE_CODE (t2) == ENUMERAL_TYPE
4142 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4143 warning_at (colon_loc, OPT_Wc___compat,
4144 ("different enum types in conditional is "
4145 "invalid in C++: %qT vs %qT"),
4146 t1, t2);
4149 /* Quickly detect the usual case where op1 and op2 have the same type
4150 after promotion. */
4151 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4153 if (type1 == type2)
4154 result_type = type1;
4155 else
4156 result_type = TYPE_MAIN_VARIANT (type1);
4158 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4159 || code1 == COMPLEX_TYPE)
4160 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4161 || code2 == COMPLEX_TYPE))
4163 result_type = c_common_type (type1, type2);
4164 do_warn_double_promotion (result_type, type1, type2,
4165 "implicit conversion from %qT to %qT to "
4166 "match other result of conditional",
4167 colon_loc);
4169 /* If -Wsign-compare, warn here if type1 and type2 have
4170 different signedness. We'll promote the signed to unsigned
4171 and later code won't know it used to be different.
4172 Do this check on the original types, so that explicit casts
4173 will be considered, but default promotions won't. */
4174 if (c_inhibit_evaluation_warnings == 0)
4176 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4177 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4179 if (unsigned_op1 ^ unsigned_op2)
4181 bool ovf;
4183 /* Do not warn if the result type is signed, since the
4184 signed type will only be chosen if it can represent
4185 all the values of the unsigned type. */
4186 if (!TYPE_UNSIGNED (result_type))
4187 /* OK */;
4188 else
4190 bool op1_maybe_const = true;
4191 bool op2_maybe_const = true;
4193 /* Do not warn if the signed quantity is an
4194 unsuffixed integer literal (or some static
4195 constant expression involving such literals) and
4196 it is non-negative. This warning requires the
4197 operands to be folded for best results, so do
4198 that folding in this case even without
4199 warn_sign_compare to avoid warning options
4200 possibly affecting code generation. */
4201 c_inhibit_evaluation_warnings
4202 += (ifexp == truthvalue_false_node);
4203 op1 = c_fully_fold (op1, require_constant_value,
4204 &op1_maybe_const);
4205 c_inhibit_evaluation_warnings
4206 -= (ifexp == truthvalue_false_node);
4208 c_inhibit_evaluation_warnings
4209 += (ifexp == truthvalue_true_node);
4210 op2 = c_fully_fold (op2, require_constant_value,
4211 &op2_maybe_const);
4212 c_inhibit_evaluation_warnings
4213 -= (ifexp == truthvalue_true_node);
4215 if (warn_sign_compare)
4217 if ((unsigned_op2
4218 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4219 || (unsigned_op1
4220 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4221 /* OK */;
4222 else
4223 warning_at (colon_loc, OPT_Wsign_compare,
4224 ("signed and unsigned type in "
4225 "conditional expression"));
4227 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4228 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4229 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4230 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4235 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4237 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4238 pedwarn (colon_loc, OPT_Wpedantic,
4239 "ISO C forbids conditional expr with only one void side");
4240 result_type = void_type_node;
4242 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4244 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4245 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4246 addr_space_t as_common;
4248 if (comp_target_types (colon_loc, type1, type2))
4249 result_type = common_pointer_type (type1, type2);
4250 else if (null_pointer_constant_p (orig_op1))
4251 result_type = type2;
4252 else if (null_pointer_constant_p (orig_op2))
4253 result_type = type1;
4254 else if (!addr_space_superset (as1, as2, &as_common))
4256 error_at (colon_loc, "pointers to disjoint address spaces "
4257 "used in conditional expression");
4258 return error_mark_node;
4260 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4262 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4263 pedwarn (colon_loc, OPT_Wpedantic,
4264 "ISO C forbids conditional expr between "
4265 "%<void *%> and function pointer");
4266 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4267 TREE_TYPE (type2)));
4269 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4271 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4272 pedwarn (colon_loc, OPT_Wpedantic,
4273 "ISO C forbids conditional expr between "
4274 "%<void *%> and function pointer");
4275 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4276 TREE_TYPE (type1)));
4278 /* Objective-C pointer comparisons are a bit more lenient. */
4279 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4280 result_type = objc_common_type (type1, type2);
4281 else
4283 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4285 pedwarn (colon_loc, 0,
4286 "pointer type mismatch in conditional expression");
4287 result_type = build_pointer_type
4288 (build_qualified_type (void_type_node, qual));
4291 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4293 if (!null_pointer_constant_p (orig_op2))
4294 pedwarn (colon_loc, 0,
4295 "pointer/integer type mismatch in conditional expression");
4296 else
4298 op2 = null_pointer_node;
4300 result_type = type1;
4302 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4304 if (!null_pointer_constant_p (orig_op1))
4305 pedwarn (colon_loc, 0,
4306 "pointer/integer type mismatch in conditional expression");
4307 else
4309 op1 = null_pointer_node;
4311 result_type = type2;
4314 if (!result_type)
4316 if (flag_cond_mismatch)
4317 result_type = void_type_node;
4318 else
4320 error_at (colon_loc, "type mismatch in conditional expression");
4321 return error_mark_node;
4325 /* Merge const and volatile flags of the incoming types. */
4326 result_type
4327 = build_type_variant (result_type,
4328 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4329 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4331 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4332 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4334 if (ifexp_bcp && ifexp == truthvalue_true_node)
4336 op2_int_operands = true;
4337 op1 = c_fully_fold (op1, require_constant_value, NULL);
4339 if (ifexp_bcp && ifexp == truthvalue_false_node)
4341 op1_int_operands = true;
4342 op2 = c_fully_fold (op2, require_constant_value, NULL);
4344 int_const = int_operands = (ifexp_int_operands
4345 && op1_int_operands
4346 && op2_int_operands);
4347 if (int_operands)
4349 int_const = ((ifexp == truthvalue_true_node
4350 && TREE_CODE (orig_op1) == INTEGER_CST
4351 && !TREE_OVERFLOW (orig_op1))
4352 || (ifexp == truthvalue_false_node
4353 && TREE_CODE (orig_op2) == INTEGER_CST
4354 && !TREE_OVERFLOW (orig_op2)));
4356 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4357 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4358 else
4360 if (int_operands)
4362 op1 = remove_c_maybe_const_expr (op1);
4363 op2 = remove_c_maybe_const_expr (op2);
4365 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4366 if (int_operands)
4367 ret = note_integer_operands (ret);
4369 if (semantic_result_type)
4370 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4372 protected_set_expr_location (ret, colon_loc);
4373 return ret;
4376 /* Return a compound expression that performs two expressions and
4377 returns the value of the second of them.
4379 LOC is the location of the COMPOUND_EXPR. */
4381 tree
4382 build_compound_expr (location_t loc, tree expr1, tree expr2)
4384 bool expr1_int_operands, expr2_int_operands;
4385 tree eptype = NULL_TREE;
4386 tree ret;
4388 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4389 if (expr1_int_operands)
4390 expr1 = remove_c_maybe_const_expr (expr1);
4391 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4392 if (expr2_int_operands)
4393 expr2 = remove_c_maybe_const_expr (expr2);
4395 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4396 expr1 = TREE_OPERAND (expr1, 0);
4397 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4399 eptype = TREE_TYPE (expr2);
4400 expr2 = TREE_OPERAND (expr2, 0);
4403 if (!TREE_SIDE_EFFECTS (expr1))
4405 /* The left-hand operand of a comma expression is like an expression
4406 statement: with -Wunused, we should warn if it doesn't have
4407 any side-effects, unless it was explicitly cast to (void). */
4408 if (warn_unused_value)
4410 if (VOID_TYPE_P (TREE_TYPE (expr1))
4411 && CONVERT_EXPR_P (expr1))
4412 ; /* (void) a, b */
4413 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4414 && TREE_CODE (expr1) == COMPOUND_EXPR
4415 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4416 ; /* (void) a, (void) b, c */
4417 else
4418 warning_at (loc, OPT_Wunused_value,
4419 "left-hand operand of comma expression has no effect");
4423 /* With -Wunused, we should also warn if the left-hand operand does have
4424 side-effects, but computes a value which is not used. For example, in
4425 `foo() + bar(), baz()' the result of the `+' operator is not used,
4426 so we should issue a warning. */
4427 else if (warn_unused_value)
4428 warn_if_unused_value (expr1, loc);
4430 if (expr2 == error_mark_node)
4431 return error_mark_node;
4433 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4435 if (flag_isoc99
4436 && expr1_int_operands
4437 && expr2_int_operands)
4438 ret = note_integer_operands (ret);
4440 if (eptype)
4441 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4443 protected_set_expr_location (ret, loc);
4444 return ret;
4447 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4448 which we are casting. OTYPE is the type of the expression being
4449 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4450 of the cast. -Wcast-qual appeared on the command line. Named
4451 address space qualifiers are not handled here, because they result
4452 in different warnings. */
4454 static void
4455 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4457 tree in_type = type;
4458 tree in_otype = otype;
4459 int added = 0;
4460 int discarded = 0;
4461 bool is_const;
4463 /* Check that the qualifiers on IN_TYPE are a superset of the
4464 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4465 nodes is uninteresting and we stop as soon as we hit a
4466 non-POINTER_TYPE node on either type. */
4469 in_otype = TREE_TYPE (in_otype);
4470 in_type = TREE_TYPE (in_type);
4472 /* GNU C allows cv-qualified function types. 'const' means the
4473 function is very pure, 'volatile' means it can't return. We
4474 need to warn when such qualifiers are added, not when they're
4475 taken away. */
4476 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4477 && TREE_CODE (in_type) == FUNCTION_TYPE)
4478 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4479 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4480 else
4481 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4482 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4484 while (TREE_CODE (in_type) == POINTER_TYPE
4485 && TREE_CODE (in_otype) == POINTER_TYPE);
4487 if (added)
4488 warning_at (loc, OPT_Wcast_qual,
4489 "cast adds %q#v qualifier to function type", added);
4491 if (discarded)
4492 /* There are qualifiers present in IN_OTYPE that are not present
4493 in IN_TYPE. */
4494 warning_at (loc, OPT_Wcast_qual,
4495 "cast discards %q#v qualifier from pointer target type",
4496 discarded);
4498 if (added || discarded)
4499 return;
4501 /* A cast from **T to const **T is unsafe, because it can cause a
4502 const value to be changed with no additional warning. We only
4503 issue this warning if T is the same on both sides, and we only
4504 issue the warning if there are the same number of pointers on
4505 both sides, as otherwise the cast is clearly unsafe anyhow. A
4506 cast is unsafe when a qualifier is added at one level and const
4507 is not present at all outer levels.
4509 To issue this warning, we check at each level whether the cast
4510 adds new qualifiers not already seen. We don't need to special
4511 case function types, as they won't have the same
4512 TYPE_MAIN_VARIANT. */
4514 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4515 return;
4516 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4517 return;
4519 in_type = type;
4520 in_otype = otype;
4521 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4524 in_type = TREE_TYPE (in_type);
4525 in_otype = TREE_TYPE (in_otype);
4526 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4527 && !is_const)
4529 warning_at (loc, OPT_Wcast_qual,
4530 "to be safe all intermediate pointers in cast from "
4531 "%qT to %qT must be %<const%> qualified",
4532 otype, type);
4533 break;
4535 if (is_const)
4536 is_const = TYPE_READONLY (in_type);
4538 while (TREE_CODE (in_type) == POINTER_TYPE);
4541 /* Build an expression representing a cast to type TYPE of expression EXPR.
4542 LOC is the location of the cast-- typically the open paren of the cast. */
4544 tree
4545 build_c_cast (location_t loc, tree type, tree expr)
4547 tree value;
4549 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4550 expr = TREE_OPERAND (expr, 0);
4552 value = expr;
4554 if (type == error_mark_node || expr == error_mark_node)
4555 return error_mark_node;
4557 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4558 only in <protocol> qualifications. But when constructing cast expressions,
4559 the protocols do matter and must be kept around. */
4560 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4561 return build1 (NOP_EXPR, type, expr);
4563 type = TYPE_MAIN_VARIANT (type);
4565 if (TREE_CODE (type) == ARRAY_TYPE)
4567 error_at (loc, "cast specifies array type");
4568 return error_mark_node;
4571 if (TREE_CODE (type) == FUNCTION_TYPE)
4573 error_at (loc, "cast specifies function type");
4574 return error_mark_node;
4577 if (!VOID_TYPE_P (type))
4579 value = require_complete_type (value);
4580 if (value == error_mark_node)
4581 return error_mark_node;
4584 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4586 if (TREE_CODE (type) == RECORD_TYPE
4587 || TREE_CODE (type) == UNION_TYPE)
4588 pedwarn (loc, OPT_Wpedantic,
4589 "ISO C forbids casting nonscalar to the same type");
4591 else if (TREE_CODE (type) == UNION_TYPE)
4593 tree field;
4595 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4596 if (TREE_TYPE (field) != error_mark_node
4597 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4598 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4599 break;
4601 if (field)
4603 tree t;
4604 bool maybe_const = true;
4606 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4607 t = c_fully_fold (value, false, &maybe_const);
4608 t = build_constructor_single (type, field, t);
4609 if (!maybe_const)
4610 t = c_wrap_maybe_const (t, true);
4611 t = digest_init (loc, type, t,
4612 NULL_TREE, false, true, 0);
4613 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4614 return t;
4616 error_at (loc, "cast to union type from type not present in union");
4617 return error_mark_node;
4619 else
4621 tree otype, ovalue;
4623 if (type == void_type_node)
4625 tree t = build1 (CONVERT_EXPR, type, value);
4626 SET_EXPR_LOCATION (t, loc);
4627 return t;
4630 otype = TREE_TYPE (value);
4632 /* Optionally warn about potentially worrisome casts. */
4633 if (warn_cast_qual
4634 && TREE_CODE (type) == POINTER_TYPE
4635 && TREE_CODE (otype) == POINTER_TYPE)
4636 handle_warn_cast_qual (loc, type, otype);
4638 /* Warn about conversions between pointers to disjoint
4639 address spaces. */
4640 if (TREE_CODE (type) == POINTER_TYPE
4641 && TREE_CODE (otype) == POINTER_TYPE
4642 && !null_pointer_constant_p (value))
4644 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4645 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4646 addr_space_t as_common;
4648 if (!addr_space_superset (as_to, as_from, &as_common))
4650 if (ADDR_SPACE_GENERIC_P (as_from))
4651 warning_at (loc, 0, "cast to %s address space pointer "
4652 "from disjoint generic address space pointer",
4653 c_addr_space_name (as_to));
4655 else if (ADDR_SPACE_GENERIC_P (as_to))
4656 warning_at (loc, 0, "cast to generic address space pointer "
4657 "from disjoint %s address space pointer",
4658 c_addr_space_name (as_from));
4660 else
4661 warning_at (loc, 0, "cast to %s address space pointer "
4662 "from disjoint %s address space pointer",
4663 c_addr_space_name (as_to),
4664 c_addr_space_name (as_from));
4668 /* Warn about possible alignment problems. */
4669 if (STRICT_ALIGNMENT
4670 && TREE_CODE (type) == POINTER_TYPE
4671 && TREE_CODE (otype) == POINTER_TYPE
4672 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4673 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4674 /* Don't warn about opaque types, where the actual alignment
4675 restriction is unknown. */
4676 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4677 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4678 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4679 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4680 warning_at (loc, OPT_Wcast_align,
4681 "cast increases required alignment of target type");
4683 if (TREE_CODE (type) == INTEGER_TYPE
4684 && TREE_CODE (otype) == POINTER_TYPE
4685 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4686 /* Unlike conversion of integers to pointers, where the
4687 warning is disabled for converting constants because
4688 of cases such as SIG_*, warn about converting constant
4689 pointers to integers. In some cases it may cause unwanted
4690 sign extension, and a warning is appropriate. */
4691 warning_at (loc, OPT_Wpointer_to_int_cast,
4692 "cast from pointer to integer of different size");
4694 if (TREE_CODE (value) == CALL_EXPR
4695 && TREE_CODE (type) != TREE_CODE (otype))
4696 warning_at (loc, OPT_Wbad_function_cast,
4697 "cast from function call of type %qT "
4698 "to non-matching type %qT", otype, type);
4700 if (TREE_CODE (type) == POINTER_TYPE
4701 && TREE_CODE (otype) == INTEGER_TYPE
4702 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4703 /* Don't warn about converting any constant. */
4704 && !TREE_CONSTANT (value))
4705 warning_at (loc,
4706 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4707 "of different size");
4709 if (warn_strict_aliasing <= 2)
4710 strict_aliasing_warning (otype, type, expr);
4712 /* If pedantic, warn for conversions between function and object
4713 pointer types, except for converting a null pointer constant
4714 to function pointer type. */
4715 if (pedantic
4716 && TREE_CODE (type) == POINTER_TYPE
4717 && TREE_CODE (otype) == POINTER_TYPE
4718 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4719 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4720 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4721 "conversion of function pointer to object pointer type");
4723 if (pedantic
4724 && TREE_CODE (type) == POINTER_TYPE
4725 && TREE_CODE (otype) == POINTER_TYPE
4726 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4727 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4728 && !null_pointer_constant_p (value))
4729 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4730 "conversion of object pointer to function pointer type");
4732 ovalue = value;
4733 value = convert (type, value);
4735 /* Ignore any integer overflow caused by the cast. */
4736 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4738 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4740 if (!TREE_OVERFLOW (value))
4742 /* Avoid clobbering a shared constant. */
4743 value = copy_node (value);
4744 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4747 else if (TREE_OVERFLOW (value))
4748 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4749 value = build_int_cst_wide (TREE_TYPE (value),
4750 TREE_INT_CST_LOW (value),
4751 TREE_INT_CST_HIGH (value));
4755 /* Don't let a cast be an lvalue. */
4756 if (value == expr)
4757 value = non_lvalue_loc (loc, value);
4759 /* Don't allow the results of casting to floating-point or complex
4760 types be confused with actual constants, or casts involving
4761 integer and pointer types other than direct integer-to-integer
4762 and integer-to-pointer be confused with integer constant
4763 expressions and null pointer constants. */
4764 if (TREE_CODE (value) == REAL_CST
4765 || TREE_CODE (value) == COMPLEX_CST
4766 || (TREE_CODE (value) == INTEGER_CST
4767 && !((TREE_CODE (expr) == INTEGER_CST
4768 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4769 || TREE_CODE (expr) == REAL_CST
4770 || TREE_CODE (expr) == COMPLEX_CST)))
4771 value = build1 (NOP_EXPR, type, value);
4773 if (CAN_HAVE_LOCATION_P (value))
4774 SET_EXPR_LOCATION (value, loc);
4775 return value;
4778 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4779 location of the open paren of the cast, or the position of the cast
4780 expr. */
4781 tree
4782 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4784 tree type;
4785 tree type_expr = NULL_TREE;
4786 bool type_expr_const = true;
4787 tree ret;
4788 int saved_wsp = warn_strict_prototypes;
4790 /* This avoids warnings about unprototyped casts on
4791 integers. E.g. "#define SIG_DFL (void(*)())0". */
4792 if (TREE_CODE (expr) == INTEGER_CST)
4793 warn_strict_prototypes = 0;
4794 type = groktypename (type_name, &type_expr, &type_expr_const);
4795 warn_strict_prototypes = saved_wsp;
4797 ret = build_c_cast (loc, type, expr);
4798 if (type_expr)
4800 bool inner_expr_const = true;
4801 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
4802 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4803 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4804 && inner_expr_const);
4805 SET_EXPR_LOCATION (ret, loc);
4808 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4809 SET_EXPR_LOCATION (ret, loc);
4811 /* C++ does not permits types to be defined in a cast, but it
4812 allows references to incomplete types. */
4813 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4814 warning_at (loc, OPT_Wc___compat,
4815 "defining a type in a cast is invalid in C++");
4817 return ret;
4820 /* Build an assignment expression of lvalue LHS from value RHS.
4821 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4822 may differ from TREE_TYPE (LHS) for an enum bitfield.
4823 MODIFYCODE is the code for a binary operator that we use
4824 to combine the old value of LHS with RHS to get the new value.
4825 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4826 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4827 which may differ from TREE_TYPE (RHS) for an enum value.
4829 LOCATION is the location of the MODIFYCODE operator.
4830 RHS_LOC is the location of the RHS. */
4832 tree
4833 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4834 enum tree_code modifycode,
4835 location_t rhs_loc, tree rhs, tree rhs_origtype)
4837 tree result;
4838 tree newrhs;
4839 tree rhs_semantic_type = NULL_TREE;
4840 tree lhstype = TREE_TYPE (lhs);
4841 tree olhstype = lhstype;
4842 bool npc;
4844 /* Types that aren't fully specified cannot be used in assignments. */
4845 lhs = require_complete_type (lhs);
4847 /* Avoid duplicate error messages from operands that had errors. */
4848 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4849 return error_mark_node;
4851 /* For ObjC properties, defer this check. */
4852 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4853 return error_mark_node;
4855 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4857 rhs_semantic_type = TREE_TYPE (rhs);
4858 rhs = TREE_OPERAND (rhs, 0);
4861 newrhs = rhs;
4863 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4865 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4866 lhs_origtype, modifycode, rhs_loc, rhs,
4867 rhs_origtype);
4868 if (inner == error_mark_node)
4869 return error_mark_node;
4870 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4871 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4872 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4873 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4874 protected_set_expr_location (result, location);
4875 return result;
4878 /* If a binary op has been requested, combine the old LHS value with the RHS
4879 producing the value we should actually store into the LHS. */
4881 if (modifycode != NOP_EXPR)
4883 lhs = c_fully_fold (lhs, false, NULL);
4884 lhs = stabilize_reference (lhs);
4885 newrhs = build_binary_op (location,
4886 modifycode, lhs, rhs, 1);
4888 /* The original type of the right hand side is no longer
4889 meaningful. */
4890 rhs_origtype = NULL_TREE;
4893 if (c_dialect_objc ())
4895 /* Check if we are modifying an Objective-C property reference;
4896 if so, we need to generate setter calls. */
4897 result = objc_maybe_build_modify_expr (lhs, newrhs);
4898 if (result)
4899 return result;
4901 /* Else, do the check that we postponed for Objective-C. */
4902 if (!lvalue_or_else (location, lhs, lv_assign))
4903 return error_mark_node;
4906 /* Give an error for storing in something that is 'const'. */
4908 if (TYPE_READONLY (lhstype)
4909 || ((TREE_CODE (lhstype) == RECORD_TYPE
4910 || TREE_CODE (lhstype) == UNION_TYPE)
4911 && C_TYPE_FIELDS_READONLY (lhstype)))
4913 readonly_error (lhs, lv_assign);
4914 return error_mark_node;
4916 else if (TREE_READONLY (lhs))
4917 readonly_warning (lhs, lv_assign);
4919 /* If storing into a structure or union member,
4920 it has probably been given type `int'.
4921 Compute the type that would go with
4922 the actual amount of storage the member occupies. */
4924 if (TREE_CODE (lhs) == COMPONENT_REF
4925 && (TREE_CODE (lhstype) == INTEGER_TYPE
4926 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4927 || TREE_CODE (lhstype) == REAL_TYPE
4928 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4929 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4931 /* If storing in a field that is in actuality a short or narrower than one,
4932 we must store in the field in its actual type. */
4934 if (lhstype != TREE_TYPE (lhs))
4936 lhs = copy_node (lhs);
4937 TREE_TYPE (lhs) = lhstype;
4940 /* Issue -Wc++-compat warnings about an assignment to an enum type
4941 when LHS does not have its original type. This happens for,
4942 e.g., an enum bitfield in a struct. */
4943 if (warn_cxx_compat
4944 && lhs_origtype != NULL_TREE
4945 && lhs_origtype != lhstype
4946 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4948 tree checktype = (rhs_origtype != NULL_TREE
4949 ? rhs_origtype
4950 : TREE_TYPE (rhs));
4951 if (checktype != error_mark_node
4952 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4953 warning_at (location, OPT_Wc___compat,
4954 "enum conversion in assignment is invalid in C++");
4957 /* Convert new value to destination type. Fold it first, then
4958 restore any excess precision information, for the sake of
4959 conversion warnings. */
4961 npc = null_pointer_constant_p (newrhs);
4962 newrhs = c_fully_fold (newrhs, false, NULL);
4963 if (rhs_semantic_type)
4964 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4965 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4966 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4967 if (TREE_CODE (newrhs) == ERROR_MARK)
4968 return error_mark_node;
4970 /* Emit ObjC write barrier, if necessary. */
4971 if (c_dialect_objc () && flag_objc_gc)
4973 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4974 if (result)
4976 protected_set_expr_location (result, location);
4977 return result;
4981 /* Scan operands. */
4983 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4984 TREE_SIDE_EFFECTS (result) = 1;
4985 protected_set_expr_location (result, location);
4987 /* If we got the LHS in a different type for storing in,
4988 convert the result back to the nominal type of LHS
4989 so that the value we return always has the same type
4990 as the LHS argument. */
4992 if (olhstype == TREE_TYPE (result))
4993 return result;
4995 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4996 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4997 protected_set_expr_location (result, location);
4998 return result;
5001 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5002 This is used to implement -fplan9-extensions. */
5004 static bool
5005 find_anonymous_field_with_type (tree struct_type, tree type)
5007 tree field;
5008 bool found;
5010 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5011 || TREE_CODE (struct_type) == UNION_TYPE);
5012 found = false;
5013 for (field = TYPE_FIELDS (struct_type);
5014 field != NULL_TREE;
5015 field = TREE_CHAIN (field))
5017 if (DECL_NAME (field) == NULL
5018 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5020 if (found)
5021 return false;
5022 found = true;
5024 else if (DECL_NAME (field) == NULL
5025 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5026 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5027 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5029 if (found)
5030 return false;
5031 found = true;
5034 return found;
5037 /* RHS is an expression whose type is pointer to struct. If there is
5038 an anonymous field in RHS with type TYPE, then return a pointer to
5039 that field in RHS. This is used with -fplan9-extensions. This
5040 returns NULL if no conversion could be found. */
5042 static tree
5043 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5045 tree rhs_struct_type, lhs_main_type;
5046 tree field, found_field;
5047 bool found_sub_field;
5048 tree ret;
5050 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5051 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5052 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5053 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5055 gcc_assert (POINTER_TYPE_P (type));
5056 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5058 found_field = NULL_TREE;
5059 found_sub_field = false;
5060 for (field = TYPE_FIELDS (rhs_struct_type);
5061 field != NULL_TREE;
5062 field = TREE_CHAIN (field))
5064 if (DECL_NAME (field) != NULL_TREE
5065 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5066 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5067 continue;
5068 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5070 if (found_field != NULL_TREE)
5071 return NULL_TREE;
5072 found_field = field;
5074 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5075 lhs_main_type))
5077 if (found_field != NULL_TREE)
5078 return NULL_TREE;
5079 found_field = field;
5080 found_sub_field = true;
5084 if (found_field == NULL_TREE)
5085 return NULL_TREE;
5087 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5088 build_fold_indirect_ref (rhs), found_field,
5089 NULL_TREE);
5090 ret = build_fold_addr_expr_loc (location, ret);
5092 if (found_sub_field)
5094 ret = convert_to_anonymous_field (location, type, ret);
5095 gcc_assert (ret != NULL_TREE);
5098 return ret;
5101 /* Convert value RHS to type TYPE as preparation for an assignment to
5102 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5103 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5104 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5105 constant before any folding.
5106 The real work of conversion is done by `convert'.
5107 The purpose of this function is to generate error messages
5108 for assignments that are not allowed in C.
5109 ERRTYPE says whether it is argument passing, assignment,
5110 initialization or return.
5112 LOCATION is the location of the RHS.
5113 FUNCTION is a tree for the function being called.
5114 PARMNUM is the number of the argument, for printing in error messages. */
5116 static tree
5117 convert_for_assignment (location_t location, tree type, tree rhs,
5118 tree origtype, enum impl_conv errtype,
5119 bool null_pointer_constant, tree fundecl,
5120 tree function, int parmnum)
5122 enum tree_code codel = TREE_CODE (type);
5123 tree orig_rhs = rhs;
5124 tree rhstype;
5125 enum tree_code coder;
5126 tree rname = NULL_TREE;
5127 bool objc_ok = false;
5129 if (errtype == ic_argpass)
5131 tree selector;
5132 /* Change pointer to function to the function itself for
5133 diagnostics. */
5134 if (TREE_CODE (function) == ADDR_EXPR
5135 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5136 function = TREE_OPERAND (function, 0);
5138 /* Handle an ObjC selector specially for diagnostics. */
5139 selector = objc_message_selector ();
5140 rname = function;
5141 if (selector && parmnum > 2)
5143 rname = selector;
5144 parmnum -= 2;
5148 /* This macro is used to emit diagnostics to ensure that all format
5149 strings are complete sentences, visible to gettext and checked at
5150 compile time. */
5151 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5152 do { \
5153 switch (errtype) \
5155 case ic_argpass: \
5156 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5157 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5158 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5159 "expected %qT but argument is of type %qT", \
5160 type, rhstype); \
5161 break; \
5162 case ic_assign: \
5163 pedwarn (LOCATION, OPT, AS); \
5164 break; \
5165 case ic_init: \
5166 pedwarn_init (LOCATION, OPT, IN); \
5167 break; \
5168 case ic_return: \
5169 pedwarn (LOCATION, OPT, RE); \
5170 break; \
5171 default: \
5172 gcc_unreachable (); \
5174 } while (0)
5176 /* This macro is used to emit diagnostics to ensure that all format
5177 strings are complete sentences, visible to gettext and checked at
5178 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5179 extra parameter to enumerate qualifiers. */
5181 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5182 do { \
5183 switch (errtype) \
5185 case ic_argpass: \
5186 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5187 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5188 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5189 "expected %qT but argument is of type %qT", \
5190 type, rhstype); \
5191 break; \
5192 case ic_assign: \
5193 pedwarn (LOCATION, OPT, AS, QUALS); \
5194 break; \
5195 case ic_init: \
5196 pedwarn (LOCATION, OPT, IN, QUALS); \
5197 break; \
5198 case ic_return: \
5199 pedwarn (LOCATION, OPT, RE, QUALS); \
5200 break; \
5201 default: \
5202 gcc_unreachable (); \
5204 } while (0)
5206 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5207 rhs = TREE_OPERAND (rhs, 0);
5209 rhstype = TREE_TYPE (rhs);
5210 coder = TREE_CODE (rhstype);
5212 if (coder == ERROR_MARK)
5213 return error_mark_node;
5215 if (c_dialect_objc ())
5217 int parmno;
5219 switch (errtype)
5221 case ic_return:
5222 parmno = 0;
5223 break;
5225 case ic_assign:
5226 parmno = -1;
5227 break;
5229 case ic_init:
5230 parmno = -2;
5231 break;
5233 default:
5234 parmno = parmnum;
5235 break;
5238 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5241 if (warn_cxx_compat)
5243 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5244 if (checktype != error_mark_node
5245 && TREE_CODE (type) == ENUMERAL_TYPE
5246 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5248 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5249 G_("enum conversion when passing argument "
5250 "%d of %qE is invalid in C++"),
5251 G_("enum conversion in assignment is "
5252 "invalid in C++"),
5253 G_("enum conversion in initialization is "
5254 "invalid in C++"),
5255 G_("enum conversion in return is "
5256 "invalid in C++"));
5260 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5261 return rhs;
5263 if (coder == VOID_TYPE)
5265 /* Except for passing an argument to an unprototyped function,
5266 this is a constraint violation. When passing an argument to
5267 an unprototyped function, it is compile-time undefined;
5268 making it a constraint in that case was rejected in
5269 DR#252. */
5270 error_at (location, "void value not ignored as it ought to be");
5271 return error_mark_node;
5273 rhs = require_complete_type (rhs);
5274 if (rhs == error_mark_node)
5275 return error_mark_node;
5276 /* A non-reference type can convert to a reference. This handles
5277 va_start, va_copy and possibly port built-ins. */
5278 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5280 if (!lvalue_p (rhs))
5282 error_at (location, "cannot pass rvalue to reference parameter");
5283 return error_mark_node;
5285 if (!c_mark_addressable (rhs))
5286 return error_mark_node;
5287 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5288 SET_EXPR_LOCATION (rhs, location);
5290 rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)),
5291 rhs, origtype, errtype, null_pointer_constant,
5292 fundecl, function, parmnum);
5293 if (rhs == error_mark_node)
5294 return error_mark_node;
5296 rhs = build1 (NOP_EXPR, type, rhs);
5297 SET_EXPR_LOCATION (rhs, location);
5298 return rhs;
5300 /* Some types can interconvert without explicit casts. */
5301 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5302 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5303 return convert (type, rhs);
5304 /* Arithmetic types all interconvert, and enum is treated like int. */
5305 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5306 || codel == FIXED_POINT_TYPE
5307 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5308 || codel == BOOLEAN_TYPE)
5309 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5310 || coder == FIXED_POINT_TYPE
5311 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5312 || coder == BOOLEAN_TYPE))
5314 tree ret;
5315 bool save = in_late_binary_op;
5316 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5317 in_late_binary_op = true;
5318 ret = convert_and_check (type, orig_rhs);
5319 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5320 in_late_binary_op = save;
5321 return ret;
5324 /* Aggregates in different TUs might need conversion. */
5325 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5326 && codel == coder
5327 && comptypes (type, rhstype))
5328 return convert_and_check (type, rhs);
5330 /* Conversion to a transparent union or record from its member types.
5331 This applies only to function arguments. */
5332 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5333 && TYPE_TRANSPARENT_AGGR (type))
5334 && errtype == ic_argpass)
5336 tree memb, marginal_memb = NULL_TREE;
5338 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5340 tree memb_type = TREE_TYPE (memb);
5342 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5343 TYPE_MAIN_VARIANT (rhstype)))
5344 break;
5346 if (TREE_CODE (memb_type) != POINTER_TYPE)
5347 continue;
5349 if (coder == POINTER_TYPE)
5351 tree ttl = TREE_TYPE (memb_type);
5352 tree ttr = TREE_TYPE (rhstype);
5354 /* Any non-function converts to a [const][volatile] void *
5355 and vice versa; otherwise, targets must be the same.
5356 Meanwhile, the lhs target must have all the qualifiers of
5357 the rhs. */
5358 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5359 || comp_target_types (location, memb_type, rhstype))
5361 /* If this type won't generate any warnings, use it. */
5362 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5363 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5364 && TREE_CODE (ttl) == FUNCTION_TYPE)
5365 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5366 == TYPE_QUALS (ttr))
5367 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5368 == TYPE_QUALS (ttl))))
5369 break;
5371 /* Keep looking for a better type, but remember this one. */
5372 if (!marginal_memb)
5373 marginal_memb = memb;
5377 /* Can convert integer zero to any pointer type. */
5378 if (null_pointer_constant)
5380 rhs = null_pointer_node;
5381 break;
5385 if (memb || marginal_memb)
5387 if (!memb)
5389 /* We have only a marginally acceptable member type;
5390 it needs a warning. */
5391 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5392 tree ttr = TREE_TYPE (rhstype);
5394 /* Const and volatile mean something different for function
5395 types, so the usual warnings are not appropriate. */
5396 if (TREE_CODE (ttr) == FUNCTION_TYPE
5397 && TREE_CODE (ttl) == FUNCTION_TYPE)
5399 /* Because const and volatile on functions are
5400 restrictions that say the function will not do
5401 certain things, it is okay to use a const or volatile
5402 function where an ordinary one is wanted, but not
5403 vice-versa. */
5404 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5405 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5406 WARN_FOR_QUALIFIERS (location, 0,
5407 G_("passing argument %d of %qE "
5408 "makes %q#v qualified function "
5409 "pointer from unqualified"),
5410 G_("assignment makes %q#v qualified "
5411 "function pointer from "
5412 "unqualified"),
5413 G_("initialization makes %q#v qualified "
5414 "function pointer from "
5415 "unqualified"),
5416 G_("return makes %q#v qualified function "
5417 "pointer from unqualified"),
5418 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5420 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5421 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5422 WARN_FOR_QUALIFIERS (location, 0,
5423 G_("passing argument %d of %qE discards "
5424 "%qv qualifier from pointer target type"),
5425 G_("assignment discards %qv qualifier "
5426 "from pointer target type"),
5427 G_("initialization discards %qv qualifier "
5428 "from pointer target type"),
5429 G_("return discards %qv qualifier from "
5430 "pointer target type"),
5431 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5433 memb = marginal_memb;
5436 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5437 pedwarn (location, OPT_Wpedantic,
5438 "ISO C prohibits argument conversion to union type");
5440 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5441 return build_constructor_single (type, memb, rhs);
5445 /* Conversions among pointers */
5446 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5447 && (coder == codel))
5449 tree ttl = TREE_TYPE (type);
5450 tree ttr = TREE_TYPE (rhstype);
5451 tree mvl = ttl;
5452 tree mvr = ttr;
5453 bool is_opaque_pointer;
5454 int target_cmp = 0; /* Cache comp_target_types () result. */
5455 addr_space_t asl;
5456 addr_space_t asr;
5458 if (TREE_CODE (mvl) != ARRAY_TYPE)
5459 mvl = TYPE_MAIN_VARIANT (mvl);
5460 if (TREE_CODE (mvr) != ARRAY_TYPE)
5461 mvr = TYPE_MAIN_VARIANT (mvr);
5462 /* Opaque pointers are treated like void pointers. */
5463 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5465 /* The Plan 9 compiler permits a pointer to a struct to be
5466 automatically converted into a pointer to an anonymous field
5467 within the struct. */
5468 if (flag_plan9_extensions
5469 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5470 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5471 && mvl != mvr)
5473 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5474 if (new_rhs != NULL_TREE)
5476 rhs = new_rhs;
5477 rhstype = TREE_TYPE (rhs);
5478 coder = TREE_CODE (rhstype);
5479 ttr = TREE_TYPE (rhstype);
5480 mvr = TYPE_MAIN_VARIANT (ttr);
5484 /* C++ does not allow the implicit conversion void* -> T*. However,
5485 for the purpose of reducing the number of false positives, we
5486 tolerate the special case of
5488 int *p = NULL;
5490 where NULL is typically defined in C to be '(void *) 0'. */
5491 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5492 warning_at (location, OPT_Wc___compat,
5493 "request for implicit conversion "
5494 "from %qT to %qT not permitted in C++", rhstype, type);
5496 /* See if the pointers point to incompatible address spaces. */
5497 asl = TYPE_ADDR_SPACE (ttl);
5498 asr = TYPE_ADDR_SPACE (ttr);
5499 if (!null_pointer_constant_p (rhs)
5500 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5502 switch (errtype)
5504 case ic_argpass:
5505 error_at (location, "passing argument %d of %qE from pointer to "
5506 "non-enclosed address space", parmnum, rname);
5507 break;
5508 case ic_assign:
5509 error_at (location, "assignment from pointer to "
5510 "non-enclosed address space");
5511 break;
5512 case ic_init:
5513 error_at (location, "initialization from pointer to "
5514 "non-enclosed address space");
5515 break;
5516 case ic_return:
5517 error_at (location, "return from pointer to "
5518 "non-enclosed address space");
5519 break;
5520 default:
5521 gcc_unreachable ();
5523 return error_mark_node;
5526 /* Check if the right-hand side has a format attribute but the
5527 left-hand side doesn't. */
5528 if (warn_suggest_attribute_format
5529 && check_missing_format_attribute (type, rhstype))
5531 switch (errtype)
5533 case ic_argpass:
5534 warning_at (location, OPT_Wsuggest_attribute_format,
5535 "argument %d of %qE might be "
5536 "a candidate for a format attribute",
5537 parmnum, rname);
5538 break;
5539 case ic_assign:
5540 warning_at (location, OPT_Wsuggest_attribute_format,
5541 "assignment left-hand side might be "
5542 "a candidate for a format attribute");
5543 break;
5544 case ic_init:
5545 warning_at (location, OPT_Wsuggest_attribute_format,
5546 "initialization left-hand side might be "
5547 "a candidate for a format attribute");
5548 break;
5549 case ic_return:
5550 warning_at (location, OPT_Wsuggest_attribute_format,
5551 "return type might be "
5552 "a candidate for a format attribute");
5553 break;
5554 default:
5555 gcc_unreachable ();
5559 /* Any non-function converts to a [const][volatile] void *
5560 and vice versa; otherwise, targets must be the same.
5561 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5562 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5563 || (target_cmp = comp_target_types (location, type, rhstype))
5564 || is_opaque_pointer
5565 || ((c_common_unsigned_type (mvl)
5566 == c_common_unsigned_type (mvr))
5567 && c_common_signed_type (mvl)
5568 == c_common_signed_type (mvr)))
5570 if (pedantic
5571 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5573 (VOID_TYPE_P (ttr)
5574 && !null_pointer_constant
5575 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5576 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
5577 G_("ISO C forbids passing argument %d of "
5578 "%qE between function pointer "
5579 "and %<void *%>"),
5580 G_("ISO C forbids assignment between "
5581 "function pointer and %<void *%>"),
5582 G_("ISO C forbids initialization between "
5583 "function pointer and %<void *%>"),
5584 G_("ISO C forbids return between function "
5585 "pointer and %<void *%>"));
5586 /* Const and volatile mean something different for function types,
5587 so the usual warnings are not appropriate. */
5588 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5589 && TREE_CODE (ttl) != FUNCTION_TYPE)
5591 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5592 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5594 WARN_FOR_QUALIFIERS (location, 0,
5595 G_("passing argument %d of %qE discards "
5596 "%qv qualifier from pointer target type"),
5597 G_("assignment discards %qv qualifier "
5598 "from pointer target type"),
5599 G_("initialization discards %qv qualifier "
5600 "from pointer target type"),
5601 G_("return discards %qv qualifier from "
5602 "pointer target type"),
5603 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5605 /* If this is not a case of ignoring a mismatch in signedness,
5606 no warning. */
5607 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5608 || target_cmp)
5610 /* If there is a mismatch, do warn. */
5611 else if (warn_pointer_sign)
5612 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5613 G_("pointer targets in passing argument "
5614 "%d of %qE differ in signedness"),
5615 G_("pointer targets in assignment "
5616 "differ in signedness"),
5617 G_("pointer targets in initialization "
5618 "differ in signedness"),
5619 G_("pointer targets in return differ "
5620 "in signedness"));
5622 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5623 && TREE_CODE (ttr) == FUNCTION_TYPE)
5625 /* Because const and volatile on functions are restrictions
5626 that say the function will not do certain things,
5627 it is okay to use a const or volatile function
5628 where an ordinary one is wanted, but not vice-versa. */
5629 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5630 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5631 WARN_FOR_QUALIFIERS (location, 0,
5632 G_("passing argument %d of %qE makes "
5633 "%q#v qualified function pointer "
5634 "from unqualified"),
5635 G_("assignment makes %q#v qualified function "
5636 "pointer from unqualified"),
5637 G_("initialization makes %q#v qualified "
5638 "function pointer from unqualified"),
5639 G_("return makes %q#v qualified function "
5640 "pointer from unqualified"),
5641 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5644 else
5645 /* Avoid warning about the volatile ObjC EH puts on decls. */
5646 if (!objc_ok)
5647 WARN_FOR_ASSIGNMENT (location, 0,
5648 G_("passing argument %d of %qE from "
5649 "incompatible pointer type"),
5650 G_("assignment from incompatible pointer type"),
5651 G_("initialization from incompatible "
5652 "pointer type"),
5653 G_("return from incompatible pointer type"));
5655 return convert (type, rhs);
5657 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5659 /* ??? This should not be an error when inlining calls to
5660 unprototyped functions. */
5661 error_at (location, "invalid use of non-lvalue array");
5662 return error_mark_node;
5664 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5666 /* An explicit constant 0 can convert to a pointer,
5667 or one that results from arithmetic, even including
5668 a cast to integer type. */
5669 if (!null_pointer_constant)
5670 WARN_FOR_ASSIGNMENT (location, 0,
5671 G_("passing argument %d of %qE makes "
5672 "pointer from integer without a cast"),
5673 G_("assignment makes pointer from integer "
5674 "without a cast"),
5675 G_("initialization makes pointer from "
5676 "integer without a cast"),
5677 G_("return makes pointer from integer "
5678 "without a cast"));
5680 return convert (type, rhs);
5682 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5684 WARN_FOR_ASSIGNMENT (location, 0,
5685 G_("passing argument %d of %qE makes integer "
5686 "from pointer without a cast"),
5687 G_("assignment makes integer from pointer "
5688 "without a cast"),
5689 G_("initialization makes integer from pointer "
5690 "without a cast"),
5691 G_("return makes integer from pointer "
5692 "without a cast"));
5693 return convert (type, rhs);
5695 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5697 tree ret;
5698 bool save = in_late_binary_op;
5699 in_late_binary_op = true;
5700 ret = convert (type, rhs);
5701 in_late_binary_op = save;
5702 return ret;
5705 switch (errtype)
5707 case ic_argpass:
5708 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5709 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5710 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5711 "expected %qT but argument is of type %qT", type, rhstype);
5712 break;
5713 case ic_assign:
5714 error_at (location, "incompatible types when assigning to type %qT from "
5715 "type %qT", type, rhstype);
5716 break;
5717 case ic_init:
5718 error_at (location,
5719 "incompatible types when initializing type %qT using type %qT",
5720 type, rhstype);
5721 break;
5722 case ic_return:
5723 error_at (location,
5724 "incompatible types when returning type %qT but %qT was "
5725 "expected", rhstype, type);
5726 break;
5727 default:
5728 gcc_unreachable ();
5731 return error_mark_node;
5734 /* If VALUE is a compound expr all of whose expressions are constant, then
5735 return its value. Otherwise, return error_mark_node.
5737 This is for handling COMPOUND_EXPRs as initializer elements
5738 which is allowed with a warning when -pedantic is specified. */
5740 static tree
5741 valid_compound_expr_initializer (tree value, tree endtype)
5743 if (TREE_CODE (value) == COMPOUND_EXPR)
5745 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5746 == error_mark_node)
5747 return error_mark_node;
5748 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5749 endtype);
5751 else if (!initializer_constant_valid_p (value, endtype))
5752 return error_mark_node;
5753 else
5754 return value;
5757 /* Perform appropriate conversions on the initial value of a variable,
5758 store it in the declaration DECL,
5759 and print any error messages that are appropriate.
5760 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5761 If the init is invalid, store an ERROR_MARK.
5763 INIT_LOC is the location of the initial value. */
5765 void
5766 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5768 tree value, type;
5769 bool npc = false;
5771 /* If variable's type was invalidly declared, just ignore it. */
5773 type = TREE_TYPE (decl);
5774 if (TREE_CODE (type) == ERROR_MARK)
5775 return;
5777 /* Digest the specified initializer into an expression. */
5779 if (init)
5780 npc = null_pointer_constant_p (init);
5781 value = digest_init (init_loc, type, init, origtype, npc,
5782 true, TREE_STATIC (decl));
5784 /* Store the expression if valid; else report error. */
5786 if (!in_system_header
5787 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5788 warning (OPT_Wtraditional, "traditional C rejects automatic "
5789 "aggregate initialization");
5791 DECL_INITIAL (decl) = value;
5793 /* ANSI wants warnings about out-of-range constant initializers. */
5794 STRIP_TYPE_NOPS (value);
5795 if (TREE_STATIC (decl))
5796 constant_expression_warning (value);
5798 /* Check if we need to set array size from compound literal size. */
5799 if (TREE_CODE (type) == ARRAY_TYPE
5800 && TYPE_DOMAIN (type) == 0
5801 && value != error_mark_node)
5803 tree inside_init = init;
5805 STRIP_TYPE_NOPS (inside_init);
5806 inside_init = fold (inside_init);
5808 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5810 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5812 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5814 /* For int foo[] = (int [3]){1}; we need to set array size
5815 now since later on array initializer will be just the
5816 brace enclosed list of the compound literal. */
5817 tree etype = strip_array_types (TREE_TYPE (decl));
5818 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5819 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5820 layout_type (type);
5821 layout_decl (cldecl, 0);
5822 TREE_TYPE (decl)
5823 = c_build_qualified_type (type, TYPE_QUALS (etype));
5829 /* Methods for storing and printing names for error messages. */
5831 /* Implement a spelling stack that allows components of a name to be pushed
5832 and popped. Each element on the stack is this structure. */
5834 struct spelling
5836 int kind;
5837 union
5839 unsigned HOST_WIDE_INT i;
5840 const char *s;
5841 } u;
5844 #define SPELLING_STRING 1
5845 #define SPELLING_MEMBER 2
5846 #define SPELLING_BOUNDS 3
5848 static struct spelling *spelling; /* Next stack element (unused). */
5849 static struct spelling *spelling_base; /* Spelling stack base. */
5850 static int spelling_size; /* Size of the spelling stack. */
5852 /* Macros to save and restore the spelling stack around push_... functions.
5853 Alternative to SAVE_SPELLING_STACK. */
5855 #define SPELLING_DEPTH() (spelling - spelling_base)
5856 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5858 /* Push an element on the spelling stack with type KIND and assign VALUE
5859 to MEMBER. */
5861 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5863 int depth = SPELLING_DEPTH (); \
5865 if (depth >= spelling_size) \
5867 spelling_size += 10; \
5868 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5869 spelling_size); \
5870 RESTORE_SPELLING_DEPTH (depth); \
5873 spelling->kind = (KIND); \
5874 spelling->MEMBER = (VALUE); \
5875 spelling++; \
5878 /* Push STRING on the stack. Printed literally. */
5880 static void
5881 push_string (const char *string)
5883 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5886 /* Push a member name on the stack. Printed as '.' STRING. */
5888 static void
5889 push_member_name (tree decl)
5891 const char *const string
5892 = (DECL_NAME (decl)
5893 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5894 : _("<anonymous>"));
5895 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5898 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5900 static void
5901 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5903 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5906 /* Compute the maximum size in bytes of the printed spelling. */
5908 static int
5909 spelling_length (void)
5911 int size = 0;
5912 struct spelling *p;
5914 for (p = spelling_base; p < spelling; p++)
5916 if (p->kind == SPELLING_BOUNDS)
5917 size += 25;
5918 else
5919 size += strlen (p->u.s) + 1;
5922 return size;
5925 /* Print the spelling to BUFFER and return it. */
5927 static char *
5928 print_spelling (char *buffer)
5930 char *d = buffer;
5931 struct spelling *p;
5933 for (p = spelling_base; p < spelling; p++)
5934 if (p->kind == SPELLING_BOUNDS)
5936 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5937 d += strlen (d);
5939 else
5941 const char *s;
5942 if (p->kind == SPELLING_MEMBER)
5943 *d++ = '.';
5944 for (s = p->u.s; (*d = *s++); d++)
5947 *d++ = '\0';
5948 return buffer;
5951 /* Issue an error message for a bad initializer component.
5952 GMSGID identifies the message.
5953 The component name is taken from the spelling stack. */
5955 void
5956 error_init (const char *gmsgid)
5958 char *ofwhat;
5960 /* The gmsgid may be a format string with %< and %>. */
5961 error (gmsgid);
5962 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5963 if (*ofwhat)
5964 error ("(near initialization for %qs)", ofwhat);
5967 /* Issue a pedantic warning for a bad initializer component. OPT is
5968 the option OPT_* (from options.h) controlling this warning or 0 if
5969 it is unconditionally given. GMSGID identifies the message. The
5970 component name is taken from the spelling stack. */
5972 void
5973 pedwarn_init (location_t location, int opt, const char *gmsgid)
5975 char *ofwhat;
5977 /* The gmsgid may be a format string with %< and %>. */
5978 pedwarn (location, opt, gmsgid);
5979 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5980 if (*ofwhat)
5981 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5984 /* Issue a warning for a bad initializer component.
5986 OPT is the OPT_W* value corresponding to the warning option that
5987 controls this warning. GMSGID identifies the message. The
5988 component name is taken from the spelling stack. */
5990 static void
5991 warning_init (int opt, const char *gmsgid)
5993 char *ofwhat;
5995 /* The gmsgid may be a format string with %< and %>. */
5996 warning (opt, gmsgid);
5997 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5998 if (*ofwhat)
5999 warning (opt, "(near initialization for %qs)", ofwhat);
6002 /* If TYPE is an array type and EXPR is a parenthesized string
6003 constant, warn if pedantic that EXPR is being used to initialize an
6004 object of type TYPE. */
6006 void
6007 maybe_warn_string_init (tree type, struct c_expr expr)
6009 if (pedantic
6010 && TREE_CODE (type) == ARRAY_TYPE
6011 && TREE_CODE (expr.value) == STRING_CST
6012 && expr.original_code != STRING_CST)
6013 pedwarn_init (input_location, OPT_Wpedantic,
6014 "array initialized from parenthesized string constant");
6017 /* Digest the parser output INIT as an initializer for type TYPE.
6018 Return a C expression of type TYPE to represent the initial value.
6020 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6022 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6024 If INIT is a string constant, STRICT_STRING is true if it is
6025 unparenthesized or we should not warn here for it being parenthesized.
6026 For other types of INIT, STRICT_STRING is not used.
6028 INIT_LOC is the location of the INIT.
6030 REQUIRE_CONSTANT requests an error if non-constant initializers or
6031 elements are seen. */
6033 static tree
6034 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6035 bool null_pointer_constant, bool strict_string,
6036 int require_constant)
6038 enum tree_code code = TREE_CODE (type);
6039 tree inside_init = init;
6040 tree semantic_type = NULL_TREE;
6041 bool maybe_const = true;
6043 if (type == error_mark_node
6044 || !init
6045 || init == error_mark_node
6046 || TREE_TYPE (init) == error_mark_node)
6047 return error_mark_node;
6049 STRIP_TYPE_NOPS (inside_init);
6051 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6053 semantic_type = TREE_TYPE (inside_init);
6054 inside_init = TREE_OPERAND (inside_init, 0);
6056 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6057 inside_init = decl_constant_value_for_optimization (inside_init);
6059 /* Initialization of an array of chars from a string constant
6060 optionally enclosed in braces. */
6062 if (code == ARRAY_TYPE && inside_init
6063 && TREE_CODE (inside_init) == STRING_CST)
6065 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6066 /* Note that an array could be both an array of character type
6067 and an array of wchar_t if wchar_t is signed char or unsigned
6068 char. */
6069 bool char_array = (typ1 == char_type_node
6070 || typ1 == signed_char_type_node
6071 || typ1 == unsigned_char_type_node);
6072 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6073 bool char16_array = !!comptypes (typ1, char16_type_node);
6074 bool char32_array = !!comptypes (typ1, char32_type_node);
6076 if (char_array || wchar_array || char16_array || char32_array)
6078 struct c_expr expr;
6079 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6080 expr.value = inside_init;
6081 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6082 expr.original_type = NULL;
6083 maybe_warn_string_init (type, expr);
6085 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6086 pedwarn_init (init_loc, OPT_Wpedantic,
6087 "initialization of a flexible array member");
6089 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6090 TYPE_MAIN_VARIANT (type)))
6091 return inside_init;
6093 if (char_array)
6095 if (typ2 != char_type_node)
6097 error_init ("char-array initialized from wide string");
6098 return error_mark_node;
6101 else
6103 if (typ2 == char_type_node)
6105 error_init ("wide character array initialized from non-wide "
6106 "string");
6107 return error_mark_node;
6109 else if (!comptypes(typ1, typ2))
6111 error_init ("wide character array initialized from "
6112 "incompatible wide string");
6113 return error_mark_node;
6117 TREE_TYPE (inside_init) = type;
6118 if (TYPE_DOMAIN (type) != 0
6119 && TYPE_SIZE (type) != 0
6120 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6122 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6124 /* Subtract the size of a single (possibly wide) character
6125 because it's ok to ignore the terminating null char
6126 that is counted in the length of the constant. */
6127 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6128 (len
6129 - (TYPE_PRECISION (typ1)
6130 / BITS_PER_UNIT))))
6131 pedwarn_init (init_loc, 0,
6132 ("initializer-string for array of chars "
6133 "is too long"));
6134 else if (warn_cxx_compat
6135 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6136 warning_at (init_loc, OPT_Wc___compat,
6137 ("initializer-string for array chars "
6138 "is too long for C++"));
6141 return inside_init;
6143 else if (INTEGRAL_TYPE_P (typ1))
6145 error_init ("array of inappropriate type initialized "
6146 "from string constant");
6147 return error_mark_node;
6151 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6152 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6153 below and handle as a constructor. */
6154 if (code == VECTOR_TYPE
6155 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6156 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6157 && TREE_CONSTANT (inside_init))
6159 if (TREE_CODE (inside_init) == VECTOR_CST
6160 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6161 TYPE_MAIN_VARIANT (type)))
6162 return inside_init;
6164 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6166 unsigned HOST_WIDE_INT ix;
6167 tree value;
6168 bool constant_p = true;
6170 /* Iterate through elements and check if all constructor
6171 elements are *_CSTs. */
6172 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6173 if (!CONSTANT_CLASS_P (value))
6175 constant_p = false;
6176 break;
6179 if (constant_p)
6180 return build_vector_from_ctor (type,
6181 CONSTRUCTOR_ELTS (inside_init));
6185 if (warn_sequence_point)
6186 verify_sequence_points (inside_init);
6188 /* Any type can be initialized
6189 from an expression of the same type, optionally with braces. */
6191 if (inside_init && TREE_TYPE (inside_init) != 0
6192 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6193 TYPE_MAIN_VARIANT (type))
6194 || (code == ARRAY_TYPE
6195 && comptypes (TREE_TYPE (inside_init), type))
6196 || (code == VECTOR_TYPE
6197 && comptypes (TREE_TYPE (inside_init), type))
6198 || (code == POINTER_TYPE
6199 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6200 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6201 TREE_TYPE (type)))))
6203 if (code == POINTER_TYPE)
6205 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6207 if (TREE_CODE (inside_init) == STRING_CST
6208 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6209 inside_init = array_to_pointer_conversion
6210 (init_loc, inside_init);
6211 else
6213 error_init ("invalid use of non-lvalue array");
6214 return error_mark_node;
6219 if (code == VECTOR_TYPE)
6220 /* Although the types are compatible, we may require a
6221 conversion. */
6222 inside_init = convert (type, inside_init);
6224 if (require_constant
6225 && (code == VECTOR_TYPE || !flag_isoc99)
6226 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6228 /* As an extension, allow initializing objects with static storage
6229 duration with compound literals (which are then treated just as
6230 the brace enclosed list they contain). Also allow this for
6231 vectors, as we can only assign them with compound literals. */
6232 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6233 inside_init = DECL_INITIAL (decl);
6236 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6237 && TREE_CODE (inside_init) != CONSTRUCTOR)
6239 error_init ("array initialized from non-constant array expression");
6240 return error_mark_node;
6243 /* Compound expressions can only occur here if -Wpedantic or
6244 -pedantic-errors is specified. In the later case, we always want
6245 an error. In the former case, we simply want a warning. */
6246 if (require_constant && pedantic
6247 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6249 inside_init
6250 = valid_compound_expr_initializer (inside_init,
6251 TREE_TYPE (inside_init));
6252 if (inside_init == error_mark_node)
6253 error_init ("initializer element is not constant");
6254 else
6255 pedwarn_init (init_loc, OPT_Wpedantic,
6256 "initializer element is not constant");
6257 if (flag_pedantic_errors)
6258 inside_init = error_mark_node;
6260 else if (require_constant
6261 && !initializer_constant_valid_p (inside_init,
6262 TREE_TYPE (inside_init)))
6264 error_init ("initializer element is not constant");
6265 inside_init = error_mark_node;
6267 else if (require_constant && !maybe_const)
6268 pedwarn_init (init_loc, 0,
6269 "initializer element is not a constant expression");
6271 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6272 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6273 inside_init = convert_for_assignment (init_loc, type, inside_init,
6274 origtype,
6275 ic_init, null_pointer_constant,
6276 NULL_TREE, NULL_TREE, 0);
6277 return inside_init;
6280 /* Handle scalar types, including conversions. */
6282 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6283 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6284 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6286 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6287 && (TREE_CODE (init) == STRING_CST
6288 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6289 inside_init = init = array_to_pointer_conversion (init_loc, init);
6290 if (semantic_type)
6291 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6292 inside_init);
6293 inside_init
6294 = convert_for_assignment (init_loc, type, inside_init, origtype,
6295 ic_init, null_pointer_constant,
6296 NULL_TREE, NULL_TREE, 0);
6298 /* Check to see if we have already given an error message. */
6299 if (inside_init == error_mark_node)
6301 else if (require_constant && !TREE_CONSTANT (inside_init))
6303 error_init ("initializer element is not constant");
6304 inside_init = error_mark_node;
6306 else if (require_constant
6307 && !initializer_constant_valid_p (inside_init,
6308 TREE_TYPE (inside_init)))
6310 error_init ("initializer element is not computable at load time");
6311 inside_init = error_mark_node;
6313 else if (require_constant && !maybe_const)
6314 pedwarn_init (init_loc, 0,
6315 "initializer element is not a constant expression");
6317 return inside_init;
6320 /* Come here only for records and arrays. */
6322 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6324 error_init ("variable-sized object may not be initialized");
6325 return error_mark_node;
6328 error_init ("invalid initializer");
6329 return error_mark_node;
6332 /* Handle initializers that use braces. */
6334 /* Type of object we are accumulating a constructor for.
6335 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6336 static tree constructor_type;
6338 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6339 left to fill. */
6340 static tree constructor_fields;
6342 /* For an ARRAY_TYPE, this is the specified index
6343 at which to store the next element we get. */
6344 static tree constructor_index;
6346 /* For an ARRAY_TYPE, this is the maximum index. */
6347 static tree constructor_max_index;
6349 /* For a RECORD_TYPE, this is the first field not yet written out. */
6350 static tree constructor_unfilled_fields;
6352 /* For an ARRAY_TYPE, this is the index of the first element
6353 not yet written out. */
6354 static tree constructor_unfilled_index;
6356 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6357 This is so we can generate gaps between fields, when appropriate. */
6358 static tree constructor_bit_index;
6360 /* If we are saving up the elements rather than allocating them,
6361 this is the list of elements so far (in reverse order,
6362 most recent first). */
6363 static vec<constructor_elt, va_gc> *constructor_elements;
6365 /* 1 if constructor should be incrementally stored into a constructor chain,
6366 0 if all the elements should be kept in AVL tree. */
6367 static int constructor_incremental;
6369 /* 1 if so far this constructor's elements are all compile-time constants. */
6370 static int constructor_constant;
6372 /* 1 if so far this constructor's elements are all valid address constants. */
6373 static int constructor_simple;
6375 /* 1 if this constructor has an element that cannot be part of a
6376 constant expression. */
6377 static int constructor_nonconst;
6379 /* 1 if this constructor is erroneous so far. */
6380 static int constructor_erroneous;
6382 /* Structure for managing pending initializer elements, organized as an
6383 AVL tree. */
6385 struct init_node
6387 struct init_node *left, *right;
6388 struct init_node *parent;
6389 int balance;
6390 tree purpose;
6391 tree value;
6392 tree origtype;
6395 /* Tree of pending elements at this constructor level.
6396 These are elements encountered out of order
6397 which belong at places we haven't reached yet in actually
6398 writing the output.
6399 Will never hold tree nodes across GC runs. */
6400 static struct init_node *constructor_pending_elts;
6402 /* The SPELLING_DEPTH of this constructor. */
6403 static int constructor_depth;
6405 /* DECL node for which an initializer is being read.
6406 0 means we are reading a constructor expression
6407 such as (struct foo) {...}. */
6408 static tree constructor_decl;
6410 /* Nonzero if this is an initializer for a top-level decl. */
6411 static int constructor_top_level;
6413 /* Nonzero if there were any member designators in this initializer. */
6414 static int constructor_designated;
6416 /* Nesting depth of designator list. */
6417 static int designator_depth;
6419 /* Nonzero if there were diagnosed errors in this designator list. */
6420 static int designator_erroneous;
6423 /* This stack has a level for each implicit or explicit level of
6424 structuring in the initializer, including the outermost one. It
6425 saves the values of most of the variables above. */
6427 struct constructor_range_stack;
6429 struct constructor_stack
6431 struct constructor_stack *next;
6432 tree type;
6433 tree fields;
6434 tree index;
6435 tree max_index;
6436 tree unfilled_index;
6437 tree unfilled_fields;
6438 tree bit_index;
6439 vec<constructor_elt, va_gc> *elements;
6440 struct init_node *pending_elts;
6441 int offset;
6442 int depth;
6443 /* If value nonzero, this value should replace the entire
6444 constructor at this level. */
6445 struct c_expr replacement_value;
6446 struct constructor_range_stack *range_stack;
6447 char constant;
6448 char simple;
6449 char nonconst;
6450 char implicit;
6451 char erroneous;
6452 char outer;
6453 char incremental;
6454 char designated;
6457 static struct constructor_stack *constructor_stack;
6459 /* This stack represents designators from some range designator up to
6460 the last designator in the list. */
6462 struct constructor_range_stack
6464 struct constructor_range_stack *next, *prev;
6465 struct constructor_stack *stack;
6466 tree range_start;
6467 tree index;
6468 tree range_end;
6469 tree fields;
6472 static struct constructor_range_stack *constructor_range_stack;
6474 /* This stack records separate initializers that are nested.
6475 Nested initializers can't happen in ANSI C, but GNU C allows them
6476 in cases like { ... (struct foo) { ... } ... }. */
6478 struct initializer_stack
6480 struct initializer_stack *next;
6481 tree decl;
6482 struct constructor_stack *constructor_stack;
6483 struct constructor_range_stack *constructor_range_stack;
6484 vec<constructor_elt, va_gc> *elements;
6485 struct spelling *spelling;
6486 struct spelling *spelling_base;
6487 int spelling_size;
6488 char top_level;
6489 char require_constant_value;
6490 char require_constant_elements;
6493 static struct initializer_stack *initializer_stack;
6495 /* Prepare to parse and output the initializer for variable DECL. */
6497 void
6498 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6500 const char *locus;
6501 struct initializer_stack *p = XNEW (struct initializer_stack);
6503 p->decl = constructor_decl;
6504 p->require_constant_value = require_constant_value;
6505 p->require_constant_elements = require_constant_elements;
6506 p->constructor_stack = constructor_stack;
6507 p->constructor_range_stack = constructor_range_stack;
6508 p->elements = constructor_elements;
6509 p->spelling = spelling;
6510 p->spelling_base = spelling_base;
6511 p->spelling_size = spelling_size;
6512 p->top_level = constructor_top_level;
6513 p->next = initializer_stack;
6514 initializer_stack = p;
6516 constructor_decl = decl;
6517 constructor_designated = 0;
6518 constructor_top_level = top_level;
6520 if (decl != 0 && decl != error_mark_node)
6522 require_constant_value = TREE_STATIC (decl);
6523 require_constant_elements
6524 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6525 /* For a scalar, you can always use any value to initialize,
6526 even within braces. */
6527 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6528 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6529 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6530 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6531 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6533 else
6535 require_constant_value = 0;
6536 require_constant_elements = 0;
6537 locus = _("(anonymous)");
6540 constructor_stack = 0;
6541 constructor_range_stack = 0;
6543 missing_braces_mentioned = 0;
6545 spelling_base = 0;
6546 spelling_size = 0;
6547 RESTORE_SPELLING_DEPTH (0);
6549 if (locus)
6550 push_string (locus);
6553 void
6554 finish_init (void)
6556 struct initializer_stack *p = initializer_stack;
6558 /* Free the whole constructor stack of this initializer. */
6559 while (constructor_stack)
6561 struct constructor_stack *q = constructor_stack;
6562 constructor_stack = q->next;
6563 free (q);
6566 gcc_assert (!constructor_range_stack);
6568 /* Pop back to the data of the outer initializer (if any). */
6569 free (spelling_base);
6571 constructor_decl = p->decl;
6572 require_constant_value = p->require_constant_value;
6573 require_constant_elements = p->require_constant_elements;
6574 constructor_stack = p->constructor_stack;
6575 constructor_range_stack = p->constructor_range_stack;
6576 constructor_elements = p->elements;
6577 spelling = p->spelling;
6578 spelling_base = p->spelling_base;
6579 spelling_size = p->spelling_size;
6580 constructor_top_level = p->top_level;
6581 initializer_stack = p->next;
6582 free (p);
6585 /* Call here when we see the initializer is surrounded by braces.
6586 This is instead of a call to push_init_level;
6587 it is matched by a call to pop_init_level.
6589 TYPE is the type to initialize, for a constructor expression.
6590 For an initializer for a decl, TYPE is zero. */
6592 void
6593 really_start_incremental_init (tree type)
6595 struct constructor_stack *p = XNEW (struct constructor_stack);
6597 if (type == 0)
6598 type = TREE_TYPE (constructor_decl);
6600 if (TREE_CODE (type) == VECTOR_TYPE
6601 && TYPE_VECTOR_OPAQUE (type))
6602 error ("opaque vector types cannot be initialized");
6604 p->type = constructor_type;
6605 p->fields = constructor_fields;
6606 p->index = constructor_index;
6607 p->max_index = constructor_max_index;
6608 p->unfilled_index = constructor_unfilled_index;
6609 p->unfilled_fields = constructor_unfilled_fields;
6610 p->bit_index = constructor_bit_index;
6611 p->elements = constructor_elements;
6612 p->constant = constructor_constant;
6613 p->simple = constructor_simple;
6614 p->nonconst = constructor_nonconst;
6615 p->erroneous = constructor_erroneous;
6616 p->pending_elts = constructor_pending_elts;
6617 p->depth = constructor_depth;
6618 p->replacement_value.value = 0;
6619 p->replacement_value.original_code = ERROR_MARK;
6620 p->replacement_value.original_type = NULL;
6621 p->implicit = 0;
6622 p->range_stack = 0;
6623 p->outer = 0;
6624 p->incremental = constructor_incremental;
6625 p->designated = constructor_designated;
6626 p->next = 0;
6627 constructor_stack = p;
6629 constructor_constant = 1;
6630 constructor_simple = 1;
6631 constructor_nonconst = 0;
6632 constructor_depth = SPELLING_DEPTH ();
6633 constructor_elements = NULL;
6634 constructor_pending_elts = 0;
6635 constructor_type = type;
6636 constructor_incremental = 1;
6637 constructor_designated = 0;
6638 designator_depth = 0;
6639 designator_erroneous = 0;
6641 if (TREE_CODE (constructor_type) == RECORD_TYPE
6642 || TREE_CODE (constructor_type) == UNION_TYPE)
6644 constructor_fields = TYPE_FIELDS (constructor_type);
6645 /* Skip any nameless bit fields at the beginning. */
6646 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6647 && DECL_NAME (constructor_fields) == 0)
6648 constructor_fields = DECL_CHAIN (constructor_fields);
6650 constructor_unfilled_fields = constructor_fields;
6651 constructor_bit_index = bitsize_zero_node;
6653 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6655 if (TYPE_DOMAIN (constructor_type))
6657 constructor_max_index
6658 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6660 /* Detect non-empty initializations of zero-length arrays. */
6661 if (constructor_max_index == NULL_TREE
6662 && TYPE_SIZE (constructor_type))
6663 constructor_max_index = integer_minus_one_node;
6665 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6666 to initialize VLAs will cause a proper error; avoid tree
6667 checking errors as well by setting a safe value. */
6668 if (constructor_max_index
6669 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6670 constructor_max_index = integer_minus_one_node;
6672 constructor_index
6673 = convert (bitsizetype,
6674 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6676 else
6678 constructor_index = bitsize_zero_node;
6679 constructor_max_index = NULL_TREE;
6682 constructor_unfilled_index = constructor_index;
6684 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6686 /* Vectors are like simple fixed-size arrays. */
6687 constructor_max_index =
6688 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6689 constructor_index = bitsize_zero_node;
6690 constructor_unfilled_index = constructor_index;
6692 else
6694 /* Handle the case of int x = {5}; */
6695 constructor_fields = constructor_type;
6696 constructor_unfilled_fields = constructor_type;
6700 /* Push down into a subobject, for initialization.
6701 If this is for an explicit set of braces, IMPLICIT is 0.
6702 If it is because the next element belongs at a lower level,
6703 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6705 void
6706 push_init_level (int implicit, struct obstack * braced_init_obstack)
6708 struct constructor_stack *p;
6709 tree value = NULL_TREE;
6711 /* If we've exhausted any levels that didn't have braces,
6712 pop them now. If implicit == 1, this will have been done in
6713 process_init_element; do not repeat it here because in the case
6714 of excess initializers for an empty aggregate this leads to an
6715 infinite cycle of popping a level and immediately recreating
6716 it. */
6717 if (implicit != 1)
6719 while (constructor_stack->implicit)
6721 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6722 || TREE_CODE (constructor_type) == UNION_TYPE)
6723 && constructor_fields == 0)
6724 process_init_element (pop_init_level (1, braced_init_obstack),
6725 true, braced_init_obstack);
6726 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6727 && constructor_max_index
6728 && tree_int_cst_lt (constructor_max_index,
6729 constructor_index))
6730 process_init_element (pop_init_level (1, braced_init_obstack),
6731 true, braced_init_obstack);
6732 else
6733 break;
6737 /* Unless this is an explicit brace, we need to preserve previous
6738 content if any. */
6739 if (implicit)
6741 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6742 || TREE_CODE (constructor_type) == UNION_TYPE)
6743 && constructor_fields)
6744 value = find_init_member (constructor_fields, braced_init_obstack);
6745 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6746 value = find_init_member (constructor_index, braced_init_obstack);
6749 p = XNEW (struct constructor_stack);
6750 p->type = constructor_type;
6751 p->fields = constructor_fields;
6752 p->index = constructor_index;
6753 p->max_index = constructor_max_index;
6754 p->unfilled_index = constructor_unfilled_index;
6755 p->unfilled_fields = constructor_unfilled_fields;
6756 p->bit_index = constructor_bit_index;
6757 p->elements = constructor_elements;
6758 p->constant = constructor_constant;
6759 p->simple = constructor_simple;
6760 p->nonconst = constructor_nonconst;
6761 p->erroneous = constructor_erroneous;
6762 p->pending_elts = constructor_pending_elts;
6763 p->depth = constructor_depth;
6764 p->replacement_value.value = 0;
6765 p->replacement_value.original_code = ERROR_MARK;
6766 p->replacement_value.original_type = NULL;
6767 p->implicit = implicit;
6768 p->outer = 0;
6769 p->incremental = constructor_incremental;
6770 p->designated = constructor_designated;
6771 p->next = constructor_stack;
6772 p->range_stack = 0;
6773 constructor_stack = p;
6775 constructor_constant = 1;
6776 constructor_simple = 1;
6777 constructor_nonconst = 0;
6778 constructor_depth = SPELLING_DEPTH ();
6779 constructor_elements = NULL;
6780 constructor_incremental = 1;
6781 constructor_designated = 0;
6782 constructor_pending_elts = 0;
6783 if (!implicit)
6785 p->range_stack = constructor_range_stack;
6786 constructor_range_stack = 0;
6787 designator_depth = 0;
6788 designator_erroneous = 0;
6791 /* Don't die if an entire brace-pair level is superfluous
6792 in the containing level. */
6793 if (constructor_type == 0)
6795 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6796 || TREE_CODE (constructor_type) == UNION_TYPE)
6798 /* Don't die if there are extra init elts at the end. */
6799 if (constructor_fields == 0)
6800 constructor_type = 0;
6801 else
6803 constructor_type = TREE_TYPE (constructor_fields);
6804 push_member_name (constructor_fields);
6805 constructor_depth++;
6808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6810 constructor_type = TREE_TYPE (constructor_type);
6811 push_array_bounds (tree_low_cst (constructor_index, 1));
6812 constructor_depth++;
6815 if (constructor_type == 0)
6817 error_init ("extra brace group at end of initializer");
6818 constructor_fields = 0;
6819 constructor_unfilled_fields = 0;
6820 return;
6823 if (value && TREE_CODE (value) == CONSTRUCTOR)
6825 constructor_constant = TREE_CONSTANT (value);
6826 constructor_simple = TREE_STATIC (value);
6827 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6828 constructor_elements = CONSTRUCTOR_ELTS (value);
6829 if (!vec_safe_is_empty (constructor_elements)
6830 && (TREE_CODE (constructor_type) == RECORD_TYPE
6831 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6832 set_nonincremental_init (braced_init_obstack);
6835 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6837 missing_braces_mentioned = 1;
6838 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6841 if (TREE_CODE (constructor_type) == RECORD_TYPE
6842 || TREE_CODE (constructor_type) == UNION_TYPE)
6844 constructor_fields = TYPE_FIELDS (constructor_type);
6845 /* Skip any nameless bit fields at the beginning. */
6846 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6847 && DECL_NAME (constructor_fields) == 0)
6848 constructor_fields = DECL_CHAIN (constructor_fields);
6850 constructor_unfilled_fields = constructor_fields;
6851 constructor_bit_index = bitsize_zero_node;
6853 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6855 /* Vectors are like simple fixed-size arrays. */
6856 constructor_max_index =
6857 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6858 constructor_index = bitsize_int (0);
6859 constructor_unfilled_index = constructor_index;
6861 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6863 if (TYPE_DOMAIN (constructor_type))
6865 constructor_max_index
6866 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6868 /* Detect non-empty initializations of zero-length arrays. */
6869 if (constructor_max_index == NULL_TREE
6870 && TYPE_SIZE (constructor_type))
6871 constructor_max_index = integer_minus_one_node;
6873 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6874 to initialize VLAs will cause a proper error; avoid tree
6875 checking errors as well by setting a safe value. */
6876 if (constructor_max_index
6877 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6878 constructor_max_index = integer_minus_one_node;
6880 constructor_index
6881 = convert (bitsizetype,
6882 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6884 else
6885 constructor_index = bitsize_zero_node;
6887 constructor_unfilled_index = constructor_index;
6888 if (value && TREE_CODE (value) == STRING_CST)
6890 /* We need to split the char/wchar array into individual
6891 characters, so that we don't have to special case it
6892 everywhere. */
6893 set_nonincremental_init_from_string (value, braced_init_obstack);
6896 else
6898 if (constructor_type != error_mark_node)
6899 warning_init (0, "braces around scalar initializer");
6900 constructor_fields = constructor_type;
6901 constructor_unfilled_fields = constructor_type;
6905 /* At the end of an implicit or explicit brace level,
6906 finish up that level of constructor. If a single expression
6907 with redundant braces initialized that level, return the
6908 c_expr structure for that expression. Otherwise, the original_code
6909 element is set to ERROR_MARK.
6910 If we were outputting the elements as they are read, return 0 as the value
6911 from inner levels (process_init_element ignores that),
6912 but return error_mark_node as the value from the outermost level
6913 (that's what we want to put in DECL_INITIAL).
6914 Otherwise, return a CONSTRUCTOR expression as the value. */
6916 struct c_expr
6917 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6919 struct constructor_stack *p;
6920 struct c_expr ret;
6921 ret.value = 0;
6922 ret.original_code = ERROR_MARK;
6923 ret.original_type = NULL;
6925 if (implicit == 0)
6927 /* When we come to an explicit close brace,
6928 pop any inner levels that didn't have explicit braces. */
6929 while (constructor_stack->implicit)
6931 process_init_element (pop_init_level (1, braced_init_obstack),
6932 true, braced_init_obstack);
6934 gcc_assert (!constructor_range_stack);
6937 /* Now output all pending elements. */
6938 constructor_incremental = 1;
6939 output_pending_init_elements (1, braced_init_obstack);
6941 p = constructor_stack;
6943 /* Error for initializing a flexible array member, or a zero-length
6944 array member in an inappropriate context. */
6945 if (constructor_type && constructor_fields
6946 && TREE_CODE (constructor_type) == ARRAY_TYPE
6947 && TYPE_DOMAIN (constructor_type)
6948 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6950 /* Silently discard empty initializations. The parser will
6951 already have pedwarned for empty brackets. */
6952 if (integer_zerop (constructor_unfilled_index))
6953 constructor_type = NULL_TREE;
6954 else
6956 gcc_assert (!TYPE_SIZE (constructor_type));
6958 if (constructor_depth > 2)
6959 error_init ("initialization of flexible array member in a nested context");
6960 else
6961 pedwarn_init (input_location, OPT_Wpedantic,
6962 "initialization of a flexible array member");
6964 /* We have already issued an error message for the existence
6965 of a flexible array member not at the end of the structure.
6966 Discard the initializer so that we do not die later. */
6967 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6968 constructor_type = NULL_TREE;
6972 /* Warn when some struct elements are implicitly initialized to zero. */
6973 if (warn_missing_field_initializers
6974 && constructor_type
6975 && TREE_CODE (constructor_type) == RECORD_TYPE
6976 && constructor_unfilled_fields)
6978 bool constructor_zeroinit =
6979 (vec_safe_length (constructor_elements) == 1
6980 && integer_zerop ((*constructor_elements)[0].value));
6982 /* Do not warn for flexible array members or zero-length arrays. */
6983 while (constructor_unfilled_fields
6984 && (!DECL_SIZE (constructor_unfilled_fields)
6985 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6986 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6988 if (constructor_unfilled_fields
6989 /* Do not warn if this level of the initializer uses member
6990 designators; it is likely to be deliberate. */
6991 && !constructor_designated
6992 /* Do not warn about initializing with ` = {0}'. */
6993 && !constructor_zeroinit)
6995 if (warning_at (input_location, OPT_Wmissing_field_initializers,
6996 "missing initializer for field %qD of %qT",
6997 constructor_unfilled_fields,
6998 constructor_type))
6999 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7000 "%qD declared here", constructor_unfilled_fields);
7004 /* Pad out the end of the structure. */
7005 if (p->replacement_value.value)
7006 /* If this closes a superfluous brace pair,
7007 just pass out the element between them. */
7008 ret = p->replacement_value;
7009 else if (constructor_type == 0)
7011 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7012 && TREE_CODE (constructor_type) != UNION_TYPE
7013 && TREE_CODE (constructor_type) != ARRAY_TYPE
7014 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7016 /* A nonincremental scalar initializer--just return
7017 the element, after verifying there is just one. */
7018 if (vec_safe_is_empty (constructor_elements))
7020 if (!constructor_erroneous)
7021 error_init ("empty scalar initializer");
7022 ret.value = error_mark_node;
7024 else if (vec_safe_length (constructor_elements) != 1)
7026 error_init ("extra elements in scalar initializer");
7027 ret.value = (*constructor_elements)[0].value;
7029 else
7030 ret.value = (*constructor_elements)[0].value;
7032 else
7034 if (constructor_erroneous)
7035 ret.value = error_mark_node;
7036 else
7038 ret.value = build_constructor (constructor_type,
7039 constructor_elements);
7040 if (constructor_constant)
7041 TREE_CONSTANT (ret.value) = 1;
7042 if (constructor_constant && constructor_simple)
7043 TREE_STATIC (ret.value) = 1;
7044 if (constructor_nonconst)
7045 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7049 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7051 if (constructor_nonconst)
7052 ret.original_code = C_MAYBE_CONST_EXPR;
7053 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7054 ret.original_code = ERROR_MARK;
7057 constructor_type = p->type;
7058 constructor_fields = p->fields;
7059 constructor_index = p->index;
7060 constructor_max_index = p->max_index;
7061 constructor_unfilled_index = p->unfilled_index;
7062 constructor_unfilled_fields = p->unfilled_fields;
7063 constructor_bit_index = p->bit_index;
7064 constructor_elements = p->elements;
7065 constructor_constant = p->constant;
7066 constructor_simple = p->simple;
7067 constructor_nonconst = p->nonconst;
7068 constructor_erroneous = p->erroneous;
7069 constructor_incremental = p->incremental;
7070 constructor_designated = p->designated;
7071 constructor_pending_elts = p->pending_elts;
7072 constructor_depth = p->depth;
7073 if (!p->implicit)
7074 constructor_range_stack = p->range_stack;
7075 RESTORE_SPELLING_DEPTH (constructor_depth);
7077 constructor_stack = p->next;
7078 free (p);
7080 if (ret.value == 0 && constructor_stack == 0)
7081 ret.value = error_mark_node;
7082 return ret;
7085 /* Common handling for both array range and field name designators.
7086 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7088 static int
7089 set_designator (int array, struct obstack * braced_init_obstack)
7091 tree subtype;
7092 enum tree_code subcode;
7094 /* Don't die if an entire brace-pair level is superfluous
7095 in the containing level. */
7096 if (constructor_type == 0)
7097 return 1;
7099 /* If there were errors in this designator list already, bail out
7100 silently. */
7101 if (designator_erroneous)
7102 return 1;
7104 if (!designator_depth)
7106 gcc_assert (!constructor_range_stack);
7108 /* Designator list starts at the level of closest explicit
7109 braces. */
7110 while (constructor_stack->implicit)
7112 process_init_element (pop_init_level (1, braced_init_obstack),
7113 true, braced_init_obstack);
7115 constructor_designated = 1;
7116 return 0;
7119 switch (TREE_CODE (constructor_type))
7121 case RECORD_TYPE:
7122 case UNION_TYPE:
7123 subtype = TREE_TYPE (constructor_fields);
7124 if (subtype != error_mark_node)
7125 subtype = TYPE_MAIN_VARIANT (subtype);
7126 break;
7127 case ARRAY_TYPE:
7128 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7129 break;
7130 default:
7131 gcc_unreachable ();
7134 subcode = TREE_CODE (subtype);
7135 if (array && subcode != ARRAY_TYPE)
7137 error_init ("array index in non-array initializer");
7138 return 1;
7140 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7142 error_init ("field name not in record or union initializer");
7143 return 1;
7146 constructor_designated = 1;
7147 push_init_level (2, braced_init_obstack);
7148 return 0;
7151 /* If there are range designators in designator list, push a new designator
7152 to constructor_range_stack. RANGE_END is end of such stack range or
7153 NULL_TREE if there is no range designator at this level. */
7155 static void
7156 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7158 struct constructor_range_stack *p;
7160 p = (struct constructor_range_stack *)
7161 obstack_alloc (braced_init_obstack,
7162 sizeof (struct constructor_range_stack));
7163 p->prev = constructor_range_stack;
7164 p->next = 0;
7165 p->fields = constructor_fields;
7166 p->range_start = constructor_index;
7167 p->index = constructor_index;
7168 p->stack = constructor_stack;
7169 p->range_end = range_end;
7170 if (constructor_range_stack)
7171 constructor_range_stack->next = p;
7172 constructor_range_stack = p;
7175 /* Within an array initializer, specify the next index to be initialized.
7176 FIRST is that index. If LAST is nonzero, then initialize a range
7177 of indices, running from FIRST through LAST. */
7179 void
7180 set_init_index (tree first, tree last,
7181 struct obstack * braced_init_obstack)
7183 if (set_designator (1, braced_init_obstack))
7184 return;
7186 designator_erroneous = 1;
7188 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7189 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7191 error_init ("array index in initializer not of integer type");
7192 return;
7195 if (TREE_CODE (first) != INTEGER_CST)
7197 first = c_fully_fold (first, false, NULL);
7198 if (TREE_CODE (first) == INTEGER_CST)
7199 pedwarn_init (input_location, OPT_Wpedantic,
7200 "array index in initializer is not "
7201 "an integer constant expression");
7204 if (last && TREE_CODE (last) != INTEGER_CST)
7206 last = c_fully_fold (last, false, NULL);
7207 if (TREE_CODE (last) == INTEGER_CST)
7208 pedwarn_init (input_location, OPT_Wpedantic,
7209 "array index in initializer is not "
7210 "an integer constant expression");
7213 if (TREE_CODE (first) != INTEGER_CST)
7214 error_init ("nonconstant array index in initializer");
7215 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7216 error_init ("nonconstant array index in initializer");
7217 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7218 error_init ("array index in non-array initializer");
7219 else if (tree_int_cst_sgn (first) == -1)
7220 error_init ("array index in initializer exceeds array bounds");
7221 else if (constructor_max_index
7222 && tree_int_cst_lt (constructor_max_index, first))
7223 error_init ("array index in initializer exceeds array bounds");
7224 else
7226 constant_expression_warning (first);
7227 if (last)
7228 constant_expression_warning (last);
7229 constructor_index = convert (bitsizetype, first);
7230 if (tree_int_cst_lt (constructor_index, first))
7232 constructor_index = copy_node (constructor_index);
7233 TREE_OVERFLOW (constructor_index) = 1;
7236 if (last)
7238 if (tree_int_cst_equal (first, last))
7239 last = 0;
7240 else if (tree_int_cst_lt (last, first))
7242 error_init ("empty index range in initializer");
7243 last = 0;
7245 else
7247 last = convert (bitsizetype, last);
7248 if (constructor_max_index != 0
7249 && tree_int_cst_lt (constructor_max_index, last))
7251 error_init ("array index range in initializer exceeds array bounds");
7252 last = 0;
7257 designator_depth++;
7258 designator_erroneous = 0;
7259 if (constructor_range_stack || last)
7260 push_range_stack (last, braced_init_obstack);
7264 /* Within a struct initializer, specify the next field to be initialized. */
7266 void
7267 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7269 tree field;
7271 if (set_designator (0, braced_init_obstack))
7272 return;
7274 designator_erroneous = 1;
7276 if (TREE_CODE (constructor_type) != RECORD_TYPE
7277 && TREE_CODE (constructor_type) != UNION_TYPE)
7279 error_init ("field name not in record or union initializer");
7280 return;
7283 field = lookup_field (constructor_type, fieldname);
7285 if (field == 0)
7286 error ("unknown field %qE specified in initializer", fieldname);
7287 else
7290 constructor_fields = TREE_VALUE (field);
7291 designator_depth++;
7292 designator_erroneous = 0;
7293 if (constructor_range_stack)
7294 push_range_stack (NULL_TREE, braced_init_obstack);
7295 field = TREE_CHAIN (field);
7296 if (field)
7298 if (set_designator (0, braced_init_obstack))
7299 return;
7302 while (field != NULL_TREE);
7305 /* Add a new initializer to the tree of pending initializers. PURPOSE
7306 identifies the initializer, either array index or field in a structure.
7307 VALUE is the value of that index or field. If ORIGTYPE is not
7308 NULL_TREE, it is the original type of VALUE.
7310 IMPLICIT is true if value comes from pop_init_level (1),
7311 the new initializer has been merged with the existing one
7312 and thus no warnings should be emitted about overriding an
7313 existing initializer. */
7315 static void
7316 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7317 struct obstack * braced_init_obstack)
7319 struct init_node *p, **q, *r;
7321 q = &constructor_pending_elts;
7322 p = 0;
7324 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7326 while (*q != 0)
7328 p = *q;
7329 if (tree_int_cst_lt (purpose, p->purpose))
7330 q = &p->left;
7331 else if (tree_int_cst_lt (p->purpose, purpose))
7332 q = &p->right;
7333 else
7335 if (!implicit)
7337 if (TREE_SIDE_EFFECTS (p->value))
7338 warning_init (0, "initialized field with side-effects overwritten");
7339 else if (warn_override_init)
7340 warning_init (OPT_Woverride_init, "initialized field overwritten");
7342 p->value = value;
7343 p->origtype = origtype;
7344 return;
7348 else
7350 tree bitpos;
7352 bitpos = bit_position (purpose);
7353 while (*q != NULL)
7355 p = *q;
7356 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7357 q = &p->left;
7358 else if (p->purpose != purpose)
7359 q = &p->right;
7360 else
7362 if (!implicit)
7364 if (TREE_SIDE_EFFECTS (p->value))
7365 warning_init (0, "initialized field with side-effects overwritten");
7366 else if (warn_override_init)
7367 warning_init (OPT_Woverride_init, "initialized field overwritten");
7369 p->value = value;
7370 p->origtype = origtype;
7371 return;
7376 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7377 sizeof (struct init_node));
7378 r->purpose = purpose;
7379 r->value = value;
7380 r->origtype = origtype;
7382 *q = r;
7383 r->parent = p;
7384 r->left = 0;
7385 r->right = 0;
7386 r->balance = 0;
7388 while (p)
7390 struct init_node *s;
7392 if (r == p->left)
7394 if (p->balance == 0)
7395 p->balance = -1;
7396 else if (p->balance < 0)
7398 if (r->balance < 0)
7400 /* L rotation. */
7401 p->left = r->right;
7402 if (p->left)
7403 p->left->parent = p;
7404 r->right = p;
7406 p->balance = 0;
7407 r->balance = 0;
7409 s = p->parent;
7410 p->parent = r;
7411 r->parent = s;
7412 if (s)
7414 if (s->left == p)
7415 s->left = r;
7416 else
7417 s->right = r;
7419 else
7420 constructor_pending_elts = r;
7422 else
7424 /* LR rotation. */
7425 struct init_node *t = r->right;
7427 r->right = t->left;
7428 if (r->right)
7429 r->right->parent = r;
7430 t->left = r;
7432 p->left = t->right;
7433 if (p->left)
7434 p->left->parent = p;
7435 t->right = p;
7437 p->balance = t->balance < 0;
7438 r->balance = -(t->balance > 0);
7439 t->balance = 0;
7441 s = p->parent;
7442 p->parent = t;
7443 r->parent = t;
7444 t->parent = s;
7445 if (s)
7447 if (s->left == p)
7448 s->left = t;
7449 else
7450 s->right = t;
7452 else
7453 constructor_pending_elts = t;
7455 break;
7457 else
7459 /* p->balance == +1; growth of left side balances the node. */
7460 p->balance = 0;
7461 break;
7464 else /* r == p->right */
7466 if (p->balance == 0)
7467 /* Growth propagation from right side. */
7468 p->balance++;
7469 else if (p->balance > 0)
7471 if (r->balance > 0)
7473 /* R rotation. */
7474 p->right = r->left;
7475 if (p->right)
7476 p->right->parent = p;
7477 r->left = p;
7479 p->balance = 0;
7480 r->balance = 0;
7482 s = p->parent;
7483 p->parent = r;
7484 r->parent = s;
7485 if (s)
7487 if (s->left == p)
7488 s->left = r;
7489 else
7490 s->right = r;
7492 else
7493 constructor_pending_elts = r;
7495 else /* r->balance == -1 */
7497 /* RL rotation */
7498 struct init_node *t = r->left;
7500 r->left = t->right;
7501 if (r->left)
7502 r->left->parent = r;
7503 t->right = r;
7505 p->right = t->left;
7506 if (p->right)
7507 p->right->parent = p;
7508 t->left = p;
7510 r->balance = (t->balance < 0);
7511 p->balance = -(t->balance > 0);
7512 t->balance = 0;
7514 s = p->parent;
7515 p->parent = t;
7516 r->parent = t;
7517 t->parent = s;
7518 if (s)
7520 if (s->left == p)
7521 s->left = t;
7522 else
7523 s->right = t;
7525 else
7526 constructor_pending_elts = t;
7528 break;
7530 else
7532 /* p->balance == -1; growth of right side balances the node. */
7533 p->balance = 0;
7534 break;
7538 r = p;
7539 p = p->parent;
7543 /* Build AVL tree from a sorted chain. */
7545 static void
7546 set_nonincremental_init (struct obstack * braced_init_obstack)
7548 unsigned HOST_WIDE_INT ix;
7549 tree index, value;
7551 if (TREE_CODE (constructor_type) != RECORD_TYPE
7552 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7553 return;
7555 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7557 add_pending_init (index, value, NULL_TREE, true,
7558 braced_init_obstack);
7560 constructor_elements = NULL;
7561 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7563 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7564 /* Skip any nameless bit fields at the beginning. */
7565 while (constructor_unfilled_fields != 0
7566 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7567 && DECL_NAME (constructor_unfilled_fields) == 0)
7568 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7571 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7573 if (TYPE_DOMAIN (constructor_type))
7574 constructor_unfilled_index
7575 = convert (bitsizetype,
7576 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7577 else
7578 constructor_unfilled_index = bitsize_zero_node;
7580 constructor_incremental = 0;
7583 /* Build AVL tree from a string constant. */
7585 static void
7586 set_nonincremental_init_from_string (tree str,
7587 struct obstack * braced_init_obstack)
7589 tree value, purpose, type;
7590 HOST_WIDE_INT val[2];
7591 const char *p, *end;
7592 int byte, wchar_bytes, charwidth, bitpos;
7594 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7596 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7597 charwidth = TYPE_PRECISION (char_type_node);
7598 type = TREE_TYPE (constructor_type);
7599 p = TREE_STRING_POINTER (str);
7600 end = p + TREE_STRING_LENGTH (str);
7602 for (purpose = bitsize_zero_node;
7603 p < end
7604 && !(constructor_max_index
7605 && tree_int_cst_lt (constructor_max_index, purpose));
7606 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7608 if (wchar_bytes == 1)
7610 val[1] = (unsigned char) *p++;
7611 val[0] = 0;
7613 else
7615 val[0] = 0;
7616 val[1] = 0;
7617 for (byte = 0; byte < wchar_bytes; byte++)
7619 if (BYTES_BIG_ENDIAN)
7620 bitpos = (wchar_bytes - byte - 1) * charwidth;
7621 else
7622 bitpos = byte * charwidth;
7623 val[bitpos < HOST_BITS_PER_WIDE_INT]
7624 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7625 << (bitpos % HOST_BITS_PER_WIDE_INT);
7629 if (!TYPE_UNSIGNED (type))
7631 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7632 if (bitpos < HOST_BITS_PER_WIDE_INT)
7634 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7636 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7637 val[0] = -1;
7640 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7642 if (val[1] < 0)
7643 val[0] = -1;
7645 else if (val[0] & (((HOST_WIDE_INT) 1)
7646 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7647 val[0] |= ((HOST_WIDE_INT) -1)
7648 << (bitpos - HOST_BITS_PER_WIDE_INT);
7651 value = build_int_cst_wide (type, val[1], val[0]);
7652 add_pending_init (purpose, value, NULL_TREE, true,
7653 braced_init_obstack);
7656 constructor_incremental = 0;
7659 /* Return value of FIELD in pending initializer or zero if the field was
7660 not initialized yet. */
7662 static tree
7663 find_init_member (tree field, struct obstack * braced_init_obstack)
7665 struct init_node *p;
7667 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7669 if (constructor_incremental
7670 && tree_int_cst_lt (field, constructor_unfilled_index))
7671 set_nonincremental_init (braced_init_obstack);
7673 p = constructor_pending_elts;
7674 while (p)
7676 if (tree_int_cst_lt (field, p->purpose))
7677 p = p->left;
7678 else if (tree_int_cst_lt (p->purpose, field))
7679 p = p->right;
7680 else
7681 return p->value;
7684 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7686 tree bitpos = bit_position (field);
7688 if (constructor_incremental
7689 && (!constructor_unfilled_fields
7690 || tree_int_cst_lt (bitpos,
7691 bit_position (constructor_unfilled_fields))))
7692 set_nonincremental_init (braced_init_obstack);
7694 p = constructor_pending_elts;
7695 while (p)
7697 if (field == p->purpose)
7698 return p->value;
7699 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7700 p = p->left;
7701 else
7702 p = p->right;
7705 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7707 if (!vec_safe_is_empty (constructor_elements)
7708 && (constructor_elements->last ().index == field))
7709 return constructor_elements->last ().value;
7711 return 0;
7714 /* "Output" the next constructor element.
7715 At top level, really output it to assembler code now.
7716 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7717 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7718 TYPE is the data type that the containing data type wants here.
7719 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7720 If VALUE is a string constant, STRICT_STRING is true if it is
7721 unparenthesized or we should not warn here for it being parenthesized.
7722 For other types of VALUE, STRICT_STRING is not used.
7724 PENDING if non-nil means output pending elements that belong
7725 right after this element. (PENDING is normally 1;
7726 it is 0 while outputting pending elements, to avoid recursion.)
7728 IMPLICIT is true if value comes from pop_init_level (1),
7729 the new initializer has been merged with the existing one
7730 and thus no warnings should be emitted about overriding an
7731 existing initializer. */
7733 static void
7734 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7735 tree field, int pending, bool implicit,
7736 struct obstack * braced_init_obstack)
7738 tree semantic_type = NULL_TREE;
7739 bool maybe_const = true;
7740 bool npc;
7742 if (type == error_mark_node || value == error_mark_node)
7744 constructor_erroneous = 1;
7745 return;
7747 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7748 && (TREE_CODE (value) == STRING_CST
7749 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7750 && !(TREE_CODE (value) == STRING_CST
7751 && TREE_CODE (type) == ARRAY_TYPE
7752 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7753 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7754 TYPE_MAIN_VARIANT (type)))
7755 value = array_to_pointer_conversion (input_location, value);
7757 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7758 && require_constant_value && !flag_isoc99 && pending)
7760 /* As an extension, allow initializing objects with static storage
7761 duration with compound literals (which are then treated just as
7762 the brace enclosed list they contain). */
7763 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7764 value = DECL_INITIAL (decl);
7767 npc = null_pointer_constant_p (value);
7768 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7770 semantic_type = TREE_TYPE (value);
7771 value = TREE_OPERAND (value, 0);
7773 value = c_fully_fold (value, require_constant_value, &maybe_const);
7775 if (value == error_mark_node)
7776 constructor_erroneous = 1;
7777 else if (!TREE_CONSTANT (value))
7778 constructor_constant = 0;
7779 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7780 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7781 || TREE_CODE (constructor_type) == UNION_TYPE)
7782 && DECL_C_BIT_FIELD (field)
7783 && TREE_CODE (value) != INTEGER_CST))
7784 constructor_simple = 0;
7785 if (!maybe_const)
7786 constructor_nonconst = 1;
7788 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7790 if (require_constant_value)
7792 error_init ("initializer element is not constant");
7793 value = error_mark_node;
7795 else if (require_constant_elements)
7796 pedwarn (input_location, 0,
7797 "initializer element is not computable at load time");
7799 else if (!maybe_const
7800 && (require_constant_value || require_constant_elements))
7801 pedwarn_init (input_location, 0,
7802 "initializer element is not a constant expression");
7804 /* Issue -Wc++-compat warnings about initializing a bitfield with
7805 enum type. */
7806 if (warn_cxx_compat
7807 && field != NULL_TREE
7808 && TREE_CODE (field) == FIELD_DECL
7809 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7810 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7811 != TYPE_MAIN_VARIANT (type))
7812 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7814 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7815 if (checktype != error_mark_node
7816 && (TYPE_MAIN_VARIANT (checktype)
7817 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7818 warning_init (OPT_Wc___compat,
7819 "enum conversion in initialization is invalid in C++");
7822 /* If this field is empty (and not at the end of structure),
7823 don't do anything other than checking the initializer. */
7824 if (field
7825 && (TREE_TYPE (field) == error_mark_node
7826 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7827 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7828 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7829 || DECL_CHAIN (field)))))
7830 return;
7832 if (semantic_type)
7833 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7834 value = digest_init (input_location, type, value, origtype, npc,
7835 strict_string, require_constant_value);
7836 if (value == error_mark_node)
7838 constructor_erroneous = 1;
7839 return;
7841 if (require_constant_value || require_constant_elements)
7842 constant_expression_warning (value);
7844 /* If this element doesn't come next in sequence,
7845 put it on constructor_pending_elts. */
7846 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7847 && (!constructor_incremental
7848 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7850 if (constructor_incremental
7851 && tree_int_cst_lt (field, constructor_unfilled_index))
7852 set_nonincremental_init (braced_init_obstack);
7854 add_pending_init (field, value, origtype, implicit,
7855 braced_init_obstack);
7856 return;
7858 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7859 && (!constructor_incremental
7860 || field != constructor_unfilled_fields))
7862 /* We do this for records but not for unions. In a union,
7863 no matter which field is specified, it can be initialized
7864 right away since it starts at the beginning of the union. */
7865 if (constructor_incremental)
7867 if (!constructor_unfilled_fields)
7868 set_nonincremental_init (braced_init_obstack);
7869 else
7871 tree bitpos, unfillpos;
7873 bitpos = bit_position (field);
7874 unfillpos = bit_position (constructor_unfilled_fields);
7876 if (tree_int_cst_lt (bitpos, unfillpos))
7877 set_nonincremental_init (braced_init_obstack);
7881 add_pending_init (field, value, origtype, implicit,
7882 braced_init_obstack);
7883 return;
7885 else if (TREE_CODE (constructor_type) == UNION_TYPE
7886 && !vec_safe_is_empty (constructor_elements))
7888 if (!implicit)
7890 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
7891 warning_init (0,
7892 "initialized field with side-effects overwritten");
7893 else if (warn_override_init)
7894 warning_init (OPT_Woverride_init, "initialized field overwritten");
7897 /* We can have just one union field set. */
7898 constructor_elements = NULL;
7901 /* Otherwise, output this element either to
7902 constructor_elements or to the assembler file. */
7904 constructor_elt celt = {field, value};
7905 vec_safe_push (constructor_elements, celt);
7907 /* Advance the variable that indicates sequential elements output. */
7908 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7909 constructor_unfilled_index
7910 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7911 bitsize_one_node);
7912 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7914 constructor_unfilled_fields
7915 = DECL_CHAIN (constructor_unfilled_fields);
7917 /* Skip any nameless bit fields. */
7918 while (constructor_unfilled_fields != 0
7919 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7920 && DECL_NAME (constructor_unfilled_fields) == 0)
7921 constructor_unfilled_fields =
7922 DECL_CHAIN (constructor_unfilled_fields);
7924 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7925 constructor_unfilled_fields = 0;
7927 /* Now output any pending elements which have become next. */
7928 if (pending)
7929 output_pending_init_elements (0, braced_init_obstack);
7932 /* Output any pending elements which have become next.
7933 As we output elements, constructor_unfilled_{fields,index}
7934 advances, which may cause other elements to become next;
7935 if so, they too are output.
7937 If ALL is 0, we return when there are
7938 no more pending elements to output now.
7940 If ALL is 1, we output space as necessary so that
7941 we can output all the pending elements. */
7942 static void
7943 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7945 struct init_node *elt = constructor_pending_elts;
7946 tree next;
7948 retry:
7950 /* Look through the whole pending tree.
7951 If we find an element that should be output now,
7952 output it. Otherwise, set NEXT to the element
7953 that comes first among those still pending. */
7955 next = 0;
7956 while (elt)
7958 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7960 if (tree_int_cst_equal (elt->purpose,
7961 constructor_unfilled_index))
7962 output_init_element (elt->value, elt->origtype, true,
7963 TREE_TYPE (constructor_type),
7964 constructor_unfilled_index, 0, false,
7965 braced_init_obstack);
7966 else if (tree_int_cst_lt (constructor_unfilled_index,
7967 elt->purpose))
7969 /* Advance to the next smaller node. */
7970 if (elt->left)
7971 elt = elt->left;
7972 else
7974 /* We have reached the smallest node bigger than the
7975 current unfilled index. Fill the space first. */
7976 next = elt->purpose;
7977 break;
7980 else
7982 /* Advance to the next bigger node. */
7983 if (elt->right)
7984 elt = elt->right;
7985 else
7987 /* We have reached the biggest node in a subtree. Find
7988 the parent of it, which is the next bigger node. */
7989 while (elt->parent && elt->parent->right == elt)
7990 elt = elt->parent;
7991 elt = elt->parent;
7992 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7993 elt->purpose))
7995 next = elt->purpose;
7996 break;
8001 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8002 || TREE_CODE (constructor_type) == UNION_TYPE)
8004 tree ctor_unfilled_bitpos, elt_bitpos;
8006 /* If the current record is complete we are done. */
8007 if (constructor_unfilled_fields == 0)
8008 break;
8010 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8011 elt_bitpos = bit_position (elt->purpose);
8012 /* We can't compare fields here because there might be empty
8013 fields in between. */
8014 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8016 constructor_unfilled_fields = elt->purpose;
8017 output_init_element (elt->value, elt->origtype, true,
8018 TREE_TYPE (elt->purpose),
8019 elt->purpose, 0, false,
8020 braced_init_obstack);
8022 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8024 /* Advance to the next smaller node. */
8025 if (elt->left)
8026 elt = elt->left;
8027 else
8029 /* We have reached the smallest node bigger than the
8030 current unfilled field. Fill the space first. */
8031 next = elt->purpose;
8032 break;
8035 else
8037 /* Advance to the next bigger node. */
8038 if (elt->right)
8039 elt = elt->right;
8040 else
8042 /* We have reached the biggest node in a subtree. Find
8043 the parent of it, which is the next bigger node. */
8044 while (elt->parent && elt->parent->right == elt)
8045 elt = elt->parent;
8046 elt = elt->parent;
8047 if (elt
8048 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8049 bit_position (elt->purpose))))
8051 next = elt->purpose;
8052 break;
8059 /* Ordinarily return, but not if we want to output all
8060 and there are elements left. */
8061 if (!(all && next != 0))
8062 return;
8064 /* If it's not incremental, just skip over the gap, so that after
8065 jumping to retry we will output the next successive element. */
8066 if (TREE_CODE (constructor_type) == RECORD_TYPE
8067 || TREE_CODE (constructor_type) == UNION_TYPE)
8068 constructor_unfilled_fields = next;
8069 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8070 constructor_unfilled_index = next;
8072 /* ELT now points to the node in the pending tree with the next
8073 initializer to output. */
8074 goto retry;
8077 /* Add one non-braced element to the current constructor level.
8078 This adjusts the current position within the constructor's type.
8079 This may also start or terminate implicit levels
8080 to handle a partly-braced initializer.
8082 Once this has found the correct level for the new element,
8083 it calls output_init_element.
8085 IMPLICIT is true if value comes from pop_init_level (1),
8086 the new initializer has been merged with the existing one
8087 and thus no warnings should be emitted about overriding an
8088 existing initializer. */
8090 void
8091 process_init_element (struct c_expr value, bool implicit,
8092 struct obstack * braced_init_obstack)
8094 tree orig_value = value.value;
8095 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8096 bool strict_string = value.original_code == STRING_CST;
8098 designator_depth = 0;
8099 designator_erroneous = 0;
8101 /* Handle superfluous braces around string cst as in
8102 char x[] = {"foo"}; */
8103 if (string_flag
8104 && constructor_type
8105 && TREE_CODE (constructor_type) == ARRAY_TYPE
8106 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8107 && integer_zerop (constructor_unfilled_index))
8109 if (constructor_stack->replacement_value.value)
8110 error_init ("excess elements in char array initializer");
8111 constructor_stack->replacement_value = value;
8112 return;
8115 if (constructor_stack->replacement_value.value != 0)
8117 error_init ("excess elements in struct initializer");
8118 return;
8121 /* Ignore elements of a brace group if it is entirely superfluous
8122 and has already been diagnosed. */
8123 if (constructor_type == 0)
8124 return;
8126 /* If we've exhausted any levels that didn't have braces,
8127 pop them now. */
8128 while (constructor_stack->implicit)
8130 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8131 || TREE_CODE (constructor_type) == UNION_TYPE)
8132 && constructor_fields == 0)
8133 process_init_element (pop_init_level (1, braced_init_obstack),
8134 true, braced_init_obstack);
8135 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8136 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8137 && constructor_max_index
8138 && tree_int_cst_lt (constructor_max_index,
8139 constructor_index))
8140 process_init_element (pop_init_level (1, braced_init_obstack),
8141 true, braced_init_obstack);
8142 else
8143 break;
8146 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8147 if (constructor_range_stack)
8149 /* If value is a compound literal and we'll be just using its
8150 content, don't put it into a SAVE_EXPR. */
8151 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8152 || !require_constant_value
8153 || flag_isoc99)
8155 tree semantic_type = NULL_TREE;
8156 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8158 semantic_type = TREE_TYPE (value.value);
8159 value.value = TREE_OPERAND (value.value, 0);
8161 value.value = c_save_expr (value.value);
8162 if (semantic_type)
8163 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8164 value.value);
8168 while (1)
8170 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8172 tree fieldtype;
8173 enum tree_code fieldcode;
8175 if (constructor_fields == 0)
8177 pedwarn_init (input_location, 0,
8178 "excess elements in struct initializer");
8179 break;
8182 fieldtype = TREE_TYPE (constructor_fields);
8183 if (fieldtype != error_mark_node)
8184 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8185 fieldcode = TREE_CODE (fieldtype);
8187 /* Error for non-static initialization of a flexible array member. */
8188 if (fieldcode == ARRAY_TYPE
8189 && !require_constant_value
8190 && TYPE_SIZE (fieldtype) == NULL_TREE
8191 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8193 error_init ("non-static initialization of a flexible array member");
8194 break;
8197 /* Accept a string constant to initialize a subarray. */
8198 if (value.value != 0
8199 && fieldcode == ARRAY_TYPE
8200 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8201 && string_flag)
8202 value.value = orig_value;
8203 /* Otherwise, if we have come to a subaggregate,
8204 and we don't have an element of its type, push into it. */
8205 else if (value.value != 0
8206 && value.value != error_mark_node
8207 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8208 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8209 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8211 push_init_level (1, braced_init_obstack);
8212 continue;
8215 if (value.value)
8217 push_member_name (constructor_fields);
8218 output_init_element (value.value, value.original_type,
8219 strict_string, fieldtype,
8220 constructor_fields, 1, implicit,
8221 braced_init_obstack);
8222 RESTORE_SPELLING_DEPTH (constructor_depth);
8224 else
8225 /* Do the bookkeeping for an element that was
8226 directly output as a constructor. */
8228 /* For a record, keep track of end position of last field. */
8229 if (DECL_SIZE (constructor_fields))
8230 constructor_bit_index
8231 = size_binop_loc (input_location, PLUS_EXPR,
8232 bit_position (constructor_fields),
8233 DECL_SIZE (constructor_fields));
8235 /* If the current field was the first one not yet written out,
8236 it isn't now, so update. */
8237 if (constructor_unfilled_fields == constructor_fields)
8239 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8240 /* Skip any nameless bit fields. */
8241 while (constructor_unfilled_fields != 0
8242 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8243 && DECL_NAME (constructor_unfilled_fields) == 0)
8244 constructor_unfilled_fields =
8245 DECL_CHAIN (constructor_unfilled_fields);
8249 constructor_fields = DECL_CHAIN (constructor_fields);
8250 /* Skip any nameless bit fields at the beginning. */
8251 while (constructor_fields != 0
8252 && DECL_C_BIT_FIELD (constructor_fields)
8253 && DECL_NAME (constructor_fields) == 0)
8254 constructor_fields = DECL_CHAIN (constructor_fields);
8256 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8258 tree fieldtype;
8259 enum tree_code fieldcode;
8261 if (constructor_fields == 0)
8263 pedwarn_init (input_location, 0,
8264 "excess elements in union initializer");
8265 break;
8268 fieldtype = TREE_TYPE (constructor_fields);
8269 if (fieldtype != error_mark_node)
8270 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8271 fieldcode = TREE_CODE (fieldtype);
8273 /* Warn that traditional C rejects initialization of unions.
8274 We skip the warning if the value is zero. This is done
8275 under the assumption that the zero initializer in user
8276 code appears conditioned on e.g. __STDC__ to avoid
8277 "missing initializer" warnings and relies on default
8278 initialization to zero in the traditional C case.
8279 We also skip the warning if the initializer is designated,
8280 again on the assumption that this must be conditional on
8281 __STDC__ anyway (and we've already complained about the
8282 member-designator already). */
8283 if (!in_system_header && !constructor_designated
8284 && !(value.value && (integer_zerop (value.value)
8285 || real_zerop (value.value))))
8286 warning (OPT_Wtraditional, "traditional C rejects initialization "
8287 "of unions");
8289 /* Accept a string constant to initialize a subarray. */
8290 if (value.value != 0
8291 && fieldcode == ARRAY_TYPE
8292 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8293 && string_flag)
8294 value.value = orig_value;
8295 /* Otherwise, if we have come to a subaggregate,
8296 and we don't have an element of its type, push into it. */
8297 else if (value.value != 0
8298 && value.value != error_mark_node
8299 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8300 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8301 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8303 push_init_level (1, braced_init_obstack);
8304 continue;
8307 if (value.value)
8309 push_member_name (constructor_fields);
8310 output_init_element (value.value, value.original_type,
8311 strict_string, fieldtype,
8312 constructor_fields, 1, implicit,
8313 braced_init_obstack);
8314 RESTORE_SPELLING_DEPTH (constructor_depth);
8316 else
8317 /* Do the bookkeeping for an element that was
8318 directly output as a constructor. */
8320 constructor_bit_index = DECL_SIZE (constructor_fields);
8321 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8324 constructor_fields = 0;
8326 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8328 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8329 enum tree_code eltcode = TREE_CODE (elttype);
8331 /* Accept a string constant to initialize a subarray. */
8332 if (value.value != 0
8333 && eltcode == ARRAY_TYPE
8334 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8335 && string_flag)
8336 value.value = orig_value;
8337 /* Otherwise, if we have come to a subaggregate,
8338 and we don't have an element of its type, push into it. */
8339 else if (value.value != 0
8340 && value.value != error_mark_node
8341 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8342 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8343 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8345 push_init_level (1, braced_init_obstack);
8346 continue;
8349 if (constructor_max_index != 0
8350 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8351 || integer_all_onesp (constructor_max_index)))
8353 pedwarn_init (input_location, 0,
8354 "excess elements in array initializer");
8355 break;
8358 /* Now output the actual element. */
8359 if (value.value)
8361 push_array_bounds (tree_low_cst (constructor_index, 1));
8362 output_init_element (value.value, value.original_type,
8363 strict_string, elttype,
8364 constructor_index, 1, implicit,
8365 braced_init_obstack);
8366 RESTORE_SPELLING_DEPTH (constructor_depth);
8369 constructor_index
8370 = size_binop_loc (input_location, PLUS_EXPR,
8371 constructor_index, bitsize_one_node);
8373 if (!value.value)
8374 /* If we are doing the bookkeeping for an element that was
8375 directly output as a constructor, we must update
8376 constructor_unfilled_index. */
8377 constructor_unfilled_index = constructor_index;
8379 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8381 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8383 /* Do a basic check of initializer size. Note that vectors
8384 always have a fixed size derived from their type. */
8385 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8387 pedwarn_init (input_location, 0,
8388 "excess elements in vector initializer");
8389 break;
8392 /* Now output the actual element. */
8393 if (value.value)
8395 if (TREE_CODE (value.value) == VECTOR_CST)
8396 elttype = TYPE_MAIN_VARIANT (constructor_type);
8397 output_init_element (value.value, value.original_type,
8398 strict_string, elttype,
8399 constructor_index, 1, implicit,
8400 braced_init_obstack);
8403 constructor_index
8404 = size_binop_loc (input_location,
8405 PLUS_EXPR, constructor_index, bitsize_one_node);
8407 if (!value.value)
8408 /* If we are doing the bookkeeping for an element that was
8409 directly output as a constructor, we must update
8410 constructor_unfilled_index. */
8411 constructor_unfilled_index = constructor_index;
8414 /* Handle the sole element allowed in a braced initializer
8415 for a scalar variable. */
8416 else if (constructor_type != error_mark_node
8417 && constructor_fields == 0)
8419 pedwarn_init (input_location, 0,
8420 "excess elements in scalar initializer");
8421 break;
8423 else
8425 if (value.value)
8426 output_init_element (value.value, value.original_type,
8427 strict_string, constructor_type,
8428 NULL_TREE, 1, implicit,
8429 braced_init_obstack);
8430 constructor_fields = 0;
8433 /* Handle range initializers either at this level or anywhere higher
8434 in the designator stack. */
8435 if (constructor_range_stack)
8437 struct constructor_range_stack *p, *range_stack;
8438 int finish = 0;
8440 range_stack = constructor_range_stack;
8441 constructor_range_stack = 0;
8442 while (constructor_stack != range_stack->stack)
8444 gcc_assert (constructor_stack->implicit);
8445 process_init_element (pop_init_level (1,
8446 braced_init_obstack),
8447 true, braced_init_obstack);
8449 for (p = range_stack;
8450 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8451 p = p->prev)
8453 gcc_assert (constructor_stack->implicit);
8454 process_init_element (pop_init_level (1, braced_init_obstack),
8455 true, braced_init_obstack);
8458 p->index = size_binop_loc (input_location,
8459 PLUS_EXPR, p->index, bitsize_one_node);
8460 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8461 finish = 1;
8463 while (1)
8465 constructor_index = p->index;
8466 constructor_fields = p->fields;
8467 if (finish && p->range_end && p->index == p->range_start)
8469 finish = 0;
8470 p->prev = 0;
8472 p = p->next;
8473 if (!p)
8474 break;
8475 push_init_level (2, braced_init_obstack);
8476 p->stack = constructor_stack;
8477 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8478 p->index = p->range_start;
8481 if (!finish)
8482 constructor_range_stack = range_stack;
8483 continue;
8486 break;
8489 constructor_range_stack = 0;
8492 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8493 (guaranteed to be 'volatile' or null) and ARGS (represented using
8494 an ASM_EXPR node). */
8495 tree
8496 build_asm_stmt (tree cv_qualifier, tree args)
8498 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8499 ASM_VOLATILE_P (args) = 1;
8500 return add_stmt (args);
8503 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8504 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8505 SIMPLE indicates whether there was anything at all after the
8506 string in the asm expression -- asm("blah") and asm("blah" : )
8507 are subtly different. We use a ASM_EXPR node to represent this. */
8508 tree
8509 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8510 tree clobbers, tree labels, bool simple)
8512 tree tail;
8513 tree args;
8514 int i;
8515 const char *constraint;
8516 const char **oconstraints;
8517 bool allows_mem, allows_reg, is_inout;
8518 int ninputs, noutputs;
8520 ninputs = list_length (inputs);
8521 noutputs = list_length (outputs);
8522 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8524 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8526 /* Remove output conversions that change the type but not the mode. */
8527 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8529 tree output = TREE_VALUE (tail);
8531 output = c_fully_fold (output, false, NULL);
8533 /* ??? Really, this should not be here. Users should be using a
8534 proper lvalue, dammit. But there's a long history of using casts
8535 in the output operands. In cases like longlong.h, this becomes a
8536 primitive form of typechecking -- if the cast can be removed, then
8537 the output operand had a type of the proper width; otherwise we'll
8538 get an error. Gross, but ... */
8539 STRIP_NOPS (output);
8541 if (!lvalue_or_else (loc, output, lv_asm))
8542 output = error_mark_node;
8544 if (output != error_mark_node
8545 && (TREE_READONLY (output)
8546 || TYPE_READONLY (TREE_TYPE (output))
8547 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8548 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8549 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8550 readonly_error (output, lv_asm);
8552 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8553 oconstraints[i] = constraint;
8555 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8556 &allows_mem, &allows_reg, &is_inout))
8558 /* If the operand is going to end up in memory,
8559 mark it addressable. */
8560 if (!allows_reg && !c_mark_addressable (output))
8561 output = error_mark_node;
8562 if (!(!allows_reg && allows_mem)
8563 && output != error_mark_node
8564 && VOID_TYPE_P (TREE_TYPE (output)))
8566 error_at (loc, "invalid use of void expression");
8567 output = error_mark_node;
8570 else
8571 output = error_mark_node;
8573 TREE_VALUE (tail) = output;
8576 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8578 tree input;
8580 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8581 input = TREE_VALUE (tail);
8583 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8584 oconstraints, &allows_mem, &allows_reg))
8586 /* If the operand is going to end up in memory,
8587 mark it addressable. */
8588 if (!allows_reg && allows_mem)
8590 input = c_fully_fold (input, false, NULL);
8592 /* Strip the nops as we allow this case. FIXME, this really
8593 should be rejected or made deprecated. */
8594 STRIP_NOPS (input);
8595 if (!c_mark_addressable (input))
8596 input = error_mark_node;
8598 else
8600 struct c_expr expr;
8601 memset (&expr, 0, sizeof (expr));
8602 expr.value = input;
8603 expr = default_function_array_conversion (loc, expr);
8604 input = c_fully_fold (expr.value, false, NULL);
8606 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8608 error_at (loc, "invalid use of void expression");
8609 input = error_mark_node;
8613 else
8614 input = error_mark_node;
8616 TREE_VALUE (tail) = input;
8619 /* ASMs with labels cannot have outputs. This should have been
8620 enforced by the parser. */
8621 gcc_assert (outputs == NULL || labels == NULL);
8623 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8625 /* asm statements without outputs, including simple ones, are treated
8626 as volatile. */
8627 ASM_INPUT_P (args) = simple;
8628 ASM_VOLATILE_P (args) = (noutputs == 0);
8630 return args;
8633 /* Generate a goto statement to LABEL. LOC is the location of the
8634 GOTO. */
8636 tree
8637 c_finish_goto_label (location_t loc, tree label)
8639 tree decl = lookup_label_for_goto (loc, label);
8640 if (!decl)
8641 return NULL_TREE;
8642 TREE_USED (decl) = 1;
8644 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8645 SET_EXPR_LOCATION (t, loc);
8646 return add_stmt (t);
8650 /* Generate a computed goto statement to EXPR. LOC is the location of
8651 the GOTO. */
8653 tree
8654 c_finish_goto_ptr (location_t loc, tree expr)
8656 tree t;
8657 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
8658 expr = c_fully_fold (expr, false, NULL);
8659 expr = convert (ptr_type_node, expr);
8660 t = build1 (GOTO_EXPR, void_type_node, expr);
8661 SET_EXPR_LOCATION (t, loc);
8662 return add_stmt (t);
8665 /* Generate a C `return' statement. RETVAL is the expression for what
8666 to return, or a null pointer for `return;' with no value. LOC is
8667 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8668 is the original type of RETVAL. */
8670 tree
8671 c_finish_return (location_t loc, tree retval, tree origtype)
8673 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8674 bool no_warning = false;
8675 bool npc = false;
8676 size_t rank = 0;
8678 if (TREE_THIS_VOLATILE (current_function_decl))
8679 warning_at (loc, 0,
8680 "function declared %<noreturn%> has a %<return%> statement");
8682 if (flag_enable_cilkplus && contains_array_notation_expr (retval))
8684 /* Array notations are allowed in a return statement if it is inside a
8685 built-in array notation reduction function. */
8686 if (!find_rank (loc, retval, retval, false, &rank))
8687 return error_mark_node;
8688 if (rank >= 1)
8690 error_at (loc, "array notation expression cannot be used as a "
8691 "return value");
8692 return error_mark_node;
8695 if (retval)
8697 tree semantic_type = NULL_TREE;
8698 npc = null_pointer_constant_p (retval);
8699 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8701 semantic_type = TREE_TYPE (retval);
8702 retval = TREE_OPERAND (retval, 0);
8704 retval = c_fully_fold (retval, false, NULL);
8705 if (semantic_type)
8706 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8709 if (!retval)
8711 current_function_returns_null = 1;
8712 if ((warn_return_type || flag_isoc99)
8713 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8715 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8716 "%<return%> with no value, in "
8717 "function returning non-void");
8718 no_warning = true;
8721 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8723 current_function_returns_null = 1;
8724 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8725 pedwarn (loc, 0,
8726 "%<return%> with a value, in function returning void");
8727 else
8728 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8729 "%<return%> with expression, in function returning void");
8731 else
8733 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8734 ic_return,
8735 npc, NULL_TREE, NULL_TREE, 0);
8736 tree res = DECL_RESULT (current_function_decl);
8737 tree inner;
8738 bool save;
8740 current_function_returns_value = 1;
8741 if (t == error_mark_node)
8742 return NULL_TREE;
8744 save = in_late_binary_op;
8745 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8746 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8747 in_late_binary_op = true;
8748 inner = t = convert (TREE_TYPE (res), t);
8749 in_late_binary_op = save;
8751 /* Strip any conversions, additions, and subtractions, and see if
8752 we are returning the address of a local variable. Warn if so. */
8753 while (1)
8755 switch (TREE_CODE (inner))
8757 CASE_CONVERT:
8758 case NON_LVALUE_EXPR:
8759 case PLUS_EXPR:
8760 case POINTER_PLUS_EXPR:
8761 inner = TREE_OPERAND (inner, 0);
8762 continue;
8764 case MINUS_EXPR:
8765 /* If the second operand of the MINUS_EXPR has a pointer
8766 type (or is converted from it), this may be valid, so
8767 don't give a warning. */
8769 tree op1 = TREE_OPERAND (inner, 1);
8771 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8772 && (CONVERT_EXPR_P (op1)
8773 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8774 op1 = TREE_OPERAND (op1, 0);
8776 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8777 break;
8779 inner = TREE_OPERAND (inner, 0);
8780 continue;
8783 case ADDR_EXPR:
8784 inner = TREE_OPERAND (inner, 0);
8786 while (REFERENCE_CLASS_P (inner)
8787 && TREE_CODE (inner) != INDIRECT_REF)
8788 inner = TREE_OPERAND (inner, 0);
8790 if (DECL_P (inner)
8791 && !DECL_EXTERNAL (inner)
8792 && !TREE_STATIC (inner)
8793 && DECL_CONTEXT (inner) == current_function_decl)
8794 warning_at (loc,
8795 OPT_Wreturn_local_addr, "function returns address "
8796 "of local variable");
8797 break;
8799 default:
8800 break;
8803 break;
8806 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8807 SET_EXPR_LOCATION (retval, loc);
8809 if (warn_sequence_point)
8810 verify_sequence_points (retval);
8813 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8814 TREE_NO_WARNING (ret_stmt) |= no_warning;
8815 return add_stmt (ret_stmt);
8818 struct c_switch {
8819 /* The SWITCH_EXPR being built. */
8820 tree switch_expr;
8822 /* The original type of the testing expression, i.e. before the
8823 default conversion is applied. */
8824 tree orig_type;
8826 /* A splay-tree mapping the low element of a case range to the high
8827 element, or NULL_TREE if there is no high element. Used to
8828 determine whether or not a new case label duplicates an old case
8829 label. We need a tree, rather than simply a hash table, because
8830 of the GNU case range extension. */
8831 splay_tree cases;
8833 /* The bindings at the point of the switch. This is used for
8834 warnings crossing decls when branching to a case label. */
8835 struct c_spot_bindings *bindings;
8837 /* The next node on the stack. */
8838 struct c_switch *next;
8841 /* A stack of the currently active switch statements. The innermost
8842 switch statement is on the top of the stack. There is no need to
8843 mark the stack for garbage collection because it is only active
8844 during the processing of the body of a function, and we never
8845 collect at that point. */
8847 struct c_switch *c_switch_stack;
8849 /* Start a C switch statement, testing expression EXP. Return the new
8850 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8851 SWITCH_COND_LOC is the location of the switch's condition. */
8853 tree
8854 c_start_case (location_t switch_loc,
8855 location_t switch_cond_loc,
8856 tree exp)
8858 tree orig_type = error_mark_node;
8859 struct c_switch *cs;
8861 if (exp != error_mark_node)
8863 orig_type = TREE_TYPE (exp);
8865 if (!INTEGRAL_TYPE_P (orig_type))
8867 if (orig_type != error_mark_node)
8869 error_at (switch_cond_loc, "switch quantity not an integer");
8870 orig_type = error_mark_node;
8872 exp = integer_zero_node;
8874 else
8876 tree type = TYPE_MAIN_VARIANT (orig_type);
8878 if (!in_system_header
8879 && (type == long_integer_type_node
8880 || type == long_unsigned_type_node))
8881 warning_at (switch_cond_loc,
8882 OPT_Wtraditional, "%<long%> switch expression not "
8883 "converted to %<int%> in ISO C");
8885 exp = c_fully_fold (exp, false, NULL);
8886 exp = default_conversion (exp);
8888 if (warn_sequence_point)
8889 verify_sequence_points (exp);
8893 /* Add this new SWITCH_EXPR to the stack. */
8894 cs = XNEW (struct c_switch);
8895 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8896 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8897 cs->orig_type = orig_type;
8898 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8899 cs->bindings = c_get_switch_bindings ();
8900 cs->next = c_switch_stack;
8901 c_switch_stack = cs;
8903 return add_stmt (cs->switch_expr);
8906 /* Process a case label at location LOC. */
8908 tree
8909 do_case (location_t loc, tree low_value, tree high_value)
8911 tree label = NULL_TREE;
8913 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8915 low_value = c_fully_fold (low_value, false, NULL);
8916 if (TREE_CODE (low_value) == INTEGER_CST)
8917 pedwarn (input_location, OPT_Wpedantic,
8918 "case label is not an integer constant expression");
8921 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8923 high_value = c_fully_fold (high_value, false, NULL);
8924 if (TREE_CODE (high_value) == INTEGER_CST)
8925 pedwarn (input_location, OPT_Wpedantic,
8926 "case label is not an integer constant expression");
8929 if (c_switch_stack == NULL)
8931 if (low_value)
8932 error_at (loc, "case label not within a switch statement");
8933 else
8934 error_at (loc, "%<default%> label not within a switch statement");
8935 return NULL_TREE;
8938 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8939 EXPR_LOCATION (c_switch_stack->switch_expr),
8940 loc))
8941 return NULL_TREE;
8943 label = c_add_case_label (loc, c_switch_stack->cases,
8944 SWITCH_COND (c_switch_stack->switch_expr),
8945 c_switch_stack->orig_type,
8946 low_value, high_value);
8947 if (label == error_mark_node)
8948 label = NULL_TREE;
8949 return label;
8952 /* Finish the switch statement. */
8954 void
8955 c_finish_case (tree body)
8957 struct c_switch *cs = c_switch_stack;
8958 location_t switch_location;
8960 SWITCH_BODY (cs->switch_expr) = body;
8962 /* Emit warnings as needed. */
8963 switch_location = EXPR_LOCATION (cs->switch_expr);
8964 c_do_switch_warnings (cs->cases, switch_location,
8965 TREE_TYPE (cs->switch_expr),
8966 SWITCH_COND (cs->switch_expr));
8968 /* Pop the stack. */
8969 c_switch_stack = cs->next;
8970 splay_tree_delete (cs->cases);
8971 c_release_switch_bindings (cs->bindings);
8972 XDELETE (cs);
8975 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8976 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8977 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8978 statement, and was not surrounded with parenthesis. */
8980 void
8981 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8982 tree else_block, bool nested_if)
8984 tree stmt;
8986 /* If the condition has array notations, then the rank of the then_block and
8987 else_block must be either 0 or be equal to the rank of the condition. If
8988 the condition does not have array notations then break them up as it is
8989 broken up in a normal expression. */
8990 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
8992 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
8993 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
8994 return;
8995 if (then_block
8996 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
8997 return;
8998 if (else_block
8999 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9000 return;
9001 if (cond_rank != then_rank && then_rank != 0)
9003 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9004 " and the then-block");
9005 return;
9007 else if (cond_rank != else_rank && else_rank != 0)
9009 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9010 " and the else-block");
9011 return;
9014 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9015 if (warn_parentheses && nested_if && else_block == NULL)
9017 tree inner_if = then_block;
9019 /* We know from the grammar productions that there is an IF nested
9020 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9021 it might not be exactly THEN_BLOCK, but should be the last
9022 non-container statement within. */
9023 while (1)
9024 switch (TREE_CODE (inner_if))
9026 case COND_EXPR:
9027 goto found;
9028 case BIND_EXPR:
9029 inner_if = BIND_EXPR_BODY (inner_if);
9030 break;
9031 case STATEMENT_LIST:
9032 inner_if = expr_last (then_block);
9033 break;
9034 case TRY_FINALLY_EXPR:
9035 case TRY_CATCH_EXPR:
9036 inner_if = TREE_OPERAND (inner_if, 0);
9037 break;
9038 default:
9039 gcc_unreachable ();
9041 found:
9043 if (COND_EXPR_ELSE (inner_if))
9044 warning_at (if_locus, OPT_Wparentheses,
9045 "suggest explicit braces to avoid ambiguous %<else%>");
9048 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9049 SET_EXPR_LOCATION (stmt, if_locus);
9050 add_stmt (stmt);
9053 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9054 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9055 is false for DO loops. INCR is the FOR increment expression. BODY is
9056 the statement controlled by the loop. BLAB is the break label. CLAB is
9057 the continue label. Everything is allowed to be NULL. */
9059 void
9060 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9061 tree blab, tree clab, bool cond_is_first)
9063 tree entry = NULL, exit = NULL, t;
9065 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9067 error_at (start_locus, "array notation expression cannot be used in a "
9068 "loop%'s condition");
9069 return;
9072 /* If the condition is zero don't generate a loop construct. */
9073 if (cond && integer_zerop (cond))
9075 if (cond_is_first)
9077 t = build_and_jump (&blab);
9078 SET_EXPR_LOCATION (t, start_locus);
9079 add_stmt (t);
9082 else
9084 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9086 /* If we have an exit condition, then we build an IF with gotos either
9087 out of the loop, or to the top of it. If there's no exit condition,
9088 then we just build a jump back to the top. */
9089 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9091 if (cond && !integer_nonzerop (cond))
9093 /* Canonicalize the loop condition to the end. This means
9094 generating a branch to the loop condition. Reuse the
9095 continue label, if possible. */
9096 if (cond_is_first)
9098 if (incr || !clab)
9100 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9101 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9103 else
9104 t = build1 (GOTO_EXPR, void_type_node, clab);
9105 SET_EXPR_LOCATION (t, start_locus);
9106 add_stmt (t);
9109 t = build_and_jump (&blab);
9110 if (cond_is_first)
9111 exit = fold_build3_loc (start_locus,
9112 COND_EXPR, void_type_node, cond, exit, t);
9113 else
9114 exit = fold_build3_loc (input_location,
9115 COND_EXPR, void_type_node, cond, exit, t);
9118 add_stmt (top);
9121 if (body)
9122 add_stmt (body);
9123 if (clab)
9124 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9125 if (incr)
9126 add_stmt (incr);
9127 if (entry)
9128 add_stmt (entry);
9129 if (exit)
9130 add_stmt (exit);
9131 if (blab)
9132 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9135 tree
9136 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9138 bool skip;
9139 tree label = *label_p;
9141 /* In switch statements break is sometimes stylistically used after
9142 a return statement. This can lead to spurious warnings about
9143 control reaching the end of a non-void function when it is
9144 inlined. Note that we are calling block_may_fallthru with
9145 language specific tree nodes; this works because
9146 block_may_fallthru returns true when given something it does not
9147 understand. */
9148 skip = !block_may_fallthru (cur_stmt_list);
9150 if (!label)
9152 if (!skip)
9153 *label_p = label = create_artificial_label (loc);
9155 else if (TREE_CODE (label) == LABEL_DECL)
9157 else switch (TREE_INT_CST_LOW (label))
9159 case 0:
9160 if (is_break)
9161 error_at (loc, "break statement not within loop or switch");
9162 else
9163 error_at (loc, "continue statement not within a loop");
9164 return NULL_TREE;
9166 case 1:
9167 gcc_assert (is_break);
9168 error_at (loc, "break statement used with OpenMP for loop");
9169 return NULL_TREE;
9171 default:
9172 gcc_unreachable ();
9175 if (skip)
9176 return NULL_TREE;
9178 if (!is_break)
9179 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9181 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9184 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9186 static void
9187 emit_side_effect_warnings (location_t loc, tree expr)
9189 if (expr == error_mark_node)
9191 else if (!TREE_SIDE_EFFECTS (expr))
9193 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9194 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9196 else
9197 warn_if_unused_value (expr, loc);
9200 /* Process an expression as if it were a complete statement. Emit
9201 diagnostics, but do not call ADD_STMT. LOC is the location of the
9202 statement. */
9204 tree
9205 c_process_expr_stmt (location_t loc, tree expr)
9207 tree exprv;
9209 if (!expr)
9210 return NULL_TREE;
9212 expr = c_fully_fold (expr, false, NULL);
9214 if (warn_sequence_point)
9215 verify_sequence_points (expr);
9217 if (TREE_TYPE (expr) != error_mark_node
9218 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9219 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9220 error_at (loc, "expression statement has incomplete type");
9222 /* If we're not processing a statement expression, warn about unused values.
9223 Warnings for statement expressions will be emitted later, once we figure
9224 out which is the result. */
9225 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9226 && warn_unused_value)
9227 emit_side_effect_warnings (loc, expr);
9229 exprv = expr;
9230 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9231 exprv = TREE_OPERAND (exprv, 1);
9232 while (CONVERT_EXPR_P (exprv))
9233 exprv = TREE_OPERAND (exprv, 0);
9234 if (DECL_P (exprv)
9235 || handled_component_p (exprv)
9236 || TREE_CODE (exprv) == ADDR_EXPR)
9237 mark_exp_read (exprv);
9239 /* If the expression is not of a type to which we cannot assign a line
9240 number, wrap the thing in a no-op NOP_EXPR. */
9241 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9243 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9244 SET_EXPR_LOCATION (expr, loc);
9247 return expr;
9250 /* Emit an expression as a statement. LOC is the location of the
9251 expression. */
9253 tree
9254 c_finish_expr_stmt (location_t loc, tree expr)
9256 if (expr)
9257 return add_stmt (c_process_expr_stmt (loc, expr));
9258 else
9259 return NULL;
9262 /* Do the opposite and emit a statement as an expression. To begin,
9263 create a new binding level and return it. */
9265 tree
9266 c_begin_stmt_expr (void)
9268 tree ret;
9270 /* We must force a BLOCK for this level so that, if it is not expanded
9271 later, there is a way to turn off the entire subtree of blocks that
9272 are contained in it. */
9273 keep_next_level ();
9274 ret = c_begin_compound_stmt (true);
9276 c_bindings_start_stmt_expr (c_switch_stack == NULL
9277 ? NULL
9278 : c_switch_stack->bindings);
9280 /* Mark the current statement list as belonging to a statement list. */
9281 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9283 return ret;
9286 /* LOC is the location of the compound statement to which this body
9287 belongs. */
9289 tree
9290 c_finish_stmt_expr (location_t loc, tree body)
9292 tree last, type, tmp, val;
9293 tree *last_p;
9295 body = c_end_compound_stmt (loc, body, true);
9297 c_bindings_end_stmt_expr (c_switch_stack == NULL
9298 ? NULL
9299 : c_switch_stack->bindings);
9301 /* Locate the last statement in BODY. See c_end_compound_stmt
9302 about always returning a BIND_EXPR. */
9303 last_p = &BIND_EXPR_BODY (body);
9304 last = BIND_EXPR_BODY (body);
9306 continue_searching:
9307 if (TREE_CODE (last) == STATEMENT_LIST)
9309 tree_stmt_iterator i;
9311 /* This can happen with degenerate cases like ({ }). No value. */
9312 if (!TREE_SIDE_EFFECTS (last))
9313 return body;
9315 /* If we're supposed to generate side effects warnings, process
9316 all of the statements except the last. */
9317 if (warn_unused_value)
9319 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9321 location_t tloc;
9322 tree t = tsi_stmt (i);
9324 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9325 emit_side_effect_warnings (tloc, t);
9328 else
9329 i = tsi_last (last);
9330 last_p = tsi_stmt_ptr (i);
9331 last = *last_p;
9334 /* If the end of the list is exception related, then the list was split
9335 by a call to push_cleanup. Continue searching. */
9336 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9337 || TREE_CODE (last) == TRY_CATCH_EXPR)
9339 last_p = &TREE_OPERAND (last, 0);
9340 last = *last_p;
9341 goto continue_searching;
9344 if (last == error_mark_node)
9345 return last;
9347 /* In the case that the BIND_EXPR is not necessary, return the
9348 expression out from inside it. */
9349 if (last == BIND_EXPR_BODY (body)
9350 && BIND_EXPR_VARS (body) == NULL)
9352 /* Even if this looks constant, do not allow it in a constant
9353 expression. */
9354 last = c_wrap_maybe_const (last, true);
9355 /* Do not warn if the return value of a statement expression is
9356 unused. */
9357 TREE_NO_WARNING (last) = 1;
9358 return last;
9361 /* Extract the type of said expression. */
9362 type = TREE_TYPE (last);
9364 /* If we're not returning a value at all, then the BIND_EXPR that
9365 we already have is a fine expression to return. */
9366 if (!type || VOID_TYPE_P (type))
9367 return body;
9369 /* Now that we've located the expression containing the value, it seems
9370 silly to make voidify_wrapper_expr repeat the process. Create a
9371 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9372 tmp = create_tmp_var_raw (type, NULL);
9374 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9375 tree_expr_nonnegative_p giving up immediately. */
9376 val = last;
9377 if (TREE_CODE (val) == NOP_EXPR
9378 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9379 val = TREE_OPERAND (val, 0);
9381 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9382 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9385 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9386 SET_EXPR_LOCATION (t, loc);
9387 return t;
9391 /* Begin and end compound statements. This is as simple as pushing
9392 and popping new statement lists from the tree. */
9394 tree
9395 c_begin_compound_stmt (bool do_scope)
9397 tree stmt = push_stmt_list ();
9398 if (do_scope)
9399 push_scope ();
9400 return stmt;
9403 /* End a compound statement. STMT is the statement. LOC is the
9404 location of the compound statement-- this is usually the location
9405 of the opening brace. */
9407 tree
9408 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9410 tree block = NULL;
9412 if (do_scope)
9414 if (c_dialect_objc ())
9415 objc_clear_super_receiver ();
9416 block = pop_scope ();
9419 stmt = pop_stmt_list (stmt);
9420 stmt = c_build_bind_expr (loc, block, stmt);
9422 /* If this compound statement is nested immediately inside a statement
9423 expression, then force a BIND_EXPR to be created. Otherwise we'll
9424 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9425 STATEMENT_LISTs merge, and thus we can lose track of what statement
9426 was really last. */
9427 if (building_stmt_list_p ()
9428 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9429 && TREE_CODE (stmt) != BIND_EXPR)
9431 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9432 TREE_SIDE_EFFECTS (stmt) = 1;
9433 SET_EXPR_LOCATION (stmt, loc);
9436 return stmt;
9439 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9440 when the current scope is exited. EH_ONLY is true when this is not
9441 meant to apply to normal control flow transfer. */
9443 void
9444 push_cleanup (tree decl, tree cleanup, bool eh_only)
9446 enum tree_code code;
9447 tree stmt, list;
9448 bool stmt_expr;
9450 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9451 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9452 add_stmt (stmt);
9453 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9454 list = push_stmt_list ();
9455 TREE_OPERAND (stmt, 0) = list;
9456 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9459 /* Build a binary-operation expression without default conversions.
9460 CODE is the kind of expression to build.
9461 LOCATION is the operator's location.
9462 This function differs from `build' in several ways:
9463 the data type of the result is computed and recorded in it,
9464 warnings are generated if arg data types are invalid,
9465 special handling for addition and subtraction of pointers is known,
9466 and some optimization is done (operations on narrow ints
9467 are done in the narrower type when that gives the same result).
9468 Constant folding is also done before the result is returned.
9470 Note that the operands will never have enumeral types, or function
9471 or array types, because either they will have the default conversions
9472 performed or they have both just been converted to some other type in which
9473 the arithmetic is to be done. */
9475 tree
9476 build_binary_op (location_t location, enum tree_code code,
9477 tree orig_op0, tree orig_op1, int convert_p)
9479 tree type0, type1, orig_type0, orig_type1;
9480 tree eptype;
9481 enum tree_code code0, code1;
9482 tree op0, op1;
9483 tree ret = error_mark_node;
9484 const char *invalid_op_diag;
9485 bool op0_int_operands, op1_int_operands;
9486 bool int_const, int_const_or_overflow, int_operands;
9488 /* Expression code to give to the expression when it is built.
9489 Normally this is CODE, which is what the caller asked for,
9490 but in some special cases we change it. */
9491 enum tree_code resultcode = code;
9493 /* Data type in which the computation is to be performed.
9494 In the simplest cases this is the common type of the arguments. */
9495 tree result_type = NULL;
9497 /* When the computation is in excess precision, the type of the
9498 final EXCESS_PRECISION_EXPR. */
9499 tree semantic_result_type = NULL;
9501 /* Nonzero means operands have already been type-converted
9502 in whatever way is necessary.
9503 Zero means they need to be converted to RESULT_TYPE. */
9504 int converted = 0;
9506 /* Nonzero means create the expression with this type, rather than
9507 RESULT_TYPE. */
9508 tree build_type = 0;
9510 /* Nonzero means after finally constructing the expression
9511 convert it to this type. */
9512 tree final_type = 0;
9514 /* Nonzero if this is an operation like MIN or MAX which can
9515 safely be computed in short if both args are promoted shorts.
9516 Also implies COMMON.
9517 -1 indicates a bitwise operation; this makes a difference
9518 in the exact conditions for when it is safe to do the operation
9519 in a narrower mode. */
9520 int shorten = 0;
9522 /* Nonzero if this is a comparison operation;
9523 if both args are promoted shorts, compare the original shorts.
9524 Also implies COMMON. */
9525 int short_compare = 0;
9527 /* Nonzero if this is a right-shift operation, which can be computed on the
9528 original short and then promoted if the operand is a promoted short. */
9529 int short_shift = 0;
9531 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9532 int common = 0;
9534 /* True means types are compatible as far as ObjC is concerned. */
9535 bool objc_ok;
9537 /* True means this is an arithmetic operation that may need excess
9538 precision. */
9539 bool may_need_excess_precision;
9541 /* True means this is a boolean operation that converts both its
9542 operands to truth-values. */
9543 bool boolean_op = false;
9545 /* Remember whether we're doing / or %. */
9546 bool doing_div_or_mod = false;
9548 /* Remember whether we're doing << or >>. */
9549 bool doing_shift = false;
9551 /* Tree holding instrumentation expression. */
9552 tree instrument_expr = NULL;
9554 if (location == UNKNOWN_LOCATION)
9555 location = input_location;
9557 op0 = orig_op0;
9558 op1 = orig_op1;
9560 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9561 if (op0_int_operands)
9562 op0 = remove_c_maybe_const_expr (op0);
9563 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9564 if (op1_int_operands)
9565 op1 = remove_c_maybe_const_expr (op1);
9566 int_operands = (op0_int_operands && op1_int_operands);
9567 if (int_operands)
9569 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9570 && TREE_CODE (orig_op1) == INTEGER_CST);
9571 int_const = (int_const_or_overflow
9572 && !TREE_OVERFLOW (orig_op0)
9573 && !TREE_OVERFLOW (orig_op1));
9575 else
9576 int_const = int_const_or_overflow = false;
9578 /* Do not apply default conversion in mixed vector/scalar expression. */
9579 if (convert_p
9580 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9581 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9583 op0 = default_conversion (op0);
9584 op1 = default_conversion (op1);
9587 /* When Cilk Plus is enabled and there are array notations inside op0, then
9588 we check to see if there are builtin array notation functions. If
9589 so, then we take on the type of the array notation inside it. */
9590 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
9591 orig_type0 = type0 = find_correct_array_notation_type (op0);
9592 else
9593 orig_type0 = type0 = TREE_TYPE (op0);
9595 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
9596 orig_type1 = type1 = find_correct_array_notation_type (op1);
9597 else
9598 orig_type1 = type1 = TREE_TYPE (op1);
9600 /* The expression codes of the data types of the arguments tell us
9601 whether the arguments are integers, floating, pointers, etc. */
9602 code0 = TREE_CODE (type0);
9603 code1 = TREE_CODE (type1);
9605 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9606 STRIP_TYPE_NOPS (op0);
9607 STRIP_TYPE_NOPS (op1);
9609 /* If an error was already reported for one of the arguments,
9610 avoid reporting another error. */
9612 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9613 return error_mark_node;
9615 if ((invalid_op_diag
9616 = targetm.invalid_binary_op (code, type0, type1)))
9618 error_at (location, invalid_op_diag);
9619 return error_mark_node;
9622 switch (code)
9624 case PLUS_EXPR:
9625 case MINUS_EXPR:
9626 case MULT_EXPR:
9627 case TRUNC_DIV_EXPR:
9628 case CEIL_DIV_EXPR:
9629 case FLOOR_DIV_EXPR:
9630 case ROUND_DIV_EXPR:
9631 case EXACT_DIV_EXPR:
9632 may_need_excess_precision = true;
9633 break;
9634 default:
9635 may_need_excess_precision = false;
9636 break;
9638 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9640 op0 = TREE_OPERAND (op0, 0);
9641 type0 = TREE_TYPE (op0);
9643 else if (may_need_excess_precision
9644 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9646 type0 = eptype;
9647 op0 = convert (eptype, op0);
9649 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9651 op1 = TREE_OPERAND (op1, 0);
9652 type1 = TREE_TYPE (op1);
9654 else if (may_need_excess_precision
9655 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9657 type1 = eptype;
9658 op1 = convert (eptype, op1);
9661 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9663 /* In case when one of the operands of the binary operation is
9664 a vector and another is a scalar -- convert scalar to vector. */
9665 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9667 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
9668 true);
9670 switch (convert_flag)
9672 case stv_error:
9673 return error_mark_node;
9674 case stv_firstarg:
9676 bool maybe_const = true;
9677 tree sc;
9678 sc = c_fully_fold (op0, false, &maybe_const);
9679 sc = save_expr (sc);
9680 sc = convert (TREE_TYPE (type1), sc);
9681 op0 = build_vector_from_val (type1, sc);
9682 if (!maybe_const)
9683 op0 = c_wrap_maybe_const (op0, true);
9684 orig_type0 = type0 = TREE_TYPE (op0);
9685 code0 = TREE_CODE (type0);
9686 converted = 1;
9687 break;
9689 case stv_secondarg:
9691 bool maybe_const = true;
9692 tree sc;
9693 sc = c_fully_fold (op1, false, &maybe_const);
9694 sc = save_expr (sc);
9695 sc = convert (TREE_TYPE (type0), sc);
9696 op1 = build_vector_from_val (type0, sc);
9697 if (!maybe_const)
9698 op1 = c_wrap_maybe_const (op1, true);
9699 orig_type1 = type1 = TREE_TYPE (op1);
9700 code1 = TREE_CODE (type1);
9701 converted = 1;
9702 break;
9704 default:
9705 break;
9709 switch (code)
9711 case PLUS_EXPR:
9712 /* Handle the pointer + int case. */
9713 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9715 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9716 goto return_build_binary_op;
9718 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9720 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9721 goto return_build_binary_op;
9723 else
9724 common = 1;
9725 break;
9727 case MINUS_EXPR:
9728 /* Subtraction of two similar pointers.
9729 We must subtract them as integers, then divide by object size. */
9730 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9731 && comp_target_types (location, type0, type1))
9733 ret = pointer_diff (location, op0, op1);
9734 goto return_build_binary_op;
9736 /* Handle pointer minus int. Just like pointer plus int. */
9737 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9739 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9740 goto return_build_binary_op;
9742 else
9743 common = 1;
9744 break;
9746 case MULT_EXPR:
9747 common = 1;
9748 break;
9750 case TRUNC_DIV_EXPR:
9751 case CEIL_DIV_EXPR:
9752 case FLOOR_DIV_EXPR:
9753 case ROUND_DIV_EXPR:
9754 case EXACT_DIV_EXPR:
9755 doing_div_or_mod = true;
9756 warn_for_div_by_zero (location, op1);
9758 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9759 || code0 == FIXED_POINT_TYPE
9760 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9761 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9762 || code1 == FIXED_POINT_TYPE
9763 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9765 enum tree_code tcode0 = code0, tcode1 = code1;
9767 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9768 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9769 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9770 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9772 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9773 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9774 resultcode = RDIV_EXPR;
9775 else
9776 /* Although it would be tempting to shorten always here, that
9777 loses on some targets, since the modulo instruction is
9778 undefined if the quotient can't be represented in the
9779 computation mode. We shorten only if unsigned or if
9780 dividing by something we know != -1. */
9781 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9782 || (TREE_CODE (op1) == INTEGER_CST
9783 && !integer_all_onesp (op1)));
9784 common = 1;
9786 break;
9788 case BIT_AND_EXPR:
9789 case BIT_IOR_EXPR:
9790 case BIT_XOR_EXPR:
9791 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9792 shorten = -1;
9793 /* Allow vector types which are not floating point types. */
9794 else if (code0 == VECTOR_TYPE
9795 && code1 == VECTOR_TYPE
9796 && !VECTOR_FLOAT_TYPE_P (type0)
9797 && !VECTOR_FLOAT_TYPE_P (type1))
9798 common = 1;
9799 break;
9801 case TRUNC_MOD_EXPR:
9802 case FLOOR_MOD_EXPR:
9803 doing_div_or_mod = true;
9804 warn_for_div_by_zero (location, op1);
9806 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9807 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9808 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9809 common = 1;
9810 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9812 /* Although it would be tempting to shorten always here, that loses
9813 on some targets, since the modulo instruction is undefined if the
9814 quotient can't be represented in the computation mode. We shorten
9815 only if unsigned or if dividing by something we know != -1. */
9816 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9817 || (TREE_CODE (op1) == INTEGER_CST
9818 && !integer_all_onesp (op1)));
9819 common = 1;
9821 break;
9823 case TRUTH_ANDIF_EXPR:
9824 case TRUTH_ORIF_EXPR:
9825 case TRUTH_AND_EXPR:
9826 case TRUTH_OR_EXPR:
9827 case TRUTH_XOR_EXPR:
9828 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9829 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9830 || code0 == FIXED_POINT_TYPE)
9831 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9832 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9833 || code1 == FIXED_POINT_TYPE))
9835 /* Result of these operations is always an int,
9836 but that does not mean the operands should be
9837 converted to ints! */
9838 result_type = integer_type_node;
9839 if (op0_int_operands)
9841 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
9842 op0 = remove_c_maybe_const_expr (op0);
9844 else
9845 op0 = c_objc_common_truthvalue_conversion (location, op0);
9846 if (op1_int_operands)
9848 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
9849 op1 = remove_c_maybe_const_expr (op1);
9851 else
9852 op1 = c_objc_common_truthvalue_conversion (location, op1);
9853 converted = 1;
9854 boolean_op = true;
9856 if (code == TRUTH_ANDIF_EXPR)
9858 int_const_or_overflow = (int_operands
9859 && TREE_CODE (orig_op0) == INTEGER_CST
9860 && (op0 == truthvalue_false_node
9861 || TREE_CODE (orig_op1) == INTEGER_CST));
9862 int_const = (int_const_or_overflow
9863 && !TREE_OVERFLOW (orig_op0)
9864 && (op0 == truthvalue_false_node
9865 || !TREE_OVERFLOW (orig_op1)));
9867 else if (code == TRUTH_ORIF_EXPR)
9869 int_const_or_overflow = (int_operands
9870 && TREE_CODE (orig_op0) == INTEGER_CST
9871 && (op0 == truthvalue_true_node
9872 || TREE_CODE (orig_op1) == INTEGER_CST));
9873 int_const = (int_const_or_overflow
9874 && !TREE_OVERFLOW (orig_op0)
9875 && (op0 == truthvalue_true_node
9876 || !TREE_OVERFLOW (orig_op1)));
9878 break;
9880 /* Shift operations: result has same type as first operand;
9881 always convert second operand to int.
9882 Also set SHORT_SHIFT if shifting rightward. */
9884 case RSHIFT_EXPR:
9885 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9886 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9888 result_type = type0;
9889 converted = 1;
9891 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9892 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9893 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9894 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9896 result_type = type0;
9897 converted = 1;
9899 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9900 && code1 == INTEGER_TYPE)
9902 doing_shift = true;
9903 if (TREE_CODE (op1) == INTEGER_CST)
9905 if (tree_int_cst_sgn (op1) < 0)
9907 int_const = false;
9908 if (c_inhibit_evaluation_warnings == 0)
9909 warning (0, "right shift count is negative");
9911 else
9913 if (!integer_zerop (op1))
9914 short_shift = 1;
9916 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9918 int_const = false;
9919 if (c_inhibit_evaluation_warnings == 0)
9920 warning (0, "right shift count >= width of type");
9925 /* Use the type of the value to be shifted. */
9926 result_type = type0;
9927 /* Convert the non vector shift-count to an integer, regardless
9928 of size of value being shifted. */
9929 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9930 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9931 op1 = convert (integer_type_node, op1);
9932 /* Avoid converting op1 to result_type later. */
9933 converted = 1;
9935 break;
9937 case LSHIFT_EXPR:
9938 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9939 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9941 result_type = type0;
9942 converted = 1;
9944 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9945 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9946 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9947 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9949 result_type = type0;
9950 converted = 1;
9952 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9953 && code1 == INTEGER_TYPE)
9955 doing_shift = true;
9956 if (TREE_CODE (op1) == INTEGER_CST)
9958 if (tree_int_cst_sgn (op1) < 0)
9960 int_const = false;
9961 if (c_inhibit_evaluation_warnings == 0)
9962 warning (0, "left shift count is negative");
9965 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9967 int_const = false;
9968 if (c_inhibit_evaluation_warnings == 0)
9969 warning (0, "left shift count >= width of type");
9973 /* Use the type of the value to be shifted. */
9974 result_type = type0;
9975 /* Convert the non vector shift-count to an integer, regardless
9976 of size of value being shifted. */
9977 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9978 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9979 op1 = convert (integer_type_node, op1);
9980 /* Avoid converting op1 to result_type later. */
9981 converted = 1;
9983 break;
9985 case EQ_EXPR:
9986 case NE_EXPR:
9987 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
9989 tree intt;
9990 if (!vector_types_compatible_elements_p (type0, type1))
9992 error_at (location, "comparing vectors with different "
9993 "element types");
9994 return error_mark_node;
9997 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
9999 error_at (location, "comparing vectors with different "
10000 "number of elements");
10001 return error_mark_node;
10004 /* Always construct signed integer vector type. */
10005 intt = c_common_type_for_size (GET_MODE_BITSIZE
10006 (TYPE_MODE (TREE_TYPE (type0))), 0);
10007 result_type = build_opaque_vector_type (intt,
10008 TYPE_VECTOR_SUBPARTS (type0));
10009 converted = 1;
10010 break;
10012 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10013 warning_at (location,
10014 OPT_Wfloat_equal,
10015 "comparing floating point with == or != is unsafe");
10016 /* Result of comparison is always int,
10017 but don't convert the args to int! */
10018 build_type = integer_type_node;
10019 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10020 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10021 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10022 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10023 short_compare = 1;
10024 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10026 if (TREE_CODE (op0) == ADDR_EXPR
10027 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10029 if (code == EQ_EXPR)
10030 warning_at (location,
10031 OPT_Waddress,
10032 "the comparison will always evaluate as %<false%> "
10033 "for the address of %qD will never be NULL",
10034 TREE_OPERAND (op0, 0));
10035 else
10036 warning_at (location,
10037 OPT_Waddress,
10038 "the comparison will always evaluate as %<true%> "
10039 "for the address of %qD will never be NULL",
10040 TREE_OPERAND (op0, 0));
10042 result_type = type0;
10044 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10046 if (TREE_CODE (op1) == ADDR_EXPR
10047 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10049 if (code == EQ_EXPR)
10050 warning_at (location,
10051 OPT_Waddress,
10052 "the comparison will always evaluate as %<false%> "
10053 "for the address of %qD will never be NULL",
10054 TREE_OPERAND (op1, 0));
10055 else
10056 warning_at (location,
10057 OPT_Waddress,
10058 "the comparison will always evaluate as %<true%> "
10059 "for the address of %qD will never be NULL",
10060 TREE_OPERAND (op1, 0));
10062 result_type = type1;
10064 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10066 tree tt0 = TREE_TYPE (type0);
10067 tree tt1 = TREE_TYPE (type1);
10068 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10069 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10070 addr_space_t as_common = ADDR_SPACE_GENERIC;
10072 /* Anything compares with void *. void * compares with anything.
10073 Otherwise, the targets must be compatible
10074 and both must be object or both incomplete. */
10075 if (comp_target_types (location, type0, type1))
10076 result_type = common_pointer_type (type0, type1);
10077 else if (!addr_space_superset (as0, as1, &as_common))
10079 error_at (location, "comparison of pointers to "
10080 "disjoint address spaces");
10081 return error_mark_node;
10083 else if (VOID_TYPE_P (tt0))
10085 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10086 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10087 "comparison of %<void *%> with function pointer");
10089 else if (VOID_TYPE_P (tt1))
10091 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10092 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10093 "comparison of %<void *%> with function pointer");
10095 else
10096 /* Avoid warning about the volatile ObjC EH puts on decls. */
10097 if (!objc_ok)
10098 pedwarn (location, 0,
10099 "comparison of distinct pointer types lacks a cast");
10101 if (result_type == NULL_TREE)
10103 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10104 result_type = build_pointer_type
10105 (build_qualified_type (void_type_node, qual));
10108 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10110 result_type = type0;
10111 pedwarn (location, 0, "comparison between pointer and integer");
10113 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10115 result_type = type1;
10116 pedwarn (location, 0, "comparison between pointer and integer");
10118 break;
10120 case LE_EXPR:
10121 case GE_EXPR:
10122 case LT_EXPR:
10123 case GT_EXPR:
10124 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10126 tree intt;
10127 if (!vector_types_compatible_elements_p (type0, type1))
10129 error_at (location, "comparing vectors with different "
10130 "element types");
10131 return error_mark_node;
10134 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10136 error_at (location, "comparing vectors with different "
10137 "number of elements");
10138 return error_mark_node;
10141 /* Always construct signed integer vector type. */
10142 intt = c_common_type_for_size (GET_MODE_BITSIZE
10143 (TYPE_MODE (TREE_TYPE (type0))), 0);
10144 result_type = build_opaque_vector_type (intt,
10145 TYPE_VECTOR_SUBPARTS (type0));
10146 converted = 1;
10147 break;
10149 build_type = integer_type_node;
10150 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10151 || code0 == FIXED_POINT_TYPE)
10152 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10153 || code1 == FIXED_POINT_TYPE))
10154 short_compare = 1;
10155 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10157 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10158 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10159 addr_space_t as_common;
10161 if (comp_target_types (location, type0, type1))
10163 result_type = common_pointer_type (type0, type1);
10164 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10165 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10166 pedwarn (location, 0,
10167 "comparison of complete and incomplete pointers");
10168 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10169 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10170 "ordered comparisons of pointers to functions");
10171 else if (null_pointer_constant_p (orig_op0)
10172 || null_pointer_constant_p (orig_op1))
10173 warning_at (location, OPT_Wextra,
10174 "ordered comparison of pointer with null pointer");
10177 else if (!addr_space_superset (as0, as1, &as_common))
10179 error_at (location, "comparison of pointers to "
10180 "disjoint address spaces");
10181 return error_mark_node;
10183 else
10185 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10186 result_type = build_pointer_type
10187 (build_qualified_type (void_type_node, qual));
10188 pedwarn (location, 0,
10189 "comparison of distinct pointer types lacks a cast");
10192 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10194 result_type = type0;
10195 if (pedantic)
10196 pedwarn (location, OPT_Wpedantic,
10197 "ordered comparison of pointer with integer zero");
10198 else if (extra_warnings)
10199 warning_at (location, OPT_Wextra,
10200 "ordered comparison of pointer with integer zero");
10202 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10204 result_type = type1;
10205 if (pedantic)
10206 pedwarn (location, OPT_Wpedantic,
10207 "ordered comparison of pointer with integer zero");
10208 else if (extra_warnings)
10209 warning_at (location, OPT_Wextra,
10210 "ordered comparison of pointer with integer zero");
10212 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10214 result_type = type0;
10215 pedwarn (location, 0, "comparison between pointer and integer");
10217 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10219 result_type = type1;
10220 pedwarn (location, 0, "comparison between pointer and integer");
10222 break;
10224 default:
10225 gcc_unreachable ();
10228 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10229 return error_mark_node;
10231 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10232 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10233 || !vector_types_compatible_elements_p (type0, type1)))
10235 binary_op_error (location, code, type0, type1);
10236 return error_mark_node;
10239 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10240 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10242 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10243 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10245 bool first_complex = (code0 == COMPLEX_TYPE);
10246 bool second_complex = (code1 == COMPLEX_TYPE);
10247 int none_complex = (!first_complex && !second_complex);
10249 if (shorten || common || short_compare)
10251 result_type = c_common_type (type0, type1);
10252 do_warn_double_promotion (result_type, type0, type1,
10253 "implicit conversion from %qT to %qT "
10254 "to match other operand of binary "
10255 "expression",
10256 location);
10257 if (result_type == error_mark_node)
10258 return error_mark_node;
10261 if (first_complex != second_complex
10262 && (code == PLUS_EXPR
10263 || code == MINUS_EXPR
10264 || code == MULT_EXPR
10265 || (code == TRUNC_DIV_EXPR && first_complex))
10266 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10267 && flag_signed_zeros)
10269 /* An operation on mixed real/complex operands must be
10270 handled specially, but the language-independent code can
10271 more easily optimize the plain complex arithmetic if
10272 -fno-signed-zeros. */
10273 tree real_type = TREE_TYPE (result_type);
10274 tree real, imag;
10275 if (type0 != orig_type0 || type1 != orig_type1)
10277 gcc_assert (may_need_excess_precision && common);
10278 semantic_result_type = c_common_type (orig_type0, orig_type1);
10280 if (first_complex)
10282 if (TREE_TYPE (op0) != result_type)
10283 op0 = convert_and_check (result_type, op0);
10284 if (TREE_TYPE (op1) != real_type)
10285 op1 = convert_and_check (real_type, op1);
10287 else
10289 if (TREE_TYPE (op0) != real_type)
10290 op0 = convert_and_check (real_type, op0);
10291 if (TREE_TYPE (op1) != result_type)
10292 op1 = convert_and_check (result_type, op1);
10294 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10295 return error_mark_node;
10296 if (first_complex)
10298 op0 = c_save_expr (op0);
10299 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10300 op0, 1);
10301 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10302 op0, 1);
10303 switch (code)
10305 case MULT_EXPR:
10306 case TRUNC_DIV_EXPR:
10307 op1 = c_save_expr (op1);
10308 imag = build2 (resultcode, real_type, imag, op1);
10309 /* Fall through. */
10310 case PLUS_EXPR:
10311 case MINUS_EXPR:
10312 real = build2 (resultcode, real_type, real, op1);
10313 break;
10314 default:
10315 gcc_unreachable();
10318 else
10320 op1 = c_save_expr (op1);
10321 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10322 op1, 1);
10323 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10324 op1, 1);
10325 switch (code)
10327 case MULT_EXPR:
10328 op0 = c_save_expr (op0);
10329 imag = build2 (resultcode, real_type, op0, imag);
10330 /* Fall through. */
10331 case PLUS_EXPR:
10332 real = build2 (resultcode, real_type, op0, real);
10333 break;
10334 case MINUS_EXPR:
10335 real = build2 (resultcode, real_type, op0, real);
10336 imag = build1 (NEGATE_EXPR, real_type, imag);
10337 break;
10338 default:
10339 gcc_unreachable();
10342 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10343 goto return_build_binary_op;
10346 /* For certain operations (which identify themselves by shorten != 0)
10347 if both args were extended from the same smaller type,
10348 do the arithmetic in that type and then extend.
10350 shorten !=0 and !=1 indicates a bitwise operation.
10351 For them, this optimization is safe only if
10352 both args are zero-extended or both are sign-extended.
10353 Otherwise, we might change the result.
10354 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10355 but calculated in (unsigned short) it would be (unsigned short)-1. */
10357 if (shorten && none_complex)
10359 final_type = result_type;
10360 result_type = shorten_binary_op (result_type, op0, op1,
10361 shorten == -1);
10364 /* Shifts can be shortened if shifting right. */
10366 if (short_shift)
10368 int unsigned_arg;
10369 tree arg0 = get_narrower (op0, &unsigned_arg);
10371 final_type = result_type;
10373 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10374 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10376 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10377 && tree_int_cst_sgn (op1) > 0
10378 /* We can shorten only if the shift count is less than the
10379 number of bits in the smaller type size. */
10380 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10381 /* We cannot drop an unsigned shift after sign-extension. */
10382 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10384 /* Do an unsigned shift if the operand was zero-extended. */
10385 result_type
10386 = c_common_signed_or_unsigned_type (unsigned_arg,
10387 TREE_TYPE (arg0));
10388 /* Convert value-to-be-shifted to that type. */
10389 if (TREE_TYPE (op0) != result_type)
10390 op0 = convert (result_type, op0);
10391 converted = 1;
10395 /* Comparison operations are shortened too but differently.
10396 They identify themselves by setting short_compare = 1. */
10398 if (short_compare)
10400 /* Don't write &op0, etc., because that would prevent op0
10401 from being kept in a register.
10402 Instead, make copies of the our local variables and
10403 pass the copies by reference, then copy them back afterward. */
10404 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10405 enum tree_code xresultcode = resultcode;
10406 tree val
10407 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10409 if (val != 0)
10411 ret = val;
10412 goto return_build_binary_op;
10415 op0 = xop0, op1 = xop1;
10416 converted = 1;
10417 resultcode = xresultcode;
10419 if (c_inhibit_evaluation_warnings == 0)
10421 bool op0_maybe_const = true;
10422 bool op1_maybe_const = true;
10423 tree orig_op0_folded, orig_op1_folded;
10425 if (in_late_binary_op)
10427 orig_op0_folded = orig_op0;
10428 orig_op1_folded = orig_op1;
10430 else
10432 /* Fold for the sake of possible warnings, as in
10433 build_conditional_expr. This requires the
10434 "original" values to be folded, not just op0 and
10435 op1. */
10436 c_inhibit_evaluation_warnings++;
10437 op0 = c_fully_fold (op0, require_constant_value,
10438 &op0_maybe_const);
10439 op1 = c_fully_fold (op1, require_constant_value,
10440 &op1_maybe_const);
10441 c_inhibit_evaluation_warnings--;
10442 orig_op0_folded = c_fully_fold (orig_op0,
10443 require_constant_value,
10444 NULL);
10445 orig_op1_folded = c_fully_fold (orig_op1,
10446 require_constant_value,
10447 NULL);
10450 if (warn_sign_compare)
10451 warn_for_sign_compare (location, orig_op0_folded,
10452 orig_op1_folded, op0, op1,
10453 result_type, resultcode);
10454 if (!in_late_binary_op && !int_operands)
10456 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10457 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10458 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10459 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10465 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10466 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10467 Then the expression will be built.
10468 It will be given type FINAL_TYPE if that is nonzero;
10469 otherwise, it will be given type RESULT_TYPE. */
10471 if (!result_type)
10473 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10474 return error_mark_node;
10477 if (build_type == NULL_TREE)
10479 build_type = result_type;
10480 if ((type0 != orig_type0 || type1 != orig_type1)
10481 && !boolean_op)
10483 gcc_assert (may_need_excess_precision && common);
10484 semantic_result_type = c_common_type (orig_type0, orig_type1);
10488 if (!converted)
10490 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10491 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10493 /* This can happen if one operand has a vector type, and the other
10494 has a different type. */
10495 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10496 return error_mark_node;
10499 if (flag_sanitize & SANITIZE_UNDEFINED
10500 && current_function_decl != 0
10501 && (doing_div_or_mod || doing_shift))
10503 /* OP0 and/or OP1 might have side-effects. */
10504 op0 = c_save_expr (op0);
10505 op1 = c_save_expr (op1);
10506 op0 = c_fully_fold (op0, false, NULL);
10507 op1 = c_fully_fold (op1, false, NULL);
10508 if (doing_div_or_mod)
10509 instrument_expr = ubsan_instrument_division (location, op0, op1);
10510 else if (doing_shift)
10511 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10514 /* Treat expressions in initializers specially as they can't trap. */
10515 if (int_const_or_overflow)
10516 ret = (require_constant_value
10517 ? fold_build2_initializer_loc (location, resultcode, build_type,
10518 op0, op1)
10519 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10520 else
10521 ret = build2 (resultcode, build_type, op0, op1);
10522 if (final_type != 0)
10523 ret = convert (final_type, ret);
10525 return_build_binary_op:
10526 gcc_assert (ret != error_mark_node);
10527 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10528 ret = (int_operands
10529 ? note_integer_operands (ret)
10530 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10531 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10532 && !in_late_binary_op)
10533 ret = note_integer_operands (ret);
10534 if (semantic_result_type)
10535 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10536 protected_set_expr_location (ret, location);
10538 if ((flag_sanitize & SANITIZE_UNDEFINED) && instrument_expr != NULL)
10539 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
10540 instrument_expr, ret);
10542 return ret;
10546 /* Convert EXPR to be a truth-value, validating its type for this
10547 purpose. LOCATION is the source location for the expression. */
10549 tree
10550 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10552 bool int_const, int_operands;
10554 switch (TREE_CODE (TREE_TYPE (expr)))
10556 case ARRAY_TYPE:
10557 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10558 return error_mark_node;
10560 case RECORD_TYPE:
10561 error_at (location, "used struct type value where scalar is required");
10562 return error_mark_node;
10564 case UNION_TYPE:
10565 error_at (location, "used union type value where scalar is required");
10566 return error_mark_node;
10568 case VOID_TYPE:
10569 error_at (location, "void value not ignored as it ought to be");
10570 return error_mark_node;
10572 case FUNCTION_TYPE:
10573 gcc_unreachable ();
10575 case VECTOR_TYPE:
10576 error_at (location, "used vector type where scalar is required");
10577 return error_mark_node;
10579 default:
10580 break;
10583 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10584 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10585 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10587 expr = remove_c_maybe_const_expr (expr);
10588 expr = build2 (NE_EXPR, integer_type_node, expr,
10589 convert (TREE_TYPE (expr), integer_zero_node));
10590 expr = note_integer_operands (expr);
10592 else
10593 /* ??? Should we also give an error for vectors rather than leaving
10594 those to give errors later? */
10595 expr = c_common_truthvalue_conversion (location, expr);
10597 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10599 if (TREE_OVERFLOW (expr))
10600 return expr;
10601 else
10602 return note_integer_operands (expr);
10604 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10605 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10606 return expr;
10610 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10611 required. */
10613 tree
10614 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10616 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10618 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10619 /* Executing a compound literal inside a function reinitializes
10620 it. */
10621 if (!TREE_STATIC (decl))
10622 *se = true;
10623 return decl;
10625 else
10626 return expr;
10629 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10631 tree
10632 c_begin_omp_parallel (void)
10634 tree block;
10636 keep_next_level ();
10637 block = c_begin_compound_stmt (true);
10639 return block;
10642 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10643 statement. LOC is the location of the OMP_PARALLEL. */
10645 tree
10646 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10648 tree stmt;
10650 block = c_end_compound_stmt (loc, block, true);
10652 stmt = make_node (OMP_PARALLEL);
10653 TREE_TYPE (stmt) = void_type_node;
10654 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10655 OMP_PARALLEL_BODY (stmt) = block;
10656 SET_EXPR_LOCATION (stmt, loc);
10658 return add_stmt (stmt);
10661 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10663 tree
10664 c_begin_omp_task (void)
10666 tree block;
10668 keep_next_level ();
10669 block = c_begin_compound_stmt (true);
10671 return block;
10674 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10675 statement. LOC is the location of the #pragma. */
10677 tree
10678 c_finish_omp_task (location_t loc, tree clauses, tree block)
10680 tree stmt;
10682 block = c_end_compound_stmt (loc, block, true);
10684 stmt = make_node (OMP_TASK);
10685 TREE_TYPE (stmt) = void_type_node;
10686 OMP_TASK_CLAUSES (stmt) = clauses;
10687 OMP_TASK_BODY (stmt) = block;
10688 SET_EXPR_LOCATION (stmt, loc);
10690 return add_stmt (stmt);
10693 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10694 Remove any elements from the list that are invalid. */
10696 tree
10697 c_finish_omp_clauses (tree clauses)
10699 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10700 tree c, t, *pc = &clauses;
10701 const char *name;
10703 bitmap_obstack_initialize (NULL);
10704 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10705 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10706 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10708 for (pc = &clauses, c = clauses; c ; c = *pc)
10710 bool remove = false;
10711 bool need_complete = false;
10712 bool need_implicitly_determined = false;
10714 switch (OMP_CLAUSE_CODE (c))
10716 case OMP_CLAUSE_SHARED:
10717 name = "shared";
10718 need_implicitly_determined = true;
10719 goto check_dup_generic;
10721 case OMP_CLAUSE_PRIVATE:
10722 name = "private";
10723 need_complete = true;
10724 need_implicitly_determined = true;
10725 goto check_dup_generic;
10727 case OMP_CLAUSE_REDUCTION:
10728 name = "reduction";
10729 need_implicitly_determined = true;
10730 t = OMP_CLAUSE_DECL (c);
10731 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10732 || POINTER_TYPE_P (TREE_TYPE (t)))
10734 error_at (OMP_CLAUSE_LOCATION (c),
10735 "%qE has invalid type for %<reduction%>", t);
10736 remove = true;
10738 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10740 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10741 const char *r_name = NULL;
10743 switch (r_code)
10745 case PLUS_EXPR:
10746 case MULT_EXPR:
10747 case MINUS_EXPR:
10748 case MIN_EXPR:
10749 case MAX_EXPR:
10750 break;
10751 case BIT_AND_EXPR:
10752 r_name = "&";
10753 break;
10754 case BIT_XOR_EXPR:
10755 r_name = "^";
10756 break;
10757 case BIT_IOR_EXPR:
10758 r_name = "|";
10759 break;
10760 case TRUTH_ANDIF_EXPR:
10761 r_name = "&&";
10762 break;
10763 case TRUTH_ORIF_EXPR:
10764 r_name = "||";
10765 break;
10766 default:
10767 gcc_unreachable ();
10769 if (r_name)
10771 error_at (OMP_CLAUSE_LOCATION (c),
10772 "%qE has invalid type for %<reduction(%s)%>",
10773 t, r_name);
10774 remove = true;
10777 goto check_dup_generic;
10779 case OMP_CLAUSE_COPYPRIVATE:
10780 name = "copyprivate";
10781 goto check_dup_generic;
10783 case OMP_CLAUSE_COPYIN:
10784 name = "copyin";
10785 t = OMP_CLAUSE_DECL (c);
10786 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10788 error_at (OMP_CLAUSE_LOCATION (c),
10789 "%qE must be %<threadprivate%> for %<copyin%>", t);
10790 remove = true;
10792 goto check_dup_generic;
10794 check_dup_generic:
10795 t = OMP_CLAUSE_DECL (c);
10796 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10798 error_at (OMP_CLAUSE_LOCATION (c),
10799 "%qE is not a variable in clause %qs", t, name);
10800 remove = true;
10802 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10803 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10804 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10806 error_at (OMP_CLAUSE_LOCATION (c),
10807 "%qE appears more than once in data clauses", t);
10808 remove = true;
10810 else
10811 bitmap_set_bit (&generic_head, DECL_UID (t));
10812 break;
10814 case OMP_CLAUSE_FIRSTPRIVATE:
10815 name = "firstprivate";
10816 t = OMP_CLAUSE_DECL (c);
10817 need_complete = true;
10818 need_implicitly_determined = true;
10819 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10821 error_at (OMP_CLAUSE_LOCATION (c),
10822 "%qE is not a variable in clause %<firstprivate%>", t);
10823 remove = true;
10825 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10826 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10828 error_at (OMP_CLAUSE_LOCATION (c),
10829 "%qE appears more than once in data clauses", t);
10830 remove = true;
10832 else
10833 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10834 break;
10836 case OMP_CLAUSE_LASTPRIVATE:
10837 name = "lastprivate";
10838 t = OMP_CLAUSE_DECL (c);
10839 need_complete = true;
10840 need_implicitly_determined = true;
10841 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10843 error_at (OMP_CLAUSE_LOCATION (c),
10844 "%qE is not a variable in clause %<lastprivate%>", t);
10845 remove = true;
10847 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10848 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10850 error_at (OMP_CLAUSE_LOCATION (c),
10851 "%qE appears more than once in data clauses", t);
10852 remove = true;
10854 else
10855 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10856 break;
10858 case OMP_CLAUSE_IF:
10859 case OMP_CLAUSE_NUM_THREADS:
10860 case OMP_CLAUSE_SCHEDULE:
10861 case OMP_CLAUSE_NOWAIT:
10862 case OMP_CLAUSE_ORDERED:
10863 case OMP_CLAUSE_DEFAULT:
10864 case OMP_CLAUSE_UNTIED:
10865 case OMP_CLAUSE_COLLAPSE:
10866 case OMP_CLAUSE_FINAL:
10867 case OMP_CLAUSE_MERGEABLE:
10868 pc = &OMP_CLAUSE_CHAIN (c);
10869 continue;
10871 default:
10872 gcc_unreachable ();
10875 if (!remove)
10877 t = OMP_CLAUSE_DECL (c);
10879 if (need_complete)
10881 t = require_complete_type (t);
10882 if (t == error_mark_node)
10883 remove = true;
10886 if (need_implicitly_determined)
10888 const char *share_name = NULL;
10890 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10891 share_name = "threadprivate";
10892 else switch (c_omp_predetermined_sharing (t))
10894 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10895 break;
10896 case OMP_CLAUSE_DEFAULT_SHARED:
10897 /* const vars may be specified in firstprivate clause. */
10898 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10899 && TREE_READONLY (t))
10900 break;
10901 share_name = "shared";
10902 break;
10903 case OMP_CLAUSE_DEFAULT_PRIVATE:
10904 share_name = "private";
10905 break;
10906 default:
10907 gcc_unreachable ();
10909 if (share_name)
10911 error_at (OMP_CLAUSE_LOCATION (c),
10912 "%qE is predetermined %qs for %qs",
10913 t, share_name, name);
10914 remove = true;
10919 if (remove)
10920 *pc = OMP_CLAUSE_CHAIN (c);
10921 else
10922 pc = &OMP_CLAUSE_CHAIN (c);
10925 bitmap_obstack_release (NULL);
10926 return clauses;
10929 /* Create a transaction node. */
10931 tree
10932 c_finish_transaction (location_t loc, tree block, int flags)
10934 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10935 if (flags & TM_STMT_ATTR_OUTER)
10936 TRANSACTION_EXPR_OUTER (stmt) = 1;
10937 if (flags & TM_STMT_ATTR_RELAXED)
10938 TRANSACTION_EXPR_RELAXED (stmt) = 1;
10939 return add_stmt (stmt);
10942 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10943 down to the element type of an array. */
10945 tree
10946 c_build_qualified_type (tree type, int type_quals)
10948 if (type == error_mark_node)
10949 return type;
10951 if (TREE_CODE (type) == ARRAY_TYPE)
10953 tree t;
10954 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10955 type_quals);
10957 /* See if we already have an identically qualified type. */
10958 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10960 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10961 && TYPE_NAME (t) == TYPE_NAME (type)
10962 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10963 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10964 TYPE_ATTRIBUTES (type)))
10965 break;
10967 if (!t)
10969 tree domain = TYPE_DOMAIN (type);
10971 t = build_variant_type_copy (type);
10972 TREE_TYPE (t) = element_type;
10974 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10975 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10976 SET_TYPE_STRUCTURAL_EQUALITY (t);
10977 else if (TYPE_CANONICAL (element_type) != element_type
10978 || (domain && TYPE_CANONICAL (domain) != domain))
10980 tree unqualified_canon
10981 = build_array_type (TYPE_CANONICAL (element_type),
10982 domain? TYPE_CANONICAL (domain)
10983 : NULL_TREE);
10984 TYPE_CANONICAL (t)
10985 = c_build_qualified_type (unqualified_canon, type_quals);
10987 else
10988 TYPE_CANONICAL (t) = t;
10990 return t;
10993 /* A restrict-qualified pointer type must be a pointer to object or
10994 incomplete type. Note that the use of POINTER_TYPE_P also allows
10995 REFERENCE_TYPEs, which is appropriate for C++. */
10996 if ((type_quals & TYPE_QUAL_RESTRICT)
10997 && (!POINTER_TYPE_P (type)
10998 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11000 error ("invalid use of %<restrict%>");
11001 type_quals &= ~TYPE_QUAL_RESTRICT;
11004 return build_qualified_type (type, type_quals);
11007 /* Build a VA_ARG_EXPR for the C parser. */
11009 tree
11010 c_build_va_arg (location_t loc, tree expr, tree type)
11012 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11013 warning_at (loc, OPT_Wc___compat,
11014 "C++ requires promoted type, not enum type, in %<va_arg%>");
11015 return build_va_arg (loc, expr, type);