PR c/63862
[official-gcc.git] / gcc / c / c-typeck.c
blobbf0f30624d6cd2a4579b476d5e9cde74cb7ccd55
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 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 "stor-layout.h"
32 #include "trans-mem.h"
33 #include "varasm.h"
34 #include "stmt.h"
35 #include "langhooks.h"
36 #include "c-tree.h"
37 #include "c-lang.h"
38 #include "flags.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "predict.h"
44 #include "vec.h"
45 #include "hashtab.h"
46 #include "hash-set.h"
47 #include "machmode.h"
48 #include "hard-reg-set.h"
49 #include "input.h"
50 #include "function.h"
51 #include "gimple-expr.h"
52 #include "gimplify.h"
53 #include "tree-inline.h"
54 #include "omp-low.h"
55 #include "c-family/c-objc.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-ubsan.h"
58 #include "cilk.h"
59 #include "wide-int.h"
61 /* Possible cases of implicit bad conversions. Used to select
62 diagnostic messages in convert_for_assignment. */
63 enum impl_conv {
64 ic_argpass,
65 ic_assign,
66 ic_init,
67 ic_return
70 /* The level of nesting inside "__alignof__". */
71 int in_alignof;
73 /* The level of nesting inside "sizeof". */
74 int in_sizeof;
76 /* The level of nesting inside "typeof". */
77 int in_typeof;
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg;
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces;
87 static int require_constant_value;
88 static int require_constant_elements;
90 static bool null_pointer_constant_p (const_tree);
91 static tree qualify_type (tree, tree);
92 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int comp_target_types (location_t, tree, tree);
95 static int function_types_compatible_p (const_tree, const_tree, bool *,
96 bool *);
97 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
98 static tree lookup_field (tree, tree);
99 static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101 tree);
102 static tree pointer_diff (location_t, tree, tree);
103 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
104 enum impl_conv, bool, tree, tree, int);
105 static tree valid_compound_expr_initializer (tree, tree);
106 static void push_string (const char *);
107 static void push_member_name (tree);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t, int, const char *);
111 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
112 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
113 bool, struct obstack *);
114 static void output_pending_init_elements (int, struct obstack *);
115 static int set_designator (location_t, int, struct obstack *);
116 static void push_range_stack (tree, struct obstack *);
117 static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
119 static void set_nonincremental_init (struct obstack *);
120 static void set_nonincremental_init_from_string (tree, struct obstack *);
121 static tree find_init_member (tree, struct obstack *);
122 static void readonly_warning (tree, enum lvalue_use);
123 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
124 static void record_maybe_used_decl (tree);
125 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
129 static bool
130 null_pointer_constant_p (const_tree expr)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
136 && !TREE_OVERFLOW (expr)
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149 static tree
150 note_integer_operands (tree expr)
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
158 else
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 return ret;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
171 static inline tree
172 remove_c_maybe_const_expr (tree expr)
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
184 const_tree t1;
185 const_tree t2;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
191 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194 /* Do `exp = require_complete_type (exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.) */
197 tree
198 require_complete_type (tree value)
200 tree type = TREE_TYPE (value);
202 if (error_operand_p (value))
203 return error_mark_node;
205 /* First, detect a valid value with a complete type. */
206 if (COMPLETE_TYPE_P (type))
207 return value;
209 c_incomplete_type_error (value, type);
210 return error_mark_node;
213 /* Print an error message for invalid use of an incomplete type.
214 VALUE is the expression that was used (or 0 if that isn't known)
215 and TYPE is the type that was invalid. */
217 void
218 c_incomplete_type_error (const_tree value, const_tree type)
220 const char *type_code_string;
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
226 if (value != 0 && (TREE_CODE (value) == VAR_DECL
227 || TREE_CODE (value) == PARM_DECL))
228 error ("%qD has an incomplete type", value);
229 else
231 retry:
232 /* We must print an error message. Be clever about what it says. */
234 switch (TREE_CODE (type))
236 case RECORD_TYPE:
237 type_code_string = "struct";
238 break;
240 case UNION_TYPE:
241 type_code_string = "union";
242 break;
244 case ENUMERAL_TYPE:
245 type_code_string = "enum";
246 break;
248 case VOID_TYPE:
249 error ("invalid use of void expression");
250 return;
252 case ARRAY_TYPE:
253 if (TYPE_DOMAIN (type))
255 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
257 error ("invalid use of flexible array member");
258 return;
260 type = TREE_TYPE (type);
261 goto retry;
263 error ("invalid use of array with unspecified bounds");
264 return;
266 default:
267 gcc_unreachable ();
270 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
271 error ("invalid use of undefined type %<%s %E%>",
272 type_code_string, TYPE_NAME (type));
273 else
274 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
275 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
279 /* Given a type, apply default promotions wrt unnamed function
280 arguments and return the new type. */
282 tree
283 c_type_promotes_to (tree type)
285 tree ret = NULL_TREE;
287 if (TYPE_MAIN_VARIANT (type) == float_type_node)
288 ret = double_type_node;
289 else if (c_promoting_integer_type_p (type))
291 /* Preserve unsignedness if not really getting any wider. */
292 if (TYPE_UNSIGNED (type)
293 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
294 ret = unsigned_type_node;
295 else
296 ret = integer_type_node;
299 if (ret != NULL_TREE)
300 return (TYPE_ATOMIC (type)
301 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
302 : ret);
304 return type;
307 /* Return true if between two named address spaces, whether there is a superset
308 named address space that encompasses both address spaces. If there is a
309 superset, return which address space is the superset. */
311 static bool
312 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
314 if (as1 == as2)
316 *common = as1;
317 return true;
319 else if (targetm.addr_space.subset_p (as1, as2))
321 *common = as2;
322 return true;
324 else if (targetm.addr_space.subset_p (as2, as1))
326 *common = as1;
327 return true;
329 else
330 return false;
333 /* Return a variant of TYPE which has all the type qualifiers of LIKE
334 as well as those of TYPE. */
336 static tree
337 qualify_type (tree type, tree like)
339 addr_space_t as_type = TYPE_ADDR_SPACE (type);
340 addr_space_t as_like = TYPE_ADDR_SPACE (like);
341 addr_space_t as_common;
343 /* If the two named address spaces are different, determine the common
344 superset address space. If there isn't one, raise an error. */
345 if (!addr_space_superset (as_type, as_like, &as_common))
347 as_common = as_type;
348 error ("%qT and %qT are in disjoint named address spaces",
349 type, like);
352 return c_build_qualified_type (type,
353 TYPE_QUALS_NO_ADDR_SPACE (type)
354 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
355 | ENCODE_QUAL_ADDR_SPACE (as_common));
358 /* Return true iff the given tree T is a variable length array. */
360 bool
361 c_vla_type_p (const_tree t)
363 if (TREE_CODE (t) == ARRAY_TYPE
364 && C_TYPE_VARIABLE_SIZE (t))
365 return true;
366 return false;
369 /* Return the composite type of two compatible types.
371 We assume that comptypes has already been done and returned
372 nonzero; if that isn't so, this may crash. In particular, we
373 assume that qualifiers match. */
375 tree
376 composite_type (tree t1, tree t2)
378 enum tree_code code1;
379 enum tree_code code2;
380 tree attributes;
382 /* Save time if the two types are the same. */
384 if (t1 == t2) return t1;
386 /* If one type is nonsense, use the other. */
387 if (t1 == error_mark_node)
388 return t2;
389 if (t2 == error_mark_node)
390 return t1;
392 code1 = TREE_CODE (t1);
393 code2 = TREE_CODE (t2);
395 /* Merge the attributes. */
396 attributes = targetm.merge_type_attributes (t1, t2);
398 /* If one is an enumerated type and the other is the compatible
399 integer type, the composite type might be either of the two
400 (DR#013 question 3). For consistency, use the enumerated type as
401 the composite type. */
403 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
404 return t1;
405 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
406 return t2;
408 gcc_assert (code1 == code2);
410 switch (code1)
412 case POINTER_TYPE:
413 /* For two pointers, do this recursively on the target type. */
415 tree pointed_to_1 = TREE_TYPE (t1);
416 tree pointed_to_2 = TREE_TYPE (t2);
417 tree target = composite_type (pointed_to_1, pointed_to_2);
418 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
419 t1 = build_type_attribute_variant (t1, attributes);
420 return qualify_type (t1, t2);
423 case ARRAY_TYPE:
425 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
426 int quals;
427 tree unqual_elt;
428 tree d1 = TYPE_DOMAIN (t1);
429 tree d2 = TYPE_DOMAIN (t2);
430 bool d1_variable, d2_variable;
431 bool d1_zero, d2_zero;
432 bool t1_complete, t2_complete;
434 /* We should not have any type quals on arrays at all. */
435 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
436 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
438 t1_complete = COMPLETE_TYPE_P (t1);
439 t2_complete = COMPLETE_TYPE_P (t2);
441 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
442 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
444 d1_variable = (!d1_zero
445 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
446 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
447 d2_variable = (!d2_zero
448 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
450 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
451 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
453 /* Save space: see if the result is identical to one of the args. */
454 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
455 && (d2_variable || d2_zero || !d1_variable))
456 return build_type_attribute_variant (t1, attributes);
457 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
458 && (d1_variable || d1_zero || !d2_variable))
459 return build_type_attribute_variant (t2, attributes);
461 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
462 return build_type_attribute_variant (t1, attributes);
463 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t2, attributes);
466 /* Merge the element types, and have a size if either arg has
467 one. We may have qualifiers on the element types. To set
468 up TYPE_MAIN_VARIANT correctly, we need to form the
469 composite of the unqualified types and add the qualifiers
470 back at the end. */
471 quals = TYPE_QUALS (strip_array_types (elt));
472 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
473 t1 = build_array_type (unqual_elt,
474 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
475 && (d2_variable
476 || d2_zero
477 || !d1_variable))
478 ? t1
479 : t2));
480 /* Ensure a composite type involving a zero-length array type
481 is a zero-length type not an incomplete type. */
482 if (d1_zero && d2_zero
483 && (t1_complete || t2_complete)
484 && !COMPLETE_TYPE_P (t1))
486 TYPE_SIZE (t1) = bitsize_zero_node;
487 TYPE_SIZE_UNIT (t1) = size_zero_node;
489 t1 = c_build_qualified_type (t1, quals);
490 return build_type_attribute_variant (t1, attributes);
493 case ENUMERAL_TYPE:
494 case RECORD_TYPE:
495 case UNION_TYPE:
496 if (attributes != NULL)
498 /* Try harder not to create a new aggregate type. */
499 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
500 return t1;
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
502 return t2;
504 return build_type_attribute_variant (t1, attributes);
506 case FUNCTION_TYPE:
507 /* Function types: prefer the one that specified arg types.
508 If both do, merge the arg types. Also merge the return types. */
510 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
511 tree p1 = TYPE_ARG_TYPES (t1);
512 tree p2 = TYPE_ARG_TYPES (t2);
513 int len;
514 tree newargs, n;
515 int i;
517 /* Save space: see if the result is identical to one of the args. */
518 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
519 return build_type_attribute_variant (t1, attributes);
520 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
521 return build_type_attribute_variant (t2, attributes);
523 /* Simple way if one arg fails to specify argument types. */
524 if (TYPE_ARG_TYPES (t1) == 0)
526 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
527 t1 = build_type_attribute_variant (t1, attributes);
528 return qualify_type (t1, t2);
530 if (TYPE_ARG_TYPES (t2) == 0)
532 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
533 t1 = build_type_attribute_variant (t1, attributes);
534 return qualify_type (t1, t2);
537 /* If both args specify argument types, we must merge the two
538 lists, argument by argument. */
540 len = list_length (p1);
541 newargs = 0;
543 for (i = 0; i < len; i++)
544 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
546 n = newargs;
548 for (; p1;
549 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
551 /* A null type means arg type is not specified.
552 Take whatever the other function type has. */
553 if (TREE_VALUE (p1) == 0)
555 TREE_VALUE (n) = TREE_VALUE (p2);
556 goto parm_done;
558 if (TREE_VALUE (p2) == 0)
560 TREE_VALUE (n) = TREE_VALUE (p1);
561 goto parm_done;
564 /* Given wait (union {union wait *u; int *i} *)
565 and wait (union wait *),
566 prefer union wait * as type of parm. */
567 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
568 && TREE_VALUE (p1) != TREE_VALUE (p2))
570 tree memb;
571 tree mv2 = TREE_VALUE (p2);
572 if (mv2 && mv2 != error_mark_node
573 && TREE_CODE (mv2) != ARRAY_TYPE)
574 mv2 = TYPE_MAIN_VARIANT (mv2);
575 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
576 memb; memb = DECL_CHAIN (memb))
578 tree mv3 = TREE_TYPE (memb);
579 if (mv3 && mv3 != error_mark_node
580 && TREE_CODE (mv3) != ARRAY_TYPE)
581 mv3 = TYPE_MAIN_VARIANT (mv3);
582 if (comptypes (mv3, mv2))
584 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
585 TREE_VALUE (p2));
586 pedwarn (input_location, OPT_Wpedantic,
587 "function types not truly compatible in ISO C");
588 goto parm_done;
592 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
593 && TREE_VALUE (p2) != TREE_VALUE (p1))
595 tree memb;
596 tree mv1 = TREE_VALUE (p1);
597 if (mv1 && mv1 != error_mark_node
598 && TREE_CODE (mv1) != ARRAY_TYPE)
599 mv1 = TYPE_MAIN_VARIANT (mv1);
600 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
601 memb; memb = DECL_CHAIN (memb))
603 tree mv3 = TREE_TYPE (memb);
604 if (mv3 && mv3 != error_mark_node
605 && TREE_CODE (mv3) != ARRAY_TYPE)
606 mv3 = TYPE_MAIN_VARIANT (mv3);
607 if (comptypes (mv3, mv1))
609 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
610 TREE_VALUE (p1));
611 pedwarn (input_location, OPT_Wpedantic,
612 "function types not truly compatible in ISO C");
613 goto parm_done;
617 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
618 parm_done: ;
621 t1 = build_function_type (valtype, newargs);
622 t1 = qualify_type (t1, t2);
623 /* ... falls through ... */
626 default:
627 return build_type_attribute_variant (t1, attributes);
632 /* Return the type of a conditional expression between pointers to
633 possibly differently qualified versions of compatible types.
635 We assume that comp_target_types has already been done and returned
636 nonzero; if that isn't so, this may crash. */
638 static tree
639 common_pointer_type (tree t1, tree t2)
641 tree attributes;
642 tree pointed_to_1, mv1;
643 tree pointed_to_2, mv2;
644 tree target;
645 unsigned target_quals;
646 addr_space_t as1, as2, as_common;
647 int quals1, quals2;
649 /* Save time if the two types are the same. */
651 if (t1 == t2) return t1;
653 /* If one type is nonsense, use the other. */
654 if (t1 == error_mark_node)
655 return t2;
656 if (t2 == error_mark_node)
657 return t1;
659 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
660 && TREE_CODE (t2) == POINTER_TYPE);
662 /* Merge the attributes. */
663 attributes = targetm.merge_type_attributes (t1, t2);
665 /* Find the composite type of the target types, and combine the
666 qualifiers of the two types' targets. Do not lose qualifiers on
667 array element types by taking the TYPE_MAIN_VARIANT. */
668 mv1 = pointed_to_1 = TREE_TYPE (t1);
669 mv2 = pointed_to_2 = TREE_TYPE (t2);
670 if (TREE_CODE (mv1) != ARRAY_TYPE)
671 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
672 if (TREE_CODE (mv2) != ARRAY_TYPE)
673 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
674 target = composite_type (mv1, mv2);
676 /* For function types do not merge const qualifiers, but drop them
677 if used inconsistently. The middle-end uses these to mark const
678 and noreturn functions. */
679 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
680 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
682 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
683 target_quals = (quals1 & quals2);
684 else
685 target_quals = (quals1 | quals2);
687 /* If the two named address spaces are different, determine the common
688 superset address space. This is guaranteed to exist due to the
689 assumption that comp_target_type returned non-zero. */
690 as1 = TYPE_ADDR_SPACE (pointed_to_1);
691 as2 = TYPE_ADDR_SPACE (pointed_to_2);
692 if (!addr_space_superset (as1, as2, &as_common))
693 gcc_unreachable ();
695 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
697 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
698 return build_type_attribute_variant (t1, attributes);
701 /* Return the common type for two arithmetic types under the usual
702 arithmetic conversions. The default conversions have already been
703 applied, and enumerated types converted to their compatible integer
704 types. The resulting type is unqualified and has no attributes.
706 This is the type for the result of most arithmetic operations
707 if the operands have the given two types. */
709 static tree
710 c_common_type (tree t1, tree t2)
712 enum tree_code code1;
713 enum tree_code code2;
715 /* If one type is nonsense, use the other. */
716 if (t1 == error_mark_node)
717 return t2;
718 if (t2 == error_mark_node)
719 return t1;
721 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
722 t1 = TYPE_MAIN_VARIANT (t1);
724 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
725 t2 = TYPE_MAIN_VARIANT (t2);
727 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
728 t1 = build_type_attribute_variant (t1, NULL_TREE);
730 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
731 t2 = build_type_attribute_variant (t2, NULL_TREE);
733 /* Save time if the two types are the same. */
735 if (t1 == t2) return t1;
737 code1 = TREE_CODE (t1);
738 code2 = TREE_CODE (t2);
740 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
741 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
742 || code1 == INTEGER_TYPE);
743 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
744 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
745 || code2 == INTEGER_TYPE);
747 /* When one operand is a decimal float type, the other operand cannot be
748 a generic float type or a complex type. We also disallow vector types
749 here. */
750 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
751 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
753 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
755 error ("can%'t mix operands of decimal float and vector types");
756 return error_mark_node;
758 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
760 error ("can%'t mix operands of decimal float and complex types");
761 return error_mark_node;
763 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
765 error ("can%'t mix operands of decimal float and other float types");
766 return error_mark_node;
770 /* If one type is a vector type, return that type. (How the usual
771 arithmetic conversions apply to the vector types extension is not
772 precisely specified.) */
773 if (code1 == VECTOR_TYPE)
774 return t1;
776 if (code2 == VECTOR_TYPE)
777 return t2;
779 /* If one type is complex, form the common type of the non-complex
780 components, then make that complex. Use T1 or T2 if it is the
781 required type. */
782 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
784 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
785 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
786 tree subtype = c_common_type (subtype1, subtype2);
788 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
789 return t1;
790 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
791 return t2;
792 else
793 return build_complex_type (subtype);
796 /* If only one is real, use it as the result. */
798 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
799 return t1;
801 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
802 return t2;
804 /* If both are real and either are decimal floating point types, use
805 the decimal floating point type with the greater precision. */
807 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
809 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
810 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
811 return dfloat128_type_node;
812 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
813 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
814 return dfloat64_type_node;
815 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
816 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
817 return dfloat32_type_node;
820 /* Deal with fixed-point types. */
821 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
823 unsigned int unsignedp = 0, satp = 0;
824 machine_mode m1, m2;
825 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
827 m1 = TYPE_MODE (t1);
828 m2 = TYPE_MODE (t2);
830 /* If one input type is saturating, the result type is saturating. */
831 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
832 satp = 1;
834 /* If both fixed-point types are unsigned, the result type is unsigned.
835 When mixing fixed-point and integer types, follow the sign of the
836 fixed-point type.
837 Otherwise, the result type is signed. */
838 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
839 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
840 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
841 && TYPE_UNSIGNED (t1))
842 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
843 && TYPE_UNSIGNED (t2)))
844 unsignedp = 1;
846 /* The result type is signed. */
847 if (unsignedp == 0)
849 /* If the input type is unsigned, we need to convert to the
850 signed type. */
851 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
853 enum mode_class mclass = (enum mode_class) 0;
854 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
855 mclass = MODE_FRACT;
856 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
857 mclass = MODE_ACCUM;
858 else
859 gcc_unreachable ();
860 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
862 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
864 enum mode_class mclass = (enum mode_class) 0;
865 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
866 mclass = MODE_FRACT;
867 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
868 mclass = MODE_ACCUM;
869 else
870 gcc_unreachable ();
871 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
875 if (code1 == FIXED_POINT_TYPE)
877 fbit1 = GET_MODE_FBIT (m1);
878 ibit1 = GET_MODE_IBIT (m1);
880 else
882 fbit1 = 0;
883 /* Signed integers need to subtract one sign bit. */
884 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
887 if (code2 == FIXED_POINT_TYPE)
889 fbit2 = GET_MODE_FBIT (m2);
890 ibit2 = GET_MODE_IBIT (m2);
892 else
894 fbit2 = 0;
895 /* Signed integers need to subtract one sign bit. */
896 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
899 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
900 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
901 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
902 satp);
905 /* Both real or both integers; use the one with greater precision. */
907 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
908 return t1;
909 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
910 return t2;
912 /* Same precision. Prefer long longs to longs to ints when the
913 same precision, following the C99 rules on integer type rank
914 (which are equivalent to the C90 rules for C90 types). */
916 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
918 return long_long_unsigned_type_node;
920 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
923 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
924 return long_long_unsigned_type_node;
925 else
926 return long_long_integer_type_node;
929 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
931 return long_unsigned_type_node;
933 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
936 /* But preserve unsignedness from the other type,
937 since long cannot hold all the values of an unsigned int. */
938 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
939 return long_unsigned_type_node;
940 else
941 return long_integer_type_node;
944 /* Likewise, prefer long double to double even if same size. */
945 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
946 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
947 return long_double_type_node;
949 /* Likewise, prefer double to float even if same size.
950 We got a couple of embedded targets with 32 bit doubles, and the
951 pdp11 might have 64 bit floats. */
952 if (TYPE_MAIN_VARIANT (t1) == double_type_node
953 || TYPE_MAIN_VARIANT (t2) == double_type_node)
954 return double_type_node;
956 /* Otherwise prefer the unsigned one. */
958 if (TYPE_UNSIGNED (t1))
959 return t1;
960 else
961 return t2;
964 /* Wrapper around c_common_type that is used by c-common.c and other
965 front end optimizations that remove promotions. ENUMERAL_TYPEs
966 are allowed here and are converted to their compatible integer types.
967 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
968 preferably a non-Boolean type as the common type. */
969 tree
970 common_type (tree t1, tree t2)
972 if (TREE_CODE (t1) == ENUMERAL_TYPE)
973 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
974 if (TREE_CODE (t2) == ENUMERAL_TYPE)
975 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
977 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
978 if (TREE_CODE (t1) == BOOLEAN_TYPE
979 && TREE_CODE (t2) == BOOLEAN_TYPE)
980 return boolean_type_node;
982 /* If either type is BOOLEAN_TYPE, then return the other. */
983 if (TREE_CODE (t1) == BOOLEAN_TYPE)
984 return t2;
985 if (TREE_CODE (t2) == BOOLEAN_TYPE)
986 return t1;
988 return c_common_type (t1, t2);
991 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
992 or various other operations. Return 2 if they are compatible
993 but a warning may be needed if you use them together. */
996 comptypes (tree type1, tree type2)
998 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
999 int val;
1001 val = comptypes_internal (type1, type2, NULL, NULL);
1002 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1004 return val;
1007 /* Like comptypes, but if it returns non-zero because enum and int are
1008 compatible, it sets *ENUM_AND_INT_P to true. */
1010 static int
1011 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1016 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019 return val;
1022 /* Like comptypes, but if it returns nonzero for different types, it
1023 sets *DIFFERENT_TYPES_P to true. */
1026 comptypes_check_different_types (tree type1, tree type2,
1027 bool *different_types_p)
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1032 val = comptypes_internal (type1, type2, NULL, different_types_p);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1035 return val;
1038 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1039 or various other operations. Return 2 if they are compatible
1040 but a warning may be needed if you use them together. If
1041 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1042 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1043 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1044 NULL, and the types are compatible but different enough not to be
1045 permitted in C11 typedef redeclarations, then this sets
1046 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1047 false, but may or may not be set if the types are incompatible.
1048 This differs from comptypes, in that we don't free the seen
1049 types. */
1051 static int
1052 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1053 bool *different_types_p)
1055 const_tree t1 = type1;
1056 const_tree t2 = type2;
1057 int attrval, val;
1059 /* Suppress errors caused by previously reported errors. */
1061 if (t1 == t2 || !t1 || !t2
1062 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1063 return 1;
1065 /* Enumerated types are compatible with integer types, but this is
1066 not transitive: two enumerated types in the same translation unit
1067 are compatible with each other only if they are the same type. */
1069 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1071 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1072 if (TREE_CODE (t2) != VOID_TYPE)
1074 if (enum_and_int_p != NULL)
1075 *enum_and_int_p = true;
1076 if (different_types_p != NULL)
1077 *different_types_p = true;
1080 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1082 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1083 if (TREE_CODE (t1) != VOID_TYPE)
1085 if (enum_and_int_p != NULL)
1086 *enum_and_int_p = true;
1087 if (different_types_p != NULL)
1088 *different_types_p = true;
1092 if (t1 == t2)
1093 return 1;
1095 /* Different classes of types can't be compatible. */
1097 if (TREE_CODE (t1) != TREE_CODE (t2))
1098 return 0;
1100 /* Qualifiers must match. C99 6.7.3p9 */
1102 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1103 return 0;
1105 /* Allow for two different type nodes which have essentially the same
1106 definition. Note that we already checked for equality of the type
1107 qualifiers (just above). */
1109 if (TREE_CODE (t1) != ARRAY_TYPE
1110 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1111 return 1;
1113 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1114 if (!(attrval = comp_type_attributes (t1, t2)))
1115 return 0;
1117 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1118 val = 0;
1120 switch (TREE_CODE (t1))
1122 case POINTER_TYPE:
1123 /* Do not remove mode or aliasing information. */
1124 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1125 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1126 break;
1127 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1128 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1129 enum_and_int_p, different_types_p));
1130 break;
1132 case FUNCTION_TYPE:
1133 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1134 different_types_p);
1135 break;
1137 case ARRAY_TYPE:
1139 tree d1 = TYPE_DOMAIN (t1);
1140 tree d2 = TYPE_DOMAIN (t2);
1141 bool d1_variable, d2_variable;
1142 bool d1_zero, d2_zero;
1143 val = 1;
1145 /* Target types must match incl. qualifiers. */
1146 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1147 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1148 enum_and_int_p,
1149 different_types_p)))
1150 return 0;
1152 if (different_types_p != NULL
1153 && (d1 == 0) != (d2 == 0))
1154 *different_types_p = true;
1155 /* Sizes must match unless one is missing or variable. */
1156 if (d1 == 0 || d2 == 0 || d1 == d2)
1157 break;
1159 d1_zero = !TYPE_MAX_VALUE (d1);
1160 d2_zero = !TYPE_MAX_VALUE (d2);
1162 d1_variable = (!d1_zero
1163 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1164 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1165 d2_variable = (!d2_zero
1166 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1167 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1168 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1169 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1171 if (different_types_p != NULL
1172 && d1_variable != d2_variable)
1173 *different_types_p = true;
1174 if (d1_variable || d2_variable)
1175 break;
1176 if (d1_zero && d2_zero)
1177 break;
1178 if (d1_zero || d2_zero
1179 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1180 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1181 val = 0;
1183 break;
1186 case ENUMERAL_TYPE:
1187 case RECORD_TYPE:
1188 case UNION_TYPE:
1189 if (val != 1 && !same_translation_unit_p (t1, t2))
1191 tree a1 = TYPE_ATTRIBUTES (t1);
1192 tree a2 = TYPE_ATTRIBUTES (t2);
1194 if (! attribute_list_contained (a1, a2)
1195 && ! attribute_list_contained (a2, a1))
1196 break;
1198 if (attrval != 2)
1199 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1200 different_types_p);
1201 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1202 different_types_p);
1204 break;
1206 case VECTOR_TYPE:
1207 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1208 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1209 enum_and_int_p, different_types_p));
1210 break;
1212 default:
1213 break;
1215 return attrval == 2 && val == 1 ? 2 : val;
1218 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1219 their qualifiers, except for named address spaces. If the pointers point to
1220 different named addresses, then we must determine if one address space is a
1221 subset of the other. */
1223 static int
1224 comp_target_types (location_t location, tree ttl, tree ttr)
1226 int val;
1227 tree mvl = TREE_TYPE (ttl);
1228 tree mvr = TREE_TYPE (ttr);
1229 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1230 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1231 addr_space_t as_common;
1232 bool enum_and_int_p;
1234 /* Fail if pointers point to incompatible address spaces. */
1235 if (!addr_space_superset (asl, asr, &as_common))
1236 return 0;
1238 /* Do not lose qualifiers on element types of array types that are
1239 pointer targets by taking their TYPE_MAIN_VARIANT. */
1240 if (TREE_CODE (mvl) != ARRAY_TYPE)
1241 mvl = (TYPE_ATOMIC (mvl)
1242 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1243 : TYPE_MAIN_VARIANT (mvl));
1244 if (TREE_CODE (mvr) != ARRAY_TYPE)
1245 mvr = (TYPE_ATOMIC (mvr)
1246 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1247 : TYPE_MAIN_VARIANT (mvr));
1248 enum_and_int_p = false;
1249 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1251 if (val == 2)
1252 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1254 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1255 warning_at (location, OPT_Wc___compat,
1256 "pointer target types incompatible in C++");
1258 return val;
1261 /* Subroutines of `comptypes'. */
1263 /* Determine whether two trees derive from the same translation unit.
1264 If the CONTEXT chain ends in a null, that tree's context is still
1265 being parsed, so if two trees have context chains ending in null,
1266 they're in the same translation unit. */
1268 same_translation_unit_p (const_tree t1, const_tree t2)
1270 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1271 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1273 case tcc_declaration:
1274 t1 = DECL_CONTEXT (t1); break;
1275 case tcc_type:
1276 t1 = TYPE_CONTEXT (t1); break;
1277 case tcc_exceptional:
1278 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1279 default: gcc_unreachable ();
1282 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1283 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1285 case tcc_declaration:
1286 t2 = DECL_CONTEXT (t2); break;
1287 case tcc_type:
1288 t2 = TYPE_CONTEXT (t2); break;
1289 case tcc_exceptional:
1290 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1291 default: gcc_unreachable ();
1294 return t1 == t2;
1297 /* Allocate the seen two types, assuming that they are compatible. */
1299 static struct tagged_tu_seen_cache *
1300 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1302 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1303 tu->next = tagged_tu_seen_base;
1304 tu->t1 = t1;
1305 tu->t2 = t2;
1307 tagged_tu_seen_base = tu;
1309 /* The C standard says that two structures in different translation
1310 units are compatible with each other only if the types of their
1311 fields are compatible (among other things). We assume that they
1312 are compatible until proven otherwise when building the cache.
1313 An example where this can occur is:
1314 struct a
1316 struct a *next;
1318 If we are comparing this against a similar struct in another TU,
1319 and did not assume they were compatible, we end up with an infinite
1320 loop. */
1321 tu->val = 1;
1322 return tu;
1325 /* Free the seen types until we get to TU_TIL. */
1327 static void
1328 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1330 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1331 while (tu != tu_til)
1333 const struct tagged_tu_seen_cache *const tu1
1334 = (const struct tagged_tu_seen_cache *) tu;
1335 tu = tu1->next;
1336 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1338 tagged_tu_seen_base = tu_til;
1341 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1342 compatible. If the two types are not the same (which has been
1343 checked earlier), this can only happen when multiple translation
1344 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1345 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1346 comptypes_internal. */
1348 static int
1349 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1350 bool *enum_and_int_p, bool *different_types_p)
1352 tree s1, s2;
1353 bool needs_warning = false;
1355 /* We have to verify that the tags of the types are the same. This
1356 is harder than it looks because this may be a typedef, so we have
1357 to go look at the original type. It may even be a typedef of a
1358 typedef...
1359 In the case of compiler-created builtin structs the TYPE_DECL
1360 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1361 while (TYPE_NAME (t1)
1362 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1363 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1364 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1366 while (TYPE_NAME (t2)
1367 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1368 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1369 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1371 /* C90 didn't have the requirement that the two tags be the same. */
1372 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1373 return 0;
1375 /* C90 didn't say what happened if one or both of the types were
1376 incomplete; we choose to follow C99 rules here, which is that they
1377 are compatible. */
1378 if (TYPE_SIZE (t1) == NULL
1379 || TYPE_SIZE (t2) == NULL)
1380 return 1;
1383 const struct tagged_tu_seen_cache * tts_i;
1384 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1385 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1386 return tts_i->val;
1389 switch (TREE_CODE (t1))
1391 case ENUMERAL_TYPE:
1393 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1394 /* Speed up the case where the type values are in the same order. */
1395 tree tv1 = TYPE_VALUES (t1);
1396 tree tv2 = TYPE_VALUES (t2);
1398 if (tv1 == tv2)
1400 return 1;
1403 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1405 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1406 break;
1407 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1409 tu->val = 0;
1410 return 0;
1414 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1416 return 1;
1418 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1420 tu->val = 0;
1421 return 0;
1424 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1426 tu->val = 0;
1427 return 0;
1430 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1432 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1433 if (s2 == NULL
1434 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1436 tu->val = 0;
1437 return 0;
1440 return 1;
1443 case UNION_TYPE:
1445 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1446 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1448 tu->val = 0;
1449 return 0;
1452 /* Speed up the common case where the fields are in the same order. */
1453 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1454 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1456 int result;
1458 if (DECL_NAME (s1) != DECL_NAME (s2))
1459 break;
1460 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1461 enum_and_int_p, different_types_p);
1463 if (result != 1 && !DECL_NAME (s1))
1464 break;
1465 if (result == 0)
1467 tu->val = 0;
1468 return 0;
1470 if (result == 2)
1471 needs_warning = true;
1473 if (TREE_CODE (s1) == FIELD_DECL
1474 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1475 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1477 tu->val = 0;
1478 return 0;
1481 if (!s1 && !s2)
1483 tu->val = needs_warning ? 2 : 1;
1484 return tu->val;
1487 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1489 bool ok = false;
1491 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1492 if (DECL_NAME (s1) == DECL_NAME (s2))
1494 int result;
1496 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1497 enum_and_int_p,
1498 different_types_p);
1500 if (result != 1 && !DECL_NAME (s1))
1501 continue;
1502 if (result == 0)
1504 tu->val = 0;
1505 return 0;
1507 if (result == 2)
1508 needs_warning = true;
1510 if (TREE_CODE (s1) == FIELD_DECL
1511 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1512 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1513 break;
1515 ok = true;
1516 break;
1518 if (!ok)
1520 tu->val = 0;
1521 return 0;
1524 tu->val = needs_warning ? 2 : 10;
1525 return tu->val;
1528 case RECORD_TYPE:
1530 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1532 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1533 s1 && s2;
1534 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1536 int result;
1537 if (TREE_CODE (s1) != TREE_CODE (s2)
1538 || DECL_NAME (s1) != DECL_NAME (s2))
1539 break;
1540 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1541 enum_and_int_p, different_types_p);
1542 if (result == 0)
1543 break;
1544 if (result == 2)
1545 needs_warning = true;
1547 if (TREE_CODE (s1) == FIELD_DECL
1548 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1549 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1550 break;
1552 if (s1 && s2)
1553 tu->val = 0;
1554 else
1555 tu->val = needs_warning ? 2 : 1;
1556 return tu->val;
1559 default:
1560 gcc_unreachable ();
1564 /* Return 1 if two function types F1 and F2 are compatible.
1565 If either type specifies no argument types,
1566 the other must specify a fixed number of self-promoting arg types.
1567 Otherwise, if one type specifies only the number of arguments,
1568 the other must specify that number of self-promoting arg types.
1569 Otherwise, the argument types must match.
1570 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1572 static int
1573 function_types_compatible_p (const_tree f1, const_tree f2,
1574 bool *enum_and_int_p, bool *different_types_p)
1576 tree args1, args2;
1577 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1578 int val = 1;
1579 int val1;
1580 tree ret1, ret2;
1582 ret1 = TREE_TYPE (f1);
1583 ret2 = TREE_TYPE (f2);
1585 /* 'volatile' qualifiers on a function's return type used to mean
1586 the function is noreturn. */
1587 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1588 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1589 if (TYPE_VOLATILE (ret1))
1590 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1591 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1592 if (TYPE_VOLATILE (ret2))
1593 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1594 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1595 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1596 if (val == 0)
1597 return 0;
1599 args1 = TYPE_ARG_TYPES (f1);
1600 args2 = TYPE_ARG_TYPES (f2);
1602 if (different_types_p != NULL
1603 && (args1 == 0) != (args2 == 0))
1604 *different_types_p = true;
1606 /* An unspecified parmlist matches any specified parmlist
1607 whose argument types don't need default promotions. */
1609 if (args1 == 0)
1611 if (!self_promoting_args_p (args2))
1612 return 0;
1613 /* If one of these types comes from a non-prototype fn definition,
1614 compare that with the other type's arglist.
1615 If they don't match, ask for a warning (but no error). */
1616 if (TYPE_ACTUAL_ARG_TYPES (f1)
1617 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1618 enum_and_int_p, different_types_p))
1619 val = 2;
1620 return val;
1622 if (args2 == 0)
1624 if (!self_promoting_args_p (args1))
1625 return 0;
1626 if (TYPE_ACTUAL_ARG_TYPES (f2)
1627 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1628 enum_and_int_p, different_types_p))
1629 val = 2;
1630 return val;
1633 /* Both types have argument lists: compare them and propagate results. */
1634 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1635 different_types_p);
1636 return val1 != 1 ? val1 : val;
1639 /* Check two lists of types for compatibility, returning 0 for
1640 incompatible, 1 for compatible, or 2 for compatible with
1641 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1642 comptypes_internal. */
1644 static int
1645 type_lists_compatible_p (const_tree args1, const_tree args2,
1646 bool *enum_and_int_p, bool *different_types_p)
1648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1649 int val = 1;
1650 int newval = 0;
1652 while (1)
1654 tree a1, mv1, a2, mv2;
1655 if (args1 == 0 && args2 == 0)
1656 return val;
1657 /* If one list is shorter than the other,
1658 they fail to match. */
1659 if (args1 == 0 || args2 == 0)
1660 return 0;
1661 mv1 = a1 = TREE_VALUE (args1);
1662 mv2 = a2 = TREE_VALUE (args2);
1663 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1664 mv1 = (TYPE_ATOMIC (mv1)
1665 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1666 TYPE_QUAL_ATOMIC)
1667 : TYPE_MAIN_VARIANT (mv1));
1668 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1669 mv2 = (TYPE_ATOMIC (mv2)
1670 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1671 TYPE_QUAL_ATOMIC)
1672 : TYPE_MAIN_VARIANT (mv2));
1673 /* A null pointer instead of a type
1674 means there is supposed to be an argument
1675 but nothing is specified about what type it has.
1676 So match anything that self-promotes. */
1677 if (different_types_p != NULL
1678 && (a1 == 0) != (a2 == 0))
1679 *different_types_p = true;
1680 if (a1 == 0)
1682 if (c_type_promotes_to (a2) != a2)
1683 return 0;
1685 else if (a2 == 0)
1687 if (c_type_promotes_to (a1) != a1)
1688 return 0;
1690 /* If one of the lists has an error marker, ignore this arg. */
1691 else if (TREE_CODE (a1) == ERROR_MARK
1692 || TREE_CODE (a2) == ERROR_MARK)
1694 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1695 different_types_p)))
1697 if (different_types_p != NULL)
1698 *different_types_p = true;
1699 /* Allow wait (union {union wait *u; int *i} *)
1700 and wait (union wait *) to be compatible. */
1701 if (TREE_CODE (a1) == UNION_TYPE
1702 && (TYPE_NAME (a1) == 0
1703 || TYPE_TRANSPARENT_AGGR (a1))
1704 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1705 && tree_int_cst_equal (TYPE_SIZE (a1),
1706 TYPE_SIZE (a2)))
1708 tree memb;
1709 for (memb = TYPE_FIELDS (a1);
1710 memb; memb = DECL_CHAIN (memb))
1712 tree mv3 = TREE_TYPE (memb);
1713 if (mv3 && mv3 != error_mark_node
1714 && TREE_CODE (mv3) != ARRAY_TYPE)
1715 mv3 = (TYPE_ATOMIC (mv3)
1716 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1717 TYPE_QUAL_ATOMIC)
1718 : TYPE_MAIN_VARIANT (mv3));
1719 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1720 different_types_p))
1721 break;
1723 if (memb == 0)
1724 return 0;
1726 else if (TREE_CODE (a2) == UNION_TYPE
1727 && (TYPE_NAME (a2) == 0
1728 || TYPE_TRANSPARENT_AGGR (a2))
1729 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1730 && tree_int_cst_equal (TYPE_SIZE (a2),
1731 TYPE_SIZE (a1)))
1733 tree memb;
1734 for (memb = TYPE_FIELDS (a2);
1735 memb; memb = DECL_CHAIN (memb))
1737 tree mv3 = TREE_TYPE (memb);
1738 if (mv3 && mv3 != error_mark_node
1739 && TREE_CODE (mv3) != ARRAY_TYPE)
1740 mv3 = (TYPE_ATOMIC (mv3)
1741 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1742 TYPE_QUAL_ATOMIC)
1743 : TYPE_MAIN_VARIANT (mv3));
1744 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1745 different_types_p))
1746 break;
1748 if (memb == 0)
1749 return 0;
1751 else
1752 return 0;
1755 /* comptypes said ok, but record if it said to warn. */
1756 if (newval > val)
1757 val = newval;
1759 args1 = TREE_CHAIN (args1);
1760 args2 = TREE_CHAIN (args2);
1764 /* Compute the size to increment a pointer by. When a function type or void
1765 type or incomplete type is passed, size_one_node is returned.
1766 This function does not emit any diagnostics; the caller is responsible
1767 for that. */
1769 static tree
1770 c_size_in_bytes (const_tree type)
1772 enum tree_code code = TREE_CODE (type);
1774 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1775 || !COMPLETE_TYPE_P (type))
1776 return size_one_node;
1778 /* Convert in case a char is more than one unit. */
1779 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1780 size_int (TYPE_PRECISION (char_type_node)
1781 / BITS_PER_UNIT));
1784 /* Return either DECL or its known constant value (if it has one). */
1786 tree
1787 decl_constant_value (tree decl)
1789 if (/* Don't change a variable array bound or initial value to a constant
1790 in a place where a variable is invalid. Note that DECL_INITIAL
1791 isn't valid for a PARM_DECL. */
1792 current_function_decl != 0
1793 && TREE_CODE (decl) != PARM_DECL
1794 && !TREE_THIS_VOLATILE (decl)
1795 && TREE_READONLY (decl)
1796 && DECL_INITIAL (decl) != 0
1797 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1798 /* This is invalid if initial value is not constant.
1799 If it has either a function call, a memory reference,
1800 or a variable, then re-evaluating it could give different results. */
1801 && TREE_CONSTANT (DECL_INITIAL (decl))
1802 /* Check for cases where this is sub-optimal, even though valid. */
1803 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1804 return DECL_INITIAL (decl);
1805 return decl;
1808 /* Convert the array expression EXP to a pointer. */
1809 static tree
1810 array_to_pointer_conversion (location_t loc, tree exp)
1812 tree orig_exp = exp;
1813 tree type = TREE_TYPE (exp);
1814 tree adr;
1815 tree restype = TREE_TYPE (type);
1816 tree ptrtype;
1818 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1820 STRIP_TYPE_NOPS (exp);
1822 if (TREE_NO_WARNING (orig_exp))
1823 TREE_NO_WARNING (exp) = 1;
1825 ptrtype = build_pointer_type (restype);
1827 if (TREE_CODE (exp) == INDIRECT_REF)
1828 return convert (ptrtype, TREE_OPERAND (exp, 0));
1830 /* In C++ array compound literals are temporary objects unless they are
1831 const or appear in namespace scope, so they are destroyed too soon
1832 to use them for much of anything (c++/53220). */
1833 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1835 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1836 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1837 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1838 "converting an array compound literal to a pointer "
1839 "is ill-formed in C++");
1842 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1843 return convert (ptrtype, adr);
1846 /* Convert the function expression EXP to a pointer. */
1847 static tree
1848 function_to_pointer_conversion (location_t loc, tree exp)
1850 tree orig_exp = exp;
1852 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1854 STRIP_TYPE_NOPS (exp);
1856 if (TREE_NO_WARNING (orig_exp))
1857 TREE_NO_WARNING (exp) = 1;
1859 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1862 /* Mark EXP as read, not just set, for set but not used -Wunused
1863 warning purposes. */
1865 void
1866 mark_exp_read (tree exp)
1868 switch (TREE_CODE (exp))
1870 case VAR_DECL:
1871 case PARM_DECL:
1872 DECL_READ_P (exp) = 1;
1873 break;
1874 case ARRAY_REF:
1875 case COMPONENT_REF:
1876 case MODIFY_EXPR:
1877 case REALPART_EXPR:
1878 case IMAGPART_EXPR:
1879 CASE_CONVERT:
1880 case ADDR_EXPR:
1881 mark_exp_read (TREE_OPERAND (exp, 0));
1882 break;
1883 case COMPOUND_EXPR:
1884 case C_MAYBE_CONST_EXPR:
1885 mark_exp_read (TREE_OPERAND (exp, 1));
1886 break;
1887 default:
1888 break;
1892 /* Perform the default conversion of arrays and functions to pointers.
1893 Return the result of converting EXP. For any other expression, just
1894 return EXP.
1896 LOC is the location of the expression. */
1898 struct c_expr
1899 default_function_array_conversion (location_t loc, struct c_expr exp)
1901 tree orig_exp = exp.value;
1902 tree type = TREE_TYPE (exp.value);
1903 enum tree_code code = TREE_CODE (type);
1905 switch (code)
1907 case ARRAY_TYPE:
1909 bool not_lvalue = false;
1910 bool lvalue_array_p;
1912 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1913 || CONVERT_EXPR_P (exp.value))
1914 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1916 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1917 not_lvalue = true;
1918 exp.value = TREE_OPERAND (exp.value, 0);
1921 if (TREE_NO_WARNING (orig_exp))
1922 TREE_NO_WARNING (exp.value) = 1;
1924 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1925 if (!flag_isoc99 && !lvalue_array_p)
1927 /* Before C99, non-lvalue arrays do not decay to pointers.
1928 Normally, using such an array would be invalid; but it can
1929 be used correctly inside sizeof or as a statement expression.
1930 Thus, do not give an error here; an error will result later. */
1931 return exp;
1934 exp.value = array_to_pointer_conversion (loc, exp.value);
1936 break;
1937 case FUNCTION_TYPE:
1938 exp.value = function_to_pointer_conversion (loc, exp.value);
1939 break;
1940 default:
1941 break;
1944 return exp;
1947 struct c_expr
1948 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1950 mark_exp_read (exp.value);
1951 return default_function_array_conversion (loc, exp);
1954 /* Return whether EXPR should be treated as an atomic lvalue for the
1955 purposes of load and store handling. */
1957 static bool
1958 really_atomic_lvalue (tree expr)
1960 if (error_operand_p (expr))
1961 return false;
1962 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1963 return false;
1964 if (!lvalue_p (expr))
1965 return false;
1967 /* Ignore _Atomic on register variables, since their addresses can't
1968 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1969 sequences wouldn't work. Ignore _Atomic on structures containing
1970 bit-fields, since accessing elements of atomic structures or
1971 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1972 it's undefined at translation time or execution time, and the
1973 normal atomic sequences again wouldn't work. */
1974 while (handled_component_p (expr))
1976 if (TREE_CODE (expr) == COMPONENT_REF
1977 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1978 return false;
1979 expr = TREE_OPERAND (expr, 0);
1981 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1982 return false;
1983 return true;
1986 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1987 including converting functions and arrays to pointers if CONVERT_P.
1988 If READ_P, also mark the expression as having been read. */
1990 struct c_expr
1991 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1992 bool convert_p, bool read_p)
1994 if (read_p)
1995 mark_exp_read (exp.value);
1996 if (convert_p)
1997 exp = default_function_array_conversion (loc, exp);
1998 if (really_atomic_lvalue (exp.value))
2000 vec<tree, va_gc> *params;
2001 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2002 tree expr_type = TREE_TYPE (exp.value);
2003 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2004 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2006 gcc_assert (TYPE_ATOMIC (expr_type));
2008 /* Expansion of a generic atomic load may require an addition
2009 element, so allocate enough to prevent a resize. */
2010 vec_alloc (params, 4);
2012 /* Remove the qualifiers for the rest of the expressions and
2013 create the VAL temp variable to hold the RHS. */
2014 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2015 tmp = create_tmp_var (nonatomic_type, NULL);
2016 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2017 TREE_ADDRESSABLE (tmp) = 1;
2018 TREE_NO_WARNING (tmp) = 1;
2020 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2021 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2022 params->quick_push (expr_addr);
2023 params->quick_push (tmp_addr);
2024 params->quick_push (seq_cst);
2025 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2027 /* EXPR is always read. */
2028 mark_exp_read (exp.value);
2030 /* Return tmp which contains the value loaded. */
2031 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2033 return exp;
2036 /* EXP is an expression of integer type. Apply the integer promotions
2037 to it and return the promoted value. */
2039 tree
2040 perform_integral_promotions (tree exp)
2042 tree type = TREE_TYPE (exp);
2043 enum tree_code code = TREE_CODE (type);
2045 gcc_assert (INTEGRAL_TYPE_P (type));
2047 /* Normally convert enums to int,
2048 but convert wide enums to something wider. */
2049 if (code == ENUMERAL_TYPE)
2051 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2052 TYPE_PRECISION (integer_type_node)),
2053 ((TYPE_PRECISION (type)
2054 >= TYPE_PRECISION (integer_type_node))
2055 && TYPE_UNSIGNED (type)));
2057 return convert (type, exp);
2060 /* ??? This should no longer be needed now bit-fields have their
2061 proper types. */
2062 if (TREE_CODE (exp) == COMPONENT_REF
2063 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2064 /* If it's thinner than an int, promote it like a
2065 c_promoting_integer_type_p, otherwise leave it alone. */
2066 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2067 TYPE_PRECISION (integer_type_node)))
2068 return convert (integer_type_node, exp);
2070 if (c_promoting_integer_type_p (type))
2072 /* Preserve unsignedness if not really getting any wider. */
2073 if (TYPE_UNSIGNED (type)
2074 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2075 return convert (unsigned_type_node, exp);
2077 return convert (integer_type_node, exp);
2080 return exp;
2084 /* Perform default promotions for C data used in expressions.
2085 Enumeral types or short or char are converted to int.
2086 In addition, manifest constants symbols are replaced by their values. */
2088 tree
2089 default_conversion (tree exp)
2091 tree orig_exp;
2092 tree type = TREE_TYPE (exp);
2093 enum tree_code code = TREE_CODE (type);
2094 tree promoted_type;
2096 mark_exp_read (exp);
2098 /* Functions and arrays have been converted during parsing. */
2099 gcc_assert (code != FUNCTION_TYPE);
2100 if (code == ARRAY_TYPE)
2101 return exp;
2103 /* Constants can be used directly unless they're not loadable. */
2104 if (TREE_CODE (exp) == CONST_DECL)
2105 exp = DECL_INITIAL (exp);
2107 /* Strip no-op conversions. */
2108 orig_exp = exp;
2109 STRIP_TYPE_NOPS (exp);
2111 if (TREE_NO_WARNING (orig_exp))
2112 TREE_NO_WARNING (exp) = 1;
2114 if (code == VOID_TYPE)
2116 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2117 "void value not ignored as it ought to be");
2118 return error_mark_node;
2121 exp = require_complete_type (exp);
2122 if (exp == error_mark_node)
2123 return error_mark_node;
2125 promoted_type = targetm.promoted_type (type);
2126 if (promoted_type)
2127 return convert (promoted_type, exp);
2129 if (INTEGRAL_TYPE_P (type))
2130 return perform_integral_promotions (exp);
2132 return exp;
2135 /* Look up COMPONENT in a structure or union TYPE.
2137 If the component name is not found, returns NULL_TREE. Otherwise,
2138 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2139 stepping down the chain to the component, which is in the last
2140 TREE_VALUE of the list. Normally the list is of length one, but if
2141 the component is embedded within (nested) anonymous structures or
2142 unions, the list steps down the chain to the component. */
2144 static tree
2145 lookup_field (tree type, tree component)
2147 tree field;
2149 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2150 to the field elements. Use a binary search on this array to quickly
2151 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2152 will always be set for structures which have many elements. */
2154 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2156 int bot, top, half;
2157 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2159 field = TYPE_FIELDS (type);
2160 bot = 0;
2161 top = TYPE_LANG_SPECIFIC (type)->s->len;
2162 while (top - bot > 1)
2164 half = (top - bot + 1) >> 1;
2165 field = field_array[bot+half];
2167 if (DECL_NAME (field) == NULL_TREE)
2169 /* Step through all anon unions in linear fashion. */
2170 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2172 field = field_array[bot++];
2173 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2174 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2176 tree anon = lookup_field (TREE_TYPE (field), component);
2178 if (anon)
2179 return tree_cons (NULL_TREE, field, anon);
2181 /* The Plan 9 compiler permits referring
2182 directly to an anonymous struct/union field
2183 using a typedef name. */
2184 if (flag_plan9_extensions
2185 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2186 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2187 == TYPE_DECL)
2188 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2189 == component))
2190 break;
2194 /* Entire record is only anon unions. */
2195 if (bot > top)
2196 return NULL_TREE;
2198 /* Restart the binary search, with new lower bound. */
2199 continue;
2202 if (DECL_NAME (field) == component)
2203 break;
2204 if (DECL_NAME (field) < component)
2205 bot += half;
2206 else
2207 top = bot + half;
2210 if (DECL_NAME (field_array[bot]) == component)
2211 field = field_array[bot];
2212 else if (DECL_NAME (field) != component)
2213 return NULL_TREE;
2215 else
2217 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2219 if (DECL_NAME (field) == NULL_TREE
2220 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2221 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2223 tree anon = lookup_field (TREE_TYPE (field), component);
2225 if (anon)
2226 return tree_cons (NULL_TREE, field, anon);
2228 /* The Plan 9 compiler permits referring directly to an
2229 anonymous struct/union field using a typedef
2230 name. */
2231 if (flag_plan9_extensions
2232 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2233 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2234 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2235 == component))
2236 break;
2239 if (DECL_NAME (field) == component)
2240 break;
2243 if (field == NULL_TREE)
2244 return NULL_TREE;
2247 return tree_cons (NULL_TREE, field, NULL_TREE);
2250 /* Make an expression to refer to the COMPONENT field of structure or
2251 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2252 location of the COMPONENT_REF. */
2254 tree
2255 build_component_ref (location_t loc, tree datum, tree component)
2257 tree type = TREE_TYPE (datum);
2258 enum tree_code code = TREE_CODE (type);
2259 tree field = NULL;
2260 tree ref;
2261 bool datum_lvalue = lvalue_p (datum);
2263 if (!objc_is_public (datum, component))
2264 return error_mark_node;
2266 /* Detect Objective-C property syntax object.property. */
2267 if (c_dialect_objc ()
2268 && (ref = objc_maybe_build_component_ref (datum, component)))
2269 return ref;
2271 /* See if there is a field or component with name COMPONENT. */
2273 if (code == RECORD_TYPE || code == UNION_TYPE)
2275 if (!COMPLETE_TYPE_P (type))
2277 c_incomplete_type_error (NULL_TREE, type);
2278 return error_mark_node;
2281 field = lookup_field (type, component);
2283 if (!field)
2285 error_at (loc, "%qT has no member named %qE", type, component);
2286 return error_mark_node;
2289 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2290 This might be better solved in future the way the C++ front
2291 end does it - by giving the anonymous entities each a
2292 separate name and type, and then have build_component_ref
2293 recursively call itself. We can't do that here. */
2296 tree subdatum = TREE_VALUE (field);
2297 int quals;
2298 tree subtype;
2299 bool use_datum_quals;
2301 if (TREE_TYPE (subdatum) == error_mark_node)
2302 return error_mark_node;
2304 /* If this is an rvalue, it does not have qualifiers in C
2305 standard terms and we must avoid propagating such
2306 qualifiers down to a non-lvalue array that is then
2307 converted to a pointer. */
2308 use_datum_quals = (datum_lvalue
2309 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2311 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2312 if (use_datum_quals)
2313 quals |= TYPE_QUALS (TREE_TYPE (datum));
2314 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2316 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2317 NULL_TREE);
2318 SET_EXPR_LOCATION (ref, loc);
2319 if (TREE_READONLY (subdatum)
2320 || (use_datum_quals && TREE_READONLY (datum)))
2321 TREE_READONLY (ref) = 1;
2322 if (TREE_THIS_VOLATILE (subdatum)
2323 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2324 TREE_THIS_VOLATILE (ref) = 1;
2326 if (TREE_DEPRECATED (subdatum))
2327 warn_deprecated_use (subdatum, NULL_TREE);
2329 datum = ref;
2331 field = TREE_CHAIN (field);
2333 while (field);
2335 return ref;
2337 else if (code != ERROR_MARK)
2338 error_at (loc,
2339 "request for member %qE in something not a structure or union",
2340 component);
2342 return error_mark_node;
2345 /* Given an expression PTR for a pointer, return an expression
2346 for the value pointed to.
2347 ERRORSTRING is the name of the operator to appear in error messages.
2349 LOC is the location to use for the generated tree. */
2351 tree
2352 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2354 tree pointer = default_conversion (ptr);
2355 tree type = TREE_TYPE (pointer);
2356 tree ref;
2358 if (TREE_CODE (type) == POINTER_TYPE)
2360 if (CONVERT_EXPR_P (pointer)
2361 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2363 /* If a warning is issued, mark it to avoid duplicates from
2364 the backend. This only needs to be done at
2365 warn_strict_aliasing > 2. */
2366 if (warn_strict_aliasing > 2)
2367 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2368 type, TREE_OPERAND (pointer, 0)))
2369 TREE_NO_WARNING (pointer) = 1;
2372 if (TREE_CODE (pointer) == ADDR_EXPR
2373 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2374 == TREE_TYPE (type)))
2376 ref = TREE_OPERAND (pointer, 0);
2377 protected_set_expr_location (ref, loc);
2378 return ref;
2380 else
2382 tree t = TREE_TYPE (type);
2384 ref = build1 (INDIRECT_REF, t, pointer);
2386 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2388 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2390 error_at (loc, "dereferencing pointer to incomplete type "
2391 "%qT", t);
2392 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2394 return error_mark_node;
2396 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2397 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2399 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2400 so that we get the proper error message if the result is used
2401 to assign to. Also, &* is supposed to be a no-op.
2402 And ANSI C seems to specify that the type of the result
2403 should be the const type. */
2404 /* A de-reference of a pointer to const is not a const. It is valid
2405 to change it via some other pointer. */
2406 TREE_READONLY (ref) = TYPE_READONLY (t);
2407 TREE_SIDE_EFFECTS (ref)
2408 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2409 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2410 protected_set_expr_location (ref, loc);
2411 return ref;
2414 else if (TREE_CODE (pointer) != ERROR_MARK)
2415 invalid_indirection_error (loc, type, errstring);
2417 return error_mark_node;
2420 /* This handles expressions of the form "a[i]", which denotes
2421 an array reference.
2423 This is logically equivalent in C to *(a+i), but we may do it differently.
2424 If A is a variable or a member, we generate a primitive ARRAY_REF.
2425 This avoids forcing the array out of registers, and can work on
2426 arrays that are not lvalues (for example, members of structures returned
2427 by functions).
2429 For vector types, allow vector[i] but not i[vector], and create
2430 *(((type*)&vectortype) + i) for the expression.
2432 LOC is the location to use for the returned expression. */
2434 tree
2435 build_array_ref (location_t loc, tree array, tree index)
2437 tree ret;
2438 bool swapped = false;
2439 if (TREE_TYPE (array) == error_mark_node
2440 || TREE_TYPE (index) == error_mark_node)
2441 return error_mark_node;
2443 if (flag_cilkplus && contains_array_notation_expr (index))
2445 size_t rank = 0;
2446 if (!find_rank (loc, index, index, true, &rank))
2447 return error_mark_node;
2448 if (rank > 1)
2450 error_at (loc, "rank of the array's index is greater than 1");
2451 return error_mark_node;
2454 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2455 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2456 /* Allow vector[index] but not index[vector]. */
2457 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2459 tree temp;
2460 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2461 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2463 error_at (loc,
2464 "subscripted value is neither array nor pointer nor vector");
2466 return error_mark_node;
2468 temp = array;
2469 array = index;
2470 index = temp;
2471 swapped = true;
2474 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2476 error_at (loc, "array subscript is not an integer");
2477 return error_mark_node;
2480 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2482 error_at (loc, "subscripted value is pointer to function");
2483 return error_mark_node;
2486 /* ??? Existing practice has been to warn only when the char
2487 index is syntactically the index, not for char[array]. */
2488 if (!swapped)
2489 warn_array_subscript_with_type_char (index);
2491 /* Apply default promotions *after* noticing character types. */
2492 index = default_conversion (index);
2493 if (index == error_mark_node)
2494 return error_mark_node;
2496 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2498 bool non_lvalue
2499 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2501 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2503 tree rval, type;
2505 /* An array that is indexed by a non-constant
2506 cannot be stored in a register; we must be able to do
2507 address arithmetic on its address.
2508 Likewise an array of elements of variable size. */
2509 if (TREE_CODE (index) != INTEGER_CST
2510 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2511 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2513 if (!c_mark_addressable (array))
2514 return error_mark_node;
2516 /* An array that is indexed by a constant value which is not within
2517 the array bounds cannot be stored in a register either; because we
2518 would get a crash in store_bit_field/extract_bit_field when trying
2519 to access a non-existent part of the register. */
2520 if (TREE_CODE (index) == INTEGER_CST
2521 && TYPE_DOMAIN (TREE_TYPE (array))
2522 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2524 if (!c_mark_addressable (array))
2525 return error_mark_node;
2528 if (pedantic || warn_c90_c99_compat)
2530 tree foo = array;
2531 while (TREE_CODE (foo) == COMPONENT_REF)
2532 foo = TREE_OPERAND (foo, 0);
2533 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2534 pedwarn (loc, OPT_Wpedantic,
2535 "ISO C forbids subscripting %<register%> array");
2536 else if (!lvalue_p (foo))
2537 pedwarn_c90 (loc, OPT_Wpedantic,
2538 "ISO C90 forbids subscripting non-lvalue "
2539 "array");
2542 type = TREE_TYPE (TREE_TYPE (array));
2543 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2544 /* Array ref is const/volatile if the array elements are
2545 or if the array is. */
2546 TREE_READONLY (rval)
2547 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2548 | TREE_READONLY (array));
2549 TREE_SIDE_EFFECTS (rval)
2550 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2551 | TREE_SIDE_EFFECTS (array));
2552 TREE_THIS_VOLATILE (rval)
2553 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2554 /* This was added by rms on 16 Nov 91.
2555 It fixes vol struct foo *a; a->elts[1]
2556 in an inline function.
2557 Hope it doesn't break something else. */
2558 | TREE_THIS_VOLATILE (array));
2559 ret = require_complete_type (rval);
2560 protected_set_expr_location (ret, loc);
2561 if (non_lvalue)
2562 ret = non_lvalue_loc (loc, ret);
2563 return ret;
2565 else
2567 tree ar = default_conversion (array);
2569 if (ar == error_mark_node)
2570 return ar;
2572 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2573 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2575 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2576 index, 0),
2577 RO_ARRAY_INDEXING);
2578 if (non_lvalue)
2579 ret = non_lvalue_loc (loc, ret);
2580 return ret;
2584 /* Build an external reference to identifier ID. FUN indicates
2585 whether this will be used for a function call. LOC is the source
2586 location of the identifier. This sets *TYPE to the type of the
2587 identifier, which is not the same as the type of the returned value
2588 for CONST_DECLs defined as enum constants. If the type of the
2589 identifier is not available, *TYPE is set to NULL. */
2590 tree
2591 build_external_ref (location_t loc, tree id, int fun, tree *type)
2593 tree ref;
2594 tree decl = lookup_name (id);
2596 /* In Objective-C, an instance variable (ivar) may be preferred to
2597 whatever lookup_name() found. */
2598 decl = objc_lookup_ivar (decl, id);
2600 *type = NULL;
2601 if (decl && decl != error_mark_node)
2603 ref = decl;
2604 *type = TREE_TYPE (ref);
2606 else if (fun)
2607 /* Implicit function declaration. */
2608 ref = implicitly_declare (loc, id);
2609 else if (decl == error_mark_node)
2610 /* Don't complain about something that's already been
2611 complained about. */
2612 return error_mark_node;
2613 else
2615 undeclared_variable (loc, id);
2616 return error_mark_node;
2619 if (TREE_TYPE (ref) == error_mark_node)
2620 return error_mark_node;
2622 if (TREE_DEPRECATED (ref))
2623 warn_deprecated_use (ref, NULL_TREE);
2625 /* Recursive call does not count as usage. */
2626 if (ref != current_function_decl)
2628 TREE_USED (ref) = 1;
2631 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2633 if (!in_sizeof && !in_typeof)
2634 C_DECL_USED (ref) = 1;
2635 else if (DECL_INITIAL (ref) == 0
2636 && DECL_EXTERNAL (ref)
2637 && !TREE_PUBLIC (ref))
2638 record_maybe_used_decl (ref);
2641 if (TREE_CODE (ref) == CONST_DECL)
2643 used_types_insert (TREE_TYPE (ref));
2645 if (warn_cxx_compat
2646 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2647 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2649 warning_at (loc, OPT_Wc___compat,
2650 ("enum constant defined in struct or union "
2651 "is not visible in C++"));
2652 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2655 ref = DECL_INITIAL (ref);
2656 TREE_CONSTANT (ref) = 1;
2658 else if (current_function_decl != 0
2659 && !DECL_FILE_SCOPE_P (current_function_decl)
2660 && (TREE_CODE (ref) == VAR_DECL
2661 || TREE_CODE (ref) == PARM_DECL
2662 || TREE_CODE (ref) == FUNCTION_DECL))
2664 tree context = decl_function_context (ref);
2666 if (context != 0 && context != current_function_decl)
2667 DECL_NONLOCAL (ref) = 1;
2669 /* C99 6.7.4p3: An inline definition of a function with external
2670 linkage ... shall not contain a reference to an identifier with
2671 internal linkage. */
2672 else if (current_function_decl != 0
2673 && DECL_DECLARED_INLINE_P (current_function_decl)
2674 && DECL_EXTERNAL (current_function_decl)
2675 && VAR_OR_FUNCTION_DECL_P (ref)
2676 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2677 && ! TREE_PUBLIC (ref)
2678 && DECL_CONTEXT (ref) != current_function_decl)
2679 record_inline_static (loc, current_function_decl, ref,
2680 csi_internal);
2682 return ref;
2685 /* Record details of decls possibly used inside sizeof or typeof. */
2686 struct maybe_used_decl
2688 /* The decl. */
2689 tree decl;
2690 /* The level seen at (in_sizeof + in_typeof). */
2691 int level;
2692 /* The next one at this level or above, or NULL. */
2693 struct maybe_used_decl *next;
2696 static struct maybe_used_decl *maybe_used_decls;
2698 /* Record that DECL, an undefined static function reference seen
2699 inside sizeof or typeof, might be used if the operand of sizeof is
2700 a VLA type or the operand of typeof is a variably modified
2701 type. */
2703 static void
2704 record_maybe_used_decl (tree decl)
2706 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2707 t->decl = decl;
2708 t->level = in_sizeof + in_typeof;
2709 t->next = maybe_used_decls;
2710 maybe_used_decls = t;
2713 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2714 USED is false, just discard them. If it is true, mark them used
2715 (if no longer inside sizeof or typeof) or move them to the next
2716 level up (if still inside sizeof or typeof). */
2718 void
2719 pop_maybe_used (bool used)
2721 struct maybe_used_decl *p = maybe_used_decls;
2722 int cur_level = in_sizeof + in_typeof;
2723 while (p && p->level > cur_level)
2725 if (used)
2727 if (cur_level == 0)
2728 C_DECL_USED (p->decl) = 1;
2729 else
2730 p->level = cur_level;
2732 p = p->next;
2734 if (!used || cur_level == 0)
2735 maybe_used_decls = p;
2738 /* Return the result of sizeof applied to EXPR. */
2740 struct c_expr
2741 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2743 struct c_expr ret;
2744 if (expr.value == error_mark_node)
2746 ret.value = error_mark_node;
2747 ret.original_code = ERROR_MARK;
2748 ret.original_type = NULL;
2749 pop_maybe_used (false);
2751 else
2753 bool expr_const_operands = true;
2755 if (TREE_CODE (expr.value) == PARM_DECL
2756 && C_ARRAY_PARAMETER (expr.value))
2758 if (warning_at (loc, OPT_Wsizeof_array_argument,
2759 "%<sizeof%> on array function parameter %qE will "
2760 "return size of %qT", expr.value,
2761 expr.original_type))
2762 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2764 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2765 &expr_const_operands);
2766 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2767 c_last_sizeof_arg = expr.value;
2768 ret.original_code = SIZEOF_EXPR;
2769 ret.original_type = NULL;
2770 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2772 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2773 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2774 folded_expr, ret.value);
2775 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2776 SET_EXPR_LOCATION (ret.value, loc);
2778 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2780 return ret;
2783 /* Return the result of sizeof applied to T, a structure for the type
2784 name passed to sizeof (rather than the type itself). LOC is the
2785 location of the original expression. */
2787 struct c_expr
2788 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2790 tree type;
2791 struct c_expr ret;
2792 tree type_expr = NULL_TREE;
2793 bool type_expr_const = true;
2794 type = groktypename (t, &type_expr, &type_expr_const);
2795 ret.value = c_sizeof (loc, type);
2796 c_last_sizeof_arg = type;
2797 ret.original_code = SIZEOF_EXPR;
2798 ret.original_type = NULL;
2799 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2800 && c_vla_type_p (type))
2802 /* If the type is a [*] array, it is a VLA but is represented as
2803 having a size of zero. In such a case we must ensure that
2804 the result of sizeof does not get folded to a constant by
2805 c_fully_fold, because if the size is evaluated the result is
2806 not constant and so constraints on zero or negative size
2807 arrays must not be applied when this sizeof call is inside
2808 another array declarator. */
2809 if (!type_expr)
2810 type_expr = integer_zero_node;
2811 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2812 type_expr, ret.value);
2813 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2815 pop_maybe_used (type != error_mark_node
2816 ? C_TYPE_VARIABLE_SIZE (type) : false);
2817 return ret;
2820 /* Build a function call to function FUNCTION with parameters PARAMS.
2821 The function call is at LOC.
2822 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2823 TREE_VALUE of each node is a parameter-expression.
2824 FUNCTION's data type may be a function type or a pointer-to-function. */
2826 tree
2827 build_function_call (location_t loc, tree function, tree params)
2829 vec<tree, va_gc> *v;
2830 tree ret;
2832 vec_alloc (v, list_length (params));
2833 for (; params; params = TREE_CHAIN (params))
2834 v->quick_push (TREE_VALUE (params));
2835 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2836 vec_free (v);
2837 return ret;
2840 /* Give a note about the location of the declaration of DECL. */
2842 static void inform_declaration (tree decl)
2844 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2845 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2848 /* Build a function call to function FUNCTION with parameters PARAMS.
2849 ORIGTYPES, if not NULL, is a vector of types; each element is
2850 either NULL or the original type of the corresponding element in
2851 PARAMS. The original type may differ from TREE_TYPE of the
2852 parameter for enums. FUNCTION's data type may be a function type
2853 or pointer-to-function. This function changes the elements of
2854 PARAMS. */
2856 tree
2857 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2858 tree function, vec<tree, va_gc> *params,
2859 vec<tree, va_gc> *origtypes)
2861 tree fntype, fundecl = 0;
2862 tree name = NULL_TREE, result;
2863 tree tem;
2864 int nargs;
2865 tree *argarray;
2868 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2869 STRIP_TYPE_NOPS (function);
2871 /* Convert anything with function type to a pointer-to-function. */
2872 if (TREE_CODE (function) == FUNCTION_DECL)
2874 name = DECL_NAME (function);
2876 if (flag_tm)
2877 tm_malloc_replacement (function);
2878 fundecl = function;
2879 /* Atomic functions have type checking/casting already done. They are
2880 often rewritten and don't match the original parameter list. */
2881 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2882 origtypes = NULL;
2884 if (flag_cilkplus
2885 && is_cilkplus_reduce_builtin (function))
2886 origtypes = NULL;
2888 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2889 function = function_to_pointer_conversion (loc, function);
2891 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2892 expressions, like those used for ObjC messenger dispatches. */
2893 if (params && !params->is_empty ())
2894 function = objc_rewrite_function_call (function, (*params)[0]);
2896 function = c_fully_fold (function, false, NULL);
2898 fntype = TREE_TYPE (function);
2900 if (TREE_CODE (fntype) == ERROR_MARK)
2901 return error_mark_node;
2903 if (!(TREE_CODE (fntype) == POINTER_TYPE
2904 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2906 if (!flag_diagnostics_show_caret)
2907 error_at (loc,
2908 "called object %qE is not a function or function pointer",
2909 function);
2910 else if (DECL_P (function))
2912 error_at (loc,
2913 "called object %qD is not a function or function pointer",
2914 function);
2915 inform_declaration (function);
2917 else
2918 error_at (loc,
2919 "called object is not a function or function pointer");
2920 return error_mark_node;
2923 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2924 current_function_returns_abnormally = 1;
2926 /* fntype now gets the type of function pointed to. */
2927 fntype = TREE_TYPE (fntype);
2929 /* Convert the parameters to the types declared in the
2930 function prototype, or apply default promotions. */
2932 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2933 origtypes, function, fundecl);
2934 if (nargs < 0)
2935 return error_mark_node;
2937 /* Check that the function is called through a compatible prototype.
2938 If it is not, warn. */
2939 if (CONVERT_EXPR_P (function)
2940 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2941 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2942 && !comptypes (fntype, TREE_TYPE (tem)))
2944 tree return_type = TREE_TYPE (fntype);
2946 /* This situation leads to run-time undefined behavior. We can't,
2947 therefore, simply error unless we can prove that all possible
2948 executions of the program must execute the code. */
2949 warning_at (loc, 0, "function called through a non-compatible type");
2951 if (VOID_TYPE_P (return_type)
2952 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2953 pedwarn (loc, 0,
2954 "function with qualified void return type called");
2957 argarray = vec_safe_address (params);
2959 /* Check that arguments to builtin functions match the expectations. */
2960 if (fundecl
2961 && DECL_BUILT_IN (fundecl)
2962 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2963 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2964 return error_mark_node;
2966 /* Check that the arguments to the function are valid. */
2967 check_function_arguments (fntype, nargs, argarray);
2969 if (name != NULL_TREE
2970 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2972 if (require_constant_value)
2973 result =
2974 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2975 function, nargs, argarray);
2976 else
2977 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2978 function, nargs, argarray);
2979 if (TREE_CODE (result) == NOP_EXPR
2980 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2981 STRIP_TYPE_NOPS (result);
2983 else
2984 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2985 function, nargs, argarray);
2987 if (VOID_TYPE_P (TREE_TYPE (result)))
2989 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2990 pedwarn (loc, 0,
2991 "function with qualified void return type called");
2992 return result;
2994 return require_complete_type (result);
2997 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2999 tree
3000 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3001 tree function, vec<tree, va_gc> *params,
3002 vec<tree, va_gc> *origtypes)
3004 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3005 STRIP_TYPE_NOPS (function);
3007 /* Convert anything with function type to a pointer-to-function. */
3008 if (TREE_CODE (function) == FUNCTION_DECL)
3010 /* Implement type-directed function overloading for builtins.
3011 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3012 handle all the type checking. The result is a complete expression
3013 that implements this function call. */
3014 tree tem = resolve_overloaded_builtin (loc, function, params);
3015 if (tem)
3016 return tem;
3018 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3021 /* Convert the argument expressions in the vector VALUES
3022 to the types in the list TYPELIST.
3024 If TYPELIST is exhausted, or when an element has NULL as its type,
3025 perform the default conversions.
3027 ORIGTYPES is the original types of the expressions in VALUES. This
3028 holds the type of enum values which have been converted to integral
3029 types. It may be NULL.
3031 FUNCTION is a tree for the called function. It is used only for
3032 error messages, where it is formatted with %qE.
3034 This is also where warnings about wrong number of args are generated.
3036 ARG_LOC are locations of function arguments (if any).
3038 Returns the actual number of arguments processed (which may be less
3039 than the length of VALUES in some error situations), or -1 on
3040 failure. */
3042 static int
3043 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3044 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3045 tree function, tree fundecl)
3047 tree typetail, val;
3048 unsigned int parmnum;
3049 bool error_args = false;
3050 const bool type_generic = fundecl
3051 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3052 bool type_generic_remove_excess_precision = false;
3053 tree selector;
3055 /* Change pointer to function to the function itself for
3056 diagnostics. */
3057 if (TREE_CODE (function) == ADDR_EXPR
3058 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3059 function = TREE_OPERAND (function, 0);
3061 /* Handle an ObjC selector specially for diagnostics. */
3062 selector = objc_message_selector ();
3064 /* For type-generic built-in functions, determine whether excess
3065 precision should be removed (classification) or not
3066 (comparison). */
3067 if (type_generic
3068 && DECL_BUILT_IN (fundecl)
3069 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3071 switch (DECL_FUNCTION_CODE (fundecl))
3073 case BUILT_IN_ISFINITE:
3074 case BUILT_IN_ISINF:
3075 case BUILT_IN_ISINF_SIGN:
3076 case BUILT_IN_ISNAN:
3077 case BUILT_IN_ISNORMAL:
3078 case BUILT_IN_FPCLASSIFY:
3079 type_generic_remove_excess_precision = true;
3080 break;
3082 default:
3083 type_generic_remove_excess_precision = false;
3084 break;
3087 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3088 return vec_safe_length (values);
3090 /* Scan the given expressions and types, producing individual
3091 converted arguments. */
3093 for (typetail = typelist, parmnum = 0;
3094 values && values->iterate (parmnum, &val);
3095 ++parmnum)
3097 tree type = typetail ? TREE_VALUE (typetail) : 0;
3098 tree valtype = TREE_TYPE (val);
3099 tree rname = function;
3100 int argnum = parmnum + 1;
3101 const char *invalid_func_diag;
3102 bool excess_precision = false;
3103 bool npc;
3104 tree parmval;
3105 /* Some __atomic_* builtins have additional hidden argument at
3106 position 0. */
3107 location_t ploc
3108 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3109 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3110 : input_location;
3112 if (type == void_type_node)
3114 if (selector)
3115 error_at (loc, "too many arguments to method %qE", selector);
3116 else
3117 error_at (loc, "too many arguments to function %qE", function);
3118 inform_declaration (fundecl);
3119 return parmnum;
3122 if (selector && argnum > 2)
3124 rname = selector;
3125 argnum -= 2;
3128 npc = null_pointer_constant_p (val);
3130 /* If there is excess precision and a prototype, convert once to
3131 the required type rather than converting via the semantic
3132 type. Likewise without a prototype a float value represented
3133 as long double should be converted once to double. But for
3134 type-generic classification functions excess precision must
3135 be removed here. */
3136 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3137 && (type || !type_generic || !type_generic_remove_excess_precision))
3139 val = TREE_OPERAND (val, 0);
3140 excess_precision = true;
3142 val = c_fully_fold (val, false, NULL);
3143 STRIP_TYPE_NOPS (val);
3145 val = require_complete_type (val);
3147 if (type != 0)
3149 /* Formal parm type is specified by a function prototype. */
3151 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3153 error_at (ploc, "type of formal parameter %d is incomplete",
3154 parmnum + 1);
3155 parmval = val;
3157 else
3159 tree origtype;
3161 /* Optionally warn about conversions that
3162 differ from the default conversions. */
3163 if (warn_traditional_conversion || warn_traditional)
3165 unsigned int formal_prec = TYPE_PRECISION (type);
3167 if (INTEGRAL_TYPE_P (type)
3168 && TREE_CODE (valtype) == REAL_TYPE)
3169 warning_at (ploc, OPT_Wtraditional_conversion,
3170 "passing argument %d of %qE as integer rather "
3171 "than floating due to prototype",
3172 argnum, rname);
3173 if (INTEGRAL_TYPE_P (type)
3174 && TREE_CODE (valtype) == COMPLEX_TYPE)
3175 warning_at (ploc, OPT_Wtraditional_conversion,
3176 "passing argument %d of %qE as integer rather "
3177 "than complex due to prototype",
3178 argnum, rname);
3179 else if (TREE_CODE (type) == COMPLEX_TYPE
3180 && TREE_CODE (valtype) == REAL_TYPE)
3181 warning_at (ploc, OPT_Wtraditional_conversion,
3182 "passing argument %d of %qE as complex rather "
3183 "than floating due to prototype",
3184 argnum, rname);
3185 else if (TREE_CODE (type) == REAL_TYPE
3186 && INTEGRAL_TYPE_P (valtype))
3187 warning_at (ploc, OPT_Wtraditional_conversion,
3188 "passing argument %d of %qE as floating rather "
3189 "than integer due to prototype",
3190 argnum, rname);
3191 else if (TREE_CODE (type) == COMPLEX_TYPE
3192 && INTEGRAL_TYPE_P (valtype))
3193 warning_at (ploc, OPT_Wtraditional_conversion,
3194 "passing argument %d of %qE as complex rather "
3195 "than integer due to prototype",
3196 argnum, rname);
3197 else if (TREE_CODE (type) == REAL_TYPE
3198 && TREE_CODE (valtype) == COMPLEX_TYPE)
3199 warning_at (ploc, OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE as floating rather "
3201 "than complex due to prototype",
3202 argnum, rname);
3203 /* ??? At some point, messages should be written about
3204 conversions between complex types, but that's too messy
3205 to do now. */
3206 else if (TREE_CODE (type) == REAL_TYPE
3207 && TREE_CODE (valtype) == REAL_TYPE)
3209 /* Warn if any argument is passed as `float',
3210 since without a prototype it would be `double'. */
3211 if (formal_prec == TYPE_PRECISION (float_type_node)
3212 && type != dfloat32_type_node)
3213 warning_at (ploc, 0,
3214 "passing argument %d of %qE as %<float%> "
3215 "rather than %<double%> due to prototype",
3216 argnum, rname);
3218 /* Warn if mismatch between argument and prototype
3219 for decimal float types. Warn of conversions with
3220 binary float types and of precision narrowing due to
3221 prototype. */
3222 else if (type != valtype
3223 && (type == dfloat32_type_node
3224 || type == dfloat64_type_node
3225 || type == dfloat128_type_node
3226 || valtype == dfloat32_type_node
3227 || valtype == dfloat64_type_node
3228 || valtype == dfloat128_type_node)
3229 && (formal_prec
3230 <= TYPE_PRECISION (valtype)
3231 || (type == dfloat128_type_node
3232 && (valtype
3233 != dfloat64_type_node
3234 && (valtype
3235 != dfloat32_type_node)))
3236 || (type == dfloat64_type_node
3237 && (valtype
3238 != dfloat32_type_node))))
3239 warning_at (ploc, 0,
3240 "passing argument %d of %qE as %qT "
3241 "rather than %qT due to prototype",
3242 argnum, rname, type, valtype);
3245 /* Detect integer changing in width or signedness.
3246 These warnings are only activated with
3247 -Wtraditional-conversion, not with -Wtraditional. */
3248 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3249 && INTEGRAL_TYPE_P (valtype))
3251 tree would_have_been = default_conversion (val);
3252 tree type1 = TREE_TYPE (would_have_been);
3254 if (TREE_CODE (type) == ENUMERAL_TYPE
3255 && (TYPE_MAIN_VARIANT (type)
3256 == TYPE_MAIN_VARIANT (valtype)))
3257 /* No warning if function asks for enum
3258 and the actual arg is that enum type. */
3260 else if (formal_prec != TYPE_PRECISION (type1))
3261 warning_at (ploc, OPT_Wtraditional_conversion,
3262 "passing argument %d of %qE "
3263 "with different width due to prototype",
3264 argnum, rname);
3265 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3267 /* Don't complain if the formal parameter type
3268 is an enum, because we can't tell now whether
3269 the value was an enum--even the same enum. */
3270 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3272 else if (TREE_CODE (val) == INTEGER_CST
3273 && int_fits_type_p (val, type))
3274 /* Change in signedness doesn't matter
3275 if a constant value is unaffected. */
3277 /* If the value is extended from a narrower
3278 unsigned type, it doesn't matter whether we
3279 pass it as signed or unsigned; the value
3280 certainly is the same either way. */
3281 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3282 && TYPE_UNSIGNED (valtype))
3284 else if (TYPE_UNSIGNED (type))
3285 warning_at (ploc, OPT_Wtraditional_conversion,
3286 "passing argument %d of %qE "
3287 "as unsigned due to prototype",
3288 argnum, rname);
3289 else
3290 warning_at (ploc, OPT_Wtraditional_conversion,
3291 "passing argument %d of %qE "
3292 "as signed due to prototype",
3293 argnum, rname);
3297 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3298 sake of better warnings from convert_and_check. */
3299 if (excess_precision)
3300 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3301 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3302 parmval = convert_for_assignment (loc, ploc, type,
3303 val, origtype, ic_argpass,
3304 npc, fundecl, function,
3305 parmnum + 1);
3307 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3308 && INTEGRAL_TYPE_P (type)
3309 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3310 parmval = default_conversion (parmval);
3313 else if (TREE_CODE (valtype) == REAL_TYPE
3314 && (TYPE_PRECISION (valtype)
3315 <= TYPE_PRECISION (double_type_node))
3316 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3317 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3318 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3320 if (type_generic)
3321 parmval = val;
3322 else
3324 /* Convert `float' to `double'. */
3325 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3326 warning_at (ploc, OPT_Wdouble_promotion,
3327 "implicit conversion from %qT to %qT when passing "
3328 "argument to function",
3329 valtype, double_type_node);
3330 parmval = convert (double_type_node, val);
3333 else if (excess_precision && !type_generic)
3334 /* A "double" argument with excess precision being passed
3335 without a prototype or in variable arguments. */
3336 parmval = convert (valtype, val);
3337 else if ((invalid_func_diag =
3338 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3340 error (invalid_func_diag);
3341 return -1;
3343 else
3344 /* Convert `short' and `char' to full-size `int'. */
3345 parmval = default_conversion (val);
3347 (*values)[parmnum] = parmval;
3348 if (parmval == error_mark_node)
3349 error_args = true;
3351 if (typetail)
3352 typetail = TREE_CHAIN (typetail);
3355 gcc_assert (parmnum == vec_safe_length (values));
3357 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3359 error_at (loc, "too few arguments to function %qE", function);
3360 inform_declaration (fundecl);
3361 return -1;
3364 return error_args ? -1 : (int) parmnum;
3367 /* This is the entry point used by the parser to build unary operators
3368 in the input. CODE, a tree_code, specifies the unary operator, and
3369 ARG is the operand. For unary plus, the C parser currently uses
3370 CONVERT_EXPR for code.
3372 LOC is the location to use for the tree generated.
3375 struct c_expr
3376 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3378 struct c_expr result;
3380 result.value = build_unary_op (loc, code, arg.value, 0);
3381 result.original_code = code;
3382 result.original_type = NULL;
3384 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3385 overflow_warning (loc, result.value);
3387 return result;
3390 /* This is the entry point used by the parser to build binary operators
3391 in the input. CODE, a tree_code, specifies the binary operator, and
3392 ARG1 and ARG2 are the operands. In addition to constructing the
3393 expression, we check for operands that were written with other binary
3394 operators in a way that is likely to confuse the user.
3396 LOCATION is the location of the binary operator. */
3398 struct c_expr
3399 parser_build_binary_op (location_t location, enum tree_code code,
3400 struct c_expr arg1, struct c_expr arg2)
3402 struct c_expr result;
3404 enum tree_code code1 = arg1.original_code;
3405 enum tree_code code2 = arg2.original_code;
3406 tree type1 = (arg1.original_type
3407 ? arg1.original_type
3408 : TREE_TYPE (arg1.value));
3409 tree type2 = (arg2.original_type
3410 ? arg2.original_type
3411 : TREE_TYPE (arg2.value));
3413 result.value = build_binary_op (location, code,
3414 arg1.value, arg2.value, 1);
3415 result.original_code = code;
3416 result.original_type = NULL;
3418 if (TREE_CODE (result.value) == ERROR_MARK)
3419 return result;
3421 if (location != UNKNOWN_LOCATION)
3422 protected_set_expr_location (result.value, location);
3424 /* Check for cases such as x+y<<z which users are likely
3425 to misinterpret. */
3426 if (warn_parentheses)
3427 warn_about_parentheses (location, code, code1, arg1.value, code2,
3428 arg2.value);
3430 if (warn_logical_op)
3431 warn_logical_operator (location, code, TREE_TYPE (result.value),
3432 code1, arg1.value, code2, arg2.value);
3434 if (warn_logical_not_paren
3435 && code1 == TRUTH_NOT_EXPR
3436 && code2 != TRUTH_NOT_EXPR)
3437 warn_logical_not_parentheses (location, code, arg2.value);
3439 /* Warn about comparisons against string literals, with the exception
3440 of testing for equality or inequality of a string literal with NULL. */
3441 if (code == EQ_EXPR || code == NE_EXPR)
3443 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3444 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3445 warning_at (location, OPT_Waddress,
3446 "comparison with string literal results in unspecified behavior");
3448 else if (TREE_CODE_CLASS (code) == tcc_comparison
3449 && (code1 == STRING_CST || code2 == STRING_CST))
3450 warning_at (location, OPT_Waddress,
3451 "comparison with string literal results in unspecified behavior");
3453 if (TREE_OVERFLOW_P (result.value)
3454 && !TREE_OVERFLOW_P (arg1.value)
3455 && !TREE_OVERFLOW_P (arg2.value))
3456 overflow_warning (location, result.value);
3458 /* Warn about comparisons of different enum types. */
3459 if (warn_enum_compare
3460 && TREE_CODE_CLASS (code) == tcc_comparison
3461 && TREE_CODE (type1) == ENUMERAL_TYPE
3462 && TREE_CODE (type2) == ENUMERAL_TYPE
3463 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3464 warning_at (location, OPT_Wenum_compare,
3465 "comparison between %qT and %qT",
3466 type1, type2);
3468 return result;
3471 /* Return a tree for the difference of pointers OP0 and OP1.
3472 The resulting tree has type int. */
3474 static tree
3475 pointer_diff (location_t loc, tree op0, tree op1)
3477 tree restype = ptrdiff_type_node;
3478 tree result, inttype;
3480 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3481 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3482 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3483 tree orig_op1 = op1;
3485 /* If the operands point into different address spaces, we need to
3486 explicitly convert them to pointers into the common address space
3487 before we can subtract the numerical address values. */
3488 if (as0 != as1)
3490 addr_space_t as_common;
3491 tree common_type;
3493 /* Determine the common superset address space. This is guaranteed
3494 to exist because the caller verified that comp_target_types
3495 returned non-zero. */
3496 if (!addr_space_superset (as0, as1, &as_common))
3497 gcc_unreachable ();
3499 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3500 op0 = convert (common_type, op0);
3501 op1 = convert (common_type, op1);
3504 /* Determine integer type to perform computations in. This will usually
3505 be the same as the result type (ptrdiff_t), but may need to be a wider
3506 type if pointers for the address space are wider than ptrdiff_t. */
3507 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3508 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3509 else
3510 inttype = restype;
3512 if (TREE_CODE (target_type) == VOID_TYPE)
3513 pedwarn (loc, OPT_Wpointer_arith,
3514 "pointer of type %<void *%> used in subtraction");
3515 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3516 pedwarn (loc, OPT_Wpointer_arith,
3517 "pointer to a function used in subtraction");
3519 /* First do the subtraction as integers;
3520 then drop through to build the divide operator.
3521 Do not do default conversions on the minus operator
3522 in case restype is a short type. */
3524 op0 = build_binary_op (loc,
3525 MINUS_EXPR, convert (inttype, op0),
3526 convert (inttype, op1), 0);
3527 /* This generates an error if op1 is pointer to incomplete type. */
3528 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3529 error_at (loc, "arithmetic on pointer to an incomplete type");
3531 op1 = c_size_in_bytes (target_type);
3533 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3534 error_at (loc, "arithmetic on pointer to an empty aggregate");
3536 /* Divide by the size, in easiest possible way. */
3537 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3538 op0, convert (inttype, op1));
3540 /* Convert to final result type if necessary. */
3541 return convert (restype, result);
3544 /* Expand atomic compound assignments into an approriate sequence as
3545 specified by the C11 standard section 6.5.16.2.
3546 given
3547 _Atomic T1 E1
3548 T2 E2
3549 E1 op= E2
3551 This sequence is used for all types for which these operations are
3552 supported.
3554 In addition, built-in versions of the 'fe' prefixed routines may
3555 need to be invoked for floating point (real, complex or vector) when
3556 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3558 T1 newval;
3559 T1 old;
3560 T1 *addr
3561 T2 val
3562 fenv_t fenv
3564 addr = &E1;
3565 val = (E2);
3566 __atomic_load (addr, &old, SEQ_CST);
3567 feholdexcept (&fenv);
3568 loop:
3569 newval = old op val;
3570 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3571 SEQ_CST))
3572 goto done;
3573 feclearexcept (FE_ALL_EXCEPT);
3574 goto loop:
3575 done:
3576 feupdateenv (&fenv);
3578 Also note that the compiler is simply issuing the generic form of
3579 the atomic operations. This requires temp(s) and has their address
3580 taken. The atomic processing is smart enough to figure out when the
3581 size of an object can utilize a lock-free version, and convert the
3582 built-in call to the appropriate lock-free routine. The optimizers
3583 will then dispose of any temps that are no longer required, and
3584 lock-free implementations are utilized as long as there is target
3585 support for the required size.
3587 If the operator is NOP_EXPR, then this is a simple assignment, and
3588 an __atomic_store is issued to perform the assignment rather than
3589 the above loop.
3593 /* Build an atomic assignment at LOC, expanding into the proper
3594 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3595 the result of the operation, unless RETURN_OLD_P in which case
3596 return the old value of LHS (this is only for postincrement and
3597 postdecrement). */
3598 static tree
3599 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3600 tree rhs, bool return_old_p)
3602 tree fndecl, func_call;
3603 vec<tree, va_gc> *params;
3604 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3605 tree old, old_addr;
3606 tree compound_stmt;
3607 tree stmt, goto_stmt;
3608 tree loop_label, loop_decl, done_label, done_decl;
3610 tree lhs_type = TREE_TYPE (lhs);
3611 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3612 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3613 tree rhs_type = TREE_TYPE (rhs);
3615 gcc_assert (TYPE_ATOMIC (lhs_type));
3617 if (return_old_p)
3618 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3620 /* Allocate enough vector items for a compare_exchange. */
3621 vec_alloc (params, 6);
3623 /* Create a compound statement to hold the sequence of statements
3624 with a loop. */
3625 compound_stmt = c_begin_compound_stmt (false);
3627 /* Fold the RHS if it hasn't already been folded. */
3628 if (modifycode != NOP_EXPR)
3629 rhs = c_fully_fold (rhs, false, NULL);
3631 /* Remove the qualifiers for the rest of the expressions and create
3632 the VAL temp variable to hold the RHS. */
3633 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3634 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3635 val = create_tmp_var (nonatomic_rhs_type, NULL);
3636 TREE_ADDRESSABLE (val) = 1;
3637 TREE_NO_WARNING (val) = 1;
3638 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3639 SET_EXPR_LOCATION (rhs, loc);
3640 add_stmt (rhs);
3642 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3643 an atomic_store. */
3644 if (modifycode == NOP_EXPR)
3646 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3647 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3648 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3649 params->quick_push (lhs_addr);
3650 params->quick_push (rhs);
3651 params->quick_push (seq_cst);
3652 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3653 add_stmt (func_call);
3655 /* Finish the compound statement. */
3656 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3658 /* VAL is the value which was stored, return a COMPOUND_STMT of
3659 the statement and that value. */
3660 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3663 /* Create the variables and labels required for the op= form. */
3664 old = create_tmp_var (nonatomic_lhs_type, NULL);
3665 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3666 TREE_ADDRESSABLE (old) = 1;
3667 TREE_NO_WARNING (old) = 1;
3669 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3670 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3671 TREE_ADDRESSABLE (newval) = 1;
3673 loop_decl = create_artificial_label (loc);
3674 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3676 done_decl = create_artificial_label (loc);
3677 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3679 /* __atomic_load (addr, &old, SEQ_CST). */
3680 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3681 params->quick_push (lhs_addr);
3682 params->quick_push (old_addr);
3683 params->quick_push (seq_cst);
3684 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3685 add_stmt (func_call);
3686 params->truncate (0);
3688 /* Create the expressions for floating-point environment
3689 manipulation, if required. */
3690 bool need_fenv = (flag_trapping_math
3691 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3692 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3693 if (need_fenv)
3694 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3696 if (hold_call)
3697 add_stmt (hold_call);
3699 /* loop: */
3700 add_stmt (loop_label);
3702 /* newval = old + val; */
3703 rhs = build_binary_op (loc, modifycode, old, val, 1);
3704 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3705 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3706 NULL_TREE, 0);
3707 if (rhs != error_mark_node)
3709 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3710 SET_EXPR_LOCATION (rhs, loc);
3711 add_stmt (rhs);
3714 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3715 goto done; */
3716 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3717 params->quick_push (lhs_addr);
3718 params->quick_push (old_addr);
3719 params->quick_push (newval_addr);
3720 params->quick_push (integer_zero_node);
3721 params->quick_push (seq_cst);
3722 params->quick_push (seq_cst);
3723 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3725 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3726 SET_EXPR_LOCATION (goto_stmt, loc);
3728 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3729 SET_EXPR_LOCATION (stmt, loc);
3730 add_stmt (stmt);
3732 if (clear_call)
3733 add_stmt (clear_call);
3735 /* goto loop; */
3736 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3737 SET_EXPR_LOCATION (goto_stmt, loc);
3738 add_stmt (goto_stmt);
3740 /* done: */
3741 add_stmt (done_label);
3743 if (update_call)
3744 add_stmt (update_call);
3746 /* Finish the compound statement. */
3747 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3749 /* NEWVAL is the value that was successfully stored, return a
3750 COMPOUND_EXPR of the statement and the appropriate value. */
3751 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3752 return_old_p ? old : newval);
3755 /* Construct and perhaps optimize a tree representation
3756 for a unary operation. CODE, a tree_code, specifies the operation
3757 and XARG is the operand.
3758 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3759 the default promotions (such as from short to int).
3760 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3761 allows non-lvalues; this is only used to handle conversion of non-lvalue
3762 arrays to pointers in C99.
3764 LOCATION is the location of the operator. */
3766 tree
3767 build_unary_op (location_t location,
3768 enum tree_code code, tree xarg, int flag)
3770 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3771 tree arg = xarg;
3772 tree argtype = 0;
3773 enum tree_code typecode;
3774 tree val;
3775 tree ret = error_mark_node;
3776 tree eptype = NULL_TREE;
3777 int noconvert = flag;
3778 const char *invalid_op_diag;
3779 bool int_operands;
3781 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3782 if (int_operands)
3783 arg = remove_c_maybe_const_expr (arg);
3785 if (code != ADDR_EXPR)
3786 arg = require_complete_type (arg);
3788 typecode = TREE_CODE (TREE_TYPE (arg));
3789 if (typecode == ERROR_MARK)
3790 return error_mark_node;
3791 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3792 typecode = INTEGER_TYPE;
3794 if ((invalid_op_diag
3795 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3797 error_at (location, invalid_op_diag);
3798 return error_mark_node;
3801 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3803 eptype = TREE_TYPE (arg);
3804 arg = TREE_OPERAND (arg, 0);
3807 switch (code)
3809 case CONVERT_EXPR:
3810 /* This is used for unary plus, because a CONVERT_EXPR
3811 is enough to prevent anybody from looking inside for
3812 associativity, but won't generate any code. */
3813 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3814 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3815 || typecode == VECTOR_TYPE))
3817 error_at (location, "wrong type argument to unary plus");
3818 return error_mark_node;
3820 else if (!noconvert)
3821 arg = default_conversion (arg);
3822 arg = non_lvalue_loc (location, arg);
3823 break;
3825 case NEGATE_EXPR:
3826 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3827 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3828 || typecode == VECTOR_TYPE))
3830 error_at (location, "wrong type argument to unary minus");
3831 return error_mark_node;
3833 else if (!noconvert)
3834 arg = default_conversion (arg);
3835 break;
3837 case BIT_NOT_EXPR:
3838 /* ~ works on integer types and non float vectors. */
3839 if (typecode == INTEGER_TYPE
3840 || (typecode == VECTOR_TYPE
3841 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3843 if (!noconvert)
3844 arg = default_conversion (arg);
3846 else if (typecode == COMPLEX_TYPE)
3848 code = CONJ_EXPR;
3849 pedwarn (location, OPT_Wpedantic,
3850 "ISO C does not support %<~%> for complex conjugation");
3851 if (!noconvert)
3852 arg = default_conversion (arg);
3854 else
3856 error_at (location, "wrong type argument to bit-complement");
3857 return error_mark_node;
3859 break;
3861 case ABS_EXPR:
3862 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3864 error_at (location, "wrong type argument to abs");
3865 return error_mark_node;
3867 else if (!noconvert)
3868 arg = default_conversion (arg);
3869 break;
3871 case CONJ_EXPR:
3872 /* Conjugating a real value is a no-op, but allow it anyway. */
3873 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3874 || typecode == COMPLEX_TYPE))
3876 error_at (location, "wrong type argument to conjugation");
3877 return error_mark_node;
3879 else if (!noconvert)
3880 arg = default_conversion (arg);
3881 break;
3883 case TRUTH_NOT_EXPR:
3884 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3885 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3886 && typecode != COMPLEX_TYPE)
3888 error_at (location,
3889 "wrong type argument to unary exclamation mark");
3890 return error_mark_node;
3892 if (int_operands)
3894 arg = c_objc_common_truthvalue_conversion (location, xarg);
3895 arg = remove_c_maybe_const_expr (arg);
3897 else
3898 arg = c_objc_common_truthvalue_conversion (location, arg);
3899 ret = invert_truthvalue_loc (location, arg);
3900 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3901 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3902 location = EXPR_LOCATION (ret);
3903 goto return_build_unary_op;
3905 case REALPART_EXPR:
3906 case IMAGPART_EXPR:
3907 ret = build_real_imag_expr (location, code, arg);
3908 if (ret == error_mark_node)
3909 return error_mark_node;
3910 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3911 eptype = TREE_TYPE (eptype);
3912 goto return_build_unary_op;
3914 case PREINCREMENT_EXPR:
3915 case POSTINCREMENT_EXPR:
3916 case PREDECREMENT_EXPR:
3917 case POSTDECREMENT_EXPR:
3919 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3921 tree inner = build_unary_op (location, code,
3922 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3923 if (inner == error_mark_node)
3924 return error_mark_node;
3925 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3926 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3927 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3928 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3929 goto return_build_unary_op;
3932 /* Complain about anything that is not a true lvalue. In
3933 Objective-C, skip this check for property_refs. */
3934 if (!objc_is_property_ref (arg)
3935 && !lvalue_or_else (location,
3936 arg, ((code == PREINCREMENT_EXPR
3937 || code == POSTINCREMENT_EXPR)
3938 ? lv_increment
3939 : lv_decrement)))
3940 return error_mark_node;
3942 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3944 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3945 warning_at (location, OPT_Wc___compat,
3946 "increment of enumeration value is invalid in C++");
3947 else
3948 warning_at (location, OPT_Wc___compat,
3949 "decrement of enumeration value is invalid in C++");
3952 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3953 arg = c_fully_fold (arg, false, NULL);
3955 bool atomic_op;
3956 atomic_op = really_atomic_lvalue (arg);
3958 /* Increment or decrement the real part of the value,
3959 and don't change the imaginary part. */
3960 if (typecode == COMPLEX_TYPE)
3962 tree real, imag;
3964 pedwarn (location, OPT_Wpedantic,
3965 "ISO C does not support %<++%> and %<--%> on complex types");
3967 if (!atomic_op)
3969 arg = stabilize_reference (arg);
3970 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3971 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3972 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3973 if (real == error_mark_node || imag == error_mark_node)
3974 return error_mark_node;
3975 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3976 real, imag);
3977 goto return_build_unary_op;
3981 /* Report invalid types. */
3983 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3984 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3985 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
3987 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3988 error_at (location, "wrong type argument to increment");
3989 else
3990 error_at (location, "wrong type argument to decrement");
3992 return error_mark_node;
3996 tree inc;
3998 argtype = TREE_TYPE (arg);
4000 /* Compute the increment. */
4002 if (typecode == POINTER_TYPE)
4004 /* If pointer target is an incomplete type,
4005 we just cannot know how to do the arithmetic. */
4006 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4008 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4009 error_at (location,
4010 "increment of pointer to an incomplete type %qT",
4011 TREE_TYPE (argtype));
4012 else
4013 error_at (location,
4014 "decrement of pointer to an incomplete type %qT",
4015 TREE_TYPE (argtype));
4017 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4018 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4020 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4021 pedwarn (location, OPT_Wpointer_arith,
4022 "wrong type argument to increment");
4023 else
4024 pedwarn (location, OPT_Wpointer_arith,
4025 "wrong type argument to decrement");
4028 inc = c_size_in_bytes (TREE_TYPE (argtype));
4029 inc = convert_to_ptrofftype_loc (location, inc);
4031 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4033 /* For signed fract types, we invert ++ to -- or
4034 -- to ++, and change inc from 1 to -1, because
4035 it is not possible to represent 1 in signed fract constants.
4036 For unsigned fract types, the result always overflows and
4037 we get an undefined (original) or the maximum value. */
4038 if (code == PREINCREMENT_EXPR)
4039 code = PREDECREMENT_EXPR;
4040 else if (code == PREDECREMENT_EXPR)
4041 code = PREINCREMENT_EXPR;
4042 else if (code == POSTINCREMENT_EXPR)
4043 code = POSTDECREMENT_EXPR;
4044 else /* code == POSTDECREMENT_EXPR */
4045 code = POSTINCREMENT_EXPR;
4047 inc = integer_minus_one_node;
4048 inc = convert (argtype, inc);
4050 else
4052 inc = VECTOR_TYPE_P (argtype)
4053 ? build_one_cst (argtype)
4054 : integer_one_node;
4055 inc = convert (argtype, inc);
4058 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4059 need to ask Objective-C to build the increment or decrement
4060 expression for it. */
4061 if (objc_is_property_ref (arg))
4062 return objc_build_incr_expr_for_property_ref (location, code,
4063 arg, inc);
4065 /* Report a read-only lvalue. */
4066 if (TYPE_READONLY (argtype))
4068 readonly_error (location, arg,
4069 ((code == PREINCREMENT_EXPR
4070 || code == POSTINCREMENT_EXPR)
4071 ? lv_increment : lv_decrement));
4072 return error_mark_node;
4074 else if (TREE_READONLY (arg))
4075 readonly_warning (arg,
4076 ((code == PREINCREMENT_EXPR
4077 || code == POSTINCREMENT_EXPR)
4078 ? lv_increment : lv_decrement));
4080 /* If the argument is atomic, use the special code sequences for
4081 atomic compound assignment. */
4082 if (atomic_op)
4084 arg = stabilize_reference (arg);
4085 ret = build_atomic_assign (location, arg,
4086 ((code == PREINCREMENT_EXPR
4087 || code == POSTINCREMENT_EXPR)
4088 ? PLUS_EXPR
4089 : MINUS_EXPR),
4090 (FRACT_MODE_P (TYPE_MODE (argtype))
4091 ? inc
4092 : integer_one_node),
4093 (code == POSTINCREMENT_EXPR
4094 || code == POSTDECREMENT_EXPR));
4095 goto return_build_unary_op;
4098 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4099 val = boolean_increment (code, arg);
4100 else
4101 val = build2 (code, TREE_TYPE (arg), arg, inc);
4102 TREE_SIDE_EFFECTS (val) = 1;
4103 if (TREE_CODE (val) != code)
4104 TREE_NO_WARNING (val) = 1;
4105 ret = val;
4106 goto return_build_unary_op;
4109 case ADDR_EXPR:
4110 /* Note that this operation never does default_conversion. */
4112 /* The operand of unary '&' must be an lvalue (which excludes
4113 expressions of type void), or, in C99, the result of a [] or
4114 unary '*' operator. */
4115 if (VOID_TYPE_P (TREE_TYPE (arg))
4116 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4117 && (TREE_CODE (arg) != INDIRECT_REF
4118 || !flag_isoc99))
4119 pedwarn (location, 0, "taking address of expression of type %<void%>");
4121 /* Let &* cancel out to simplify resulting code. */
4122 if (TREE_CODE (arg) == INDIRECT_REF)
4124 /* Don't let this be an lvalue. */
4125 if (lvalue_p (TREE_OPERAND (arg, 0)))
4126 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4127 ret = TREE_OPERAND (arg, 0);
4128 goto return_build_unary_op;
4131 /* For &x[y], return x+y */
4132 if (TREE_CODE (arg) == ARRAY_REF)
4134 tree op0 = TREE_OPERAND (arg, 0);
4135 if (!c_mark_addressable (op0))
4136 return error_mark_node;
4139 /* Anything not already handled and not a true memory reference
4140 or a non-lvalue array is an error. */
4141 else if (typecode != FUNCTION_TYPE && !flag
4142 && !lvalue_or_else (location, arg, lv_addressof))
4143 return error_mark_node;
4145 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4146 folding later. */
4147 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4149 tree inner = build_unary_op (location, code,
4150 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4151 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4152 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4153 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4154 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4155 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4156 goto return_build_unary_op;
4159 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4160 argtype = TREE_TYPE (arg);
4162 /* If the lvalue is const or volatile, merge that into the type
4163 to which the address will point. This is only needed
4164 for function types. */
4165 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4166 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4167 && TREE_CODE (argtype) == FUNCTION_TYPE)
4169 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4170 int quals = orig_quals;
4172 if (TREE_READONLY (arg))
4173 quals |= TYPE_QUAL_CONST;
4174 if (TREE_THIS_VOLATILE (arg))
4175 quals |= TYPE_QUAL_VOLATILE;
4177 argtype = c_build_qualified_type (argtype, quals);
4180 if (!c_mark_addressable (arg))
4181 return error_mark_node;
4183 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4184 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4186 argtype = build_pointer_type (argtype);
4188 /* ??? Cope with user tricks that amount to offsetof. Delete this
4189 when we have proper support for integer constant expressions. */
4190 val = get_base_address (arg);
4191 if (val && TREE_CODE (val) == INDIRECT_REF
4192 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4194 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4195 goto return_build_unary_op;
4198 val = build1 (ADDR_EXPR, argtype, arg);
4200 ret = val;
4201 goto return_build_unary_op;
4203 default:
4204 gcc_unreachable ();
4207 if (argtype == 0)
4208 argtype = TREE_TYPE (arg);
4209 if (TREE_CODE (arg) == INTEGER_CST)
4210 ret = (require_constant_value
4211 ? fold_build1_initializer_loc (location, code, argtype, arg)
4212 : fold_build1_loc (location, code, argtype, arg));
4213 else
4214 ret = build1 (code, argtype, arg);
4215 return_build_unary_op:
4216 gcc_assert (ret != error_mark_node);
4217 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4218 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4219 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4220 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4221 ret = note_integer_operands (ret);
4222 if (eptype)
4223 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4224 protected_set_expr_location (ret, location);
4225 return ret;
4228 /* Return nonzero if REF is an lvalue valid for this language.
4229 Lvalues can be assigned, unless their type has TYPE_READONLY.
4230 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4232 bool
4233 lvalue_p (const_tree ref)
4235 const enum tree_code code = TREE_CODE (ref);
4237 switch (code)
4239 case REALPART_EXPR:
4240 case IMAGPART_EXPR:
4241 case COMPONENT_REF:
4242 return lvalue_p (TREE_OPERAND (ref, 0));
4244 case C_MAYBE_CONST_EXPR:
4245 return lvalue_p (TREE_OPERAND (ref, 1));
4247 case COMPOUND_LITERAL_EXPR:
4248 case STRING_CST:
4249 return 1;
4251 case INDIRECT_REF:
4252 case ARRAY_REF:
4253 case ARRAY_NOTATION_REF:
4254 case VAR_DECL:
4255 case PARM_DECL:
4256 case RESULT_DECL:
4257 case ERROR_MARK:
4258 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4259 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4261 case BIND_EXPR:
4262 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4264 default:
4265 return 0;
4269 /* Give a warning for storing in something that is read-only in GCC
4270 terms but not const in ISO C terms. */
4272 static void
4273 readonly_warning (tree arg, enum lvalue_use use)
4275 switch (use)
4277 case lv_assign:
4278 warning (0, "assignment of read-only location %qE", arg);
4279 break;
4280 case lv_increment:
4281 warning (0, "increment of read-only location %qE", arg);
4282 break;
4283 case lv_decrement:
4284 warning (0, "decrement of read-only location %qE", arg);
4285 break;
4286 default:
4287 gcc_unreachable ();
4289 return;
4293 /* Return nonzero if REF is an lvalue valid for this language;
4294 otherwise, print an error message and return zero. USE says
4295 how the lvalue is being used and so selects the error message.
4296 LOCATION is the location at which any error should be reported. */
4298 static int
4299 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4301 int win = lvalue_p (ref);
4303 if (!win)
4304 lvalue_error (loc, use);
4306 return win;
4309 /* Mark EXP saying that we need to be able to take the
4310 address of it; it should not be allocated in a register.
4311 Returns true if successful. */
4313 bool
4314 c_mark_addressable (tree exp)
4316 tree x = exp;
4318 while (1)
4319 switch (TREE_CODE (x))
4321 case COMPONENT_REF:
4322 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4324 error
4325 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4326 return false;
4329 /* ... fall through ... */
4331 case ADDR_EXPR:
4332 case ARRAY_REF:
4333 case REALPART_EXPR:
4334 case IMAGPART_EXPR:
4335 x = TREE_OPERAND (x, 0);
4336 break;
4338 case COMPOUND_LITERAL_EXPR:
4339 case CONSTRUCTOR:
4340 TREE_ADDRESSABLE (x) = 1;
4341 return true;
4343 case VAR_DECL:
4344 case CONST_DECL:
4345 case PARM_DECL:
4346 case RESULT_DECL:
4347 if (C_DECL_REGISTER (x)
4348 && DECL_NONLOCAL (x))
4350 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4352 error
4353 ("global register variable %qD used in nested function", x);
4354 return false;
4356 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4358 else if (C_DECL_REGISTER (x))
4360 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4361 error ("address of global register variable %qD requested", x);
4362 else
4363 error ("address of register variable %qD requested", x);
4364 return false;
4367 /* drops in */
4368 case FUNCTION_DECL:
4369 TREE_ADDRESSABLE (x) = 1;
4370 /* drops out */
4371 default:
4372 return true;
4376 /* Convert EXPR to TYPE, warning about conversion problems with
4377 constants. SEMANTIC_TYPE is the type this conversion would use
4378 without excess precision. If SEMANTIC_TYPE is NULL, this function
4379 is equivalent to convert_and_check. This function is a wrapper that
4380 handles conversions that may be different than
4381 the usual ones because of excess precision. */
4383 static tree
4384 ep_convert_and_check (location_t loc, tree type, tree expr,
4385 tree semantic_type)
4387 if (TREE_TYPE (expr) == type)
4388 return expr;
4390 if (!semantic_type)
4391 return convert_and_check (loc, type, expr);
4393 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4394 && TREE_TYPE (expr) != semantic_type)
4396 /* For integers, we need to check the real conversion, not
4397 the conversion to the excess precision type. */
4398 expr = convert_and_check (loc, semantic_type, expr);
4400 /* Result type is the excess precision type, which should be
4401 large enough, so do not check. */
4402 return convert (type, expr);
4405 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4406 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4407 if folded to an integer constant then the unselected half may
4408 contain arbitrary operations not normally permitted in constant
4409 expressions. Set the location of the expression to LOC. */
4411 tree
4412 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4413 tree op1, tree op1_original_type, tree op2,
4414 tree op2_original_type)
4416 tree type1;
4417 tree type2;
4418 enum tree_code code1;
4419 enum tree_code code2;
4420 tree result_type = NULL;
4421 tree semantic_result_type = NULL;
4422 tree orig_op1 = op1, orig_op2 = op2;
4423 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4424 bool ifexp_int_operands;
4425 tree ret;
4427 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4428 if (op1_int_operands)
4429 op1 = remove_c_maybe_const_expr (op1);
4430 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4431 if (op2_int_operands)
4432 op2 = remove_c_maybe_const_expr (op2);
4433 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4434 if (ifexp_int_operands)
4435 ifexp = remove_c_maybe_const_expr (ifexp);
4437 /* Promote both alternatives. */
4439 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4440 op1 = default_conversion (op1);
4441 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4442 op2 = default_conversion (op2);
4444 if (TREE_CODE (ifexp) == ERROR_MARK
4445 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4446 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4447 return error_mark_node;
4449 type1 = TREE_TYPE (op1);
4450 code1 = TREE_CODE (type1);
4451 type2 = TREE_TYPE (op2);
4452 code2 = TREE_CODE (type2);
4454 /* C90 does not permit non-lvalue arrays in conditional expressions.
4455 In C99 they will be pointers by now. */
4456 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4458 error_at (colon_loc, "non-lvalue array in conditional expression");
4459 return error_mark_node;
4462 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4463 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4464 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4465 || code1 == COMPLEX_TYPE)
4466 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4467 || code2 == COMPLEX_TYPE))
4469 semantic_result_type = c_common_type (type1, type2);
4470 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4472 op1 = TREE_OPERAND (op1, 0);
4473 type1 = TREE_TYPE (op1);
4474 gcc_assert (TREE_CODE (type1) == code1);
4476 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4478 op2 = TREE_OPERAND (op2, 0);
4479 type2 = TREE_TYPE (op2);
4480 gcc_assert (TREE_CODE (type2) == code2);
4484 if (warn_cxx_compat)
4486 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4487 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4489 if (TREE_CODE (t1) == ENUMERAL_TYPE
4490 && TREE_CODE (t2) == ENUMERAL_TYPE
4491 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4492 warning_at (colon_loc, OPT_Wc___compat,
4493 ("different enum types in conditional is "
4494 "invalid in C++: %qT vs %qT"),
4495 t1, t2);
4498 /* Quickly detect the usual case where op1 and op2 have the same type
4499 after promotion. */
4500 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4502 if (type1 == type2)
4503 result_type = type1;
4504 else
4505 result_type = TYPE_MAIN_VARIANT (type1);
4507 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4508 || code1 == COMPLEX_TYPE)
4509 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4510 || code2 == COMPLEX_TYPE))
4512 result_type = c_common_type (type1, type2);
4513 do_warn_double_promotion (result_type, type1, type2,
4514 "implicit conversion from %qT to %qT to "
4515 "match other result of conditional",
4516 colon_loc);
4518 /* If -Wsign-compare, warn here if type1 and type2 have
4519 different signedness. We'll promote the signed to unsigned
4520 and later code won't know it used to be different.
4521 Do this check on the original types, so that explicit casts
4522 will be considered, but default promotions won't. */
4523 if (c_inhibit_evaluation_warnings == 0)
4525 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4526 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4528 if (unsigned_op1 ^ unsigned_op2)
4530 bool ovf;
4532 /* Do not warn if the result type is signed, since the
4533 signed type will only be chosen if it can represent
4534 all the values of the unsigned type. */
4535 if (!TYPE_UNSIGNED (result_type))
4536 /* OK */;
4537 else
4539 bool op1_maybe_const = true;
4540 bool op2_maybe_const = true;
4542 /* Do not warn if the signed quantity is an
4543 unsuffixed integer literal (or some static
4544 constant expression involving such literals) and
4545 it is non-negative. This warning requires the
4546 operands to be folded for best results, so do
4547 that folding in this case even without
4548 warn_sign_compare to avoid warning options
4549 possibly affecting code generation. */
4550 c_inhibit_evaluation_warnings
4551 += (ifexp == truthvalue_false_node);
4552 op1 = c_fully_fold (op1, require_constant_value,
4553 &op1_maybe_const);
4554 c_inhibit_evaluation_warnings
4555 -= (ifexp == truthvalue_false_node);
4557 c_inhibit_evaluation_warnings
4558 += (ifexp == truthvalue_true_node);
4559 op2 = c_fully_fold (op2, require_constant_value,
4560 &op2_maybe_const);
4561 c_inhibit_evaluation_warnings
4562 -= (ifexp == truthvalue_true_node);
4564 if (warn_sign_compare)
4566 if ((unsigned_op2
4567 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4568 || (unsigned_op1
4569 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4570 /* OK */;
4571 else
4572 warning_at (colon_loc, OPT_Wsign_compare,
4573 ("signed and unsigned type in "
4574 "conditional expression"));
4576 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4577 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4578 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4579 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4584 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4586 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4587 pedwarn (colon_loc, OPT_Wpedantic,
4588 "ISO C forbids conditional expr with only one void side");
4589 result_type = void_type_node;
4591 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4593 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4594 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4595 addr_space_t as_common;
4597 if (comp_target_types (colon_loc, type1, type2))
4598 result_type = common_pointer_type (type1, type2);
4599 else if (null_pointer_constant_p (orig_op1))
4600 result_type = type2;
4601 else if (null_pointer_constant_p (orig_op2))
4602 result_type = type1;
4603 else if (!addr_space_superset (as1, as2, &as_common))
4605 error_at (colon_loc, "pointers to disjoint address spaces "
4606 "used in conditional expression");
4607 return error_mark_node;
4609 else if (VOID_TYPE_P (TREE_TYPE (type1))
4610 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4612 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4613 pedwarn (colon_loc, OPT_Wpedantic,
4614 "ISO C forbids conditional expr between "
4615 "%<void *%> and function pointer");
4616 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4617 TREE_TYPE (type2)));
4619 else if (VOID_TYPE_P (TREE_TYPE (type2))
4620 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4622 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4623 pedwarn (colon_loc, OPT_Wpedantic,
4624 "ISO C forbids conditional expr between "
4625 "%<void *%> and function pointer");
4626 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4627 TREE_TYPE (type1)));
4629 /* Objective-C pointer comparisons are a bit more lenient. */
4630 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4631 result_type = objc_common_type (type1, type2);
4632 else
4634 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4636 pedwarn (colon_loc, 0,
4637 "pointer type mismatch in conditional expression");
4638 result_type = build_pointer_type
4639 (build_qualified_type (void_type_node, qual));
4642 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4644 if (!null_pointer_constant_p (orig_op2))
4645 pedwarn (colon_loc, 0,
4646 "pointer/integer type mismatch in conditional expression");
4647 else
4649 op2 = null_pointer_node;
4651 result_type = type1;
4653 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4655 if (!null_pointer_constant_p (orig_op1))
4656 pedwarn (colon_loc, 0,
4657 "pointer/integer type mismatch in conditional expression");
4658 else
4660 op1 = null_pointer_node;
4662 result_type = type2;
4665 if (!result_type)
4667 if (flag_cond_mismatch)
4668 result_type = void_type_node;
4669 else
4671 error_at (colon_loc, "type mismatch in conditional expression");
4672 return error_mark_node;
4676 /* Merge const and volatile flags of the incoming types. */
4677 result_type
4678 = build_type_variant (result_type,
4679 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4680 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4682 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4683 semantic_result_type);
4684 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4685 semantic_result_type);
4687 if (ifexp_bcp && ifexp == truthvalue_true_node)
4689 op2_int_operands = true;
4690 op1 = c_fully_fold (op1, require_constant_value, NULL);
4692 if (ifexp_bcp && ifexp == truthvalue_false_node)
4694 op1_int_operands = true;
4695 op2 = c_fully_fold (op2, require_constant_value, NULL);
4697 int_const = int_operands = (ifexp_int_operands
4698 && op1_int_operands
4699 && op2_int_operands);
4700 if (int_operands)
4702 int_const = ((ifexp == truthvalue_true_node
4703 && TREE_CODE (orig_op1) == INTEGER_CST
4704 && !TREE_OVERFLOW (orig_op1))
4705 || (ifexp == truthvalue_false_node
4706 && TREE_CODE (orig_op2) == INTEGER_CST
4707 && !TREE_OVERFLOW (orig_op2)));
4709 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4710 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4711 else
4713 if (int_operands)
4715 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4716 nested inside of the expression. */
4717 op1 = c_fully_fold (op1, false, NULL);
4718 op2 = c_fully_fold (op2, false, NULL);
4720 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4721 if (int_operands)
4722 ret = note_integer_operands (ret);
4724 if (semantic_result_type)
4725 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4727 protected_set_expr_location (ret, colon_loc);
4728 return ret;
4731 /* Return a compound expression that performs two expressions and
4732 returns the value of the second of them.
4734 LOC is the location of the COMPOUND_EXPR. */
4736 tree
4737 build_compound_expr (location_t loc, tree expr1, tree expr2)
4739 bool expr1_int_operands, expr2_int_operands;
4740 tree eptype = NULL_TREE;
4741 tree ret;
4743 if (flag_cilkplus
4744 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4745 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4747 error_at (loc,
4748 "spawned function call cannot be part of a comma expression");
4749 return error_mark_node;
4751 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4752 if (expr1_int_operands)
4753 expr1 = remove_c_maybe_const_expr (expr1);
4754 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4755 if (expr2_int_operands)
4756 expr2 = remove_c_maybe_const_expr (expr2);
4758 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4759 expr1 = TREE_OPERAND (expr1, 0);
4760 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4762 eptype = TREE_TYPE (expr2);
4763 expr2 = TREE_OPERAND (expr2, 0);
4766 if (!TREE_SIDE_EFFECTS (expr1))
4768 /* The left-hand operand of a comma expression is like an expression
4769 statement: with -Wunused, we should warn if it doesn't have
4770 any side-effects, unless it was explicitly cast to (void). */
4771 if (warn_unused_value)
4773 if (VOID_TYPE_P (TREE_TYPE (expr1))
4774 && CONVERT_EXPR_P (expr1))
4775 ; /* (void) a, b */
4776 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4777 && TREE_CODE (expr1) == COMPOUND_EXPR
4778 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4779 ; /* (void) a, (void) b, c */
4780 else
4781 warning_at (loc, OPT_Wunused_value,
4782 "left-hand operand of comma expression has no effect");
4785 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4786 && warn_unused_value)
4788 tree r = expr1;
4789 location_t cloc = loc;
4790 while (TREE_CODE (r) == COMPOUND_EXPR)
4792 if (EXPR_HAS_LOCATION (r))
4793 cloc = EXPR_LOCATION (r);
4794 r = TREE_OPERAND (r, 1);
4796 if (!TREE_SIDE_EFFECTS (r)
4797 && !VOID_TYPE_P (TREE_TYPE (r))
4798 && !CONVERT_EXPR_P (r))
4799 warning_at (cloc, OPT_Wunused_value,
4800 "right-hand operand of comma expression has no effect");
4803 /* With -Wunused, we should also warn if the left-hand operand does have
4804 side-effects, but computes a value which is not used. For example, in
4805 `foo() + bar(), baz()' the result of the `+' operator is not used,
4806 so we should issue a warning. */
4807 else if (warn_unused_value)
4808 warn_if_unused_value (expr1, loc);
4810 if (expr2 == error_mark_node)
4811 return error_mark_node;
4813 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4815 if (flag_isoc99
4816 && expr1_int_operands
4817 && expr2_int_operands)
4818 ret = note_integer_operands (ret);
4820 if (eptype)
4821 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4823 protected_set_expr_location (ret, loc);
4824 return ret;
4827 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4828 which we are casting. OTYPE is the type of the expression being
4829 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4830 of the cast. -Wcast-qual appeared on the command line. Named
4831 address space qualifiers are not handled here, because they result
4832 in different warnings. */
4834 static void
4835 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4837 tree in_type = type;
4838 tree in_otype = otype;
4839 int added = 0;
4840 int discarded = 0;
4841 bool is_const;
4843 /* Check that the qualifiers on IN_TYPE are a superset of the
4844 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4845 nodes is uninteresting and we stop as soon as we hit a
4846 non-POINTER_TYPE node on either type. */
4849 in_otype = TREE_TYPE (in_otype);
4850 in_type = TREE_TYPE (in_type);
4852 /* GNU C allows cv-qualified function types. 'const' means the
4853 function is very pure, 'volatile' means it can't return. We
4854 need to warn when such qualifiers are added, not when they're
4855 taken away. */
4856 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4857 && TREE_CODE (in_type) == FUNCTION_TYPE)
4858 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4859 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4860 else
4861 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4862 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4864 while (TREE_CODE (in_type) == POINTER_TYPE
4865 && TREE_CODE (in_otype) == POINTER_TYPE);
4867 if (added)
4868 warning_at (loc, OPT_Wcast_qual,
4869 "cast adds %q#v qualifier to function type", added);
4871 if (discarded)
4872 /* There are qualifiers present in IN_OTYPE that are not present
4873 in IN_TYPE. */
4874 warning_at (loc, OPT_Wcast_qual,
4875 "cast discards %qv qualifier from pointer target type",
4876 discarded);
4878 if (added || discarded)
4879 return;
4881 /* A cast from **T to const **T is unsafe, because it can cause a
4882 const value to be changed with no additional warning. We only
4883 issue this warning if T is the same on both sides, and we only
4884 issue the warning if there are the same number of pointers on
4885 both sides, as otherwise the cast is clearly unsafe anyhow. A
4886 cast is unsafe when a qualifier is added at one level and const
4887 is not present at all outer levels.
4889 To issue this warning, we check at each level whether the cast
4890 adds new qualifiers not already seen. We don't need to special
4891 case function types, as they won't have the same
4892 TYPE_MAIN_VARIANT. */
4894 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4895 return;
4896 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4897 return;
4899 in_type = type;
4900 in_otype = otype;
4901 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4904 in_type = TREE_TYPE (in_type);
4905 in_otype = TREE_TYPE (in_otype);
4906 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4907 && !is_const)
4909 warning_at (loc, OPT_Wcast_qual,
4910 "to be safe all intermediate pointers in cast from "
4911 "%qT to %qT must be %<const%> qualified",
4912 otype, type);
4913 break;
4915 if (is_const)
4916 is_const = TYPE_READONLY (in_type);
4918 while (TREE_CODE (in_type) == POINTER_TYPE);
4921 /* Build an expression representing a cast to type TYPE of expression EXPR.
4922 LOC is the location of the cast-- typically the open paren of the cast. */
4924 tree
4925 build_c_cast (location_t loc, tree type, tree expr)
4927 tree value;
4929 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4930 expr = TREE_OPERAND (expr, 0);
4932 value = expr;
4934 if (type == error_mark_node || expr == error_mark_node)
4935 return error_mark_node;
4937 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4938 only in <protocol> qualifications. But when constructing cast expressions,
4939 the protocols do matter and must be kept around. */
4940 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4941 return build1 (NOP_EXPR, type, expr);
4943 type = TYPE_MAIN_VARIANT (type);
4945 if (TREE_CODE (type) == ARRAY_TYPE)
4947 error_at (loc, "cast specifies array type");
4948 return error_mark_node;
4951 if (TREE_CODE (type) == FUNCTION_TYPE)
4953 error_at (loc, "cast specifies function type");
4954 return error_mark_node;
4957 if (!VOID_TYPE_P (type))
4959 value = require_complete_type (value);
4960 if (value == error_mark_node)
4961 return error_mark_node;
4964 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4966 if (TREE_CODE (type) == RECORD_TYPE
4967 || TREE_CODE (type) == UNION_TYPE)
4968 pedwarn (loc, OPT_Wpedantic,
4969 "ISO C forbids casting nonscalar to the same type");
4971 /* Convert to remove any qualifiers from VALUE's type. */
4972 value = convert (type, value);
4974 else if (TREE_CODE (type) == UNION_TYPE)
4976 tree field;
4978 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4979 if (TREE_TYPE (field) != error_mark_node
4980 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4981 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4982 break;
4984 if (field)
4986 tree t;
4987 bool maybe_const = true;
4989 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4990 t = c_fully_fold (value, false, &maybe_const);
4991 t = build_constructor_single (type, field, t);
4992 if (!maybe_const)
4993 t = c_wrap_maybe_const (t, true);
4994 t = digest_init (loc, type, t,
4995 NULL_TREE, false, true, 0);
4996 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4997 return t;
4999 error_at (loc, "cast to union type from type not present in union");
5000 return error_mark_node;
5002 else
5004 tree otype, ovalue;
5006 if (type == void_type_node)
5008 tree t = build1 (CONVERT_EXPR, type, value);
5009 SET_EXPR_LOCATION (t, loc);
5010 return t;
5013 otype = TREE_TYPE (value);
5015 /* Optionally warn about potentially worrisome casts. */
5016 if (warn_cast_qual
5017 && TREE_CODE (type) == POINTER_TYPE
5018 && TREE_CODE (otype) == POINTER_TYPE)
5019 handle_warn_cast_qual (loc, type, otype);
5021 /* Warn about conversions between pointers to disjoint
5022 address spaces. */
5023 if (TREE_CODE (type) == POINTER_TYPE
5024 && TREE_CODE (otype) == POINTER_TYPE
5025 && !null_pointer_constant_p (value))
5027 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5028 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5029 addr_space_t as_common;
5031 if (!addr_space_superset (as_to, as_from, &as_common))
5033 if (ADDR_SPACE_GENERIC_P (as_from))
5034 warning_at (loc, 0, "cast to %s address space pointer "
5035 "from disjoint generic address space pointer",
5036 c_addr_space_name (as_to));
5038 else if (ADDR_SPACE_GENERIC_P (as_to))
5039 warning_at (loc, 0, "cast to generic address space pointer "
5040 "from disjoint %s address space pointer",
5041 c_addr_space_name (as_from));
5043 else
5044 warning_at (loc, 0, "cast to %s address space pointer "
5045 "from disjoint %s address space pointer",
5046 c_addr_space_name (as_to),
5047 c_addr_space_name (as_from));
5051 /* Warn about possible alignment problems. */
5052 if (STRICT_ALIGNMENT
5053 && TREE_CODE (type) == POINTER_TYPE
5054 && TREE_CODE (otype) == POINTER_TYPE
5055 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5056 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5057 /* Don't warn about opaque types, where the actual alignment
5058 restriction is unknown. */
5059 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5060 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5061 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5062 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5063 warning_at (loc, OPT_Wcast_align,
5064 "cast increases required alignment of target type");
5066 if (TREE_CODE (type) == INTEGER_TYPE
5067 && TREE_CODE (otype) == POINTER_TYPE
5068 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5069 /* Unlike conversion of integers to pointers, where the
5070 warning is disabled for converting constants because
5071 of cases such as SIG_*, warn about converting constant
5072 pointers to integers. In some cases it may cause unwanted
5073 sign extension, and a warning is appropriate. */
5074 warning_at (loc, OPT_Wpointer_to_int_cast,
5075 "cast from pointer to integer of different size");
5077 if (TREE_CODE (value) == CALL_EXPR
5078 && TREE_CODE (type) != TREE_CODE (otype))
5079 warning_at (loc, OPT_Wbad_function_cast,
5080 "cast from function call of type %qT "
5081 "to non-matching type %qT", otype, type);
5083 if (TREE_CODE (type) == POINTER_TYPE
5084 && TREE_CODE (otype) == INTEGER_TYPE
5085 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5086 /* Don't warn about converting any constant. */
5087 && !TREE_CONSTANT (value))
5088 warning_at (loc,
5089 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5090 "of different size");
5092 if (warn_strict_aliasing <= 2)
5093 strict_aliasing_warning (otype, type, expr);
5095 /* If pedantic, warn for conversions between function and object
5096 pointer types, except for converting a null pointer constant
5097 to function pointer type. */
5098 if (pedantic
5099 && TREE_CODE (type) == POINTER_TYPE
5100 && TREE_CODE (otype) == POINTER_TYPE
5101 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5102 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5103 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5104 "conversion of function pointer to object pointer type");
5106 if (pedantic
5107 && TREE_CODE (type) == POINTER_TYPE
5108 && TREE_CODE (otype) == POINTER_TYPE
5109 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5110 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5111 && !null_pointer_constant_p (value))
5112 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5113 "conversion of object pointer to function pointer type");
5115 ovalue = value;
5116 value = convert (type, value);
5118 /* Ignore any integer overflow caused by the cast. */
5119 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5121 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5123 if (!TREE_OVERFLOW (value))
5125 /* Avoid clobbering a shared constant. */
5126 value = copy_node (value);
5127 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5130 else if (TREE_OVERFLOW (value))
5131 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5132 value = wide_int_to_tree (TREE_TYPE (value), value);
5136 /* Don't let a cast be an lvalue. */
5137 if (value == expr)
5138 value = non_lvalue_loc (loc, value);
5140 /* Don't allow the results of casting to floating-point or complex
5141 types be confused with actual constants, or casts involving
5142 integer and pointer types other than direct integer-to-integer
5143 and integer-to-pointer be confused with integer constant
5144 expressions and null pointer constants. */
5145 if (TREE_CODE (value) == REAL_CST
5146 || TREE_CODE (value) == COMPLEX_CST
5147 || (TREE_CODE (value) == INTEGER_CST
5148 && !((TREE_CODE (expr) == INTEGER_CST
5149 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5150 || TREE_CODE (expr) == REAL_CST
5151 || TREE_CODE (expr) == COMPLEX_CST)))
5152 value = build1 (NOP_EXPR, type, value);
5154 if (CAN_HAVE_LOCATION_P (value))
5155 SET_EXPR_LOCATION (value, loc);
5156 return value;
5159 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5160 location of the open paren of the cast, or the position of the cast
5161 expr. */
5162 tree
5163 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5165 tree type;
5166 tree type_expr = NULL_TREE;
5167 bool type_expr_const = true;
5168 tree ret;
5169 int saved_wsp = warn_strict_prototypes;
5171 /* This avoids warnings about unprototyped casts on
5172 integers. E.g. "#define SIG_DFL (void(*)())0". */
5173 if (TREE_CODE (expr) == INTEGER_CST)
5174 warn_strict_prototypes = 0;
5175 type = groktypename (type_name, &type_expr, &type_expr_const);
5176 warn_strict_prototypes = saved_wsp;
5178 ret = build_c_cast (loc, type, expr);
5179 if (type_expr)
5181 bool inner_expr_const = true;
5182 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5183 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5184 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5185 && inner_expr_const);
5186 SET_EXPR_LOCATION (ret, loc);
5189 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5190 SET_EXPR_LOCATION (ret, loc);
5192 /* C++ does not permits types to be defined in a cast, but it
5193 allows references to incomplete types. */
5194 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5195 warning_at (loc, OPT_Wc___compat,
5196 "defining a type in a cast is invalid in C++");
5198 return ret;
5201 /* Build an assignment expression of lvalue LHS from value RHS.
5202 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5203 may differ from TREE_TYPE (LHS) for an enum bitfield.
5204 MODIFYCODE is the code for a binary operator that we use
5205 to combine the old value of LHS with RHS to get the new value.
5206 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5207 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5208 which may differ from TREE_TYPE (RHS) for an enum value.
5210 LOCATION is the location of the MODIFYCODE operator.
5211 RHS_LOC is the location of the RHS. */
5213 tree
5214 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5215 enum tree_code modifycode,
5216 location_t rhs_loc, tree rhs, tree rhs_origtype)
5218 tree result;
5219 tree newrhs;
5220 tree rhseval = NULL_TREE;
5221 tree rhs_semantic_type = NULL_TREE;
5222 tree lhstype = TREE_TYPE (lhs);
5223 tree olhstype = lhstype;
5224 bool npc;
5225 bool is_atomic_op;
5227 /* Types that aren't fully specified cannot be used in assignments. */
5228 lhs = require_complete_type (lhs);
5230 /* Avoid duplicate error messages from operands that had errors. */
5231 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5232 return error_mark_node;
5234 /* Ensure an error for assigning a non-lvalue array to an array in
5235 C90. */
5236 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5238 error_at (location, "assignment to expression with array type");
5239 return error_mark_node;
5242 /* For ObjC properties, defer this check. */
5243 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5244 return error_mark_node;
5246 is_atomic_op = really_atomic_lvalue (lhs);
5248 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5250 rhs_semantic_type = TREE_TYPE (rhs);
5251 rhs = TREE_OPERAND (rhs, 0);
5254 newrhs = rhs;
5256 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5258 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5259 lhs_origtype, modifycode, rhs_loc, rhs,
5260 rhs_origtype);
5261 if (inner == error_mark_node)
5262 return error_mark_node;
5263 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5264 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5265 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5266 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5267 protected_set_expr_location (result, location);
5268 return result;
5271 /* If a binary op has been requested, combine the old LHS value with the RHS
5272 producing the value we should actually store into the LHS. */
5274 if (modifycode != NOP_EXPR)
5276 lhs = c_fully_fold (lhs, false, NULL);
5277 lhs = stabilize_reference (lhs);
5279 /* Construct the RHS for any non-atomic compound assignemnt. */
5280 if (!is_atomic_op)
5282 /* If in LHS op= RHS the RHS has side-effects, ensure they
5283 are preevaluated before the rest of the assignment expression's
5284 side-effects, because RHS could contain e.g. function calls
5285 that modify LHS. */
5286 if (TREE_SIDE_EFFECTS (rhs))
5288 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5289 rhseval = newrhs;
5291 newrhs = build_binary_op (location,
5292 modifycode, lhs, newrhs, 1);
5294 /* The original type of the right hand side is no longer
5295 meaningful. */
5296 rhs_origtype = NULL_TREE;
5300 if (c_dialect_objc ())
5302 /* Check if we are modifying an Objective-C property reference;
5303 if so, we need to generate setter calls. */
5304 result = objc_maybe_build_modify_expr (lhs, newrhs);
5305 if (result)
5306 goto return_result;
5308 /* Else, do the check that we postponed for Objective-C. */
5309 if (!lvalue_or_else (location, lhs, lv_assign))
5310 return error_mark_node;
5313 /* Give an error for storing in something that is 'const'. */
5315 if (TYPE_READONLY (lhstype)
5316 || ((TREE_CODE (lhstype) == RECORD_TYPE
5317 || TREE_CODE (lhstype) == UNION_TYPE)
5318 && C_TYPE_FIELDS_READONLY (lhstype)))
5320 readonly_error (location, lhs, lv_assign);
5321 return error_mark_node;
5323 else if (TREE_READONLY (lhs))
5324 readonly_warning (lhs, lv_assign);
5326 /* If storing into a structure or union member,
5327 it has probably been given type `int'.
5328 Compute the type that would go with
5329 the actual amount of storage the member occupies. */
5331 if (TREE_CODE (lhs) == COMPONENT_REF
5332 && (TREE_CODE (lhstype) == INTEGER_TYPE
5333 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5334 || TREE_CODE (lhstype) == REAL_TYPE
5335 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5336 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5338 /* If storing in a field that is in actuality a short or narrower than one,
5339 we must store in the field in its actual type. */
5341 if (lhstype != TREE_TYPE (lhs))
5343 lhs = copy_node (lhs);
5344 TREE_TYPE (lhs) = lhstype;
5347 /* Issue -Wc++-compat warnings about an assignment to an enum type
5348 when LHS does not have its original type. This happens for,
5349 e.g., an enum bitfield in a struct. */
5350 if (warn_cxx_compat
5351 && lhs_origtype != NULL_TREE
5352 && lhs_origtype != lhstype
5353 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5355 tree checktype = (rhs_origtype != NULL_TREE
5356 ? rhs_origtype
5357 : TREE_TYPE (rhs));
5358 if (checktype != error_mark_node
5359 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5360 || (is_atomic_op && modifycode != NOP_EXPR)))
5361 warning_at (location, OPT_Wc___compat,
5362 "enum conversion in assignment is invalid in C++");
5365 /* If the lhs is atomic, remove that qualifier. */
5366 if (is_atomic_op)
5368 lhstype = build_qualified_type (lhstype,
5369 (TYPE_QUALS (lhstype)
5370 & ~TYPE_QUAL_ATOMIC));
5371 olhstype = build_qualified_type (olhstype,
5372 (TYPE_QUALS (lhstype)
5373 & ~TYPE_QUAL_ATOMIC));
5376 /* Convert new value to destination type. Fold it first, then
5377 restore any excess precision information, for the sake of
5378 conversion warnings. */
5380 if (!(is_atomic_op && modifycode != NOP_EXPR))
5382 npc = null_pointer_constant_p (newrhs);
5383 newrhs = c_fully_fold (newrhs, false, NULL);
5384 if (rhs_semantic_type)
5385 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5386 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5387 rhs_origtype, ic_assign, npc,
5388 NULL_TREE, NULL_TREE, 0);
5389 if (TREE_CODE (newrhs) == ERROR_MARK)
5390 return error_mark_node;
5393 /* Emit ObjC write barrier, if necessary. */
5394 if (c_dialect_objc () && flag_objc_gc)
5396 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5397 if (result)
5399 protected_set_expr_location (result, location);
5400 goto return_result;
5404 /* Scan operands. */
5406 if (is_atomic_op)
5407 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5408 else
5410 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5411 TREE_SIDE_EFFECTS (result) = 1;
5412 protected_set_expr_location (result, location);
5415 /* If we got the LHS in a different type for storing in,
5416 convert the result back to the nominal type of LHS
5417 so that the value we return always has the same type
5418 as the LHS argument. */
5420 if (olhstype == TREE_TYPE (result))
5421 goto return_result;
5423 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5424 rhs_origtype, ic_assign, false, NULL_TREE,
5425 NULL_TREE, 0);
5426 protected_set_expr_location (result, location);
5428 return_result:
5429 if (rhseval)
5430 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5431 return result;
5434 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5435 This is used to implement -fplan9-extensions. */
5437 static bool
5438 find_anonymous_field_with_type (tree struct_type, tree type)
5440 tree field;
5441 bool found;
5443 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5444 || TREE_CODE (struct_type) == UNION_TYPE);
5445 found = false;
5446 for (field = TYPE_FIELDS (struct_type);
5447 field != NULL_TREE;
5448 field = TREE_CHAIN (field))
5450 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5451 ? c_build_qualified_type (TREE_TYPE (field),
5452 TYPE_QUAL_ATOMIC)
5453 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5454 if (DECL_NAME (field) == NULL
5455 && comptypes (type, fieldtype))
5457 if (found)
5458 return false;
5459 found = true;
5461 else if (DECL_NAME (field) == NULL
5462 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5463 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5464 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5466 if (found)
5467 return false;
5468 found = true;
5471 return found;
5474 /* RHS is an expression whose type is pointer to struct. If there is
5475 an anonymous field in RHS with type TYPE, then return a pointer to
5476 that field in RHS. This is used with -fplan9-extensions. This
5477 returns NULL if no conversion could be found. */
5479 static tree
5480 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5482 tree rhs_struct_type, lhs_main_type;
5483 tree field, found_field;
5484 bool found_sub_field;
5485 tree ret;
5487 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5488 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5489 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5490 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5492 gcc_assert (POINTER_TYPE_P (type));
5493 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5494 ? c_build_qualified_type (TREE_TYPE (type),
5495 TYPE_QUAL_ATOMIC)
5496 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5498 found_field = NULL_TREE;
5499 found_sub_field = false;
5500 for (field = TYPE_FIELDS (rhs_struct_type);
5501 field != NULL_TREE;
5502 field = TREE_CHAIN (field))
5504 if (DECL_NAME (field) != NULL_TREE
5505 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5506 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5507 continue;
5508 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5509 ? c_build_qualified_type (TREE_TYPE (field),
5510 TYPE_QUAL_ATOMIC)
5511 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5512 if (comptypes (lhs_main_type, fieldtype))
5514 if (found_field != NULL_TREE)
5515 return NULL_TREE;
5516 found_field = field;
5518 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5519 lhs_main_type))
5521 if (found_field != NULL_TREE)
5522 return NULL_TREE;
5523 found_field = field;
5524 found_sub_field = true;
5528 if (found_field == NULL_TREE)
5529 return NULL_TREE;
5531 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5532 build_fold_indirect_ref (rhs), found_field,
5533 NULL_TREE);
5534 ret = build_fold_addr_expr_loc (location, ret);
5536 if (found_sub_field)
5538 ret = convert_to_anonymous_field (location, type, ret);
5539 gcc_assert (ret != NULL_TREE);
5542 return ret;
5545 /* Issue an error message for a bad initializer component.
5546 GMSGID identifies the message.
5547 The component name is taken from the spelling stack. */
5549 static void
5550 error_init (location_t loc, const char *gmsgid)
5552 char *ofwhat;
5554 /* The gmsgid may be a format string with %< and %>. */
5555 error_at (loc, gmsgid);
5556 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5557 if (*ofwhat)
5558 inform (loc, "(near initialization for %qs)", ofwhat);
5561 /* Issue a pedantic warning for a bad initializer component. OPT is
5562 the option OPT_* (from options.h) controlling this warning or 0 if
5563 it is unconditionally given. GMSGID identifies the message. The
5564 component name is taken from the spelling stack. */
5566 static void
5567 pedwarn_init (location_t location, int opt, const char *gmsgid)
5569 char *ofwhat;
5570 bool warned;
5572 /* The gmsgid may be a format string with %< and %>. */
5573 warned = pedwarn (location, opt, gmsgid);
5574 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5575 if (*ofwhat && warned)
5576 inform (location, "(near initialization for %qs)", ofwhat);
5579 /* Issue a warning for a bad initializer component.
5581 OPT is the OPT_W* value corresponding to the warning option that
5582 controls this warning. GMSGID identifies the message. The
5583 component name is taken from the spelling stack. */
5585 static void
5586 warning_init (location_t loc, int opt, const char *gmsgid)
5588 char *ofwhat;
5589 bool warned;
5591 /* The gmsgid may be a format string with %< and %>. */
5592 warned = warning_at (loc, opt, gmsgid);
5593 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5594 if (*ofwhat && warned)
5595 inform (loc, "(near initialization for %qs)", ofwhat);
5598 /* If TYPE is an array type and EXPR is a parenthesized string
5599 constant, warn if pedantic that EXPR is being used to initialize an
5600 object of type TYPE. */
5602 void
5603 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5605 if (pedantic
5606 && TREE_CODE (type) == ARRAY_TYPE
5607 && TREE_CODE (expr.value) == STRING_CST
5608 && expr.original_code != STRING_CST)
5609 pedwarn_init (loc, OPT_Wpedantic,
5610 "array initialized from parenthesized string constant");
5613 /* Convert value RHS to type TYPE as preparation for an assignment to
5614 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5615 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5616 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5617 constant before any folding.
5618 The real work of conversion is done by `convert'.
5619 The purpose of this function is to generate error messages
5620 for assignments that are not allowed in C.
5621 ERRTYPE says whether it is argument passing, assignment,
5622 initialization or return.
5624 LOCATION is the location of the assignment, EXPR_LOC is the location of
5625 the RHS or, for a function, location of an argument.
5626 FUNCTION is a tree for the function being called.
5627 PARMNUM is the number of the argument, for printing in error messages. */
5629 static tree
5630 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5631 tree rhs, tree origtype, enum impl_conv errtype,
5632 bool null_pointer_constant, tree fundecl,
5633 tree function, int parmnum)
5635 enum tree_code codel = TREE_CODE (type);
5636 tree orig_rhs = rhs;
5637 tree rhstype;
5638 enum tree_code coder;
5639 tree rname = NULL_TREE;
5640 bool objc_ok = false;
5642 if (errtype == ic_argpass)
5644 tree selector;
5645 /* Change pointer to function to the function itself for
5646 diagnostics. */
5647 if (TREE_CODE (function) == ADDR_EXPR
5648 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5649 function = TREE_OPERAND (function, 0);
5651 /* Handle an ObjC selector specially for diagnostics. */
5652 selector = objc_message_selector ();
5653 rname = function;
5654 if (selector && parmnum > 2)
5656 rname = selector;
5657 parmnum -= 2;
5661 /* This macro is used to emit diagnostics to ensure that all format
5662 strings are complete sentences, visible to gettext and checked at
5663 compile time. */
5664 #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5665 do { \
5666 switch (errtype) \
5668 case ic_argpass: \
5669 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5670 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5671 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5672 "expected %qT but argument is of type %qT", \
5673 type, rhstype); \
5674 break; \
5675 case ic_assign: \
5676 pedwarn (LOCATION, OPT, AS); \
5677 break; \
5678 case ic_init: \
5679 pedwarn_init (LOCATION, OPT, IN); \
5680 break; \
5681 case ic_return: \
5682 pedwarn (LOCATION, OPT, RE); \
5683 break; \
5684 default: \
5685 gcc_unreachable (); \
5687 } while (0)
5689 /* This macro is used to emit diagnostics to ensure that all format
5690 strings are complete sentences, visible to gettext and checked at
5691 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5692 extra parameter to enumerate qualifiers. */
5694 #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5695 do { \
5696 switch (errtype) \
5698 case ic_argpass: \
5699 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5700 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5701 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5702 "expected %qT but argument is of type %qT", \
5703 type, rhstype); \
5704 break; \
5705 case ic_assign: \
5706 pedwarn (LOCATION, OPT, AS, QUALS); \
5707 break; \
5708 case ic_init: \
5709 pedwarn (LOCATION, OPT, IN, QUALS); \
5710 break; \
5711 case ic_return: \
5712 pedwarn (LOCATION, OPT, RE, QUALS); \
5713 break; \
5714 default: \
5715 gcc_unreachable (); \
5717 } while (0)
5719 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5720 rhs = TREE_OPERAND (rhs, 0);
5722 rhstype = TREE_TYPE (rhs);
5723 coder = TREE_CODE (rhstype);
5725 if (coder == ERROR_MARK)
5726 return error_mark_node;
5728 if (c_dialect_objc ())
5730 int parmno;
5732 switch (errtype)
5734 case ic_return:
5735 parmno = 0;
5736 break;
5738 case ic_assign:
5739 parmno = -1;
5740 break;
5742 case ic_init:
5743 parmno = -2;
5744 break;
5746 default:
5747 parmno = parmnum;
5748 break;
5751 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5754 if (warn_cxx_compat)
5756 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5757 if (checktype != error_mark_node
5758 && TREE_CODE (type) == ENUMERAL_TYPE
5759 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5761 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5762 G_("enum conversion when passing argument "
5763 "%d of %qE is invalid in C++"),
5764 G_("enum conversion in assignment is "
5765 "invalid in C++"),
5766 G_("enum conversion in initialization is "
5767 "invalid in C++"),
5768 G_("enum conversion in return is "
5769 "invalid in C++"));
5773 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5774 return rhs;
5776 if (coder == VOID_TYPE)
5778 /* Except for passing an argument to an unprototyped function,
5779 this is a constraint violation. When passing an argument to
5780 an unprototyped function, it is compile-time undefined;
5781 making it a constraint in that case was rejected in
5782 DR#252. */
5783 error_at (location, "void value not ignored as it ought to be");
5784 return error_mark_node;
5786 rhs = require_complete_type (rhs);
5787 if (rhs == error_mark_node)
5788 return error_mark_node;
5789 /* A non-reference type can convert to a reference. This handles
5790 va_start, va_copy and possibly port built-ins. */
5791 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5793 if (!lvalue_p (rhs))
5795 error_at (location, "cannot pass rvalue to reference parameter");
5796 return error_mark_node;
5798 if (!c_mark_addressable (rhs))
5799 return error_mark_node;
5800 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5801 SET_EXPR_LOCATION (rhs, location);
5803 rhs = convert_for_assignment (location, expr_loc,
5804 build_pointer_type (TREE_TYPE (type)),
5805 rhs, origtype, errtype,
5806 null_pointer_constant, fundecl, function,
5807 parmnum);
5808 if (rhs == error_mark_node)
5809 return error_mark_node;
5811 rhs = build1 (NOP_EXPR, type, rhs);
5812 SET_EXPR_LOCATION (rhs, location);
5813 return rhs;
5815 /* Some types can interconvert without explicit casts. */
5816 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5817 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5818 return convert (type, rhs);
5819 /* Arithmetic types all interconvert, and enum is treated like int. */
5820 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5821 || codel == FIXED_POINT_TYPE
5822 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5823 || codel == BOOLEAN_TYPE)
5824 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5825 || coder == FIXED_POINT_TYPE
5826 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5827 || coder == BOOLEAN_TYPE))
5829 tree ret;
5830 bool save = in_late_binary_op;
5831 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5832 in_late_binary_op = true;
5833 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5834 ? expr_loc : location, type, orig_rhs);
5835 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5836 in_late_binary_op = save;
5837 return ret;
5840 /* Aggregates in different TUs might need conversion. */
5841 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5842 && codel == coder
5843 && comptypes (type, rhstype))
5844 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5845 ? expr_loc : location, type, rhs);
5847 /* Conversion to a transparent union or record from its member types.
5848 This applies only to function arguments. */
5849 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5850 && TYPE_TRANSPARENT_AGGR (type))
5851 && errtype == ic_argpass)
5853 tree memb, marginal_memb = NULL_TREE;
5855 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5857 tree memb_type = TREE_TYPE (memb);
5859 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5860 TYPE_MAIN_VARIANT (rhstype)))
5861 break;
5863 if (TREE_CODE (memb_type) != POINTER_TYPE)
5864 continue;
5866 if (coder == POINTER_TYPE)
5868 tree ttl = TREE_TYPE (memb_type);
5869 tree ttr = TREE_TYPE (rhstype);
5871 /* Any non-function converts to a [const][volatile] void *
5872 and vice versa; otherwise, targets must be the same.
5873 Meanwhile, the lhs target must have all the qualifiers of
5874 the rhs. */
5875 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5876 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5877 || comp_target_types (location, memb_type, rhstype))
5879 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5880 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5881 /* If this type won't generate any warnings, use it. */
5882 if (lquals == rquals
5883 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5884 && TREE_CODE (ttl) == FUNCTION_TYPE)
5885 ? ((lquals | rquals) == rquals)
5886 : ((lquals | rquals) == lquals)))
5887 break;
5889 /* Keep looking for a better type, but remember this one. */
5890 if (!marginal_memb)
5891 marginal_memb = memb;
5895 /* Can convert integer zero to any pointer type. */
5896 if (null_pointer_constant)
5898 rhs = null_pointer_node;
5899 break;
5903 if (memb || marginal_memb)
5905 if (!memb)
5907 /* We have only a marginally acceptable member type;
5908 it needs a warning. */
5909 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5910 tree ttr = TREE_TYPE (rhstype);
5912 /* Const and volatile mean something different for function
5913 types, so the usual warnings are not appropriate. */
5914 if (TREE_CODE (ttr) == FUNCTION_TYPE
5915 && TREE_CODE (ttl) == FUNCTION_TYPE)
5917 /* Because const and volatile on functions are
5918 restrictions that say the function will not do
5919 certain things, it is okay to use a const or volatile
5920 function where an ordinary one is wanted, but not
5921 vice-versa. */
5922 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5923 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5924 WARN_FOR_QUALIFIERS (location, expr_loc,
5925 OPT_Wdiscarded_qualifiers,
5926 G_("passing argument %d of %qE "
5927 "makes %q#v qualified function "
5928 "pointer from unqualified"),
5929 G_("assignment makes %q#v qualified "
5930 "function pointer from "
5931 "unqualified"),
5932 G_("initialization makes %q#v qualified "
5933 "function pointer from "
5934 "unqualified"),
5935 G_("return makes %q#v qualified function "
5936 "pointer from unqualified"),
5937 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5939 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5940 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5941 WARN_FOR_QUALIFIERS (location, expr_loc,
5942 OPT_Wdiscarded_qualifiers,
5943 G_("passing argument %d of %qE discards "
5944 "%qv qualifier from pointer target type"),
5945 G_("assignment discards %qv qualifier "
5946 "from pointer target type"),
5947 G_("initialization discards %qv qualifier "
5948 "from pointer target type"),
5949 G_("return discards %qv qualifier from "
5950 "pointer target type"),
5951 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5953 memb = marginal_memb;
5956 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5957 pedwarn (location, OPT_Wpedantic,
5958 "ISO C prohibits argument conversion to union type");
5960 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5961 return build_constructor_single (type, memb, rhs);
5965 /* Conversions among pointers */
5966 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5967 && (coder == codel))
5969 tree ttl = TREE_TYPE (type);
5970 tree ttr = TREE_TYPE (rhstype);
5971 tree mvl = ttl;
5972 tree mvr = ttr;
5973 bool is_opaque_pointer;
5974 int target_cmp = 0; /* Cache comp_target_types () result. */
5975 addr_space_t asl;
5976 addr_space_t asr;
5978 if (TREE_CODE (mvl) != ARRAY_TYPE)
5979 mvl = (TYPE_ATOMIC (mvl)
5980 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5981 TYPE_QUAL_ATOMIC)
5982 : TYPE_MAIN_VARIANT (mvl));
5983 if (TREE_CODE (mvr) != ARRAY_TYPE)
5984 mvr = (TYPE_ATOMIC (mvr)
5985 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5986 TYPE_QUAL_ATOMIC)
5987 : TYPE_MAIN_VARIANT (mvr));
5988 /* Opaque pointers are treated like void pointers. */
5989 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5991 /* The Plan 9 compiler permits a pointer to a struct to be
5992 automatically converted into a pointer to an anonymous field
5993 within the struct. */
5994 if (flag_plan9_extensions
5995 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5996 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5997 && mvl != mvr)
5999 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6000 if (new_rhs != NULL_TREE)
6002 rhs = new_rhs;
6003 rhstype = TREE_TYPE (rhs);
6004 coder = TREE_CODE (rhstype);
6005 ttr = TREE_TYPE (rhstype);
6006 mvr = TYPE_MAIN_VARIANT (ttr);
6010 /* C++ does not allow the implicit conversion void* -> T*. However,
6011 for the purpose of reducing the number of false positives, we
6012 tolerate the special case of
6014 int *p = NULL;
6016 where NULL is typically defined in C to be '(void *) 0'. */
6017 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6018 warning_at (errtype == ic_argpass ? expr_loc : location,
6019 OPT_Wc___compat,
6020 "request for implicit conversion "
6021 "from %qT to %qT not permitted in C++", rhstype, type);
6023 /* See if the pointers point to incompatible address spaces. */
6024 asl = TYPE_ADDR_SPACE (ttl);
6025 asr = TYPE_ADDR_SPACE (ttr);
6026 if (!null_pointer_constant_p (rhs)
6027 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6029 switch (errtype)
6031 case ic_argpass:
6032 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6033 "non-enclosed address space", parmnum, rname);
6034 break;
6035 case ic_assign:
6036 error_at (location, "assignment from pointer to "
6037 "non-enclosed address space");
6038 break;
6039 case ic_init:
6040 error_at (location, "initialization from pointer to "
6041 "non-enclosed address space");
6042 break;
6043 case ic_return:
6044 error_at (location, "return from pointer to "
6045 "non-enclosed address space");
6046 break;
6047 default:
6048 gcc_unreachable ();
6050 return error_mark_node;
6053 /* Check if the right-hand side has a format attribute but the
6054 left-hand side doesn't. */
6055 if (warn_suggest_attribute_format
6056 && check_missing_format_attribute (type, rhstype))
6058 switch (errtype)
6060 case ic_argpass:
6061 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6062 "argument %d of %qE might be "
6063 "a candidate for a format attribute",
6064 parmnum, rname);
6065 break;
6066 case ic_assign:
6067 warning_at (location, OPT_Wsuggest_attribute_format,
6068 "assignment left-hand side might be "
6069 "a candidate for a format attribute");
6070 break;
6071 case ic_init:
6072 warning_at (location, OPT_Wsuggest_attribute_format,
6073 "initialization left-hand side might be "
6074 "a candidate for a format attribute");
6075 break;
6076 case ic_return:
6077 warning_at (location, OPT_Wsuggest_attribute_format,
6078 "return type might be "
6079 "a candidate for a format attribute");
6080 break;
6081 default:
6082 gcc_unreachable ();
6086 /* Any non-function converts to a [const][volatile] void *
6087 and vice versa; otherwise, targets must be the same.
6088 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6089 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6090 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6091 || (target_cmp = comp_target_types (location, type, rhstype))
6092 || is_opaque_pointer
6093 || ((c_common_unsigned_type (mvl)
6094 == c_common_unsigned_type (mvr))
6095 && (c_common_signed_type (mvl)
6096 == c_common_signed_type (mvr))
6097 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6099 if (pedantic
6100 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6102 (VOID_TYPE_P (ttr)
6103 && !null_pointer_constant
6104 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6105 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6106 G_("ISO C forbids passing argument %d of "
6107 "%qE between function pointer "
6108 "and %<void *%>"),
6109 G_("ISO C forbids assignment between "
6110 "function pointer and %<void *%>"),
6111 G_("ISO C forbids initialization between "
6112 "function pointer and %<void *%>"),
6113 G_("ISO C forbids return between function "
6114 "pointer and %<void *%>"));
6115 /* Const and volatile mean something different for function types,
6116 so the usual warnings are not appropriate. */
6117 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6118 && TREE_CODE (ttl) != FUNCTION_TYPE)
6120 /* Assignments between atomic and non-atomic objects are OK. */
6121 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6122 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6124 WARN_FOR_QUALIFIERS (location, expr_loc,
6125 OPT_Wdiscarded_qualifiers,
6126 G_("passing argument %d of %qE discards "
6127 "%qv qualifier from pointer target type"),
6128 G_("assignment discards %qv qualifier "
6129 "from pointer target type"),
6130 G_("initialization discards %qv qualifier "
6131 "from pointer target type"),
6132 G_("return discards %qv qualifier from "
6133 "pointer target type"),
6134 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6136 /* If this is not a case of ignoring a mismatch in signedness,
6137 no warning. */
6138 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6139 || target_cmp)
6141 /* If there is a mismatch, do warn. */
6142 else if (warn_pointer_sign)
6143 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6144 G_("pointer targets in passing argument "
6145 "%d of %qE differ in signedness"),
6146 G_("pointer targets in assignment "
6147 "differ in signedness"),
6148 G_("pointer targets in initialization "
6149 "differ in signedness"),
6150 G_("pointer targets in return differ "
6151 "in signedness"));
6153 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6154 && TREE_CODE (ttr) == FUNCTION_TYPE)
6156 /* Because const and volatile on functions are restrictions
6157 that say the function will not do certain things,
6158 it is okay to use a const or volatile function
6159 where an ordinary one is wanted, but not vice-versa. */
6160 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6161 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6162 WARN_FOR_QUALIFIERS (location, expr_loc,
6163 OPT_Wdiscarded_qualifiers,
6164 G_("passing argument %d of %qE makes "
6165 "%q#v qualified function pointer "
6166 "from unqualified"),
6167 G_("assignment makes %q#v qualified function "
6168 "pointer from unqualified"),
6169 G_("initialization makes %q#v qualified "
6170 "function pointer from unqualified"),
6171 G_("return makes %q#v qualified function "
6172 "pointer from unqualified"),
6173 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6176 else
6177 /* Avoid warning about the volatile ObjC EH puts on decls. */
6178 if (!objc_ok)
6179 WARN_FOR_ASSIGNMENT (location, expr_loc,
6180 OPT_Wincompatible_pointer_types,
6181 G_("passing argument %d of %qE from "
6182 "incompatible pointer type"),
6183 G_("assignment from incompatible pointer type"),
6184 G_("initialization from incompatible "
6185 "pointer type"),
6186 G_("return from incompatible pointer type"));
6188 return convert (type, rhs);
6190 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6192 /* ??? This should not be an error when inlining calls to
6193 unprototyped functions. */
6194 error_at (location, "invalid use of non-lvalue array");
6195 return error_mark_node;
6197 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6199 /* An explicit constant 0 can convert to a pointer,
6200 or one that results from arithmetic, even including
6201 a cast to integer type. */
6202 if (!null_pointer_constant)
6203 WARN_FOR_ASSIGNMENT (location, expr_loc,
6204 OPT_Wint_conversion,
6205 G_("passing argument %d of %qE makes "
6206 "pointer from integer without a cast"),
6207 G_("assignment makes pointer from integer "
6208 "without a cast"),
6209 G_("initialization makes pointer from "
6210 "integer without a cast"),
6211 G_("return makes pointer from integer "
6212 "without a cast"));
6214 return convert (type, rhs);
6216 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6218 WARN_FOR_ASSIGNMENT (location, expr_loc,
6219 OPT_Wint_conversion,
6220 G_("passing argument %d of %qE makes integer "
6221 "from pointer without a cast"),
6222 G_("assignment makes integer from pointer "
6223 "without a cast"),
6224 G_("initialization makes integer from pointer "
6225 "without a cast"),
6226 G_("return makes integer from pointer "
6227 "without a cast"));
6228 return convert (type, rhs);
6230 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6232 tree ret;
6233 bool save = in_late_binary_op;
6234 in_late_binary_op = true;
6235 ret = convert (type, rhs);
6236 in_late_binary_op = save;
6237 return ret;
6240 switch (errtype)
6242 case ic_argpass:
6243 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6244 rname);
6245 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6246 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6247 "expected %qT but argument is of type %qT", type, rhstype);
6248 break;
6249 case ic_assign:
6250 error_at (location, "incompatible types when assigning to type %qT from "
6251 "type %qT", type, rhstype);
6252 break;
6253 case ic_init:
6254 error_at (location,
6255 "incompatible types when initializing type %qT using type %qT",
6256 type, rhstype);
6257 break;
6258 case ic_return:
6259 error_at (location,
6260 "incompatible types when returning type %qT but %qT was "
6261 "expected", rhstype, type);
6262 break;
6263 default:
6264 gcc_unreachable ();
6267 return error_mark_node;
6270 /* If VALUE is a compound expr all of whose expressions are constant, then
6271 return its value. Otherwise, return error_mark_node.
6273 This is for handling COMPOUND_EXPRs as initializer elements
6274 which is allowed with a warning when -pedantic is specified. */
6276 static tree
6277 valid_compound_expr_initializer (tree value, tree endtype)
6279 if (TREE_CODE (value) == COMPOUND_EXPR)
6281 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6282 == error_mark_node)
6283 return error_mark_node;
6284 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6285 endtype);
6287 else if (!initializer_constant_valid_p (value, endtype))
6288 return error_mark_node;
6289 else
6290 return value;
6293 /* Perform appropriate conversions on the initial value of a variable,
6294 store it in the declaration DECL,
6295 and print any error messages that are appropriate.
6296 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6297 If the init is invalid, store an ERROR_MARK.
6299 INIT_LOC is the location of the initial value. */
6301 void
6302 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6304 tree value, type;
6305 bool npc = false;
6307 /* If variable's type was invalidly declared, just ignore it. */
6309 type = TREE_TYPE (decl);
6310 if (TREE_CODE (type) == ERROR_MARK)
6311 return;
6313 /* Digest the specified initializer into an expression. */
6315 if (init)
6316 npc = null_pointer_constant_p (init);
6317 value = digest_init (init_loc, type, init, origtype, npc,
6318 true, TREE_STATIC (decl));
6320 /* Store the expression if valid; else report error. */
6322 if (!in_system_header_at (input_location)
6323 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6324 warning (OPT_Wtraditional, "traditional C rejects automatic "
6325 "aggregate initialization");
6327 DECL_INITIAL (decl) = value;
6329 /* ANSI wants warnings about out-of-range constant initializers. */
6330 STRIP_TYPE_NOPS (value);
6331 if (TREE_STATIC (decl))
6332 constant_expression_warning (value);
6334 /* Check if we need to set array size from compound literal size. */
6335 if (TREE_CODE (type) == ARRAY_TYPE
6336 && TYPE_DOMAIN (type) == 0
6337 && value != error_mark_node)
6339 tree inside_init = init;
6341 STRIP_TYPE_NOPS (inside_init);
6342 inside_init = fold (inside_init);
6344 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6346 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6348 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6350 /* For int foo[] = (int [3]){1}; we need to set array size
6351 now since later on array initializer will be just the
6352 brace enclosed list of the compound literal. */
6353 tree etype = strip_array_types (TREE_TYPE (decl));
6354 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6355 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6356 layout_type (type);
6357 layout_decl (cldecl, 0);
6358 TREE_TYPE (decl)
6359 = c_build_qualified_type (type, TYPE_QUALS (etype));
6365 /* Methods for storing and printing names for error messages. */
6367 /* Implement a spelling stack that allows components of a name to be pushed
6368 and popped. Each element on the stack is this structure. */
6370 struct spelling
6372 int kind;
6373 union
6375 unsigned HOST_WIDE_INT i;
6376 const char *s;
6377 } u;
6380 #define SPELLING_STRING 1
6381 #define SPELLING_MEMBER 2
6382 #define SPELLING_BOUNDS 3
6384 static struct spelling *spelling; /* Next stack element (unused). */
6385 static struct spelling *spelling_base; /* Spelling stack base. */
6386 static int spelling_size; /* Size of the spelling stack. */
6388 /* Macros to save and restore the spelling stack around push_... functions.
6389 Alternative to SAVE_SPELLING_STACK. */
6391 #define SPELLING_DEPTH() (spelling - spelling_base)
6392 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6394 /* Push an element on the spelling stack with type KIND and assign VALUE
6395 to MEMBER. */
6397 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6399 int depth = SPELLING_DEPTH (); \
6401 if (depth >= spelling_size) \
6403 spelling_size += 10; \
6404 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6405 spelling_size); \
6406 RESTORE_SPELLING_DEPTH (depth); \
6409 spelling->kind = (KIND); \
6410 spelling->MEMBER = (VALUE); \
6411 spelling++; \
6414 /* Push STRING on the stack. Printed literally. */
6416 static void
6417 push_string (const char *string)
6419 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6422 /* Push a member name on the stack. Printed as '.' STRING. */
6424 static void
6425 push_member_name (tree decl)
6427 const char *const string
6428 = (DECL_NAME (decl)
6429 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6430 : _("<anonymous>"));
6431 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6434 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6436 static void
6437 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6439 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6442 /* Compute the maximum size in bytes of the printed spelling. */
6444 static int
6445 spelling_length (void)
6447 int size = 0;
6448 struct spelling *p;
6450 for (p = spelling_base; p < spelling; p++)
6452 if (p->kind == SPELLING_BOUNDS)
6453 size += 25;
6454 else
6455 size += strlen (p->u.s) + 1;
6458 return size;
6461 /* Print the spelling to BUFFER and return it. */
6463 static char *
6464 print_spelling (char *buffer)
6466 char *d = buffer;
6467 struct spelling *p;
6469 for (p = spelling_base; p < spelling; p++)
6470 if (p->kind == SPELLING_BOUNDS)
6472 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6473 d += strlen (d);
6475 else
6477 const char *s;
6478 if (p->kind == SPELLING_MEMBER)
6479 *d++ = '.';
6480 for (s = p->u.s; (*d = *s++); d++)
6483 *d++ = '\0';
6484 return buffer;
6487 /* Digest the parser output INIT as an initializer for type TYPE.
6488 Return a C expression of type TYPE to represent the initial value.
6490 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6492 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6494 If INIT is a string constant, STRICT_STRING is true if it is
6495 unparenthesized or we should not warn here for it being parenthesized.
6496 For other types of INIT, STRICT_STRING is not used.
6498 INIT_LOC is the location of the INIT.
6500 REQUIRE_CONSTANT requests an error if non-constant initializers or
6501 elements are seen. */
6503 static tree
6504 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6505 bool null_pointer_constant, bool strict_string,
6506 int require_constant)
6508 enum tree_code code = TREE_CODE (type);
6509 tree inside_init = init;
6510 tree semantic_type = NULL_TREE;
6511 bool maybe_const = true;
6513 if (type == error_mark_node
6514 || !init
6515 || error_operand_p (init))
6516 return error_mark_node;
6518 STRIP_TYPE_NOPS (inside_init);
6520 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6522 semantic_type = TREE_TYPE (inside_init);
6523 inside_init = TREE_OPERAND (inside_init, 0);
6525 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6526 inside_init = decl_constant_value_for_optimization (inside_init);
6528 /* Initialization of an array of chars from a string constant
6529 optionally enclosed in braces. */
6531 if (code == ARRAY_TYPE && inside_init
6532 && TREE_CODE (inside_init) == STRING_CST)
6534 tree typ1
6535 = (TYPE_ATOMIC (TREE_TYPE (type))
6536 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6537 TYPE_QUAL_ATOMIC)
6538 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6539 /* Note that an array could be both an array of character type
6540 and an array of wchar_t if wchar_t is signed char or unsigned
6541 char. */
6542 bool char_array = (typ1 == char_type_node
6543 || typ1 == signed_char_type_node
6544 || typ1 == unsigned_char_type_node);
6545 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6546 bool char16_array = !!comptypes (typ1, char16_type_node);
6547 bool char32_array = !!comptypes (typ1, char32_type_node);
6549 if (char_array || wchar_array || char16_array || char32_array)
6551 struct c_expr expr;
6552 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6553 expr.value = inside_init;
6554 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6555 expr.original_type = NULL;
6556 maybe_warn_string_init (init_loc, type, expr);
6558 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6559 pedwarn_init (init_loc, OPT_Wpedantic,
6560 "initialization of a flexible array member");
6562 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6563 TYPE_MAIN_VARIANT (type)))
6564 return inside_init;
6566 if (char_array)
6568 if (typ2 != char_type_node)
6570 error_init (init_loc, "char-array initialized from wide "
6571 "string");
6572 return error_mark_node;
6575 else
6577 if (typ2 == char_type_node)
6579 error_init (init_loc, "wide character array initialized "
6580 "from non-wide string");
6581 return error_mark_node;
6583 else if (!comptypes(typ1, typ2))
6585 error_init (init_loc, "wide character array initialized "
6586 "from incompatible wide string");
6587 return error_mark_node;
6591 TREE_TYPE (inside_init) = type;
6592 if (TYPE_DOMAIN (type) != 0
6593 && TYPE_SIZE (type) != 0
6594 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6596 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6598 /* Subtract the size of a single (possibly wide) character
6599 because it's ok to ignore the terminating null char
6600 that is counted in the length of the constant. */
6601 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6602 (len
6603 - (TYPE_PRECISION (typ1)
6604 / BITS_PER_UNIT))))
6605 pedwarn_init (init_loc, 0,
6606 ("initializer-string for array of chars "
6607 "is too long"));
6608 else if (warn_cxx_compat
6609 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6610 warning_at (init_loc, OPT_Wc___compat,
6611 ("initializer-string for array chars "
6612 "is too long for C++"));
6615 return inside_init;
6617 else if (INTEGRAL_TYPE_P (typ1))
6619 error_init (init_loc, "array of inappropriate type initialized "
6620 "from string constant");
6621 return error_mark_node;
6625 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6626 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6627 below and handle as a constructor. */
6628 if (code == VECTOR_TYPE
6629 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6630 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6631 && TREE_CONSTANT (inside_init))
6633 if (TREE_CODE (inside_init) == VECTOR_CST
6634 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6635 TYPE_MAIN_VARIANT (type)))
6636 return inside_init;
6638 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6640 unsigned HOST_WIDE_INT ix;
6641 tree value;
6642 bool constant_p = true;
6644 /* Iterate through elements and check if all constructor
6645 elements are *_CSTs. */
6646 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6647 if (!CONSTANT_CLASS_P (value))
6649 constant_p = false;
6650 break;
6653 if (constant_p)
6654 return build_vector_from_ctor (type,
6655 CONSTRUCTOR_ELTS (inside_init));
6659 if (warn_sequence_point)
6660 verify_sequence_points (inside_init);
6662 /* Any type can be initialized
6663 from an expression of the same type, optionally with braces. */
6665 if (inside_init && TREE_TYPE (inside_init) != 0
6666 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6667 TYPE_MAIN_VARIANT (type))
6668 || (code == ARRAY_TYPE
6669 && comptypes (TREE_TYPE (inside_init), type))
6670 || (code == VECTOR_TYPE
6671 && comptypes (TREE_TYPE (inside_init), type))
6672 || (code == POINTER_TYPE
6673 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6674 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6675 TREE_TYPE (type)))))
6677 if (code == POINTER_TYPE)
6679 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6681 if (TREE_CODE (inside_init) == STRING_CST
6682 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6683 inside_init = array_to_pointer_conversion
6684 (init_loc, inside_init);
6685 else
6687 error_init (init_loc, "invalid use of non-lvalue array");
6688 return error_mark_node;
6693 if (code == VECTOR_TYPE)
6694 /* Although the types are compatible, we may require a
6695 conversion. */
6696 inside_init = convert (type, inside_init);
6698 if (require_constant
6699 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6701 /* As an extension, allow initializing objects with static storage
6702 duration with compound literals (which are then treated just as
6703 the brace enclosed list they contain). Also allow this for
6704 vectors, as we can only assign them with compound literals. */
6705 if (flag_isoc99 && code != VECTOR_TYPE)
6706 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6707 "is not constant");
6708 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6709 inside_init = DECL_INITIAL (decl);
6712 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6713 && TREE_CODE (inside_init) != CONSTRUCTOR)
6715 error_init (init_loc, "array initialized from non-constant array "
6716 "expression");
6717 return error_mark_node;
6720 /* Compound expressions can only occur here if -Wpedantic or
6721 -pedantic-errors is specified. In the later case, we always want
6722 an error. In the former case, we simply want a warning. */
6723 if (require_constant && pedantic
6724 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6726 inside_init
6727 = valid_compound_expr_initializer (inside_init,
6728 TREE_TYPE (inside_init));
6729 if (inside_init == error_mark_node)
6730 error_init (init_loc, "initializer element is not constant");
6731 else
6732 pedwarn_init (init_loc, OPT_Wpedantic,
6733 "initializer element is not constant");
6734 if (flag_pedantic_errors)
6735 inside_init = error_mark_node;
6737 else if (require_constant
6738 && !initializer_constant_valid_p (inside_init,
6739 TREE_TYPE (inside_init)))
6741 error_init (init_loc, "initializer element is not constant");
6742 inside_init = error_mark_node;
6744 else if (require_constant && !maybe_const)
6745 pedwarn_init (init_loc, 0,
6746 "initializer element is not a constant expression");
6748 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6749 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6750 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6751 type, inside_init, origtype,
6752 ic_init, null_pointer_constant,
6753 NULL_TREE, NULL_TREE, 0);
6754 return inside_init;
6757 /* Handle scalar types, including conversions. */
6759 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6760 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6761 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6763 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6764 && (TREE_CODE (init) == STRING_CST
6765 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6766 inside_init = init = array_to_pointer_conversion (init_loc, init);
6767 if (semantic_type)
6768 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6769 inside_init);
6770 inside_init
6771 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6772 inside_init, origtype, ic_init,
6773 null_pointer_constant, NULL_TREE, NULL_TREE,
6776 /* Check to see if we have already given an error message. */
6777 if (inside_init == error_mark_node)
6779 else if (require_constant && !TREE_CONSTANT (inside_init))
6781 error_init (init_loc, "initializer element is not constant");
6782 inside_init = error_mark_node;
6784 else if (require_constant
6785 && !initializer_constant_valid_p (inside_init,
6786 TREE_TYPE (inside_init)))
6788 error_init (init_loc, "initializer element is not computable at "
6789 "load time");
6790 inside_init = error_mark_node;
6792 else if (require_constant && !maybe_const)
6793 pedwarn_init (init_loc, 0,
6794 "initializer element is not a constant expression");
6796 return inside_init;
6799 /* Come here only for records and arrays. */
6801 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6803 error_init (init_loc, "variable-sized object may not be initialized");
6804 return error_mark_node;
6807 error_init (init_loc, "invalid initializer");
6808 return error_mark_node;
6811 /* Handle initializers that use braces. */
6813 /* Type of object we are accumulating a constructor for.
6814 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6815 static tree constructor_type;
6817 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6818 left to fill. */
6819 static tree constructor_fields;
6821 /* For an ARRAY_TYPE, this is the specified index
6822 at which to store the next element we get. */
6823 static tree constructor_index;
6825 /* For an ARRAY_TYPE, this is the maximum index. */
6826 static tree constructor_max_index;
6828 /* For a RECORD_TYPE, this is the first field not yet written out. */
6829 static tree constructor_unfilled_fields;
6831 /* For an ARRAY_TYPE, this is the index of the first element
6832 not yet written out. */
6833 static tree constructor_unfilled_index;
6835 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6836 This is so we can generate gaps between fields, when appropriate. */
6837 static tree constructor_bit_index;
6839 /* If we are saving up the elements rather than allocating them,
6840 this is the list of elements so far (in reverse order,
6841 most recent first). */
6842 static vec<constructor_elt, va_gc> *constructor_elements;
6844 /* 1 if constructor should be incrementally stored into a constructor chain,
6845 0 if all the elements should be kept in AVL tree. */
6846 static int constructor_incremental;
6848 /* 1 if so far this constructor's elements are all compile-time constants. */
6849 static int constructor_constant;
6851 /* 1 if so far this constructor's elements are all valid address constants. */
6852 static int constructor_simple;
6854 /* 1 if this constructor has an element that cannot be part of a
6855 constant expression. */
6856 static int constructor_nonconst;
6858 /* 1 if this constructor is erroneous so far. */
6859 static int constructor_erroneous;
6861 /* 1 if this constructor is the universal zero initializer { 0 }. */
6862 static int constructor_zeroinit;
6864 /* Structure for managing pending initializer elements, organized as an
6865 AVL tree. */
6867 struct init_node
6869 struct init_node *left, *right;
6870 struct init_node *parent;
6871 int balance;
6872 tree purpose;
6873 tree value;
6874 tree origtype;
6877 /* Tree of pending elements at this constructor level.
6878 These are elements encountered out of order
6879 which belong at places we haven't reached yet in actually
6880 writing the output.
6881 Will never hold tree nodes across GC runs. */
6882 static struct init_node *constructor_pending_elts;
6884 /* The SPELLING_DEPTH of this constructor. */
6885 static int constructor_depth;
6887 /* DECL node for which an initializer is being read.
6888 0 means we are reading a constructor expression
6889 such as (struct foo) {...}. */
6890 static tree constructor_decl;
6892 /* Nonzero if this is an initializer for a top-level decl. */
6893 static int constructor_top_level;
6895 /* Nonzero if there were any member designators in this initializer. */
6896 static int constructor_designated;
6898 /* Nesting depth of designator list. */
6899 static int designator_depth;
6901 /* Nonzero if there were diagnosed errors in this designator list. */
6902 static int designator_erroneous;
6905 /* This stack has a level for each implicit or explicit level of
6906 structuring in the initializer, including the outermost one. It
6907 saves the values of most of the variables above. */
6909 struct constructor_range_stack;
6911 struct constructor_stack
6913 struct constructor_stack *next;
6914 tree type;
6915 tree fields;
6916 tree index;
6917 tree max_index;
6918 tree unfilled_index;
6919 tree unfilled_fields;
6920 tree bit_index;
6921 vec<constructor_elt, va_gc> *elements;
6922 struct init_node *pending_elts;
6923 int offset;
6924 int depth;
6925 /* If value nonzero, this value should replace the entire
6926 constructor at this level. */
6927 struct c_expr replacement_value;
6928 struct constructor_range_stack *range_stack;
6929 char constant;
6930 char simple;
6931 char nonconst;
6932 char implicit;
6933 char erroneous;
6934 char outer;
6935 char incremental;
6936 char designated;
6937 int designator_depth;
6940 static struct constructor_stack *constructor_stack;
6942 /* This stack represents designators from some range designator up to
6943 the last designator in the list. */
6945 struct constructor_range_stack
6947 struct constructor_range_stack *next, *prev;
6948 struct constructor_stack *stack;
6949 tree range_start;
6950 tree index;
6951 tree range_end;
6952 tree fields;
6955 static struct constructor_range_stack *constructor_range_stack;
6957 /* This stack records separate initializers that are nested.
6958 Nested initializers can't happen in ANSI C, but GNU C allows them
6959 in cases like { ... (struct foo) { ... } ... }. */
6961 struct initializer_stack
6963 struct initializer_stack *next;
6964 tree decl;
6965 struct constructor_stack *constructor_stack;
6966 struct constructor_range_stack *constructor_range_stack;
6967 vec<constructor_elt, va_gc> *elements;
6968 struct spelling *spelling;
6969 struct spelling *spelling_base;
6970 int spelling_size;
6971 char top_level;
6972 char require_constant_value;
6973 char require_constant_elements;
6976 static struct initializer_stack *initializer_stack;
6978 /* Prepare to parse and output the initializer for variable DECL. */
6980 void
6981 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6983 const char *locus;
6984 struct initializer_stack *p = XNEW (struct initializer_stack);
6986 p->decl = constructor_decl;
6987 p->require_constant_value = require_constant_value;
6988 p->require_constant_elements = require_constant_elements;
6989 p->constructor_stack = constructor_stack;
6990 p->constructor_range_stack = constructor_range_stack;
6991 p->elements = constructor_elements;
6992 p->spelling = spelling;
6993 p->spelling_base = spelling_base;
6994 p->spelling_size = spelling_size;
6995 p->top_level = constructor_top_level;
6996 p->next = initializer_stack;
6997 initializer_stack = p;
6999 constructor_decl = decl;
7000 constructor_designated = 0;
7001 constructor_top_level = top_level;
7003 if (decl != 0 && decl != error_mark_node)
7005 require_constant_value = TREE_STATIC (decl);
7006 require_constant_elements
7007 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7008 /* For a scalar, you can always use any value to initialize,
7009 even within braces. */
7010 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7011 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7012 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7013 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7014 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7016 else
7018 require_constant_value = 0;
7019 require_constant_elements = 0;
7020 locus = _("(anonymous)");
7023 constructor_stack = 0;
7024 constructor_range_stack = 0;
7026 found_missing_braces = 0;
7028 spelling_base = 0;
7029 spelling_size = 0;
7030 RESTORE_SPELLING_DEPTH (0);
7032 if (locus)
7033 push_string (locus);
7036 void
7037 finish_init (void)
7039 struct initializer_stack *p = initializer_stack;
7041 /* Free the whole constructor stack of this initializer. */
7042 while (constructor_stack)
7044 struct constructor_stack *q = constructor_stack;
7045 constructor_stack = q->next;
7046 free (q);
7049 gcc_assert (!constructor_range_stack);
7051 /* Pop back to the data of the outer initializer (if any). */
7052 free (spelling_base);
7054 constructor_decl = p->decl;
7055 require_constant_value = p->require_constant_value;
7056 require_constant_elements = p->require_constant_elements;
7057 constructor_stack = p->constructor_stack;
7058 constructor_range_stack = p->constructor_range_stack;
7059 constructor_elements = p->elements;
7060 spelling = p->spelling;
7061 spelling_base = p->spelling_base;
7062 spelling_size = p->spelling_size;
7063 constructor_top_level = p->top_level;
7064 initializer_stack = p->next;
7065 free (p);
7068 /* Call here when we see the initializer is surrounded by braces.
7069 This is instead of a call to push_init_level;
7070 it is matched by a call to pop_init_level.
7072 TYPE is the type to initialize, for a constructor expression.
7073 For an initializer for a decl, TYPE is zero. */
7075 void
7076 really_start_incremental_init (tree type)
7078 struct constructor_stack *p = XNEW (struct constructor_stack);
7080 if (type == 0)
7081 type = TREE_TYPE (constructor_decl);
7083 if (TREE_CODE (type) == VECTOR_TYPE
7084 && TYPE_VECTOR_OPAQUE (type))
7085 error ("opaque vector types cannot be initialized");
7087 p->type = constructor_type;
7088 p->fields = constructor_fields;
7089 p->index = constructor_index;
7090 p->max_index = constructor_max_index;
7091 p->unfilled_index = constructor_unfilled_index;
7092 p->unfilled_fields = constructor_unfilled_fields;
7093 p->bit_index = constructor_bit_index;
7094 p->elements = constructor_elements;
7095 p->constant = constructor_constant;
7096 p->simple = constructor_simple;
7097 p->nonconst = constructor_nonconst;
7098 p->erroneous = constructor_erroneous;
7099 p->pending_elts = constructor_pending_elts;
7100 p->depth = constructor_depth;
7101 p->replacement_value.value = 0;
7102 p->replacement_value.original_code = ERROR_MARK;
7103 p->replacement_value.original_type = NULL;
7104 p->implicit = 0;
7105 p->range_stack = 0;
7106 p->outer = 0;
7107 p->incremental = constructor_incremental;
7108 p->designated = constructor_designated;
7109 p->designator_depth = designator_depth;
7110 p->next = 0;
7111 constructor_stack = p;
7113 constructor_constant = 1;
7114 constructor_simple = 1;
7115 constructor_nonconst = 0;
7116 constructor_depth = SPELLING_DEPTH ();
7117 constructor_elements = NULL;
7118 constructor_pending_elts = 0;
7119 constructor_type = type;
7120 constructor_incremental = 1;
7121 constructor_designated = 0;
7122 constructor_zeroinit = 1;
7123 designator_depth = 0;
7124 designator_erroneous = 0;
7126 if (TREE_CODE (constructor_type) == RECORD_TYPE
7127 || TREE_CODE (constructor_type) == UNION_TYPE)
7129 constructor_fields = TYPE_FIELDS (constructor_type);
7130 /* Skip any nameless bit fields at the beginning. */
7131 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7132 && DECL_NAME (constructor_fields) == 0)
7133 constructor_fields = DECL_CHAIN (constructor_fields);
7135 constructor_unfilled_fields = constructor_fields;
7136 constructor_bit_index = bitsize_zero_node;
7138 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7140 if (TYPE_DOMAIN (constructor_type))
7142 constructor_max_index
7143 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7145 /* Detect non-empty initializations of zero-length arrays. */
7146 if (constructor_max_index == NULL_TREE
7147 && TYPE_SIZE (constructor_type))
7148 constructor_max_index = integer_minus_one_node;
7150 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7151 to initialize VLAs will cause a proper error; avoid tree
7152 checking errors as well by setting a safe value. */
7153 if (constructor_max_index
7154 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7155 constructor_max_index = integer_minus_one_node;
7157 constructor_index
7158 = convert (bitsizetype,
7159 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7161 else
7163 constructor_index = bitsize_zero_node;
7164 constructor_max_index = NULL_TREE;
7167 constructor_unfilled_index = constructor_index;
7169 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7171 /* Vectors are like simple fixed-size arrays. */
7172 constructor_max_index =
7173 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7174 constructor_index = bitsize_zero_node;
7175 constructor_unfilled_index = constructor_index;
7177 else
7179 /* Handle the case of int x = {5}; */
7180 constructor_fields = constructor_type;
7181 constructor_unfilled_fields = constructor_type;
7185 /* Push down into a subobject, for initialization.
7186 If this is for an explicit set of braces, IMPLICIT is 0.
7187 If it is because the next element belongs at a lower level,
7188 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7190 void
7191 push_init_level (location_t loc, int implicit,
7192 struct obstack *braced_init_obstack)
7194 struct constructor_stack *p;
7195 tree value = NULL_TREE;
7197 /* If we've exhausted any levels that didn't have braces,
7198 pop them now. If implicit == 1, this will have been done in
7199 process_init_element; do not repeat it here because in the case
7200 of excess initializers for an empty aggregate this leads to an
7201 infinite cycle of popping a level and immediately recreating
7202 it. */
7203 if (implicit != 1)
7205 while (constructor_stack->implicit)
7207 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7208 || TREE_CODE (constructor_type) == UNION_TYPE)
7209 && constructor_fields == 0)
7210 process_init_element (input_location,
7211 pop_init_level (loc, 1, braced_init_obstack),
7212 true, braced_init_obstack);
7213 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7214 && constructor_max_index
7215 && tree_int_cst_lt (constructor_max_index,
7216 constructor_index))
7217 process_init_element (input_location,
7218 pop_init_level (loc, 1, braced_init_obstack),
7219 true, braced_init_obstack);
7220 else
7221 break;
7225 /* Unless this is an explicit brace, we need to preserve previous
7226 content if any. */
7227 if (implicit)
7229 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7230 || TREE_CODE (constructor_type) == UNION_TYPE)
7231 && constructor_fields)
7232 value = find_init_member (constructor_fields, braced_init_obstack);
7233 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7234 value = find_init_member (constructor_index, braced_init_obstack);
7237 p = XNEW (struct constructor_stack);
7238 p->type = constructor_type;
7239 p->fields = constructor_fields;
7240 p->index = constructor_index;
7241 p->max_index = constructor_max_index;
7242 p->unfilled_index = constructor_unfilled_index;
7243 p->unfilled_fields = constructor_unfilled_fields;
7244 p->bit_index = constructor_bit_index;
7245 p->elements = constructor_elements;
7246 p->constant = constructor_constant;
7247 p->simple = constructor_simple;
7248 p->nonconst = constructor_nonconst;
7249 p->erroneous = constructor_erroneous;
7250 p->pending_elts = constructor_pending_elts;
7251 p->depth = constructor_depth;
7252 p->replacement_value.value = 0;
7253 p->replacement_value.original_code = ERROR_MARK;
7254 p->replacement_value.original_type = NULL;
7255 p->implicit = implicit;
7256 p->outer = 0;
7257 p->incremental = constructor_incremental;
7258 p->designated = constructor_designated;
7259 p->designator_depth = designator_depth;
7260 p->next = constructor_stack;
7261 p->range_stack = 0;
7262 constructor_stack = p;
7264 constructor_constant = 1;
7265 constructor_simple = 1;
7266 constructor_nonconst = 0;
7267 constructor_depth = SPELLING_DEPTH ();
7268 constructor_elements = NULL;
7269 constructor_incremental = 1;
7270 constructor_designated = 0;
7271 constructor_pending_elts = 0;
7272 if (!implicit)
7274 p->range_stack = constructor_range_stack;
7275 constructor_range_stack = 0;
7276 designator_depth = 0;
7277 designator_erroneous = 0;
7280 /* Don't die if an entire brace-pair level is superfluous
7281 in the containing level. */
7282 if (constructor_type == 0)
7284 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7285 || TREE_CODE (constructor_type) == UNION_TYPE)
7287 /* Don't die if there are extra init elts at the end. */
7288 if (constructor_fields == 0)
7289 constructor_type = 0;
7290 else
7292 constructor_type = TREE_TYPE (constructor_fields);
7293 push_member_name (constructor_fields);
7294 constructor_depth++;
7296 /* If upper initializer is designated, then mark this as
7297 designated too to prevent bogus warnings. */
7298 constructor_designated = p->designated;
7300 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7302 constructor_type = TREE_TYPE (constructor_type);
7303 push_array_bounds (tree_to_uhwi (constructor_index));
7304 constructor_depth++;
7307 if (constructor_type == 0)
7309 error_init (loc, "extra brace group at end of initializer");
7310 constructor_fields = 0;
7311 constructor_unfilled_fields = 0;
7312 return;
7315 if (value && TREE_CODE (value) == CONSTRUCTOR)
7317 constructor_constant = TREE_CONSTANT (value);
7318 constructor_simple = TREE_STATIC (value);
7319 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7320 constructor_elements = CONSTRUCTOR_ELTS (value);
7321 if (!vec_safe_is_empty (constructor_elements)
7322 && (TREE_CODE (constructor_type) == RECORD_TYPE
7323 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7324 set_nonincremental_init (braced_init_obstack);
7327 if (implicit == 1)
7328 found_missing_braces = 1;
7330 if (TREE_CODE (constructor_type) == RECORD_TYPE
7331 || TREE_CODE (constructor_type) == UNION_TYPE)
7333 constructor_fields = TYPE_FIELDS (constructor_type);
7334 /* Skip any nameless bit fields at the beginning. */
7335 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7336 && DECL_NAME (constructor_fields) == 0)
7337 constructor_fields = DECL_CHAIN (constructor_fields);
7339 constructor_unfilled_fields = constructor_fields;
7340 constructor_bit_index = bitsize_zero_node;
7342 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7344 /* Vectors are like simple fixed-size arrays. */
7345 constructor_max_index =
7346 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7347 constructor_index = bitsize_int (0);
7348 constructor_unfilled_index = constructor_index;
7350 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7352 if (TYPE_DOMAIN (constructor_type))
7354 constructor_max_index
7355 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7357 /* Detect non-empty initializations of zero-length arrays. */
7358 if (constructor_max_index == NULL_TREE
7359 && TYPE_SIZE (constructor_type))
7360 constructor_max_index = integer_minus_one_node;
7362 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7363 to initialize VLAs will cause a proper error; avoid tree
7364 checking errors as well by setting a safe value. */
7365 if (constructor_max_index
7366 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7367 constructor_max_index = integer_minus_one_node;
7369 constructor_index
7370 = convert (bitsizetype,
7371 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7373 else
7374 constructor_index = bitsize_zero_node;
7376 constructor_unfilled_index = constructor_index;
7377 if (value && TREE_CODE (value) == STRING_CST)
7379 /* We need to split the char/wchar array into individual
7380 characters, so that we don't have to special case it
7381 everywhere. */
7382 set_nonincremental_init_from_string (value, braced_init_obstack);
7385 else
7387 if (constructor_type != error_mark_node)
7388 warning_init (input_location, 0, "braces around scalar initializer");
7389 constructor_fields = constructor_type;
7390 constructor_unfilled_fields = constructor_type;
7394 /* At the end of an implicit or explicit brace level,
7395 finish up that level of constructor. If a single expression
7396 with redundant braces initialized that level, return the
7397 c_expr structure for that expression. Otherwise, the original_code
7398 element is set to ERROR_MARK.
7399 If we were outputting the elements as they are read, return 0 as the value
7400 from inner levels (process_init_element ignores that),
7401 but return error_mark_node as the value from the outermost level
7402 (that's what we want to put in DECL_INITIAL).
7403 Otherwise, return a CONSTRUCTOR expression as the value. */
7405 struct c_expr
7406 pop_init_level (location_t loc, int implicit,
7407 struct obstack *braced_init_obstack)
7409 struct constructor_stack *p;
7410 struct c_expr ret;
7411 ret.value = 0;
7412 ret.original_code = ERROR_MARK;
7413 ret.original_type = NULL;
7415 if (implicit == 0)
7417 /* When we come to an explicit close brace,
7418 pop any inner levels that didn't have explicit braces. */
7419 while (constructor_stack->implicit)
7420 process_init_element (input_location,
7421 pop_init_level (loc, 1, braced_init_obstack),
7422 true, braced_init_obstack);
7423 gcc_assert (!constructor_range_stack);
7426 /* Now output all pending elements. */
7427 constructor_incremental = 1;
7428 output_pending_init_elements (1, braced_init_obstack);
7430 p = constructor_stack;
7432 /* Error for initializing a flexible array member, or a zero-length
7433 array member in an inappropriate context. */
7434 if (constructor_type && constructor_fields
7435 && TREE_CODE (constructor_type) == ARRAY_TYPE
7436 && TYPE_DOMAIN (constructor_type)
7437 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7439 /* Silently discard empty initializations. The parser will
7440 already have pedwarned for empty brackets. */
7441 if (integer_zerop (constructor_unfilled_index))
7442 constructor_type = NULL_TREE;
7443 else
7445 gcc_assert (!TYPE_SIZE (constructor_type));
7447 if (constructor_depth > 2)
7448 error_init (loc, "initialization of flexible array member in a nested context");
7449 else
7450 pedwarn_init (loc, OPT_Wpedantic,
7451 "initialization of a flexible array member");
7453 /* We have already issued an error message for the existence
7454 of a flexible array member not at the end of the structure.
7455 Discard the initializer so that we do not die later. */
7456 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7457 constructor_type = NULL_TREE;
7461 /* Initialization with { } counts as zeroinit. */
7462 if (vec_safe_length (constructor_elements) == 0)
7463 constructor_zeroinit = 1;
7464 /* If the constructor has more than one element, it can't be { 0 }. */
7465 else if (vec_safe_length (constructor_elements) != 1)
7466 constructor_zeroinit = 0;
7468 /* Warn when some structs are initialized with direct aggregation. */
7469 if (!implicit && found_missing_braces && warn_missing_braces
7470 && !constructor_zeroinit)
7472 warning_init (loc, OPT_Wmissing_braces,
7473 "missing braces around initializer");
7476 /* Warn when some struct elements are implicitly initialized to zero. */
7477 if (warn_missing_field_initializers
7478 && constructor_type
7479 && TREE_CODE (constructor_type) == RECORD_TYPE
7480 && constructor_unfilled_fields)
7482 /* Do not warn for flexible array members or zero-length arrays. */
7483 while (constructor_unfilled_fields
7484 && (!DECL_SIZE (constructor_unfilled_fields)
7485 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7486 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7488 if (constructor_unfilled_fields
7489 /* Do not warn if this level of the initializer uses member
7490 designators; it is likely to be deliberate. */
7491 && !constructor_designated
7492 /* Do not warn about initializing with { 0 } or with { }. */
7493 && !constructor_zeroinit)
7495 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7496 "missing initializer for field %qD of %qT",
7497 constructor_unfilled_fields,
7498 constructor_type))
7499 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7500 "%qD declared here", constructor_unfilled_fields);
7504 /* Pad out the end of the structure. */
7505 if (p->replacement_value.value)
7506 /* If this closes a superfluous brace pair,
7507 just pass out the element between them. */
7508 ret = p->replacement_value;
7509 else if (constructor_type == 0)
7511 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7512 && TREE_CODE (constructor_type) != UNION_TYPE
7513 && TREE_CODE (constructor_type) != ARRAY_TYPE
7514 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7516 /* A nonincremental scalar initializer--just return
7517 the element, after verifying there is just one. */
7518 if (vec_safe_is_empty (constructor_elements))
7520 if (!constructor_erroneous)
7521 error_init (loc, "empty scalar initializer");
7522 ret.value = error_mark_node;
7524 else if (vec_safe_length (constructor_elements) != 1)
7526 error_init (loc, "extra elements in scalar initializer");
7527 ret.value = (*constructor_elements)[0].value;
7529 else
7530 ret.value = (*constructor_elements)[0].value;
7532 else
7534 if (constructor_erroneous)
7535 ret.value = error_mark_node;
7536 else
7538 ret.value = build_constructor (constructor_type,
7539 constructor_elements);
7540 if (constructor_constant)
7541 TREE_CONSTANT (ret.value) = 1;
7542 if (constructor_constant && constructor_simple)
7543 TREE_STATIC (ret.value) = 1;
7544 if (constructor_nonconst)
7545 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7549 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7551 if (constructor_nonconst)
7552 ret.original_code = C_MAYBE_CONST_EXPR;
7553 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7554 ret.original_code = ERROR_MARK;
7557 constructor_type = p->type;
7558 constructor_fields = p->fields;
7559 constructor_index = p->index;
7560 constructor_max_index = p->max_index;
7561 constructor_unfilled_index = p->unfilled_index;
7562 constructor_unfilled_fields = p->unfilled_fields;
7563 constructor_bit_index = p->bit_index;
7564 constructor_elements = p->elements;
7565 constructor_constant = p->constant;
7566 constructor_simple = p->simple;
7567 constructor_nonconst = p->nonconst;
7568 constructor_erroneous = p->erroneous;
7569 constructor_incremental = p->incremental;
7570 constructor_designated = p->designated;
7571 designator_depth = p->designator_depth;
7572 constructor_pending_elts = p->pending_elts;
7573 constructor_depth = p->depth;
7574 if (!p->implicit)
7575 constructor_range_stack = p->range_stack;
7576 RESTORE_SPELLING_DEPTH (constructor_depth);
7578 constructor_stack = p->next;
7579 free (p);
7581 if (ret.value == 0 && constructor_stack == 0)
7582 ret.value = error_mark_node;
7583 return ret;
7586 /* Common handling for both array range and field name designators.
7587 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7589 static int
7590 set_designator (location_t loc, int array,
7591 struct obstack *braced_init_obstack)
7593 tree subtype;
7594 enum tree_code subcode;
7596 /* Don't die if an entire brace-pair level is superfluous
7597 in the containing level. */
7598 if (constructor_type == 0)
7599 return 1;
7601 /* If there were errors in this designator list already, bail out
7602 silently. */
7603 if (designator_erroneous)
7604 return 1;
7606 if (!designator_depth)
7608 gcc_assert (!constructor_range_stack);
7610 /* Designator list starts at the level of closest explicit
7611 braces. */
7612 while (constructor_stack->implicit)
7613 process_init_element (input_location,
7614 pop_init_level (loc, 1, braced_init_obstack),
7615 true, braced_init_obstack);
7616 constructor_designated = 1;
7617 return 0;
7620 switch (TREE_CODE (constructor_type))
7622 case RECORD_TYPE:
7623 case UNION_TYPE:
7624 subtype = TREE_TYPE (constructor_fields);
7625 if (subtype != error_mark_node)
7626 subtype = TYPE_MAIN_VARIANT (subtype);
7627 break;
7628 case ARRAY_TYPE:
7629 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7630 break;
7631 default:
7632 gcc_unreachable ();
7635 subcode = TREE_CODE (subtype);
7636 if (array && subcode != ARRAY_TYPE)
7638 error_init (loc, "array index in non-array initializer");
7639 return 1;
7641 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7643 error_init (loc, "field name not in record or union initializer");
7644 return 1;
7647 constructor_designated = 1;
7648 push_init_level (loc, 2, braced_init_obstack);
7649 return 0;
7652 /* If there are range designators in designator list, push a new designator
7653 to constructor_range_stack. RANGE_END is end of such stack range or
7654 NULL_TREE if there is no range designator at this level. */
7656 static void
7657 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7659 struct constructor_range_stack *p;
7661 p = (struct constructor_range_stack *)
7662 obstack_alloc (braced_init_obstack,
7663 sizeof (struct constructor_range_stack));
7664 p->prev = constructor_range_stack;
7665 p->next = 0;
7666 p->fields = constructor_fields;
7667 p->range_start = constructor_index;
7668 p->index = constructor_index;
7669 p->stack = constructor_stack;
7670 p->range_end = range_end;
7671 if (constructor_range_stack)
7672 constructor_range_stack->next = p;
7673 constructor_range_stack = p;
7676 /* Within an array initializer, specify the next index to be initialized.
7677 FIRST is that index. If LAST is nonzero, then initialize a range
7678 of indices, running from FIRST through LAST. */
7680 void
7681 set_init_index (location_t loc, tree first, tree last,
7682 struct obstack *braced_init_obstack)
7684 if (set_designator (loc, 1, braced_init_obstack))
7685 return;
7687 designator_erroneous = 1;
7689 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7690 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7692 error_init (loc, "array index in initializer not of integer type");
7693 return;
7696 if (TREE_CODE (first) != INTEGER_CST)
7698 first = c_fully_fold (first, false, NULL);
7699 if (TREE_CODE (first) == INTEGER_CST)
7700 pedwarn_init (loc, OPT_Wpedantic,
7701 "array index in initializer is not "
7702 "an integer constant expression");
7705 if (last && TREE_CODE (last) != INTEGER_CST)
7707 last = c_fully_fold (last, false, NULL);
7708 if (TREE_CODE (last) == INTEGER_CST)
7709 pedwarn_init (loc, OPT_Wpedantic,
7710 "array index in initializer is not "
7711 "an integer constant expression");
7714 if (TREE_CODE (first) != INTEGER_CST)
7715 error_init (loc, "nonconstant array index in initializer");
7716 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7717 error_init (loc, "nonconstant array index in initializer");
7718 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7719 error_init (loc, "array index in non-array initializer");
7720 else if (tree_int_cst_sgn (first) == -1)
7721 error_init (loc, "array index in initializer exceeds array bounds");
7722 else if (constructor_max_index
7723 && tree_int_cst_lt (constructor_max_index, first))
7724 error_init (loc, "array index in initializer exceeds array bounds");
7725 else
7727 constant_expression_warning (first);
7728 if (last)
7729 constant_expression_warning (last);
7730 constructor_index = convert (bitsizetype, first);
7731 if (tree_int_cst_lt (constructor_index, first))
7733 constructor_index = copy_node (constructor_index);
7734 TREE_OVERFLOW (constructor_index) = 1;
7737 if (last)
7739 if (tree_int_cst_equal (first, last))
7740 last = 0;
7741 else if (tree_int_cst_lt (last, first))
7743 error_init (loc, "empty index range in initializer");
7744 last = 0;
7746 else
7748 last = convert (bitsizetype, last);
7749 if (constructor_max_index != 0
7750 && tree_int_cst_lt (constructor_max_index, last))
7752 error_init (loc, "array index range in initializer exceeds "
7753 "array bounds");
7754 last = 0;
7759 designator_depth++;
7760 designator_erroneous = 0;
7761 if (constructor_range_stack || last)
7762 push_range_stack (last, braced_init_obstack);
7766 /* Within a struct initializer, specify the next field to be initialized. */
7768 void
7769 set_init_label (location_t loc, tree fieldname,
7770 struct obstack *braced_init_obstack)
7772 tree field;
7774 if (set_designator (loc, 0, braced_init_obstack))
7775 return;
7777 designator_erroneous = 1;
7779 if (TREE_CODE (constructor_type) != RECORD_TYPE
7780 && TREE_CODE (constructor_type) != UNION_TYPE)
7782 error_init (loc, "field name not in record or union initializer");
7783 return;
7786 field = lookup_field (constructor_type, fieldname);
7788 if (field == 0)
7789 error ("unknown field %qE specified in initializer", fieldname);
7790 else
7793 constructor_fields = TREE_VALUE (field);
7794 designator_depth++;
7795 designator_erroneous = 0;
7796 if (constructor_range_stack)
7797 push_range_stack (NULL_TREE, braced_init_obstack);
7798 field = TREE_CHAIN (field);
7799 if (field)
7801 if (set_designator (loc, 0, braced_init_obstack))
7802 return;
7805 while (field != NULL_TREE);
7808 /* Add a new initializer to the tree of pending initializers. PURPOSE
7809 identifies the initializer, either array index or field in a structure.
7810 VALUE is the value of that index or field. If ORIGTYPE is not
7811 NULL_TREE, it is the original type of VALUE.
7813 IMPLICIT is true if value comes from pop_init_level (1),
7814 the new initializer has been merged with the existing one
7815 and thus no warnings should be emitted about overriding an
7816 existing initializer. */
7818 static void
7819 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7820 bool implicit, struct obstack *braced_init_obstack)
7822 struct init_node *p, **q, *r;
7824 q = &constructor_pending_elts;
7825 p = 0;
7827 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7829 while (*q != 0)
7831 p = *q;
7832 if (tree_int_cst_lt (purpose, p->purpose))
7833 q = &p->left;
7834 else if (tree_int_cst_lt (p->purpose, purpose))
7835 q = &p->right;
7836 else
7838 if (!implicit)
7840 if (TREE_SIDE_EFFECTS (p->value))
7841 warning_init (loc, 0,
7842 "initialized field with side-effects "
7843 "overwritten");
7844 else if (warn_override_init)
7845 warning_init (loc, OPT_Woverride_init,
7846 "initialized field overwritten");
7848 p->value = value;
7849 p->origtype = origtype;
7850 return;
7854 else
7856 tree bitpos;
7858 bitpos = bit_position (purpose);
7859 while (*q != NULL)
7861 p = *q;
7862 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7863 q = &p->left;
7864 else if (p->purpose != purpose)
7865 q = &p->right;
7866 else
7868 if (!implicit)
7870 if (TREE_SIDE_EFFECTS (p->value))
7871 warning_init (loc, 0,
7872 "initialized field with side-effects "
7873 "overwritten");
7874 else if (warn_override_init)
7875 warning_init (loc, OPT_Woverride_init,
7876 "initialized field overwritten");
7878 p->value = value;
7879 p->origtype = origtype;
7880 return;
7885 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7886 sizeof (struct init_node));
7887 r->purpose = purpose;
7888 r->value = value;
7889 r->origtype = origtype;
7891 *q = r;
7892 r->parent = p;
7893 r->left = 0;
7894 r->right = 0;
7895 r->balance = 0;
7897 while (p)
7899 struct init_node *s;
7901 if (r == p->left)
7903 if (p->balance == 0)
7904 p->balance = -1;
7905 else if (p->balance < 0)
7907 if (r->balance < 0)
7909 /* L rotation. */
7910 p->left = r->right;
7911 if (p->left)
7912 p->left->parent = p;
7913 r->right = p;
7915 p->balance = 0;
7916 r->balance = 0;
7918 s = p->parent;
7919 p->parent = r;
7920 r->parent = s;
7921 if (s)
7923 if (s->left == p)
7924 s->left = r;
7925 else
7926 s->right = r;
7928 else
7929 constructor_pending_elts = r;
7931 else
7933 /* LR rotation. */
7934 struct init_node *t = r->right;
7936 r->right = t->left;
7937 if (r->right)
7938 r->right->parent = r;
7939 t->left = r;
7941 p->left = t->right;
7942 if (p->left)
7943 p->left->parent = p;
7944 t->right = p;
7946 p->balance = t->balance < 0;
7947 r->balance = -(t->balance > 0);
7948 t->balance = 0;
7950 s = p->parent;
7951 p->parent = t;
7952 r->parent = t;
7953 t->parent = s;
7954 if (s)
7956 if (s->left == p)
7957 s->left = t;
7958 else
7959 s->right = t;
7961 else
7962 constructor_pending_elts = t;
7964 break;
7966 else
7968 /* p->balance == +1; growth of left side balances the node. */
7969 p->balance = 0;
7970 break;
7973 else /* r == p->right */
7975 if (p->balance == 0)
7976 /* Growth propagation from right side. */
7977 p->balance++;
7978 else if (p->balance > 0)
7980 if (r->balance > 0)
7982 /* R rotation. */
7983 p->right = r->left;
7984 if (p->right)
7985 p->right->parent = p;
7986 r->left = p;
7988 p->balance = 0;
7989 r->balance = 0;
7991 s = p->parent;
7992 p->parent = r;
7993 r->parent = s;
7994 if (s)
7996 if (s->left == p)
7997 s->left = r;
7998 else
7999 s->right = r;
8001 else
8002 constructor_pending_elts = r;
8004 else /* r->balance == -1 */
8006 /* RL rotation */
8007 struct init_node *t = r->left;
8009 r->left = t->right;
8010 if (r->left)
8011 r->left->parent = r;
8012 t->right = r;
8014 p->right = t->left;
8015 if (p->right)
8016 p->right->parent = p;
8017 t->left = p;
8019 r->balance = (t->balance < 0);
8020 p->balance = -(t->balance > 0);
8021 t->balance = 0;
8023 s = p->parent;
8024 p->parent = t;
8025 r->parent = t;
8026 t->parent = s;
8027 if (s)
8029 if (s->left == p)
8030 s->left = t;
8031 else
8032 s->right = t;
8034 else
8035 constructor_pending_elts = t;
8037 break;
8039 else
8041 /* p->balance == -1; growth of right side balances the node. */
8042 p->balance = 0;
8043 break;
8047 r = p;
8048 p = p->parent;
8052 /* Build AVL tree from a sorted chain. */
8054 static void
8055 set_nonincremental_init (struct obstack * braced_init_obstack)
8057 unsigned HOST_WIDE_INT ix;
8058 tree index, value;
8060 if (TREE_CODE (constructor_type) != RECORD_TYPE
8061 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8062 return;
8064 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8065 add_pending_init (input_location, index, value, NULL_TREE, true,
8066 braced_init_obstack);
8067 constructor_elements = NULL;
8068 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8070 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8071 /* Skip any nameless bit fields at the beginning. */
8072 while (constructor_unfilled_fields != 0
8073 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8074 && DECL_NAME (constructor_unfilled_fields) == 0)
8075 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8078 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8080 if (TYPE_DOMAIN (constructor_type))
8081 constructor_unfilled_index
8082 = convert (bitsizetype,
8083 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8084 else
8085 constructor_unfilled_index = bitsize_zero_node;
8087 constructor_incremental = 0;
8090 /* Build AVL tree from a string constant. */
8092 static void
8093 set_nonincremental_init_from_string (tree str,
8094 struct obstack * braced_init_obstack)
8096 tree value, purpose, type;
8097 HOST_WIDE_INT val[2];
8098 const char *p, *end;
8099 int byte, wchar_bytes, charwidth, bitpos;
8101 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8103 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8104 charwidth = TYPE_PRECISION (char_type_node);
8105 type = TREE_TYPE (constructor_type);
8106 p = TREE_STRING_POINTER (str);
8107 end = p + TREE_STRING_LENGTH (str);
8109 for (purpose = bitsize_zero_node;
8110 p < end
8111 && !(constructor_max_index
8112 && tree_int_cst_lt (constructor_max_index, purpose));
8113 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8115 if (wchar_bytes == 1)
8117 val[0] = (unsigned char) *p++;
8118 val[1] = 0;
8120 else
8122 val[1] = 0;
8123 val[0] = 0;
8124 for (byte = 0; byte < wchar_bytes; byte++)
8126 if (BYTES_BIG_ENDIAN)
8127 bitpos = (wchar_bytes - byte - 1) * charwidth;
8128 else
8129 bitpos = byte * charwidth;
8130 val[bitpos % HOST_BITS_PER_WIDE_INT]
8131 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8132 << (bitpos % HOST_BITS_PER_WIDE_INT);
8136 if (!TYPE_UNSIGNED (type))
8138 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8139 if (bitpos < HOST_BITS_PER_WIDE_INT)
8141 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8143 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8144 val[1] = -1;
8147 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8149 if (val[0] < 0)
8150 val[1] = -1;
8152 else if (val[1] & (((HOST_WIDE_INT) 1)
8153 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8154 val[1] |= ((HOST_WIDE_INT) -1)
8155 << (bitpos - HOST_BITS_PER_WIDE_INT);
8158 value = wide_int_to_tree (type,
8159 wide_int::from_array (val, 2,
8160 HOST_BITS_PER_WIDE_INT * 2));
8161 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8162 braced_init_obstack);
8165 constructor_incremental = 0;
8168 /* Return value of FIELD in pending initializer or zero if the field was
8169 not initialized yet. */
8171 static tree
8172 find_init_member (tree field, struct obstack * braced_init_obstack)
8174 struct init_node *p;
8176 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8178 if (constructor_incremental
8179 && tree_int_cst_lt (field, constructor_unfilled_index))
8180 set_nonincremental_init (braced_init_obstack);
8182 p = constructor_pending_elts;
8183 while (p)
8185 if (tree_int_cst_lt (field, p->purpose))
8186 p = p->left;
8187 else if (tree_int_cst_lt (p->purpose, field))
8188 p = p->right;
8189 else
8190 return p->value;
8193 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8195 tree bitpos = bit_position (field);
8197 if (constructor_incremental
8198 && (!constructor_unfilled_fields
8199 || tree_int_cst_lt (bitpos,
8200 bit_position (constructor_unfilled_fields))))
8201 set_nonincremental_init (braced_init_obstack);
8203 p = constructor_pending_elts;
8204 while (p)
8206 if (field == p->purpose)
8207 return p->value;
8208 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8209 p = p->left;
8210 else
8211 p = p->right;
8214 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8216 if (!vec_safe_is_empty (constructor_elements)
8217 && (constructor_elements->last ().index == field))
8218 return constructor_elements->last ().value;
8220 return 0;
8223 /* "Output" the next constructor element.
8224 At top level, really output it to assembler code now.
8225 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8226 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8227 TYPE is the data type that the containing data type wants here.
8228 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8229 If VALUE is a string constant, STRICT_STRING is true if it is
8230 unparenthesized or we should not warn here for it being parenthesized.
8231 For other types of VALUE, STRICT_STRING is not used.
8233 PENDING if non-nil means output pending elements that belong
8234 right after this element. (PENDING is normally 1;
8235 it is 0 while outputting pending elements, to avoid recursion.)
8237 IMPLICIT is true if value comes from pop_init_level (1),
8238 the new initializer has been merged with the existing one
8239 and thus no warnings should be emitted about overriding an
8240 existing initializer. */
8242 static void
8243 output_init_element (location_t loc, tree value, tree origtype,
8244 bool strict_string, tree type, tree field, int pending,
8245 bool implicit, struct obstack * braced_init_obstack)
8247 tree semantic_type = NULL_TREE;
8248 bool maybe_const = true;
8249 bool npc;
8251 if (type == error_mark_node || value == error_mark_node)
8253 constructor_erroneous = 1;
8254 return;
8256 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8257 && (TREE_CODE (value) == STRING_CST
8258 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8259 && !(TREE_CODE (value) == STRING_CST
8260 && TREE_CODE (type) == ARRAY_TYPE
8261 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8262 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8263 TYPE_MAIN_VARIANT (type)))
8264 value = array_to_pointer_conversion (input_location, value);
8266 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8267 && require_constant_value && pending)
8269 /* As an extension, allow initializing objects with static storage
8270 duration with compound literals (which are then treated just as
8271 the brace enclosed list they contain). */
8272 if (flag_isoc99)
8273 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8274 "constant");
8275 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8276 value = DECL_INITIAL (decl);
8279 npc = null_pointer_constant_p (value);
8280 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8282 semantic_type = TREE_TYPE (value);
8283 value = TREE_OPERAND (value, 0);
8285 value = c_fully_fold (value, require_constant_value, &maybe_const);
8287 if (value == error_mark_node)
8288 constructor_erroneous = 1;
8289 else if (!TREE_CONSTANT (value))
8290 constructor_constant = 0;
8291 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8292 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8293 || TREE_CODE (constructor_type) == UNION_TYPE)
8294 && DECL_C_BIT_FIELD (field)
8295 && TREE_CODE (value) != INTEGER_CST))
8296 constructor_simple = 0;
8297 if (!maybe_const)
8298 constructor_nonconst = 1;
8300 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8302 if (require_constant_value)
8304 error_init (loc, "initializer element is not constant");
8305 value = error_mark_node;
8307 else if (require_constant_elements)
8308 pedwarn (loc, OPT_Wpedantic,
8309 "initializer element is not computable at load time");
8311 else if (!maybe_const
8312 && (require_constant_value || require_constant_elements))
8313 pedwarn_init (loc, OPT_Wpedantic,
8314 "initializer element is not a constant expression");
8316 /* Issue -Wc++-compat warnings about initializing a bitfield with
8317 enum type. */
8318 if (warn_cxx_compat
8319 && field != NULL_TREE
8320 && TREE_CODE (field) == FIELD_DECL
8321 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8322 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8323 != TYPE_MAIN_VARIANT (type))
8324 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8326 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8327 if (checktype != error_mark_node
8328 && (TYPE_MAIN_VARIANT (checktype)
8329 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8330 warning_init (loc, OPT_Wc___compat,
8331 "enum conversion in initialization is invalid in C++");
8334 /* If this field is empty (and not at the end of structure),
8335 don't do anything other than checking the initializer. */
8336 if (field
8337 && (TREE_TYPE (field) == error_mark_node
8338 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8339 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8340 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8341 || DECL_CHAIN (field)))))
8342 return;
8344 if (semantic_type)
8345 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8346 value = digest_init (loc, type, value, origtype, npc, strict_string,
8347 require_constant_value);
8348 if (value == error_mark_node)
8350 constructor_erroneous = 1;
8351 return;
8353 if (require_constant_value || require_constant_elements)
8354 constant_expression_warning (value);
8356 /* If this element doesn't come next in sequence,
8357 put it on constructor_pending_elts. */
8358 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8359 && (!constructor_incremental
8360 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8362 if (constructor_incremental
8363 && tree_int_cst_lt (field, constructor_unfilled_index))
8364 set_nonincremental_init (braced_init_obstack);
8366 add_pending_init (loc, field, value, origtype, implicit,
8367 braced_init_obstack);
8368 return;
8370 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8371 && (!constructor_incremental
8372 || field != constructor_unfilled_fields))
8374 /* We do this for records but not for unions. In a union,
8375 no matter which field is specified, it can be initialized
8376 right away since it starts at the beginning of the union. */
8377 if (constructor_incremental)
8379 if (!constructor_unfilled_fields)
8380 set_nonincremental_init (braced_init_obstack);
8381 else
8383 tree bitpos, unfillpos;
8385 bitpos = bit_position (field);
8386 unfillpos = bit_position (constructor_unfilled_fields);
8388 if (tree_int_cst_lt (bitpos, unfillpos))
8389 set_nonincremental_init (braced_init_obstack);
8393 add_pending_init (loc, field, value, origtype, implicit,
8394 braced_init_obstack);
8395 return;
8397 else if (TREE_CODE (constructor_type) == UNION_TYPE
8398 && !vec_safe_is_empty (constructor_elements))
8400 if (!implicit)
8402 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8403 warning_init (loc, 0,
8404 "initialized field with side-effects overwritten");
8405 else if (warn_override_init)
8406 warning_init (loc, OPT_Woverride_init,
8407 "initialized field overwritten");
8410 /* We can have just one union field set. */
8411 constructor_elements = NULL;
8414 /* Otherwise, output this element either to
8415 constructor_elements or to the assembler file. */
8417 constructor_elt celt = {field, value};
8418 vec_safe_push (constructor_elements, celt);
8420 /* Advance the variable that indicates sequential elements output. */
8421 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8422 constructor_unfilled_index
8423 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8424 bitsize_one_node);
8425 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8427 constructor_unfilled_fields
8428 = DECL_CHAIN (constructor_unfilled_fields);
8430 /* Skip any nameless bit fields. */
8431 while (constructor_unfilled_fields != 0
8432 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8433 && DECL_NAME (constructor_unfilled_fields) == 0)
8434 constructor_unfilled_fields =
8435 DECL_CHAIN (constructor_unfilled_fields);
8437 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8438 constructor_unfilled_fields = 0;
8440 /* Now output any pending elements which have become next. */
8441 if (pending)
8442 output_pending_init_elements (0, braced_init_obstack);
8445 /* Output any pending elements which have become next.
8446 As we output elements, constructor_unfilled_{fields,index}
8447 advances, which may cause other elements to become next;
8448 if so, they too are output.
8450 If ALL is 0, we return when there are
8451 no more pending elements to output now.
8453 If ALL is 1, we output space as necessary so that
8454 we can output all the pending elements. */
8455 static void
8456 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8458 struct init_node *elt = constructor_pending_elts;
8459 tree next;
8461 retry:
8463 /* Look through the whole pending tree.
8464 If we find an element that should be output now,
8465 output it. Otherwise, set NEXT to the element
8466 that comes first among those still pending. */
8468 next = 0;
8469 while (elt)
8471 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8473 if (tree_int_cst_equal (elt->purpose,
8474 constructor_unfilled_index))
8475 output_init_element (input_location, elt->value, elt->origtype,
8476 true, TREE_TYPE (constructor_type),
8477 constructor_unfilled_index, 0, false,
8478 braced_init_obstack);
8479 else if (tree_int_cst_lt (constructor_unfilled_index,
8480 elt->purpose))
8482 /* Advance to the next smaller node. */
8483 if (elt->left)
8484 elt = elt->left;
8485 else
8487 /* We have reached the smallest node bigger than the
8488 current unfilled index. Fill the space first. */
8489 next = elt->purpose;
8490 break;
8493 else
8495 /* Advance to the next bigger node. */
8496 if (elt->right)
8497 elt = elt->right;
8498 else
8500 /* We have reached the biggest node in a subtree. Find
8501 the parent of it, which is the next bigger node. */
8502 while (elt->parent && elt->parent->right == elt)
8503 elt = elt->parent;
8504 elt = elt->parent;
8505 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8506 elt->purpose))
8508 next = elt->purpose;
8509 break;
8514 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8515 || TREE_CODE (constructor_type) == UNION_TYPE)
8517 tree ctor_unfilled_bitpos, elt_bitpos;
8519 /* If the current record is complete we are done. */
8520 if (constructor_unfilled_fields == 0)
8521 break;
8523 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8524 elt_bitpos = bit_position (elt->purpose);
8525 /* We can't compare fields here because there might be empty
8526 fields in between. */
8527 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8529 constructor_unfilled_fields = elt->purpose;
8530 output_init_element (input_location, elt->value, elt->origtype,
8531 true, TREE_TYPE (elt->purpose),
8532 elt->purpose, 0, false,
8533 braced_init_obstack);
8535 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8537 /* Advance to the next smaller node. */
8538 if (elt->left)
8539 elt = elt->left;
8540 else
8542 /* We have reached the smallest node bigger than the
8543 current unfilled field. Fill the space first. */
8544 next = elt->purpose;
8545 break;
8548 else
8550 /* Advance to the next bigger node. */
8551 if (elt->right)
8552 elt = elt->right;
8553 else
8555 /* We have reached the biggest node in a subtree. Find
8556 the parent of it, which is the next bigger node. */
8557 while (elt->parent && elt->parent->right == elt)
8558 elt = elt->parent;
8559 elt = elt->parent;
8560 if (elt
8561 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8562 bit_position (elt->purpose))))
8564 next = elt->purpose;
8565 break;
8572 /* Ordinarily return, but not if we want to output all
8573 and there are elements left. */
8574 if (!(all && next != 0))
8575 return;
8577 /* If it's not incremental, just skip over the gap, so that after
8578 jumping to retry we will output the next successive element. */
8579 if (TREE_CODE (constructor_type) == RECORD_TYPE
8580 || TREE_CODE (constructor_type) == UNION_TYPE)
8581 constructor_unfilled_fields = next;
8582 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8583 constructor_unfilled_index = next;
8585 /* ELT now points to the node in the pending tree with the next
8586 initializer to output. */
8587 goto retry;
8590 /* Add one non-braced element to the current constructor level.
8591 This adjusts the current position within the constructor's type.
8592 This may also start or terminate implicit levels
8593 to handle a partly-braced initializer.
8595 Once this has found the correct level for the new element,
8596 it calls output_init_element.
8598 IMPLICIT is true if value comes from pop_init_level (1),
8599 the new initializer has been merged with the existing one
8600 and thus no warnings should be emitted about overriding an
8601 existing initializer. */
8603 void
8604 process_init_element (location_t loc, struct c_expr value, bool implicit,
8605 struct obstack * braced_init_obstack)
8607 tree orig_value = value.value;
8608 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8609 bool strict_string = value.original_code == STRING_CST;
8610 bool was_designated = designator_depth != 0;
8612 designator_depth = 0;
8613 designator_erroneous = 0;
8615 if (!implicit && value.value && !integer_zerop (value.value))
8616 constructor_zeroinit = 0;
8618 /* Handle superfluous braces around string cst as in
8619 char x[] = {"foo"}; */
8620 if (string_flag
8621 && constructor_type
8622 && !was_designated
8623 && TREE_CODE (constructor_type) == ARRAY_TYPE
8624 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8625 && integer_zerop (constructor_unfilled_index))
8627 if (constructor_stack->replacement_value.value)
8628 error_init (loc, "excess elements in char array initializer");
8629 constructor_stack->replacement_value = value;
8630 return;
8633 if (constructor_stack->replacement_value.value != 0)
8635 error_init (loc, "excess elements in struct initializer");
8636 return;
8639 /* Ignore elements of a brace group if it is entirely superfluous
8640 and has already been diagnosed. */
8641 if (constructor_type == 0)
8642 return;
8644 if (!implicit && warn_designated_init && !was_designated
8645 && TREE_CODE (constructor_type) == RECORD_TYPE
8646 && lookup_attribute ("designated_init",
8647 TYPE_ATTRIBUTES (constructor_type)))
8648 warning_init (loc,
8649 OPT_Wdesignated_init,
8650 "positional initialization of field "
8651 "in %<struct%> declared with %<designated_init%> attribute");
8653 /* If we've exhausted any levels that didn't have braces,
8654 pop them now. */
8655 while (constructor_stack->implicit)
8657 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8658 || TREE_CODE (constructor_type) == UNION_TYPE)
8659 && constructor_fields == 0)
8660 process_init_element (loc,
8661 pop_init_level (loc, 1, braced_init_obstack),
8662 true, braced_init_obstack);
8663 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8664 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8665 && constructor_max_index
8666 && tree_int_cst_lt (constructor_max_index,
8667 constructor_index))
8668 process_init_element (loc,
8669 pop_init_level (loc, 1, braced_init_obstack),
8670 true, braced_init_obstack);
8671 else
8672 break;
8675 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8676 if (constructor_range_stack)
8678 /* If value is a compound literal and we'll be just using its
8679 content, don't put it into a SAVE_EXPR. */
8680 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8681 || !require_constant_value
8682 || flag_isoc99)
8684 tree semantic_type = NULL_TREE;
8685 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8687 semantic_type = TREE_TYPE (value.value);
8688 value.value = TREE_OPERAND (value.value, 0);
8690 value.value = c_save_expr (value.value);
8691 if (semantic_type)
8692 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8693 value.value);
8697 while (1)
8699 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8701 tree fieldtype;
8702 enum tree_code fieldcode;
8704 if (constructor_fields == 0)
8706 pedwarn_init (loc, 0, "excess elements in struct initializer");
8707 break;
8710 fieldtype = TREE_TYPE (constructor_fields);
8711 if (fieldtype != error_mark_node)
8712 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8713 fieldcode = TREE_CODE (fieldtype);
8715 /* Error for non-static initialization of a flexible array member. */
8716 if (fieldcode == ARRAY_TYPE
8717 && !require_constant_value
8718 && TYPE_SIZE (fieldtype) == NULL_TREE
8719 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8721 error_init (loc, "non-static initialization of a flexible "
8722 "array member");
8723 break;
8726 /* Accept a string constant to initialize a subarray. */
8727 if (value.value != 0
8728 && fieldcode == ARRAY_TYPE
8729 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8730 && string_flag)
8731 value.value = orig_value;
8732 /* Otherwise, if we have come to a subaggregate,
8733 and we don't have an element of its type, push into it. */
8734 else if (value.value != 0
8735 && value.value != error_mark_node
8736 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8737 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8738 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8740 push_init_level (loc, 1, braced_init_obstack);
8741 continue;
8744 if (value.value)
8746 push_member_name (constructor_fields);
8747 output_init_element (loc, value.value, value.original_type,
8748 strict_string, fieldtype,
8749 constructor_fields, 1, implicit,
8750 braced_init_obstack);
8751 RESTORE_SPELLING_DEPTH (constructor_depth);
8753 else
8754 /* Do the bookkeeping for an element that was
8755 directly output as a constructor. */
8757 /* For a record, keep track of end position of last field. */
8758 if (DECL_SIZE (constructor_fields))
8759 constructor_bit_index
8760 = size_binop_loc (input_location, PLUS_EXPR,
8761 bit_position (constructor_fields),
8762 DECL_SIZE (constructor_fields));
8764 /* If the current field was the first one not yet written out,
8765 it isn't now, so update. */
8766 if (constructor_unfilled_fields == constructor_fields)
8768 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8769 /* Skip any nameless bit fields. */
8770 while (constructor_unfilled_fields != 0
8771 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8772 && DECL_NAME (constructor_unfilled_fields) == 0)
8773 constructor_unfilled_fields =
8774 DECL_CHAIN (constructor_unfilled_fields);
8778 constructor_fields = DECL_CHAIN (constructor_fields);
8779 /* Skip any nameless bit fields at the beginning. */
8780 while (constructor_fields != 0
8781 && DECL_C_BIT_FIELD (constructor_fields)
8782 && DECL_NAME (constructor_fields) == 0)
8783 constructor_fields = DECL_CHAIN (constructor_fields);
8785 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8787 tree fieldtype;
8788 enum tree_code fieldcode;
8790 if (constructor_fields == 0)
8792 pedwarn_init (loc, 0,
8793 "excess elements in union initializer");
8794 break;
8797 fieldtype = TREE_TYPE (constructor_fields);
8798 if (fieldtype != error_mark_node)
8799 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8800 fieldcode = TREE_CODE (fieldtype);
8802 /* Warn that traditional C rejects initialization of unions.
8803 We skip the warning if the value is zero. This is done
8804 under the assumption that the zero initializer in user
8805 code appears conditioned on e.g. __STDC__ to avoid
8806 "missing initializer" warnings and relies on default
8807 initialization to zero in the traditional C case.
8808 We also skip the warning if the initializer is designated,
8809 again on the assumption that this must be conditional on
8810 __STDC__ anyway (and we've already complained about the
8811 member-designator already). */
8812 if (!in_system_header_at (input_location) && !constructor_designated
8813 && !(value.value && (integer_zerop (value.value)
8814 || real_zerop (value.value))))
8815 warning (OPT_Wtraditional, "traditional C rejects initialization "
8816 "of unions");
8818 /* Accept a string constant to initialize a subarray. */
8819 if (value.value != 0
8820 && fieldcode == ARRAY_TYPE
8821 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8822 && string_flag)
8823 value.value = orig_value;
8824 /* Otherwise, if we have come to a subaggregate,
8825 and we don't have an element of its type, push into it. */
8826 else if (value.value != 0
8827 && value.value != error_mark_node
8828 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8829 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8830 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8832 push_init_level (loc, 1, braced_init_obstack);
8833 continue;
8836 if (value.value)
8838 push_member_name (constructor_fields);
8839 output_init_element (loc, value.value, value.original_type,
8840 strict_string, fieldtype,
8841 constructor_fields, 1, implicit,
8842 braced_init_obstack);
8843 RESTORE_SPELLING_DEPTH (constructor_depth);
8845 else
8846 /* Do the bookkeeping for an element that was
8847 directly output as a constructor. */
8849 constructor_bit_index = DECL_SIZE (constructor_fields);
8850 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8853 constructor_fields = 0;
8855 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8857 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8858 enum tree_code eltcode = TREE_CODE (elttype);
8860 /* Accept a string constant to initialize a subarray. */
8861 if (value.value != 0
8862 && eltcode == ARRAY_TYPE
8863 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8864 && string_flag)
8865 value.value = orig_value;
8866 /* Otherwise, if we have come to a subaggregate,
8867 and we don't have an element of its type, push into it. */
8868 else if (value.value != 0
8869 && value.value != error_mark_node
8870 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8871 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8872 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8874 push_init_level (loc, 1, braced_init_obstack);
8875 continue;
8878 if (constructor_max_index != 0
8879 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8880 || integer_all_onesp (constructor_max_index)))
8882 pedwarn_init (loc, 0,
8883 "excess elements in array initializer");
8884 break;
8887 /* Now output the actual element. */
8888 if (value.value)
8890 push_array_bounds (tree_to_uhwi (constructor_index));
8891 output_init_element (loc, value.value, value.original_type,
8892 strict_string, elttype,
8893 constructor_index, 1, implicit,
8894 braced_init_obstack);
8895 RESTORE_SPELLING_DEPTH (constructor_depth);
8898 constructor_index
8899 = size_binop_loc (input_location, PLUS_EXPR,
8900 constructor_index, bitsize_one_node);
8902 if (!value.value)
8903 /* If we are doing the bookkeeping for an element that was
8904 directly output as a constructor, we must update
8905 constructor_unfilled_index. */
8906 constructor_unfilled_index = constructor_index;
8908 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8910 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8912 /* Do a basic check of initializer size. Note that vectors
8913 always have a fixed size derived from their type. */
8914 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8916 pedwarn_init (loc, 0,
8917 "excess elements in vector initializer");
8918 break;
8921 /* Now output the actual element. */
8922 if (value.value)
8924 if (TREE_CODE (value.value) == VECTOR_CST)
8925 elttype = TYPE_MAIN_VARIANT (constructor_type);
8926 output_init_element (loc, value.value, value.original_type,
8927 strict_string, elttype,
8928 constructor_index, 1, implicit,
8929 braced_init_obstack);
8932 constructor_index
8933 = size_binop_loc (input_location,
8934 PLUS_EXPR, constructor_index, bitsize_one_node);
8936 if (!value.value)
8937 /* If we are doing the bookkeeping for an element that was
8938 directly output as a constructor, we must update
8939 constructor_unfilled_index. */
8940 constructor_unfilled_index = constructor_index;
8943 /* Handle the sole element allowed in a braced initializer
8944 for a scalar variable. */
8945 else if (constructor_type != error_mark_node
8946 && constructor_fields == 0)
8948 pedwarn_init (loc, 0,
8949 "excess elements in scalar initializer");
8950 break;
8952 else
8954 if (value.value)
8955 output_init_element (loc, value.value, value.original_type,
8956 strict_string, constructor_type,
8957 NULL_TREE, 1, implicit,
8958 braced_init_obstack);
8959 constructor_fields = 0;
8962 /* Handle range initializers either at this level or anywhere higher
8963 in the designator stack. */
8964 if (constructor_range_stack)
8966 struct constructor_range_stack *p, *range_stack;
8967 int finish = 0;
8969 range_stack = constructor_range_stack;
8970 constructor_range_stack = 0;
8971 while (constructor_stack != range_stack->stack)
8973 gcc_assert (constructor_stack->implicit);
8974 process_init_element (loc,
8975 pop_init_level (loc, 1,
8976 braced_init_obstack),
8977 true, braced_init_obstack);
8979 for (p = range_stack;
8980 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8981 p = p->prev)
8983 gcc_assert (constructor_stack->implicit);
8984 process_init_element (loc,
8985 pop_init_level (loc, 1,
8986 braced_init_obstack),
8987 true, braced_init_obstack);
8990 p->index = size_binop_loc (input_location,
8991 PLUS_EXPR, p->index, bitsize_one_node);
8992 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8993 finish = 1;
8995 while (1)
8997 constructor_index = p->index;
8998 constructor_fields = p->fields;
8999 if (finish && p->range_end && p->index == p->range_start)
9001 finish = 0;
9002 p->prev = 0;
9004 p = p->next;
9005 if (!p)
9006 break;
9007 push_init_level (loc, 2, braced_init_obstack);
9008 p->stack = constructor_stack;
9009 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9010 p->index = p->range_start;
9013 if (!finish)
9014 constructor_range_stack = range_stack;
9015 continue;
9018 break;
9021 constructor_range_stack = 0;
9024 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9025 (guaranteed to be 'volatile' or null) and ARGS (represented using
9026 an ASM_EXPR node). */
9027 tree
9028 build_asm_stmt (tree cv_qualifier, tree args)
9030 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9031 ASM_VOLATILE_P (args) = 1;
9032 return add_stmt (args);
9035 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9036 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9037 SIMPLE indicates whether there was anything at all after the
9038 string in the asm expression -- asm("blah") and asm("blah" : )
9039 are subtly different. We use a ASM_EXPR node to represent this. */
9040 tree
9041 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9042 tree clobbers, tree labels, bool simple)
9044 tree tail;
9045 tree args;
9046 int i;
9047 const char *constraint;
9048 const char **oconstraints;
9049 bool allows_mem, allows_reg, is_inout;
9050 int ninputs, noutputs;
9052 ninputs = list_length (inputs);
9053 noutputs = list_length (outputs);
9054 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9056 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9058 /* Remove output conversions that change the type but not the mode. */
9059 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9061 tree output = TREE_VALUE (tail);
9063 output = c_fully_fold (output, false, NULL);
9065 /* ??? Really, this should not be here. Users should be using a
9066 proper lvalue, dammit. But there's a long history of using casts
9067 in the output operands. In cases like longlong.h, this becomes a
9068 primitive form of typechecking -- if the cast can be removed, then
9069 the output operand had a type of the proper width; otherwise we'll
9070 get an error. Gross, but ... */
9071 STRIP_NOPS (output);
9073 if (!lvalue_or_else (loc, output, lv_asm))
9074 output = error_mark_node;
9076 if (output != error_mark_node
9077 && (TREE_READONLY (output)
9078 || TYPE_READONLY (TREE_TYPE (output))
9079 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9080 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9081 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9082 readonly_error (loc, output, lv_asm);
9084 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9085 oconstraints[i] = constraint;
9087 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9088 &allows_mem, &allows_reg, &is_inout))
9090 /* If the operand is going to end up in memory,
9091 mark it addressable. */
9092 if (!allows_reg && !c_mark_addressable (output))
9093 output = error_mark_node;
9094 if (!(!allows_reg && allows_mem)
9095 && output != error_mark_node
9096 && VOID_TYPE_P (TREE_TYPE (output)))
9098 error_at (loc, "invalid use of void expression");
9099 output = error_mark_node;
9102 else
9103 output = error_mark_node;
9105 TREE_VALUE (tail) = output;
9108 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9110 tree input;
9112 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9113 input = TREE_VALUE (tail);
9115 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9116 oconstraints, &allows_mem, &allows_reg))
9118 /* If the operand is going to end up in memory,
9119 mark it addressable. */
9120 if (!allows_reg && allows_mem)
9122 input = c_fully_fold (input, false, NULL);
9124 /* Strip the nops as we allow this case. FIXME, this really
9125 should be rejected or made deprecated. */
9126 STRIP_NOPS (input);
9127 if (!c_mark_addressable (input))
9128 input = error_mark_node;
9130 else
9132 struct c_expr expr;
9133 memset (&expr, 0, sizeof (expr));
9134 expr.value = input;
9135 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9136 input = c_fully_fold (expr.value, false, NULL);
9138 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9140 error_at (loc, "invalid use of void expression");
9141 input = error_mark_node;
9145 else
9146 input = error_mark_node;
9148 TREE_VALUE (tail) = input;
9151 /* ASMs with labels cannot have outputs. This should have been
9152 enforced by the parser. */
9153 gcc_assert (outputs == NULL || labels == NULL);
9155 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9157 /* asm statements without outputs, including simple ones, are treated
9158 as volatile. */
9159 ASM_INPUT_P (args) = simple;
9160 ASM_VOLATILE_P (args) = (noutputs == 0);
9162 return args;
9165 /* Generate a goto statement to LABEL. LOC is the location of the
9166 GOTO. */
9168 tree
9169 c_finish_goto_label (location_t loc, tree label)
9171 tree decl = lookup_label_for_goto (loc, label);
9172 if (!decl)
9173 return NULL_TREE;
9174 TREE_USED (decl) = 1;
9176 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9177 SET_EXPR_LOCATION (t, loc);
9178 return add_stmt (t);
9182 /* Generate a computed goto statement to EXPR. LOC is the location of
9183 the GOTO. */
9185 tree
9186 c_finish_goto_ptr (location_t loc, tree expr)
9188 tree t;
9189 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9190 expr = c_fully_fold (expr, false, NULL);
9191 expr = convert (ptr_type_node, expr);
9192 t = build1 (GOTO_EXPR, void_type_node, expr);
9193 SET_EXPR_LOCATION (t, loc);
9194 return add_stmt (t);
9197 /* Generate a C `return' statement. RETVAL is the expression for what
9198 to return, or a null pointer for `return;' with no value. LOC is
9199 the location of the return statement, or the location of the expression,
9200 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9201 is the original type of RETVAL. */
9203 tree
9204 c_finish_return (location_t loc, tree retval, tree origtype)
9206 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9207 bool no_warning = false;
9208 bool npc = false;
9209 size_t rank = 0;
9211 if (TREE_THIS_VOLATILE (current_function_decl))
9212 warning_at (loc, 0,
9213 "function declared %<noreturn%> has a %<return%> statement");
9215 if (flag_cilkplus && contains_array_notation_expr (retval))
9217 /* Array notations are allowed in a return statement if it is inside a
9218 built-in array notation reduction function. */
9219 if (!find_rank (loc, retval, retval, false, &rank))
9220 return error_mark_node;
9221 if (rank >= 1)
9223 error_at (loc, "array notation expression cannot be used as a "
9224 "return value");
9225 return error_mark_node;
9228 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9230 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9231 "allowed");
9232 return error_mark_node;
9234 if (retval)
9236 tree semantic_type = NULL_TREE;
9237 npc = null_pointer_constant_p (retval);
9238 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9240 semantic_type = TREE_TYPE (retval);
9241 retval = TREE_OPERAND (retval, 0);
9243 retval = c_fully_fold (retval, false, NULL);
9244 if (semantic_type)
9245 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9248 if (!retval)
9250 current_function_returns_null = 1;
9251 if ((warn_return_type || flag_isoc99)
9252 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9254 if (flag_isoc99)
9255 pedwarn (loc, 0, "%<return%> with no value, in "
9256 "function returning non-void");
9257 else
9258 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9259 "in function returning non-void");
9260 no_warning = true;
9263 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9265 current_function_returns_null = 1;
9266 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9267 pedwarn (loc, 0,
9268 "%<return%> with a value, in function returning void");
9269 else
9270 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9271 "%<return%> with expression, in function returning void");
9273 else
9275 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9276 retval, origtype, ic_return,
9277 npc, NULL_TREE, NULL_TREE, 0);
9278 tree res = DECL_RESULT (current_function_decl);
9279 tree inner;
9280 bool save;
9282 current_function_returns_value = 1;
9283 if (t == error_mark_node)
9284 return NULL_TREE;
9286 save = in_late_binary_op;
9287 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9288 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9289 in_late_binary_op = true;
9290 inner = t = convert (TREE_TYPE (res), t);
9291 in_late_binary_op = save;
9293 /* Strip any conversions, additions, and subtractions, and see if
9294 we are returning the address of a local variable. Warn if so. */
9295 while (1)
9297 switch (TREE_CODE (inner))
9299 CASE_CONVERT:
9300 case NON_LVALUE_EXPR:
9301 case PLUS_EXPR:
9302 case POINTER_PLUS_EXPR:
9303 inner = TREE_OPERAND (inner, 0);
9304 continue;
9306 case MINUS_EXPR:
9307 /* If the second operand of the MINUS_EXPR has a pointer
9308 type (or is converted from it), this may be valid, so
9309 don't give a warning. */
9311 tree op1 = TREE_OPERAND (inner, 1);
9313 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9314 && (CONVERT_EXPR_P (op1)
9315 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9316 op1 = TREE_OPERAND (op1, 0);
9318 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9319 break;
9321 inner = TREE_OPERAND (inner, 0);
9322 continue;
9325 case ADDR_EXPR:
9326 inner = TREE_OPERAND (inner, 0);
9328 while (REFERENCE_CLASS_P (inner)
9329 && TREE_CODE (inner) != INDIRECT_REF)
9330 inner = TREE_OPERAND (inner, 0);
9332 if (DECL_P (inner)
9333 && !DECL_EXTERNAL (inner)
9334 && !TREE_STATIC (inner)
9335 && DECL_CONTEXT (inner) == current_function_decl)
9337 if (TREE_CODE (inner) == LABEL_DECL)
9338 warning_at (loc, OPT_Wreturn_local_addr,
9339 "function returns address of label");
9340 else
9342 warning_at (loc, OPT_Wreturn_local_addr,
9343 "function returns address of local variable");
9344 tree zero = build_zero_cst (TREE_TYPE (res));
9345 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9348 break;
9350 default:
9351 break;
9354 break;
9357 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9358 SET_EXPR_LOCATION (retval, loc);
9360 if (warn_sequence_point)
9361 verify_sequence_points (retval);
9364 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9365 TREE_NO_WARNING (ret_stmt) |= no_warning;
9366 return add_stmt (ret_stmt);
9369 struct c_switch {
9370 /* The SWITCH_EXPR being built. */
9371 tree switch_expr;
9373 /* The original type of the testing expression, i.e. before the
9374 default conversion is applied. */
9375 tree orig_type;
9377 /* A splay-tree mapping the low element of a case range to the high
9378 element, or NULL_TREE if there is no high element. Used to
9379 determine whether or not a new case label duplicates an old case
9380 label. We need a tree, rather than simply a hash table, because
9381 of the GNU case range extension. */
9382 splay_tree cases;
9384 /* The bindings at the point of the switch. This is used for
9385 warnings crossing decls when branching to a case label. */
9386 struct c_spot_bindings *bindings;
9388 /* The next node on the stack. */
9389 struct c_switch *next;
9392 /* A stack of the currently active switch statements. The innermost
9393 switch statement is on the top of the stack. There is no need to
9394 mark the stack for garbage collection because it is only active
9395 during the processing of the body of a function, and we never
9396 collect at that point. */
9398 struct c_switch *c_switch_stack;
9400 /* Start a C switch statement, testing expression EXP. Return the new
9401 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9402 SWITCH_COND_LOC is the location of the switch's condition.
9403 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9405 tree
9406 c_start_case (location_t switch_loc,
9407 location_t switch_cond_loc,
9408 tree exp, bool explicit_cast_p)
9410 tree orig_type = error_mark_node;
9411 struct c_switch *cs;
9413 if (exp != error_mark_node)
9415 orig_type = TREE_TYPE (exp);
9417 if (!INTEGRAL_TYPE_P (orig_type))
9419 if (orig_type != error_mark_node)
9421 error_at (switch_cond_loc, "switch quantity not an integer");
9422 orig_type = error_mark_node;
9424 exp = integer_zero_node;
9426 else
9428 tree type = TYPE_MAIN_VARIANT (orig_type);
9429 tree e = exp;
9431 /* Warn if the condition has boolean value. */
9432 while (TREE_CODE (e) == COMPOUND_EXPR)
9433 e = TREE_OPERAND (e, 1);
9435 if ((TREE_CODE (type) == BOOLEAN_TYPE
9436 || truth_value_p (TREE_CODE (e)))
9437 /* Explicit cast to int suppresses this warning. */
9438 && !(TREE_CODE (type) == INTEGER_TYPE
9439 && explicit_cast_p))
9440 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9441 "switch condition has boolean value");
9443 if (!in_system_header_at (input_location)
9444 && (type == long_integer_type_node
9445 || type == long_unsigned_type_node))
9446 warning_at (switch_cond_loc,
9447 OPT_Wtraditional, "%<long%> switch expression not "
9448 "converted to %<int%> in ISO C");
9450 exp = c_fully_fold (exp, false, NULL);
9451 exp = default_conversion (exp);
9453 if (warn_sequence_point)
9454 verify_sequence_points (exp);
9458 /* Add this new SWITCH_EXPR to the stack. */
9459 cs = XNEW (struct c_switch);
9460 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9461 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9462 cs->orig_type = orig_type;
9463 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9464 cs->bindings = c_get_switch_bindings ();
9465 cs->next = c_switch_stack;
9466 c_switch_stack = cs;
9468 return add_stmt (cs->switch_expr);
9471 /* Process a case label at location LOC. */
9473 tree
9474 do_case (location_t loc, tree low_value, tree high_value)
9476 tree label = NULL_TREE;
9478 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9480 low_value = c_fully_fold (low_value, false, NULL);
9481 if (TREE_CODE (low_value) == INTEGER_CST)
9482 pedwarn (loc, OPT_Wpedantic,
9483 "case label is not an integer constant expression");
9486 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9488 high_value = c_fully_fold (high_value, false, NULL);
9489 if (TREE_CODE (high_value) == INTEGER_CST)
9490 pedwarn (input_location, OPT_Wpedantic,
9491 "case label is not an integer constant expression");
9494 if (c_switch_stack == NULL)
9496 if (low_value)
9497 error_at (loc, "case label not within a switch statement");
9498 else
9499 error_at (loc, "%<default%> label not within a switch statement");
9500 return NULL_TREE;
9503 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9504 EXPR_LOCATION (c_switch_stack->switch_expr),
9505 loc))
9506 return NULL_TREE;
9508 label = c_add_case_label (loc, c_switch_stack->cases,
9509 SWITCH_COND (c_switch_stack->switch_expr),
9510 c_switch_stack->orig_type,
9511 low_value, high_value);
9512 if (label == error_mark_node)
9513 label = NULL_TREE;
9514 return label;
9517 /* Finish the switch statement. TYPE is the original type of the
9518 controlling expression of the switch, or NULL_TREE. */
9520 void
9521 c_finish_case (tree body, tree type)
9523 struct c_switch *cs = c_switch_stack;
9524 location_t switch_location;
9526 SWITCH_BODY (cs->switch_expr) = body;
9528 /* Emit warnings as needed. */
9529 switch_location = EXPR_LOCATION (cs->switch_expr);
9530 c_do_switch_warnings (cs->cases, switch_location,
9531 type ? type : TREE_TYPE (cs->switch_expr),
9532 SWITCH_COND (cs->switch_expr));
9534 /* Pop the stack. */
9535 c_switch_stack = cs->next;
9536 splay_tree_delete (cs->cases);
9537 c_release_switch_bindings (cs->bindings);
9538 XDELETE (cs);
9541 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9542 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9543 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9544 statement, and was not surrounded with parenthesis. */
9546 void
9547 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9548 tree else_block, bool nested_if)
9550 tree stmt;
9552 /* If the condition has array notations, then the rank of the then_block and
9553 else_block must be either 0 or be equal to the rank of the condition. If
9554 the condition does not have array notations then break them up as it is
9555 broken up in a normal expression. */
9556 if (flag_cilkplus && contains_array_notation_expr (cond))
9558 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9559 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9560 return;
9561 if (then_block
9562 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9563 return;
9564 if (else_block
9565 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9566 return;
9567 if (cond_rank != then_rank && then_rank != 0)
9569 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9570 " and the then-block");
9571 return;
9573 else if (cond_rank != else_rank && else_rank != 0)
9575 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9576 " and the else-block");
9577 return;
9580 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9581 if (warn_parentheses && nested_if && else_block == NULL)
9583 tree inner_if = then_block;
9585 /* We know from the grammar productions that there is an IF nested
9586 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9587 it might not be exactly THEN_BLOCK, but should be the last
9588 non-container statement within. */
9589 while (1)
9590 switch (TREE_CODE (inner_if))
9592 case COND_EXPR:
9593 goto found;
9594 case BIND_EXPR:
9595 inner_if = BIND_EXPR_BODY (inner_if);
9596 break;
9597 case STATEMENT_LIST:
9598 inner_if = expr_last (then_block);
9599 break;
9600 case TRY_FINALLY_EXPR:
9601 case TRY_CATCH_EXPR:
9602 inner_if = TREE_OPERAND (inner_if, 0);
9603 break;
9604 default:
9605 gcc_unreachable ();
9607 found:
9609 if (COND_EXPR_ELSE (inner_if))
9610 warning_at (if_locus, OPT_Wparentheses,
9611 "suggest explicit braces to avoid ambiguous %<else%>");
9614 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9615 SET_EXPR_LOCATION (stmt, if_locus);
9616 add_stmt (stmt);
9619 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9620 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9621 is false for DO loops. INCR is the FOR increment expression. BODY is
9622 the statement controlled by the loop. BLAB is the break label. CLAB is
9623 the continue label. Everything is allowed to be NULL. */
9625 void
9626 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9627 tree blab, tree clab, bool cond_is_first)
9629 tree entry = NULL, exit = NULL, t;
9631 /* In theory could forbid cilk spawn for loop increment expression,
9632 but it should work just fine. */
9634 /* If the condition is zero don't generate a loop construct. */
9635 if (cond && integer_zerop (cond))
9637 if (cond_is_first)
9639 t = build_and_jump (&blab);
9640 SET_EXPR_LOCATION (t, start_locus);
9641 add_stmt (t);
9644 else
9646 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9648 /* If we have an exit condition, then we build an IF with gotos either
9649 out of the loop, or to the top of it. If there's no exit condition,
9650 then we just build a jump back to the top. */
9651 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9653 if (cond && !integer_nonzerop (cond))
9655 /* Canonicalize the loop condition to the end. This means
9656 generating a branch to the loop condition. Reuse the
9657 continue label, if possible. */
9658 if (cond_is_first)
9660 if (incr || !clab)
9662 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9663 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9665 else
9666 t = build1 (GOTO_EXPR, void_type_node, clab);
9667 SET_EXPR_LOCATION (t, start_locus);
9668 add_stmt (t);
9671 t = build_and_jump (&blab);
9672 if (cond_is_first)
9673 exit = fold_build3_loc (start_locus,
9674 COND_EXPR, void_type_node, cond, exit, t);
9675 else
9676 exit = fold_build3_loc (input_location,
9677 COND_EXPR, void_type_node, cond, exit, t);
9680 add_stmt (top);
9683 if (body)
9684 add_stmt (body);
9685 if (clab)
9686 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9687 if (incr)
9688 add_stmt (incr);
9689 if (entry)
9690 add_stmt (entry);
9691 if (exit)
9692 add_stmt (exit);
9693 if (blab)
9694 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9697 tree
9698 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9700 bool skip;
9701 tree label = *label_p;
9703 /* In switch statements break is sometimes stylistically used after
9704 a return statement. This can lead to spurious warnings about
9705 control reaching the end of a non-void function when it is
9706 inlined. Note that we are calling block_may_fallthru with
9707 language specific tree nodes; this works because
9708 block_may_fallthru returns true when given something it does not
9709 understand. */
9710 skip = !block_may_fallthru (cur_stmt_list);
9712 if (!label)
9714 if (!skip)
9715 *label_p = label = create_artificial_label (loc);
9717 else if (TREE_CODE (label) == LABEL_DECL)
9719 else switch (TREE_INT_CST_LOW (label))
9721 case 0:
9722 if (is_break)
9723 error_at (loc, "break statement not within loop or switch");
9724 else
9725 error_at (loc, "continue statement not within a loop");
9726 return NULL_TREE;
9728 case 1:
9729 gcc_assert (is_break);
9730 error_at (loc, "break statement used with OpenMP for loop");
9731 return NULL_TREE;
9733 case 2:
9734 if (is_break)
9735 error ("break statement within %<#pragma simd%> loop body");
9736 else
9737 error ("continue statement within %<#pragma simd%> loop body");
9738 return NULL_TREE;
9740 default:
9741 gcc_unreachable ();
9744 if (skip)
9745 return NULL_TREE;
9747 if (!is_break)
9748 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9750 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9753 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9755 static void
9756 emit_side_effect_warnings (location_t loc, tree expr)
9758 if (expr == error_mark_node)
9760 else if (!TREE_SIDE_EFFECTS (expr))
9762 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9763 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9765 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9767 tree r = expr;
9768 location_t cloc = loc;
9769 while (TREE_CODE (r) == COMPOUND_EXPR)
9771 if (EXPR_HAS_LOCATION (r))
9772 cloc = EXPR_LOCATION (r);
9773 r = TREE_OPERAND (r, 1);
9775 if (!TREE_SIDE_EFFECTS (r)
9776 && !VOID_TYPE_P (TREE_TYPE (r))
9777 && !CONVERT_EXPR_P (r)
9778 && !TREE_NO_WARNING (r)
9779 && !TREE_NO_WARNING (expr))
9780 warning_at (cloc, OPT_Wunused_value,
9781 "right-hand operand of comma expression has no effect");
9783 else
9784 warn_if_unused_value (expr, loc);
9787 /* Process an expression as if it were a complete statement. Emit
9788 diagnostics, but do not call ADD_STMT. LOC is the location of the
9789 statement. */
9791 tree
9792 c_process_expr_stmt (location_t loc, tree expr)
9794 tree exprv;
9796 if (!expr)
9797 return NULL_TREE;
9799 expr = c_fully_fold (expr, false, NULL);
9801 if (warn_sequence_point)
9802 verify_sequence_points (expr);
9804 if (TREE_TYPE (expr) != error_mark_node
9805 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9806 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9807 error_at (loc, "expression statement has incomplete type");
9809 /* If we're not processing a statement expression, warn about unused values.
9810 Warnings for statement expressions will be emitted later, once we figure
9811 out which is the result. */
9812 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9813 && warn_unused_value)
9814 emit_side_effect_warnings (loc, expr);
9816 exprv = expr;
9817 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9818 exprv = TREE_OPERAND (exprv, 1);
9819 while (CONVERT_EXPR_P (exprv))
9820 exprv = TREE_OPERAND (exprv, 0);
9821 if (DECL_P (exprv)
9822 || handled_component_p (exprv)
9823 || TREE_CODE (exprv) == ADDR_EXPR)
9824 mark_exp_read (exprv);
9826 /* If the expression is not of a type to which we cannot assign a line
9827 number, wrap the thing in a no-op NOP_EXPR. */
9828 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9830 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9831 SET_EXPR_LOCATION (expr, loc);
9834 return expr;
9837 /* Emit an expression as a statement. LOC is the location of the
9838 expression. */
9840 tree
9841 c_finish_expr_stmt (location_t loc, tree expr)
9843 if (expr)
9844 return add_stmt (c_process_expr_stmt (loc, expr));
9845 else
9846 return NULL;
9849 /* Do the opposite and emit a statement as an expression. To begin,
9850 create a new binding level and return it. */
9852 tree
9853 c_begin_stmt_expr (void)
9855 tree ret;
9857 /* We must force a BLOCK for this level so that, if it is not expanded
9858 later, there is a way to turn off the entire subtree of blocks that
9859 are contained in it. */
9860 keep_next_level ();
9861 ret = c_begin_compound_stmt (true);
9863 c_bindings_start_stmt_expr (c_switch_stack == NULL
9864 ? NULL
9865 : c_switch_stack->bindings);
9867 /* Mark the current statement list as belonging to a statement list. */
9868 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9870 return ret;
9873 /* LOC is the location of the compound statement to which this body
9874 belongs. */
9876 tree
9877 c_finish_stmt_expr (location_t loc, tree body)
9879 tree last, type, tmp, val;
9880 tree *last_p;
9882 body = c_end_compound_stmt (loc, body, true);
9884 c_bindings_end_stmt_expr (c_switch_stack == NULL
9885 ? NULL
9886 : c_switch_stack->bindings);
9888 /* Locate the last statement in BODY. See c_end_compound_stmt
9889 about always returning a BIND_EXPR. */
9890 last_p = &BIND_EXPR_BODY (body);
9891 last = BIND_EXPR_BODY (body);
9893 continue_searching:
9894 if (TREE_CODE (last) == STATEMENT_LIST)
9896 tree_stmt_iterator i;
9898 /* This can happen with degenerate cases like ({ }). No value. */
9899 if (!TREE_SIDE_EFFECTS (last))
9900 return body;
9902 /* If we're supposed to generate side effects warnings, process
9903 all of the statements except the last. */
9904 if (warn_unused_value)
9906 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9908 location_t tloc;
9909 tree t = tsi_stmt (i);
9911 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9912 emit_side_effect_warnings (tloc, t);
9915 else
9916 i = tsi_last (last);
9917 last_p = tsi_stmt_ptr (i);
9918 last = *last_p;
9921 /* If the end of the list is exception related, then the list was split
9922 by a call to push_cleanup. Continue searching. */
9923 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9924 || TREE_CODE (last) == TRY_CATCH_EXPR)
9926 last_p = &TREE_OPERAND (last, 0);
9927 last = *last_p;
9928 goto continue_searching;
9931 if (last == error_mark_node)
9932 return last;
9934 /* In the case that the BIND_EXPR is not necessary, return the
9935 expression out from inside it. */
9936 if (last == BIND_EXPR_BODY (body)
9937 && BIND_EXPR_VARS (body) == NULL)
9939 /* Even if this looks constant, do not allow it in a constant
9940 expression. */
9941 last = c_wrap_maybe_const (last, true);
9942 /* Do not warn if the return value of a statement expression is
9943 unused. */
9944 TREE_NO_WARNING (last) = 1;
9945 return last;
9948 /* Extract the type of said expression. */
9949 type = TREE_TYPE (last);
9951 /* If we're not returning a value at all, then the BIND_EXPR that
9952 we already have is a fine expression to return. */
9953 if (!type || VOID_TYPE_P (type))
9954 return body;
9956 /* Now that we've located the expression containing the value, it seems
9957 silly to make voidify_wrapper_expr repeat the process. Create a
9958 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9959 tmp = create_tmp_var_raw (type, NULL);
9961 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9962 tree_expr_nonnegative_p giving up immediately. */
9963 val = last;
9964 if (TREE_CODE (val) == NOP_EXPR
9965 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9966 val = TREE_OPERAND (val, 0);
9968 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9969 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9972 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9973 SET_EXPR_LOCATION (t, loc);
9974 return t;
9978 /* Begin and end compound statements. This is as simple as pushing
9979 and popping new statement lists from the tree. */
9981 tree
9982 c_begin_compound_stmt (bool do_scope)
9984 tree stmt = push_stmt_list ();
9985 if (do_scope)
9986 push_scope ();
9987 return stmt;
9990 /* End a compound statement. STMT is the statement. LOC is the
9991 location of the compound statement-- this is usually the location
9992 of the opening brace. */
9994 tree
9995 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9997 tree block = NULL;
9999 if (do_scope)
10001 if (c_dialect_objc ())
10002 objc_clear_super_receiver ();
10003 block = pop_scope ();
10006 stmt = pop_stmt_list (stmt);
10007 stmt = c_build_bind_expr (loc, block, stmt);
10009 /* If this compound statement is nested immediately inside a statement
10010 expression, then force a BIND_EXPR to be created. Otherwise we'll
10011 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10012 STATEMENT_LISTs merge, and thus we can lose track of what statement
10013 was really last. */
10014 if (building_stmt_list_p ()
10015 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10016 && TREE_CODE (stmt) != BIND_EXPR)
10018 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10019 TREE_SIDE_EFFECTS (stmt) = 1;
10020 SET_EXPR_LOCATION (stmt, loc);
10023 return stmt;
10026 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10027 when the current scope is exited. EH_ONLY is true when this is not
10028 meant to apply to normal control flow transfer. */
10030 void
10031 push_cleanup (tree decl, tree cleanup, bool eh_only)
10033 enum tree_code code;
10034 tree stmt, list;
10035 bool stmt_expr;
10037 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10038 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10039 add_stmt (stmt);
10040 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10041 list = push_stmt_list ();
10042 TREE_OPERAND (stmt, 0) = list;
10043 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10046 /* Build a binary-operation expression without default conversions.
10047 CODE is the kind of expression to build.
10048 LOCATION is the operator's location.
10049 This function differs from `build' in several ways:
10050 the data type of the result is computed and recorded in it,
10051 warnings are generated if arg data types are invalid,
10052 special handling for addition and subtraction of pointers is known,
10053 and some optimization is done (operations on narrow ints
10054 are done in the narrower type when that gives the same result).
10055 Constant folding is also done before the result is returned.
10057 Note that the operands will never have enumeral types, or function
10058 or array types, because either they will have the default conversions
10059 performed or they have both just been converted to some other type in which
10060 the arithmetic is to be done. */
10062 tree
10063 build_binary_op (location_t location, enum tree_code code,
10064 tree orig_op0, tree orig_op1, int convert_p)
10066 tree type0, type1, orig_type0, orig_type1;
10067 tree eptype;
10068 enum tree_code code0, code1;
10069 tree op0, op1;
10070 tree ret = error_mark_node;
10071 const char *invalid_op_diag;
10072 bool op0_int_operands, op1_int_operands;
10073 bool int_const, int_const_or_overflow, int_operands;
10075 /* Expression code to give to the expression when it is built.
10076 Normally this is CODE, which is what the caller asked for,
10077 but in some special cases we change it. */
10078 enum tree_code resultcode = code;
10080 /* Data type in which the computation is to be performed.
10081 In the simplest cases this is the common type of the arguments. */
10082 tree result_type = NULL;
10084 /* When the computation is in excess precision, the type of the
10085 final EXCESS_PRECISION_EXPR. */
10086 tree semantic_result_type = NULL;
10088 /* Nonzero means operands have already been type-converted
10089 in whatever way is necessary.
10090 Zero means they need to be converted to RESULT_TYPE. */
10091 int converted = 0;
10093 /* Nonzero means create the expression with this type, rather than
10094 RESULT_TYPE. */
10095 tree build_type = 0;
10097 /* Nonzero means after finally constructing the expression
10098 convert it to this type. */
10099 tree final_type = 0;
10101 /* Nonzero if this is an operation like MIN or MAX which can
10102 safely be computed in short if both args are promoted shorts.
10103 Also implies COMMON.
10104 -1 indicates a bitwise operation; this makes a difference
10105 in the exact conditions for when it is safe to do the operation
10106 in a narrower mode. */
10107 int shorten = 0;
10109 /* Nonzero if this is a comparison operation;
10110 if both args are promoted shorts, compare the original shorts.
10111 Also implies COMMON. */
10112 int short_compare = 0;
10114 /* Nonzero if this is a right-shift operation, which can be computed on the
10115 original short and then promoted if the operand is a promoted short. */
10116 int short_shift = 0;
10118 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10119 int common = 0;
10121 /* True means types are compatible as far as ObjC is concerned. */
10122 bool objc_ok;
10124 /* True means this is an arithmetic operation that may need excess
10125 precision. */
10126 bool may_need_excess_precision;
10128 /* True means this is a boolean operation that converts both its
10129 operands to truth-values. */
10130 bool boolean_op = false;
10132 /* Remember whether we're doing / or %. */
10133 bool doing_div_or_mod = false;
10135 /* Remember whether we're doing << or >>. */
10136 bool doing_shift = false;
10138 /* Tree holding instrumentation expression. */
10139 tree instrument_expr = NULL;
10141 if (location == UNKNOWN_LOCATION)
10142 location = input_location;
10144 op0 = orig_op0;
10145 op1 = orig_op1;
10147 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10148 if (op0_int_operands)
10149 op0 = remove_c_maybe_const_expr (op0);
10150 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10151 if (op1_int_operands)
10152 op1 = remove_c_maybe_const_expr (op1);
10153 int_operands = (op0_int_operands && op1_int_operands);
10154 if (int_operands)
10156 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10157 && TREE_CODE (orig_op1) == INTEGER_CST);
10158 int_const = (int_const_or_overflow
10159 && !TREE_OVERFLOW (orig_op0)
10160 && !TREE_OVERFLOW (orig_op1));
10162 else
10163 int_const = int_const_or_overflow = false;
10165 /* Do not apply default conversion in mixed vector/scalar expression. */
10166 if (convert_p
10167 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10168 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10170 op0 = default_conversion (op0);
10171 op1 = default_conversion (op1);
10174 /* When Cilk Plus is enabled and there are array notations inside op0, then
10175 we check to see if there are builtin array notation functions. If
10176 so, then we take on the type of the array notation inside it. */
10177 if (flag_cilkplus && contains_array_notation_expr (op0))
10178 orig_type0 = type0 = find_correct_array_notation_type (op0);
10179 else
10180 orig_type0 = type0 = TREE_TYPE (op0);
10182 if (flag_cilkplus && contains_array_notation_expr (op1))
10183 orig_type1 = type1 = find_correct_array_notation_type (op1);
10184 else
10185 orig_type1 = type1 = TREE_TYPE (op1);
10187 /* The expression codes of the data types of the arguments tell us
10188 whether the arguments are integers, floating, pointers, etc. */
10189 code0 = TREE_CODE (type0);
10190 code1 = TREE_CODE (type1);
10192 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10193 STRIP_TYPE_NOPS (op0);
10194 STRIP_TYPE_NOPS (op1);
10196 /* If an error was already reported for one of the arguments,
10197 avoid reporting another error. */
10199 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10200 return error_mark_node;
10202 if ((invalid_op_diag
10203 = targetm.invalid_binary_op (code, type0, type1)))
10205 error_at (location, invalid_op_diag);
10206 return error_mark_node;
10209 switch (code)
10211 case PLUS_EXPR:
10212 case MINUS_EXPR:
10213 case MULT_EXPR:
10214 case TRUNC_DIV_EXPR:
10215 case CEIL_DIV_EXPR:
10216 case FLOOR_DIV_EXPR:
10217 case ROUND_DIV_EXPR:
10218 case EXACT_DIV_EXPR:
10219 may_need_excess_precision = true;
10220 break;
10221 default:
10222 may_need_excess_precision = false;
10223 break;
10225 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10227 op0 = TREE_OPERAND (op0, 0);
10228 type0 = TREE_TYPE (op0);
10230 else if (may_need_excess_precision
10231 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10233 type0 = eptype;
10234 op0 = convert (eptype, op0);
10236 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10238 op1 = TREE_OPERAND (op1, 0);
10239 type1 = TREE_TYPE (op1);
10241 else if (may_need_excess_precision
10242 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10244 type1 = eptype;
10245 op1 = convert (eptype, op1);
10248 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10250 /* In case when one of the operands of the binary operation is
10251 a vector and another is a scalar -- convert scalar to vector. */
10252 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10254 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10255 true);
10257 switch (convert_flag)
10259 case stv_error:
10260 return error_mark_node;
10261 case stv_firstarg:
10263 bool maybe_const = true;
10264 tree sc;
10265 sc = c_fully_fold (op0, false, &maybe_const);
10266 sc = save_expr (sc);
10267 sc = convert (TREE_TYPE (type1), sc);
10268 op0 = build_vector_from_val (type1, sc);
10269 if (!maybe_const)
10270 op0 = c_wrap_maybe_const (op0, true);
10271 orig_type0 = type0 = TREE_TYPE (op0);
10272 code0 = TREE_CODE (type0);
10273 converted = 1;
10274 break;
10276 case stv_secondarg:
10278 bool maybe_const = true;
10279 tree sc;
10280 sc = c_fully_fold (op1, false, &maybe_const);
10281 sc = save_expr (sc);
10282 sc = convert (TREE_TYPE (type0), sc);
10283 op1 = build_vector_from_val (type0, sc);
10284 if (!maybe_const)
10285 op1 = c_wrap_maybe_const (op1, true);
10286 orig_type1 = type1 = TREE_TYPE (op1);
10287 code1 = TREE_CODE (type1);
10288 converted = 1;
10289 break;
10291 default:
10292 break;
10296 switch (code)
10298 case PLUS_EXPR:
10299 /* Handle the pointer + int case. */
10300 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10302 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10303 goto return_build_binary_op;
10305 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10307 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10308 goto return_build_binary_op;
10310 else
10311 common = 1;
10312 break;
10314 case MINUS_EXPR:
10315 /* Subtraction of two similar pointers.
10316 We must subtract them as integers, then divide by object size. */
10317 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10318 && comp_target_types (location, type0, type1))
10320 ret = pointer_diff (location, op0, op1);
10321 goto return_build_binary_op;
10323 /* Handle pointer minus int. Just like pointer plus int. */
10324 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10326 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10327 goto return_build_binary_op;
10329 else
10330 common = 1;
10331 break;
10333 case MULT_EXPR:
10334 common = 1;
10335 break;
10337 case TRUNC_DIV_EXPR:
10338 case CEIL_DIV_EXPR:
10339 case FLOOR_DIV_EXPR:
10340 case ROUND_DIV_EXPR:
10341 case EXACT_DIV_EXPR:
10342 doing_div_or_mod = true;
10343 warn_for_div_by_zero (location, op1);
10345 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10346 || code0 == FIXED_POINT_TYPE
10347 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10348 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10349 || code1 == FIXED_POINT_TYPE
10350 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10352 enum tree_code tcode0 = code0, tcode1 = code1;
10354 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10355 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10356 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10357 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10359 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10360 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10361 resultcode = RDIV_EXPR;
10362 else
10363 /* Although it would be tempting to shorten always here, that
10364 loses on some targets, since the modulo instruction is
10365 undefined if the quotient can't be represented in the
10366 computation mode. We shorten only if unsigned or if
10367 dividing by something we know != -1. */
10368 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10369 || (TREE_CODE (op1) == INTEGER_CST
10370 && !integer_all_onesp (op1)));
10371 common = 1;
10373 break;
10375 case BIT_AND_EXPR:
10376 case BIT_IOR_EXPR:
10377 case BIT_XOR_EXPR:
10378 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10379 shorten = -1;
10380 /* Allow vector types which are not floating point types. */
10381 else if (code0 == VECTOR_TYPE
10382 && code1 == VECTOR_TYPE
10383 && !VECTOR_FLOAT_TYPE_P (type0)
10384 && !VECTOR_FLOAT_TYPE_P (type1))
10385 common = 1;
10386 break;
10388 case TRUNC_MOD_EXPR:
10389 case FLOOR_MOD_EXPR:
10390 doing_div_or_mod = true;
10391 warn_for_div_by_zero (location, op1);
10393 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10394 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10395 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10396 common = 1;
10397 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10399 /* Although it would be tempting to shorten always here, that loses
10400 on some targets, since the modulo instruction is undefined if the
10401 quotient can't be represented in the computation mode. We shorten
10402 only if unsigned or if dividing by something we know != -1. */
10403 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10404 || (TREE_CODE (op1) == INTEGER_CST
10405 && !integer_all_onesp (op1)));
10406 common = 1;
10408 break;
10410 case TRUTH_ANDIF_EXPR:
10411 case TRUTH_ORIF_EXPR:
10412 case TRUTH_AND_EXPR:
10413 case TRUTH_OR_EXPR:
10414 case TRUTH_XOR_EXPR:
10415 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10416 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10417 || code0 == FIXED_POINT_TYPE)
10418 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10419 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10420 || code1 == FIXED_POINT_TYPE))
10422 /* Result of these operations is always an int,
10423 but that does not mean the operands should be
10424 converted to ints! */
10425 result_type = integer_type_node;
10426 if (op0_int_operands)
10428 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10429 op0 = remove_c_maybe_const_expr (op0);
10431 else
10432 op0 = c_objc_common_truthvalue_conversion (location, op0);
10433 if (op1_int_operands)
10435 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10436 op1 = remove_c_maybe_const_expr (op1);
10438 else
10439 op1 = c_objc_common_truthvalue_conversion (location, op1);
10440 converted = 1;
10441 boolean_op = true;
10443 if (code == TRUTH_ANDIF_EXPR)
10445 int_const_or_overflow = (int_operands
10446 && TREE_CODE (orig_op0) == INTEGER_CST
10447 && (op0 == truthvalue_false_node
10448 || TREE_CODE (orig_op1) == INTEGER_CST));
10449 int_const = (int_const_or_overflow
10450 && !TREE_OVERFLOW (orig_op0)
10451 && (op0 == truthvalue_false_node
10452 || !TREE_OVERFLOW (orig_op1)));
10454 else if (code == TRUTH_ORIF_EXPR)
10456 int_const_or_overflow = (int_operands
10457 && TREE_CODE (orig_op0) == INTEGER_CST
10458 && (op0 == truthvalue_true_node
10459 || TREE_CODE (orig_op1) == INTEGER_CST));
10460 int_const = (int_const_or_overflow
10461 && !TREE_OVERFLOW (orig_op0)
10462 && (op0 == truthvalue_true_node
10463 || !TREE_OVERFLOW (orig_op1)));
10465 break;
10467 /* Shift operations: result has same type as first operand;
10468 always convert second operand to int.
10469 Also set SHORT_SHIFT if shifting rightward. */
10471 case RSHIFT_EXPR:
10472 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10473 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10475 result_type = type0;
10476 converted = 1;
10478 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10479 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10480 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10481 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10483 result_type = type0;
10484 converted = 1;
10486 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10487 && code1 == INTEGER_TYPE)
10489 doing_shift = true;
10490 if (TREE_CODE (op1) == INTEGER_CST)
10492 if (tree_int_cst_sgn (op1) < 0)
10494 int_const = false;
10495 if (c_inhibit_evaluation_warnings == 0)
10496 warning_at (location, OPT_Wshift_count_negative,
10497 "right shift count is negative");
10499 else
10501 if (!integer_zerop (op1))
10502 short_shift = 1;
10504 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10506 int_const = false;
10507 if (c_inhibit_evaluation_warnings == 0)
10508 warning_at (location, OPT_Wshift_count_overflow,
10509 "right shift count >= width of type");
10514 /* Use the type of the value to be shifted. */
10515 result_type = type0;
10516 /* Avoid converting op1 to result_type later. */
10517 converted = 1;
10519 break;
10521 case LSHIFT_EXPR:
10522 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10523 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10525 result_type = type0;
10526 converted = 1;
10528 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10529 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10530 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10531 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10533 result_type = type0;
10534 converted = 1;
10536 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10537 && code1 == INTEGER_TYPE)
10539 doing_shift = true;
10540 if (TREE_CODE (op1) == INTEGER_CST)
10542 if (tree_int_cst_sgn (op1) < 0)
10544 int_const = false;
10545 if (c_inhibit_evaluation_warnings == 0)
10546 warning_at (location, OPT_Wshift_count_negative,
10547 "left shift count is negative");
10550 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10552 int_const = false;
10553 if (c_inhibit_evaluation_warnings == 0)
10554 warning_at (location, OPT_Wshift_count_overflow,
10555 "left shift count >= width of type");
10559 /* Use the type of the value to be shifted. */
10560 result_type = type0;
10561 /* Avoid converting op1 to result_type later. */
10562 converted = 1;
10564 break;
10566 case EQ_EXPR:
10567 case NE_EXPR:
10568 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10570 tree intt;
10571 if (!vector_types_compatible_elements_p (type0, type1))
10573 error_at (location, "comparing vectors with different "
10574 "element types");
10575 return error_mark_node;
10578 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10580 error_at (location, "comparing vectors with different "
10581 "number of elements");
10582 return error_mark_node;
10585 /* Always construct signed integer vector type. */
10586 intt = c_common_type_for_size (GET_MODE_BITSIZE
10587 (TYPE_MODE (TREE_TYPE (type0))), 0);
10588 result_type = build_opaque_vector_type (intt,
10589 TYPE_VECTOR_SUBPARTS (type0));
10590 converted = 1;
10591 break;
10593 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10594 warning_at (location,
10595 OPT_Wfloat_equal,
10596 "comparing floating point with == or != is unsafe");
10597 /* Result of comparison is always int,
10598 but don't convert the args to int! */
10599 build_type = integer_type_node;
10600 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10601 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10602 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10603 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10604 short_compare = 1;
10605 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10607 if (TREE_CODE (op0) == ADDR_EXPR
10608 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10610 if (code == EQ_EXPR)
10611 warning_at (location,
10612 OPT_Waddress,
10613 "the comparison will always evaluate as %<false%> "
10614 "for the address of %qD will never be NULL",
10615 TREE_OPERAND (op0, 0));
10616 else
10617 warning_at (location,
10618 OPT_Waddress,
10619 "the comparison will always evaluate as %<true%> "
10620 "for the address of %qD will never be NULL",
10621 TREE_OPERAND (op0, 0));
10623 result_type = type0;
10625 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10627 if (TREE_CODE (op1) == ADDR_EXPR
10628 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10630 if (code == EQ_EXPR)
10631 warning_at (location,
10632 OPT_Waddress,
10633 "the comparison will always evaluate as %<false%> "
10634 "for the address of %qD will never be NULL",
10635 TREE_OPERAND (op1, 0));
10636 else
10637 warning_at (location,
10638 OPT_Waddress,
10639 "the comparison will always evaluate as %<true%> "
10640 "for the address of %qD will never be NULL",
10641 TREE_OPERAND (op1, 0));
10643 result_type = type1;
10645 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10647 tree tt0 = TREE_TYPE (type0);
10648 tree tt1 = TREE_TYPE (type1);
10649 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10650 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10651 addr_space_t as_common = ADDR_SPACE_GENERIC;
10653 /* Anything compares with void *. void * compares with anything.
10654 Otherwise, the targets must be compatible
10655 and both must be object or both incomplete. */
10656 if (comp_target_types (location, type0, type1))
10657 result_type = common_pointer_type (type0, type1);
10658 else if (!addr_space_superset (as0, as1, &as_common))
10660 error_at (location, "comparison of pointers to "
10661 "disjoint address spaces");
10662 return error_mark_node;
10664 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10666 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10667 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10668 "comparison of %<void *%> with function pointer");
10670 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10672 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10673 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10674 "comparison of %<void *%> with function pointer");
10676 else
10677 /* Avoid warning about the volatile ObjC EH puts on decls. */
10678 if (!objc_ok)
10679 pedwarn (location, 0,
10680 "comparison of distinct pointer types lacks a cast");
10682 if (result_type == NULL_TREE)
10684 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10685 result_type = build_pointer_type
10686 (build_qualified_type (void_type_node, qual));
10689 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10691 result_type = type0;
10692 pedwarn (location, 0, "comparison between pointer and integer");
10694 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10696 result_type = type1;
10697 pedwarn (location, 0, "comparison between pointer and integer");
10699 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10700 || truth_value_p (TREE_CODE (orig_op0)))
10701 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10702 || truth_value_p (TREE_CODE (orig_op1))))
10703 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10704 break;
10706 case LE_EXPR:
10707 case GE_EXPR:
10708 case LT_EXPR:
10709 case GT_EXPR:
10710 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10712 tree intt;
10713 if (!vector_types_compatible_elements_p (type0, type1))
10715 error_at (location, "comparing vectors with different "
10716 "element types");
10717 return error_mark_node;
10720 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10722 error_at (location, "comparing vectors with different "
10723 "number of elements");
10724 return error_mark_node;
10727 /* Always construct signed integer vector type. */
10728 intt = c_common_type_for_size (GET_MODE_BITSIZE
10729 (TYPE_MODE (TREE_TYPE (type0))), 0);
10730 result_type = build_opaque_vector_type (intt,
10731 TYPE_VECTOR_SUBPARTS (type0));
10732 converted = 1;
10733 break;
10735 build_type = integer_type_node;
10736 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10737 || code0 == FIXED_POINT_TYPE)
10738 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10739 || code1 == FIXED_POINT_TYPE))
10740 short_compare = 1;
10741 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10743 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10744 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10745 addr_space_t as_common;
10747 if (comp_target_types (location, type0, type1))
10749 result_type = common_pointer_type (type0, type1);
10750 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10751 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10752 pedwarn (location, 0,
10753 "comparison of complete and incomplete pointers");
10754 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10755 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10756 "ordered comparisons of pointers to functions");
10757 else if (null_pointer_constant_p (orig_op0)
10758 || null_pointer_constant_p (orig_op1))
10759 warning_at (location, OPT_Wextra,
10760 "ordered comparison of pointer with null pointer");
10763 else if (!addr_space_superset (as0, as1, &as_common))
10765 error_at (location, "comparison of pointers to "
10766 "disjoint address spaces");
10767 return error_mark_node;
10769 else
10771 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10772 result_type = build_pointer_type
10773 (build_qualified_type (void_type_node, qual));
10774 pedwarn (location, 0,
10775 "comparison of distinct pointer types lacks a cast");
10778 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10780 result_type = type0;
10781 if (pedantic)
10782 pedwarn (location, OPT_Wpedantic,
10783 "ordered comparison of pointer with integer zero");
10784 else if (extra_warnings)
10785 warning_at (location, OPT_Wextra,
10786 "ordered comparison of pointer with integer zero");
10788 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10790 result_type = type1;
10791 if (pedantic)
10792 pedwarn (location, OPT_Wpedantic,
10793 "ordered comparison of pointer with integer zero");
10794 else if (extra_warnings)
10795 warning_at (location, OPT_Wextra,
10796 "ordered comparison of pointer with integer zero");
10798 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10800 result_type = type0;
10801 pedwarn (location, 0, "comparison between pointer and integer");
10803 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10805 result_type = type1;
10806 pedwarn (location, 0, "comparison between pointer and integer");
10808 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10809 || truth_value_p (TREE_CODE (orig_op0)))
10810 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10811 || truth_value_p (TREE_CODE (orig_op1))))
10812 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10813 break;
10815 default:
10816 gcc_unreachable ();
10819 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10820 return error_mark_node;
10822 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10823 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10824 || !vector_types_compatible_elements_p (type0, type1)))
10826 binary_op_error (location, code, type0, type1);
10827 return error_mark_node;
10830 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10831 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10833 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10834 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10836 bool first_complex = (code0 == COMPLEX_TYPE);
10837 bool second_complex = (code1 == COMPLEX_TYPE);
10838 int none_complex = (!first_complex && !second_complex);
10840 if (shorten || common || short_compare)
10842 result_type = c_common_type (type0, type1);
10843 do_warn_double_promotion (result_type, type0, type1,
10844 "implicit conversion from %qT to %qT "
10845 "to match other operand of binary "
10846 "expression",
10847 location);
10848 if (result_type == error_mark_node)
10849 return error_mark_node;
10852 if (first_complex != second_complex
10853 && (code == PLUS_EXPR
10854 || code == MINUS_EXPR
10855 || code == MULT_EXPR
10856 || (code == TRUNC_DIV_EXPR && first_complex))
10857 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10858 && flag_signed_zeros)
10860 /* An operation on mixed real/complex operands must be
10861 handled specially, but the language-independent code can
10862 more easily optimize the plain complex arithmetic if
10863 -fno-signed-zeros. */
10864 tree real_type = TREE_TYPE (result_type);
10865 tree real, imag;
10866 if (type0 != orig_type0 || type1 != orig_type1)
10868 gcc_assert (may_need_excess_precision && common);
10869 semantic_result_type = c_common_type (orig_type0, orig_type1);
10871 if (first_complex)
10873 if (TREE_TYPE (op0) != result_type)
10874 op0 = convert_and_check (location, result_type, op0);
10875 if (TREE_TYPE (op1) != real_type)
10876 op1 = convert_and_check (location, real_type, op1);
10878 else
10880 if (TREE_TYPE (op0) != real_type)
10881 op0 = convert_and_check (location, real_type, op0);
10882 if (TREE_TYPE (op1) != result_type)
10883 op1 = convert_and_check (location, result_type, op1);
10885 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10886 return error_mark_node;
10887 if (first_complex)
10889 op0 = c_save_expr (op0);
10890 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10891 op0, 1);
10892 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10893 op0, 1);
10894 switch (code)
10896 case MULT_EXPR:
10897 case TRUNC_DIV_EXPR:
10898 op1 = c_save_expr (op1);
10899 imag = build2 (resultcode, real_type, imag, op1);
10900 /* Fall through. */
10901 case PLUS_EXPR:
10902 case MINUS_EXPR:
10903 real = build2 (resultcode, real_type, real, op1);
10904 break;
10905 default:
10906 gcc_unreachable();
10909 else
10911 op1 = c_save_expr (op1);
10912 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10913 op1, 1);
10914 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10915 op1, 1);
10916 switch (code)
10918 case MULT_EXPR:
10919 op0 = c_save_expr (op0);
10920 imag = build2 (resultcode, real_type, op0, imag);
10921 /* Fall through. */
10922 case PLUS_EXPR:
10923 real = build2 (resultcode, real_type, op0, real);
10924 break;
10925 case MINUS_EXPR:
10926 real = build2 (resultcode, real_type, op0, real);
10927 imag = build1 (NEGATE_EXPR, real_type, imag);
10928 break;
10929 default:
10930 gcc_unreachable();
10933 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10934 goto return_build_binary_op;
10937 /* For certain operations (which identify themselves by shorten != 0)
10938 if both args were extended from the same smaller type,
10939 do the arithmetic in that type and then extend.
10941 shorten !=0 and !=1 indicates a bitwise operation.
10942 For them, this optimization is safe only if
10943 both args are zero-extended or both are sign-extended.
10944 Otherwise, we might change the result.
10945 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10946 but calculated in (unsigned short) it would be (unsigned short)-1. */
10948 if (shorten && none_complex)
10950 final_type = result_type;
10951 result_type = shorten_binary_op (result_type, op0, op1,
10952 shorten == -1);
10955 /* Shifts can be shortened if shifting right. */
10957 if (short_shift)
10959 int unsigned_arg;
10960 tree arg0 = get_narrower (op0, &unsigned_arg);
10962 final_type = result_type;
10964 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10965 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10967 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10968 && tree_int_cst_sgn (op1) > 0
10969 /* We can shorten only if the shift count is less than the
10970 number of bits in the smaller type size. */
10971 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10972 /* We cannot drop an unsigned shift after sign-extension. */
10973 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10975 /* Do an unsigned shift if the operand was zero-extended. */
10976 result_type
10977 = c_common_signed_or_unsigned_type (unsigned_arg,
10978 TREE_TYPE (arg0));
10979 /* Convert value-to-be-shifted to that type. */
10980 if (TREE_TYPE (op0) != result_type)
10981 op0 = convert (result_type, op0);
10982 converted = 1;
10986 /* Comparison operations are shortened too but differently.
10987 They identify themselves by setting short_compare = 1. */
10989 if (short_compare)
10991 /* Don't write &op0, etc., because that would prevent op0
10992 from being kept in a register.
10993 Instead, make copies of the our local variables and
10994 pass the copies by reference, then copy them back afterward. */
10995 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10996 enum tree_code xresultcode = resultcode;
10997 tree val
10998 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10999 &xresultcode);
11001 if (val != 0)
11003 ret = val;
11004 goto return_build_binary_op;
11007 op0 = xop0, op1 = xop1;
11008 converted = 1;
11009 resultcode = xresultcode;
11011 if (c_inhibit_evaluation_warnings == 0)
11013 bool op0_maybe_const = true;
11014 bool op1_maybe_const = true;
11015 tree orig_op0_folded, orig_op1_folded;
11017 if (in_late_binary_op)
11019 orig_op0_folded = orig_op0;
11020 orig_op1_folded = orig_op1;
11022 else
11024 /* Fold for the sake of possible warnings, as in
11025 build_conditional_expr. This requires the
11026 "original" values to be folded, not just op0 and
11027 op1. */
11028 c_inhibit_evaluation_warnings++;
11029 op0 = c_fully_fold (op0, require_constant_value,
11030 &op0_maybe_const);
11031 op1 = c_fully_fold (op1, require_constant_value,
11032 &op1_maybe_const);
11033 c_inhibit_evaluation_warnings--;
11034 orig_op0_folded = c_fully_fold (orig_op0,
11035 require_constant_value,
11036 NULL);
11037 orig_op1_folded = c_fully_fold (orig_op1,
11038 require_constant_value,
11039 NULL);
11042 if (warn_sign_compare)
11043 warn_for_sign_compare (location, orig_op0_folded,
11044 orig_op1_folded, op0, op1,
11045 result_type, resultcode);
11046 if (!in_late_binary_op && !int_operands)
11048 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11049 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11050 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11051 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11057 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11058 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11059 Then the expression will be built.
11060 It will be given type FINAL_TYPE if that is nonzero;
11061 otherwise, it will be given type RESULT_TYPE. */
11063 if (!result_type)
11065 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11066 return error_mark_node;
11069 if (build_type == NULL_TREE)
11071 build_type = result_type;
11072 if ((type0 != orig_type0 || type1 != orig_type1)
11073 && !boolean_op)
11075 gcc_assert (may_need_excess_precision && common);
11076 semantic_result_type = c_common_type (orig_type0, orig_type1);
11080 if (!converted)
11082 op0 = ep_convert_and_check (location, result_type, op0,
11083 semantic_result_type);
11084 op1 = ep_convert_and_check (location, result_type, op1,
11085 semantic_result_type);
11087 /* This can happen if one operand has a vector type, and the other
11088 has a different type. */
11089 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11090 return error_mark_node;
11093 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11094 | SANITIZE_FLOAT_DIVIDE))
11095 && current_function_decl != 0
11096 && !lookup_attribute ("no_sanitize_undefined",
11097 DECL_ATTRIBUTES (current_function_decl))
11098 && (doing_div_or_mod || doing_shift))
11100 /* OP0 and/or OP1 might have side-effects. */
11101 op0 = c_save_expr (op0);
11102 op1 = c_save_expr (op1);
11103 op0 = c_fully_fold (op0, false, NULL);
11104 op1 = c_fully_fold (op1, false, NULL);
11105 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11106 | SANITIZE_FLOAT_DIVIDE)))
11107 instrument_expr = ubsan_instrument_division (location, op0, op1);
11108 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11109 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11112 /* Treat expressions in initializers specially as they can't trap. */
11113 if (int_const_or_overflow)
11114 ret = (require_constant_value
11115 ? fold_build2_initializer_loc (location, resultcode, build_type,
11116 op0, op1)
11117 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11118 else
11119 ret = build2 (resultcode, build_type, op0, op1);
11120 if (final_type != 0)
11121 ret = convert (final_type, ret);
11123 return_build_binary_op:
11124 gcc_assert (ret != error_mark_node);
11125 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11126 ret = (int_operands
11127 ? note_integer_operands (ret)
11128 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11129 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11130 && !in_late_binary_op)
11131 ret = note_integer_operands (ret);
11132 if (semantic_result_type)
11133 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11134 protected_set_expr_location (ret, location);
11136 if (instrument_expr != NULL)
11137 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11138 instrument_expr, ret);
11140 return ret;
11144 /* Convert EXPR to be a truth-value, validating its type for this
11145 purpose. LOCATION is the source location for the expression. */
11147 tree
11148 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11150 bool int_const, int_operands;
11152 switch (TREE_CODE (TREE_TYPE (expr)))
11154 case ARRAY_TYPE:
11155 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11156 return error_mark_node;
11158 case RECORD_TYPE:
11159 error_at (location, "used struct type value where scalar is required");
11160 return error_mark_node;
11162 case UNION_TYPE:
11163 error_at (location, "used union type value where scalar is required");
11164 return error_mark_node;
11166 case VOID_TYPE:
11167 error_at (location, "void value not ignored as it ought to be");
11168 return error_mark_node;
11170 case FUNCTION_TYPE:
11171 gcc_unreachable ();
11173 case VECTOR_TYPE:
11174 error_at (location, "used vector type where scalar is required");
11175 return error_mark_node;
11177 default:
11178 break;
11181 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11182 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11183 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11185 expr = remove_c_maybe_const_expr (expr);
11186 expr = build2 (NE_EXPR, integer_type_node, expr,
11187 convert (TREE_TYPE (expr), integer_zero_node));
11188 expr = note_integer_operands (expr);
11190 else
11191 /* ??? Should we also give an error for vectors rather than leaving
11192 those to give errors later? */
11193 expr = c_common_truthvalue_conversion (location, expr);
11195 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11197 if (TREE_OVERFLOW (expr))
11198 return expr;
11199 else
11200 return note_integer_operands (expr);
11202 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11203 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11204 return expr;
11208 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11209 required. */
11211 tree
11212 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11214 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11216 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11217 /* Executing a compound literal inside a function reinitializes
11218 it. */
11219 if (!TREE_STATIC (decl))
11220 *se = true;
11221 return decl;
11223 else
11224 return expr;
11227 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11229 tree
11230 c_begin_omp_parallel (void)
11232 tree block;
11234 keep_next_level ();
11235 block = c_begin_compound_stmt (true);
11237 return block;
11240 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11241 statement. LOC is the location of the OMP_PARALLEL. */
11243 tree
11244 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11246 tree stmt;
11248 block = c_end_compound_stmt (loc, block, true);
11250 stmt = make_node (OMP_PARALLEL);
11251 TREE_TYPE (stmt) = void_type_node;
11252 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11253 OMP_PARALLEL_BODY (stmt) = block;
11254 SET_EXPR_LOCATION (stmt, loc);
11256 return add_stmt (stmt);
11259 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11261 tree
11262 c_begin_omp_task (void)
11264 tree block;
11266 keep_next_level ();
11267 block = c_begin_compound_stmt (true);
11269 return block;
11272 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11273 statement. LOC is the location of the #pragma. */
11275 tree
11276 c_finish_omp_task (location_t loc, tree clauses, tree block)
11278 tree stmt;
11280 block = c_end_compound_stmt (loc, block, true);
11282 stmt = make_node (OMP_TASK);
11283 TREE_TYPE (stmt) = void_type_node;
11284 OMP_TASK_CLAUSES (stmt) = clauses;
11285 OMP_TASK_BODY (stmt) = block;
11286 SET_EXPR_LOCATION (stmt, loc);
11288 return add_stmt (stmt);
11291 /* Generate GOMP_cancel call for #pragma omp cancel. */
11293 void
11294 c_finish_omp_cancel (location_t loc, tree clauses)
11296 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11297 int mask = 0;
11298 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11299 mask = 1;
11300 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11301 mask = 2;
11302 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11303 mask = 4;
11304 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11305 mask = 8;
11306 else
11308 error_at (loc, "%<#pragma omp cancel must specify one of "
11309 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11310 "clauses");
11311 return;
11313 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11314 if (ifc != NULL_TREE)
11316 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11317 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11318 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11319 build_zero_cst (type));
11321 else
11322 ifc = boolean_true_node;
11323 tree stmt = build_call_expr_loc (loc, fn, 2,
11324 build_int_cst (integer_type_node, mask),
11325 ifc);
11326 add_stmt (stmt);
11329 /* Generate GOMP_cancellation_point call for
11330 #pragma omp cancellation point. */
11332 void
11333 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11335 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11336 int mask = 0;
11337 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11338 mask = 1;
11339 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11340 mask = 2;
11341 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11342 mask = 4;
11343 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11344 mask = 8;
11345 else
11347 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11348 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11349 "clauses");
11350 return;
11352 tree stmt = build_call_expr_loc (loc, fn, 1,
11353 build_int_cst (integer_type_node, mask));
11354 add_stmt (stmt);
11357 /* Helper function for handle_omp_array_sections. Called recursively
11358 to handle multiple array-section-subscripts. C is the clause,
11359 T current expression (initially OMP_CLAUSE_DECL), which is either
11360 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11361 expression if specified, TREE_VALUE length expression if specified,
11362 TREE_CHAIN is what it has been specified after, or some decl.
11363 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11364 set to true if any of the array-section-subscript could have length
11365 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11366 first array-section-subscript which is known not to have length
11367 of one. Given say:
11368 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11369 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11370 all are or may have length of 1, array-section-subscript [:2] is the
11371 first one knonwn not to have length 1. For array-section-subscript
11372 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11373 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11374 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11375 case though, as some lengths could be zero. */
11377 static tree
11378 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11379 bool &maybe_zero_len, unsigned int &first_non_one)
11381 tree ret, low_bound, length, type;
11382 if (TREE_CODE (t) != TREE_LIST)
11384 if (error_operand_p (t))
11385 return error_mark_node;
11386 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11388 if (DECL_P (t))
11389 error_at (OMP_CLAUSE_LOCATION (c),
11390 "%qD is not a variable in %qs clause", t,
11391 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11392 else
11393 error_at (OMP_CLAUSE_LOCATION (c),
11394 "%qE is not a variable in %qs clause", t,
11395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11396 return error_mark_node;
11398 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11399 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11401 error_at (OMP_CLAUSE_LOCATION (c),
11402 "%qD is threadprivate variable in %qs clause", t,
11403 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11404 return error_mark_node;
11406 return t;
11409 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11410 maybe_zero_len, first_non_one);
11411 if (ret == error_mark_node || ret == NULL_TREE)
11412 return ret;
11414 type = TREE_TYPE (ret);
11415 low_bound = TREE_PURPOSE (t);
11416 length = TREE_VALUE (t);
11418 if (low_bound == error_mark_node || length == error_mark_node)
11419 return error_mark_node;
11421 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11423 error_at (OMP_CLAUSE_LOCATION (c),
11424 "low bound %qE of array section does not have integral type",
11425 low_bound);
11426 return error_mark_node;
11428 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11430 error_at (OMP_CLAUSE_LOCATION (c),
11431 "length %qE of array section does not have integral type",
11432 length);
11433 return error_mark_node;
11435 if (low_bound
11436 && TREE_CODE (low_bound) == INTEGER_CST
11437 && TYPE_PRECISION (TREE_TYPE (low_bound))
11438 > TYPE_PRECISION (sizetype))
11439 low_bound = fold_convert (sizetype, low_bound);
11440 if (length
11441 && TREE_CODE (length) == INTEGER_CST
11442 && TYPE_PRECISION (TREE_TYPE (length))
11443 > TYPE_PRECISION (sizetype))
11444 length = fold_convert (sizetype, length);
11445 if (low_bound == NULL_TREE)
11446 low_bound = integer_zero_node;
11448 if (length != NULL_TREE)
11450 if (!integer_nonzerop (length))
11451 maybe_zero_len = true;
11452 if (first_non_one == types.length ()
11453 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11454 first_non_one++;
11456 if (TREE_CODE (type) == ARRAY_TYPE)
11458 if (length == NULL_TREE
11459 && (TYPE_DOMAIN (type) == NULL_TREE
11460 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11462 error_at (OMP_CLAUSE_LOCATION (c),
11463 "for unknown bound array type length expression must "
11464 "be specified");
11465 return error_mark_node;
11467 if (TREE_CODE (low_bound) == INTEGER_CST
11468 && tree_int_cst_sgn (low_bound) == -1)
11470 error_at (OMP_CLAUSE_LOCATION (c),
11471 "negative low bound in array section in %qs clause",
11472 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11473 return error_mark_node;
11475 if (length != NULL_TREE
11476 && TREE_CODE (length) == INTEGER_CST
11477 && tree_int_cst_sgn (length) == -1)
11479 error_at (OMP_CLAUSE_LOCATION (c),
11480 "negative length in array section in %qs clause",
11481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11482 return error_mark_node;
11484 if (TYPE_DOMAIN (type)
11485 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11486 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11487 == INTEGER_CST)
11489 tree size = size_binop (PLUS_EXPR,
11490 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11491 size_one_node);
11492 if (TREE_CODE (low_bound) == INTEGER_CST)
11494 if (tree_int_cst_lt (size, low_bound))
11496 error_at (OMP_CLAUSE_LOCATION (c),
11497 "low bound %qE above array section size "
11498 "in %qs clause", low_bound,
11499 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11500 return error_mark_node;
11502 if (tree_int_cst_equal (size, low_bound))
11503 maybe_zero_len = true;
11504 else if (length == NULL_TREE
11505 && first_non_one == types.length ()
11506 && tree_int_cst_equal
11507 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11508 low_bound))
11509 first_non_one++;
11511 else if (length == NULL_TREE)
11513 maybe_zero_len = true;
11514 if (first_non_one == types.length ())
11515 first_non_one++;
11517 if (length && TREE_CODE (length) == INTEGER_CST)
11519 if (tree_int_cst_lt (size, length))
11521 error_at (OMP_CLAUSE_LOCATION (c),
11522 "length %qE above array section size "
11523 "in %qs clause", length,
11524 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11525 return error_mark_node;
11527 if (TREE_CODE (low_bound) == INTEGER_CST)
11529 tree lbpluslen
11530 = size_binop (PLUS_EXPR,
11531 fold_convert (sizetype, low_bound),
11532 fold_convert (sizetype, length));
11533 if (TREE_CODE (lbpluslen) == INTEGER_CST
11534 && tree_int_cst_lt (size, lbpluslen))
11536 error_at (OMP_CLAUSE_LOCATION (c),
11537 "high bound %qE above array section size "
11538 "in %qs clause", lbpluslen,
11539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11540 return error_mark_node;
11545 else if (length == NULL_TREE)
11547 maybe_zero_len = true;
11548 if (first_non_one == types.length ())
11549 first_non_one++;
11552 /* For [lb:] we will need to evaluate lb more than once. */
11553 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11555 tree lb = c_save_expr (low_bound);
11556 if (lb != low_bound)
11558 TREE_PURPOSE (t) = lb;
11559 low_bound = lb;
11563 else if (TREE_CODE (type) == POINTER_TYPE)
11565 if (length == NULL_TREE)
11567 error_at (OMP_CLAUSE_LOCATION (c),
11568 "for pointer type length expression must be specified");
11569 return error_mark_node;
11571 /* If there is a pointer type anywhere but in the very first
11572 array-section-subscript, the array section can't be contiguous. */
11573 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11574 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11576 error_at (OMP_CLAUSE_LOCATION (c),
11577 "array section is not contiguous in %qs clause",
11578 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11579 return error_mark_node;
11582 else
11584 error_at (OMP_CLAUSE_LOCATION (c),
11585 "%qE does not have pointer or array type", ret);
11586 return error_mark_node;
11588 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11589 types.safe_push (TREE_TYPE (ret));
11590 /* We will need to evaluate lb more than once. */
11591 tree lb = c_save_expr (low_bound);
11592 if (lb != low_bound)
11594 TREE_PURPOSE (t) = lb;
11595 low_bound = lb;
11597 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11598 return ret;
11601 /* Handle array sections for clause C. */
11603 static bool
11604 handle_omp_array_sections (tree c)
11606 bool maybe_zero_len = false;
11607 unsigned int first_non_one = 0;
11608 vec<tree> types = vNULL;
11609 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11610 maybe_zero_len, first_non_one);
11611 if (first == error_mark_node)
11613 types.release ();
11614 return true;
11616 if (first == NULL_TREE)
11618 types.release ();
11619 return false;
11621 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11623 tree t = OMP_CLAUSE_DECL (c);
11624 tree tem = NULL_TREE;
11625 types.release ();
11626 /* Need to evaluate side effects in the length expressions
11627 if any. */
11628 while (TREE_CODE (t) == TREE_LIST)
11630 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11632 if (tem == NULL_TREE)
11633 tem = TREE_VALUE (t);
11634 else
11635 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11636 TREE_VALUE (t), tem);
11638 t = TREE_CHAIN (t);
11640 if (tem)
11641 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11642 first = c_fully_fold (first, false, NULL);
11643 OMP_CLAUSE_DECL (c) = first;
11645 else
11647 unsigned int num = types.length (), i;
11648 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11649 tree condition = NULL_TREE;
11651 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11652 maybe_zero_len = true;
11654 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11655 t = TREE_CHAIN (t))
11657 tree low_bound = TREE_PURPOSE (t);
11658 tree length = TREE_VALUE (t);
11660 i--;
11661 if (low_bound
11662 && TREE_CODE (low_bound) == INTEGER_CST
11663 && TYPE_PRECISION (TREE_TYPE (low_bound))
11664 > TYPE_PRECISION (sizetype))
11665 low_bound = fold_convert (sizetype, low_bound);
11666 if (length
11667 && TREE_CODE (length) == INTEGER_CST
11668 && TYPE_PRECISION (TREE_TYPE (length))
11669 > TYPE_PRECISION (sizetype))
11670 length = fold_convert (sizetype, length);
11671 if (low_bound == NULL_TREE)
11672 low_bound = integer_zero_node;
11673 if (!maybe_zero_len && i > first_non_one)
11675 if (integer_nonzerop (low_bound))
11676 goto do_warn_noncontiguous;
11677 if (length != NULL_TREE
11678 && TREE_CODE (length) == INTEGER_CST
11679 && TYPE_DOMAIN (types[i])
11680 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11681 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11682 == INTEGER_CST)
11684 tree size;
11685 size = size_binop (PLUS_EXPR,
11686 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11687 size_one_node);
11688 if (!tree_int_cst_equal (length, size))
11690 do_warn_noncontiguous:
11691 error_at (OMP_CLAUSE_LOCATION (c),
11692 "array section is not contiguous in %qs "
11693 "clause",
11694 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11695 types.release ();
11696 return true;
11699 if (length != NULL_TREE
11700 && TREE_SIDE_EFFECTS (length))
11702 if (side_effects == NULL_TREE)
11703 side_effects = length;
11704 else
11705 side_effects = build2 (COMPOUND_EXPR,
11706 TREE_TYPE (side_effects),
11707 length, side_effects);
11710 else
11712 tree l;
11714 if (i > first_non_one && length && integer_nonzerop (length))
11715 continue;
11716 if (length)
11717 l = fold_convert (sizetype, length);
11718 else
11720 l = size_binop (PLUS_EXPR,
11721 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11722 size_one_node);
11723 l = size_binop (MINUS_EXPR, l,
11724 fold_convert (sizetype, low_bound));
11726 if (i > first_non_one)
11728 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11729 size_zero_node);
11730 if (condition == NULL_TREE)
11731 condition = l;
11732 else
11733 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11734 l, condition);
11736 else if (size == NULL_TREE)
11738 size = size_in_bytes (TREE_TYPE (types[i]));
11739 size = size_binop (MULT_EXPR, size, l);
11740 if (condition)
11741 size = fold_build3 (COND_EXPR, sizetype, condition,
11742 size, size_zero_node);
11744 else
11745 size = size_binop (MULT_EXPR, size, l);
11748 types.release ();
11749 if (side_effects)
11750 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11751 first = c_fully_fold (first, false, NULL);
11752 OMP_CLAUSE_DECL (c) = first;
11753 if (size)
11754 size = c_fully_fold (size, false, NULL);
11755 OMP_CLAUSE_SIZE (c) = size;
11756 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11757 return false;
11758 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11759 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11760 if (!c_mark_addressable (t))
11761 return false;
11762 OMP_CLAUSE_DECL (c2) = t;
11763 t = build_fold_addr_expr (first);
11764 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11765 tree ptr = OMP_CLAUSE_DECL (c2);
11766 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11767 ptr = build_fold_addr_expr (ptr);
11768 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11769 ptrdiff_type_node, t,
11770 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11771 ptrdiff_type_node, ptr));
11772 t = c_fully_fold (t, false, NULL);
11773 OMP_CLAUSE_SIZE (c2) = t;
11774 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11775 OMP_CLAUSE_CHAIN (c) = c2;
11777 return false;
11780 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11781 an inline call. But, remap
11782 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11783 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11785 static tree
11786 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11787 tree decl, tree placeholder)
11789 copy_body_data id;
11790 hash_map<tree, tree> decl_map;
11792 decl_map.put (omp_decl1, placeholder);
11793 decl_map.put (omp_decl2, decl);
11794 memset (&id, 0, sizeof (id));
11795 id.src_fn = DECL_CONTEXT (omp_decl1);
11796 id.dst_fn = current_function_decl;
11797 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11798 id.decl_map = &decl_map;
11800 id.copy_decl = copy_decl_no_change;
11801 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11802 id.transform_new_cfg = true;
11803 id.transform_return_to_modify = false;
11804 id.transform_lang_insert_block = NULL;
11805 id.eh_lp_nr = 0;
11806 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11807 return stmt;
11810 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11811 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11813 static tree
11814 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11816 if (*tp == (tree) data)
11817 return *tp;
11818 return NULL_TREE;
11821 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11822 Remove any elements from the list that are invalid. */
11824 tree
11825 c_finish_omp_clauses (tree clauses)
11827 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11828 bitmap_head aligned_head;
11829 tree c, t, *pc;
11830 bool branch_seen = false;
11831 bool copyprivate_seen = false;
11832 tree *nowait_clause = NULL;
11834 bitmap_obstack_initialize (NULL);
11835 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11836 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11837 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11838 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11840 for (pc = &clauses, c = clauses; c ; c = *pc)
11842 bool remove = false;
11843 bool need_complete = false;
11844 bool need_implicitly_determined = false;
11846 switch (OMP_CLAUSE_CODE (c))
11848 case OMP_CLAUSE_SHARED:
11849 need_implicitly_determined = true;
11850 goto check_dup_generic;
11852 case OMP_CLAUSE_PRIVATE:
11853 need_complete = true;
11854 need_implicitly_determined = true;
11855 goto check_dup_generic;
11857 case OMP_CLAUSE_REDUCTION:
11858 need_implicitly_determined = true;
11859 t = OMP_CLAUSE_DECL (c);
11860 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11861 && (FLOAT_TYPE_P (TREE_TYPE (t))
11862 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11864 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11865 const char *r_name = NULL;
11867 switch (r_code)
11869 case PLUS_EXPR:
11870 case MULT_EXPR:
11871 case MINUS_EXPR:
11872 break;
11873 case MIN_EXPR:
11874 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11875 r_name = "min";
11876 break;
11877 case MAX_EXPR:
11878 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11879 r_name = "max";
11880 break;
11881 case BIT_AND_EXPR:
11882 r_name = "&";
11883 break;
11884 case BIT_XOR_EXPR:
11885 r_name = "^";
11886 break;
11887 case BIT_IOR_EXPR:
11888 r_name = "|";
11889 break;
11890 case TRUTH_ANDIF_EXPR:
11891 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11892 r_name = "&&";
11893 break;
11894 case TRUTH_ORIF_EXPR:
11895 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11896 r_name = "||";
11897 break;
11898 default:
11899 gcc_unreachable ();
11901 if (r_name)
11903 error_at (OMP_CLAUSE_LOCATION (c),
11904 "%qE has invalid type for %<reduction(%s)%>",
11905 t, r_name);
11906 remove = true;
11907 break;
11910 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11912 error_at (OMP_CLAUSE_LOCATION (c),
11913 "user defined reduction not found for %qD", t);
11914 remove = true;
11915 break;
11917 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11919 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11920 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11921 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11922 VAR_DECL, NULL_TREE, type);
11923 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11924 DECL_ARTIFICIAL (placeholder) = 1;
11925 DECL_IGNORED_P (placeholder) = 1;
11926 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11927 c_mark_addressable (placeholder);
11928 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11929 c_mark_addressable (OMP_CLAUSE_DECL (c));
11930 OMP_CLAUSE_REDUCTION_MERGE (c)
11931 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11932 TREE_VEC_ELT (list, 0),
11933 TREE_VEC_ELT (list, 1),
11934 OMP_CLAUSE_DECL (c), placeholder);
11935 OMP_CLAUSE_REDUCTION_MERGE (c)
11936 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11937 void_type_node, NULL_TREE,
11938 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11939 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11940 if (TREE_VEC_LENGTH (list) == 6)
11942 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11943 c_mark_addressable (OMP_CLAUSE_DECL (c));
11944 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11945 c_mark_addressable (placeholder);
11946 tree init = TREE_VEC_ELT (list, 5);
11947 if (init == error_mark_node)
11948 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11949 OMP_CLAUSE_REDUCTION_INIT (c)
11950 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11951 TREE_VEC_ELT (list, 3),
11952 OMP_CLAUSE_DECL (c), placeholder);
11953 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11954 OMP_CLAUSE_REDUCTION_INIT (c)
11955 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11956 OMP_CLAUSE_REDUCTION_INIT (c));
11957 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11958 c_find_omp_placeholder_r,
11959 placeholder, NULL))
11960 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11962 else
11964 tree init;
11965 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11966 init = build_constructor (TREE_TYPE (t), NULL);
11967 else
11968 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11969 OMP_CLAUSE_REDUCTION_INIT (c)
11970 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11972 OMP_CLAUSE_REDUCTION_INIT (c)
11973 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11974 void_type_node, NULL_TREE,
11975 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11976 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11978 goto check_dup_generic;
11980 case OMP_CLAUSE_COPYPRIVATE:
11981 copyprivate_seen = true;
11982 if (nowait_clause)
11984 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11985 "%<nowait%> clause must not be used together "
11986 "with %<copyprivate%>");
11987 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11988 nowait_clause = NULL;
11990 goto check_dup_generic;
11992 case OMP_CLAUSE_COPYIN:
11993 t = OMP_CLAUSE_DECL (c);
11994 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11996 error_at (OMP_CLAUSE_LOCATION (c),
11997 "%qE must be %<threadprivate%> for %<copyin%>", t);
11998 remove = true;
11999 break;
12001 goto check_dup_generic;
12003 case OMP_CLAUSE_LINEAR:
12004 t = OMP_CLAUSE_DECL (c);
12005 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12006 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12008 error_at (OMP_CLAUSE_LOCATION (c),
12009 "linear clause applied to non-integral non-pointer "
12010 "variable with type %qT", TREE_TYPE (t));
12011 remove = true;
12012 break;
12014 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12016 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12017 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12018 OMP_CLAUSE_DECL (c), s);
12019 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12020 sizetype, s, OMP_CLAUSE_DECL (c));
12021 if (s == error_mark_node)
12022 s = size_one_node;
12023 OMP_CLAUSE_LINEAR_STEP (c) = s;
12025 else
12026 OMP_CLAUSE_LINEAR_STEP (c)
12027 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12028 goto check_dup_generic;
12030 check_dup_generic:
12031 t = OMP_CLAUSE_DECL (c);
12032 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12034 error_at (OMP_CLAUSE_LOCATION (c),
12035 "%qE is not a variable in clause %qs", t,
12036 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12037 remove = true;
12039 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12040 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12041 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12043 error_at (OMP_CLAUSE_LOCATION (c),
12044 "%qE appears more than once in data clauses", t);
12045 remove = true;
12047 else
12048 bitmap_set_bit (&generic_head, DECL_UID (t));
12049 break;
12051 case OMP_CLAUSE_FIRSTPRIVATE:
12052 t = OMP_CLAUSE_DECL (c);
12053 need_complete = true;
12054 need_implicitly_determined = true;
12055 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12057 error_at (OMP_CLAUSE_LOCATION (c),
12058 "%qE is not a variable in clause %<firstprivate%>", t);
12059 remove = true;
12061 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12062 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12064 error_at (OMP_CLAUSE_LOCATION (c),
12065 "%qE appears more than once in data clauses", t);
12066 remove = true;
12068 else
12069 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12070 break;
12072 case OMP_CLAUSE_LASTPRIVATE:
12073 t = OMP_CLAUSE_DECL (c);
12074 need_complete = true;
12075 need_implicitly_determined = true;
12076 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12078 error_at (OMP_CLAUSE_LOCATION (c),
12079 "%qE is not a variable in clause %<lastprivate%>", t);
12080 remove = true;
12082 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12083 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12085 error_at (OMP_CLAUSE_LOCATION (c),
12086 "%qE appears more than once in data clauses", t);
12087 remove = true;
12089 else
12090 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12091 break;
12093 case OMP_CLAUSE_ALIGNED:
12094 t = OMP_CLAUSE_DECL (c);
12095 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12097 error_at (OMP_CLAUSE_LOCATION (c),
12098 "%qE is not a variable in %<aligned%> clause", t);
12099 remove = true;
12101 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12102 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12104 error_at (OMP_CLAUSE_LOCATION (c),
12105 "%qE in %<aligned%> clause is neither a pointer nor "
12106 "an array", t);
12107 remove = true;
12109 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12111 error_at (OMP_CLAUSE_LOCATION (c),
12112 "%qE appears more than once in %<aligned%> clauses",
12114 remove = true;
12116 else
12117 bitmap_set_bit (&aligned_head, DECL_UID (t));
12118 break;
12120 case OMP_CLAUSE_DEPEND:
12121 t = OMP_CLAUSE_DECL (c);
12122 if (TREE_CODE (t) == TREE_LIST)
12124 if (handle_omp_array_sections (c))
12125 remove = true;
12126 break;
12128 if (t == error_mark_node)
12129 remove = true;
12130 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12132 error_at (OMP_CLAUSE_LOCATION (c),
12133 "%qE is not a variable in %<depend%> clause", t);
12134 remove = true;
12136 else if (!c_mark_addressable (t))
12137 remove = true;
12138 break;
12140 case OMP_CLAUSE_MAP:
12141 case OMP_CLAUSE_TO:
12142 case OMP_CLAUSE_FROM:
12143 t = OMP_CLAUSE_DECL (c);
12144 if (TREE_CODE (t) == TREE_LIST)
12146 if (handle_omp_array_sections (c))
12147 remove = true;
12148 else
12150 t = OMP_CLAUSE_DECL (c);
12151 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12153 error_at (OMP_CLAUSE_LOCATION (c),
12154 "array section does not have mappable type "
12155 "in %qs clause",
12156 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12157 remove = true;
12160 break;
12162 if (t == error_mark_node)
12163 remove = true;
12164 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12166 error_at (OMP_CLAUSE_LOCATION (c),
12167 "%qE is not a variable in %qs clause", t,
12168 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12169 remove = true;
12171 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12173 error_at (OMP_CLAUSE_LOCATION (c),
12174 "%qD is threadprivate variable in %qs clause", t,
12175 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12176 remove = true;
12178 else if (!c_mark_addressable (t))
12179 remove = true;
12180 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12181 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12182 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12184 error_at (OMP_CLAUSE_LOCATION (c),
12185 "%qD does not have a mappable type in %qs clause", t,
12186 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12187 remove = true;
12189 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12191 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12192 error ("%qD appears more than once in motion clauses", t);
12193 else
12194 error ("%qD appears more than once in map clauses", t);
12195 remove = true;
12197 else
12198 bitmap_set_bit (&generic_head, DECL_UID (t));
12199 break;
12201 case OMP_CLAUSE_UNIFORM:
12202 t = OMP_CLAUSE_DECL (c);
12203 if (TREE_CODE (t) != PARM_DECL)
12205 if (DECL_P (t))
12206 error_at (OMP_CLAUSE_LOCATION (c),
12207 "%qD is not an argument in %<uniform%> clause", t);
12208 else
12209 error_at (OMP_CLAUSE_LOCATION (c),
12210 "%qE is not an argument in %<uniform%> clause", t);
12211 remove = true;
12212 break;
12214 goto check_dup_generic;
12216 case OMP_CLAUSE_NOWAIT:
12217 if (copyprivate_seen)
12219 error_at (OMP_CLAUSE_LOCATION (c),
12220 "%<nowait%> clause must not be used together "
12221 "with %<copyprivate%>");
12222 remove = true;
12223 break;
12225 nowait_clause = pc;
12226 pc = &OMP_CLAUSE_CHAIN (c);
12227 continue;
12229 case OMP_CLAUSE_IF:
12230 case OMP_CLAUSE_NUM_THREADS:
12231 case OMP_CLAUSE_NUM_TEAMS:
12232 case OMP_CLAUSE_THREAD_LIMIT:
12233 case OMP_CLAUSE_SCHEDULE:
12234 case OMP_CLAUSE_ORDERED:
12235 case OMP_CLAUSE_DEFAULT:
12236 case OMP_CLAUSE_UNTIED:
12237 case OMP_CLAUSE_COLLAPSE:
12238 case OMP_CLAUSE_FINAL:
12239 case OMP_CLAUSE_MERGEABLE:
12240 case OMP_CLAUSE_SAFELEN:
12241 case OMP_CLAUSE_SIMDLEN:
12242 case OMP_CLAUSE_DEVICE:
12243 case OMP_CLAUSE_DIST_SCHEDULE:
12244 case OMP_CLAUSE_PARALLEL:
12245 case OMP_CLAUSE_FOR:
12246 case OMP_CLAUSE_SECTIONS:
12247 case OMP_CLAUSE_TASKGROUP:
12248 case OMP_CLAUSE_PROC_BIND:
12249 case OMP_CLAUSE__CILK_FOR_COUNT_:
12250 pc = &OMP_CLAUSE_CHAIN (c);
12251 continue;
12253 case OMP_CLAUSE_INBRANCH:
12254 case OMP_CLAUSE_NOTINBRANCH:
12255 if (branch_seen)
12257 error_at (OMP_CLAUSE_LOCATION (c),
12258 "%<inbranch%> clause is incompatible with "
12259 "%<notinbranch%>");
12260 remove = true;
12261 break;
12263 branch_seen = true;
12264 pc = &OMP_CLAUSE_CHAIN (c);
12265 continue;
12267 default:
12268 gcc_unreachable ();
12271 if (!remove)
12273 t = OMP_CLAUSE_DECL (c);
12275 if (need_complete)
12277 t = require_complete_type (t);
12278 if (t == error_mark_node)
12279 remove = true;
12282 if (need_implicitly_determined)
12284 const char *share_name = NULL;
12286 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12287 share_name = "threadprivate";
12288 else switch (c_omp_predetermined_sharing (t))
12290 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12291 break;
12292 case OMP_CLAUSE_DEFAULT_SHARED:
12293 /* const vars may be specified in firstprivate clause. */
12294 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12295 && TREE_READONLY (t))
12296 break;
12297 share_name = "shared";
12298 break;
12299 case OMP_CLAUSE_DEFAULT_PRIVATE:
12300 share_name = "private";
12301 break;
12302 default:
12303 gcc_unreachable ();
12305 if (share_name)
12307 error_at (OMP_CLAUSE_LOCATION (c),
12308 "%qE is predetermined %qs for %qs",
12309 t, share_name,
12310 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12311 remove = true;
12316 if (remove)
12317 *pc = OMP_CLAUSE_CHAIN (c);
12318 else
12319 pc = &OMP_CLAUSE_CHAIN (c);
12322 bitmap_obstack_release (NULL);
12323 return clauses;
12326 /* Create a transaction node. */
12328 tree
12329 c_finish_transaction (location_t loc, tree block, int flags)
12331 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12332 if (flags & TM_STMT_ATTR_OUTER)
12333 TRANSACTION_EXPR_OUTER (stmt) = 1;
12334 if (flags & TM_STMT_ATTR_RELAXED)
12335 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12336 return add_stmt (stmt);
12339 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12340 down to the element type of an array. */
12342 tree
12343 c_build_qualified_type (tree type, int type_quals)
12345 if (type == error_mark_node)
12346 return type;
12348 if (TREE_CODE (type) == ARRAY_TYPE)
12350 tree t;
12351 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12352 type_quals);
12354 /* See if we already have an identically qualified type. */
12355 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12357 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12358 && TYPE_NAME (t) == TYPE_NAME (type)
12359 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12360 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12361 TYPE_ATTRIBUTES (type)))
12362 break;
12364 if (!t)
12366 tree domain = TYPE_DOMAIN (type);
12368 t = build_variant_type_copy (type);
12369 TREE_TYPE (t) = element_type;
12371 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12372 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12373 SET_TYPE_STRUCTURAL_EQUALITY (t);
12374 else if (TYPE_CANONICAL (element_type) != element_type
12375 || (domain && TYPE_CANONICAL (domain) != domain))
12377 tree unqualified_canon
12378 = build_array_type (TYPE_CANONICAL (element_type),
12379 domain? TYPE_CANONICAL (domain)
12380 : NULL_TREE);
12381 TYPE_CANONICAL (t)
12382 = c_build_qualified_type (unqualified_canon, type_quals);
12384 else
12385 TYPE_CANONICAL (t) = t;
12387 return t;
12390 /* A restrict-qualified pointer type must be a pointer to object or
12391 incomplete type. Note that the use of POINTER_TYPE_P also allows
12392 REFERENCE_TYPEs, which is appropriate for C++. */
12393 if ((type_quals & TYPE_QUAL_RESTRICT)
12394 && (!POINTER_TYPE_P (type)
12395 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12397 error ("invalid use of %<restrict%>");
12398 type_quals &= ~TYPE_QUAL_RESTRICT;
12401 return build_qualified_type (type, type_quals);
12404 /* Build a VA_ARG_EXPR for the C parser. */
12406 tree
12407 c_build_va_arg (location_t loc, tree expr, tree type)
12409 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12410 warning_at (loc, OPT_Wc___compat,
12411 "C++ requires promoted type, not enum type, in %<va_arg%>");
12412 return build_va_arg (loc, expr, type);
12415 /* Return truthvalue of whether T1 is the same tree structure as T2.
12416 Return 1 if they are the same. Return 0 if they are different. */
12418 bool
12419 c_tree_equal (tree t1, tree t2)
12421 enum tree_code code1, code2;
12423 if (t1 == t2)
12424 return true;
12425 if (!t1 || !t2)
12426 return false;
12428 for (code1 = TREE_CODE (t1);
12429 CONVERT_EXPR_CODE_P (code1)
12430 || code1 == NON_LVALUE_EXPR;
12431 code1 = TREE_CODE (t1))
12432 t1 = TREE_OPERAND (t1, 0);
12433 for (code2 = TREE_CODE (t2);
12434 CONVERT_EXPR_CODE_P (code2)
12435 || code2 == NON_LVALUE_EXPR;
12436 code2 = TREE_CODE (t2))
12437 t2 = TREE_OPERAND (t2, 0);
12439 /* They might have become equal now. */
12440 if (t1 == t2)
12441 return true;
12443 if (code1 != code2)
12444 return false;
12446 switch (code1)
12448 case INTEGER_CST:
12449 return wi::eq_p (t1, t2);
12451 case REAL_CST:
12452 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12454 case STRING_CST:
12455 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12456 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12457 TREE_STRING_LENGTH (t1));
12459 case FIXED_CST:
12460 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12461 TREE_FIXED_CST (t2));
12463 case COMPLEX_CST:
12464 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12465 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12467 case VECTOR_CST:
12468 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12470 case CONSTRUCTOR:
12471 /* We need to do this when determining whether or not two
12472 non-type pointer to member function template arguments
12473 are the same. */
12474 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12475 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12476 return false;
12478 tree field, value;
12479 unsigned int i;
12480 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12482 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12483 if (!c_tree_equal (field, elt2->index)
12484 || !c_tree_equal (value, elt2->value))
12485 return false;
12488 return true;
12490 case TREE_LIST:
12491 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12492 return false;
12493 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12494 return false;
12495 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12497 case SAVE_EXPR:
12498 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12500 case CALL_EXPR:
12502 tree arg1, arg2;
12503 call_expr_arg_iterator iter1, iter2;
12504 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12505 return false;
12506 for (arg1 = first_call_expr_arg (t1, &iter1),
12507 arg2 = first_call_expr_arg (t2, &iter2);
12508 arg1 && arg2;
12509 arg1 = next_call_expr_arg (&iter1),
12510 arg2 = next_call_expr_arg (&iter2))
12511 if (!c_tree_equal (arg1, arg2))
12512 return false;
12513 if (arg1 || arg2)
12514 return false;
12515 return true;
12518 case TARGET_EXPR:
12520 tree o1 = TREE_OPERAND (t1, 0);
12521 tree o2 = TREE_OPERAND (t2, 0);
12523 /* Special case: if either target is an unallocated VAR_DECL,
12524 it means that it's going to be unified with whatever the
12525 TARGET_EXPR is really supposed to initialize, so treat it
12526 as being equivalent to anything. */
12527 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12528 && !DECL_RTL_SET_P (o1))
12529 /*Nop*/;
12530 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12531 && !DECL_RTL_SET_P (o2))
12532 /*Nop*/;
12533 else if (!c_tree_equal (o1, o2))
12534 return false;
12536 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12539 case COMPONENT_REF:
12540 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12541 return false;
12542 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12544 case PARM_DECL:
12545 case VAR_DECL:
12546 case CONST_DECL:
12547 case FIELD_DECL:
12548 case FUNCTION_DECL:
12549 case IDENTIFIER_NODE:
12550 case SSA_NAME:
12551 return false;
12553 case TREE_VEC:
12555 unsigned ix;
12556 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12557 return false;
12558 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12559 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12560 TREE_VEC_ELT (t2, ix)))
12561 return false;
12562 return true;
12565 default:
12566 break;
12569 switch (TREE_CODE_CLASS (code1))
12571 case tcc_unary:
12572 case tcc_binary:
12573 case tcc_comparison:
12574 case tcc_expression:
12575 case tcc_vl_exp:
12576 case tcc_reference:
12577 case tcc_statement:
12579 int i, n = TREE_OPERAND_LENGTH (t1);
12581 switch (code1)
12583 case PREINCREMENT_EXPR:
12584 case PREDECREMENT_EXPR:
12585 case POSTINCREMENT_EXPR:
12586 case POSTDECREMENT_EXPR:
12587 n = 1;
12588 break;
12589 case ARRAY_REF:
12590 n = 2;
12591 break;
12592 default:
12593 break;
12596 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12597 && n != TREE_OPERAND_LENGTH (t2))
12598 return false;
12600 for (i = 0; i < n; ++i)
12601 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12602 return false;
12604 return true;
12607 case tcc_type:
12608 return comptypes (t1, t2);
12609 default:
12610 gcc_unreachable ();
12612 /* We can get here with --disable-checking. */
12613 return false;
12616 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12617 spawn-helper and BODY is the newly created body for FNDECL. */
12619 void
12620 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12622 tree list = alloc_stmt_list ();
12623 tree frame = make_cilk_frame (fndecl);
12624 tree dtor = create_cilk_function_exit (frame, false, true);
12625 add_local_decl (cfun, frame);
12627 DECL_SAVED_TREE (fndecl) = list;
12628 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12629 frame);
12630 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12631 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12633 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12634 append_to_statement_list (detach_expr, &body_list);
12636 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12637 body = fold_build_cleanup_point_expr (void_type_node, body);
12639 append_to_statement_list (body, &body_list);
12640 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12641 body_list, dtor), &list);