gcc/ada/
[official-gcc.git] / gcc / c / c-typeck.c
blob79dbc3dff91c62c69649aa527291710f0536f894
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 convert_vector_to_pointer_for_subscript (loc, &array, index);
2500 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2502 tree rval, type;
2504 /* An array that is indexed by a non-constant
2505 cannot be stored in a register; we must be able to do
2506 address arithmetic on its address.
2507 Likewise an array of elements of variable size. */
2508 if (TREE_CODE (index) != INTEGER_CST
2509 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2510 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2512 if (!c_mark_addressable (array))
2513 return error_mark_node;
2515 /* An array that is indexed by a constant value which is not within
2516 the array bounds cannot be stored in a register either; because we
2517 would get a crash in store_bit_field/extract_bit_field when trying
2518 to access a non-existent part of the register. */
2519 if (TREE_CODE (index) == INTEGER_CST
2520 && TYPE_DOMAIN (TREE_TYPE (array))
2521 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2523 if (!c_mark_addressable (array))
2524 return error_mark_node;
2527 if (pedantic || warn_c90_c99_compat)
2529 tree foo = array;
2530 while (TREE_CODE (foo) == COMPONENT_REF)
2531 foo = TREE_OPERAND (foo, 0);
2532 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2533 pedwarn (loc, OPT_Wpedantic,
2534 "ISO C forbids subscripting %<register%> array");
2535 else if (!lvalue_p (foo))
2536 pedwarn_c90 (loc, OPT_Wpedantic,
2537 "ISO C90 forbids subscripting non-lvalue "
2538 "array");
2541 type = TREE_TYPE (TREE_TYPE (array));
2542 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2543 /* Array ref is const/volatile if the array elements are
2544 or if the array is. */
2545 TREE_READONLY (rval)
2546 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2547 | TREE_READONLY (array));
2548 TREE_SIDE_EFFECTS (rval)
2549 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2550 | TREE_SIDE_EFFECTS (array));
2551 TREE_THIS_VOLATILE (rval)
2552 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2553 /* This was added by rms on 16 Nov 91.
2554 It fixes vol struct foo *a; a->elts[1]
2555 in an inline function.
2556 Hope it doesn't break something else. */
2557 | TREE_THIS_VOLATILE (array));
2558 ret = require_complete_type (rval);
2559 protected_set_expr_location (ret, loc);
2560 return ret;
2562 else
2564 tree ar = default_conversion (array);
2566 if (ar == error_mark_node)
2567 return ar;
2569 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2570 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2572 return build_indirect_ref
2573 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2574 RO_ARRAY_INDEXING);
2578 /* Build an external reference to identifier ID. FUN indicates
2579 whether this will be used for a function call. LOC is the source
2580 location of the identifier. This sets *TYPE to the type of the
2581 identifier, which is not the same as the type of the returned value
2582 for CONST_DECLs defined as enum constants. If the type of the
2583 identifier is not available, *TYPE is set to NULL. */
2584 tree
2585 build_external_ref (location_t loc, tree id, int fun, tree *type)
2587 tree ref;
2588 tree decl = lookup_name (id);
2590 /* In Objective-C, an instance variable (ivar) may be preferred to
2591 whatever lookup_name() found. */
2592 decl = objc_lookup_ivar (decl, id);
2594 *type = NULL;
2595 if (decl && decl != error_mark_node)
2597 ref = decl;
2598 *type = TREE_TYPE (ref);
2600 else if (fun)
2601 /* Implicit function declaration. */
2602 ref = implicitly_declare (loc, id);
2603 else if (decl == error_mark_node)
2604 /* Don't complain about something that's already been
2605 complained about. */
2606 return error_mark_node;
2607 else
2609 undeclared_variable (loc, id);
2610 return error_mark_node;
2613 if (TREE_TYPE (ref) == error_mark_node)
2614 return error_mark_node;
2616 if (TREE_DEPRECATED (ref))
2617 warn_deprecated_use (ref, NULL_TREE);
2619 /* Recursive call does not count as usage. */
2620 if (ref != current_function_decl)
2622 TREE_USED (ref) = 1;
2625 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2627 if (!in_sizeof && !in_typeof)
2628 C_DECL_USED (ref) = 1;
2629 else if (DECL_INITIAL (ref) == 0
2630 && DECL_EXTERNAL (ref)
2631 && !TREE_PUBLIC (ref))
2632 record_maybe_used_decl (ref);
2635 if (TREE_CODE (ref) == CONST_DECL)
2637 used_types_insert (TREE_TYPE (ref));
2639 if (warn_cxx_compat
2640 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2641 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2643 warning_at (loc, OPT_Wc___compat,
2644 ("enum constant defined in struct or union "
2645 "is not visible in C++"));
2646 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2649 ref = DECL_INITIAL (ref);
2650 TREE_CONSTANT (ref) = 1;
2652 else if (current_function_decl != 0
2653 && !DECL_FILE_SCOPE_P (current_function_decl)
2654 && (TREE_CODE (ref) == VAR_DECL
2655 || TREE_CODE (ref) == PARM_DECL
2656 || TREE_CODE (ref) == FUNCTION_DECL))
2658 tree context = decl_function_context (ref);
2660 if (context != 0 && context != current_function_decl)
2661 DECL_NONLOCAL (ref) = 1;
2663 /* C99 6.7.4p3: An inline definition of a function with external
2664 linkage ... shall not contain a reference to an identifier with
2665 internal linkage. */
2666 else if (current_function_decl != 0
2667 && DECL_DECLARED_INLINE_P (current_function_decl)
2668 && DECL_EXTERNAL (current_function_decl)
2669 && VAR_OR_FUNCTION_DECL_P (ref)
2670 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2671 && ! TREE_PUBLIC (ref)
2672 && DECL_CONTEXT (ref) != current_function_decl)
2673 record_inline_static (loc, current_function_decl, ref,
2674 csi_internal);
2676 return ref;
2679 /* Record details of decls possibly used inside sizeof or typeof. */
2680 struct maybe_used_decl
2682 /* The decl. */
2683 tree decl;
2684 /* The level seen at (in_sizeof + in_typeof). */
2685 int level;
2686 /* The next one at this level or above, or NULL. */
2687 struct maybe_used_decl *next;
2690 static struct maybe_used_decl *maybe_used_decls;
2692 /* Record that DECL, an undefined static function reference seen
2693 inside sizeof or typeof, might be used if the operand of sizeof is
2694 a VLA type or the operand of typeof is a variably modified
2695 type. */
2697 static void
2698 record_maybe_used_decl (tree decl)
2700 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2701 t->decl = decl;
2702 t->level = in_sizeof + in_typeof;
2703 t->next = maybe_used_decls;
2704 maybe_used_decls = t;
2707 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2708 USED is false, just discard them. If it is true, mark them used
2709 (if no longer inside sizeof or typeof) or move them to the next
2710 level up (if still inside sizeof or typeof). */
2712 void
2713 pop_maybe_used (bool used)
2715 struct maybe_used_decl *p = maybe_used_decls;
2716 int cur_level = in_sizeof + in_typeof;
2717 while (p && p->level > cur_level)
2719 if (used)
2721 if (cur_level == 0)
2722 C_DECL_USED (p->decl) = 1;
2723 else
2724 p->level = cur_level;
2726 p = p->next;
2728 if (!used || cur_level == 0)
2729 maybe_used_decls = p;
2732 /* Return the result of sizeof applied to EXPR. */
2734 struct c_expr
2735 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2737 struct c_expr ret;
2738 if (expr.value == error_mark_node)
2740 ret.value = error_mark_node;
2741 ret.original_code = ERROR_MARK;
2742 ret.original_type = NULL;
2743 pop_maybe_used (false);
2745 else
2747 bool expr_const_operands = true;
2749 if (TREE_CODE (expr.value) == PARM_DECL
2750 && C_ARRAY_PARAMETER (expr.value))
2752 if (warning_at (loc, OPT_Wsizeof_array_argument,
2753 "%<sizeof%> on array function parameter %qE will "
2754 "return size of %qT", expr.value,
2755 expr.original_type))
2756 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2758 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2759 &expr_const_operands);
2760 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2761 c_last_sizeof_arg = expr.value;
2762 ret.original_code = SIZEOF_EXPR;
2763 ret.original_type = NULL;
2764 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2766 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2767 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2768 folded_expr, ret.value);
2769 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2770 SET_EXPR_LOCATION (ret.value, loc);
2772 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2774 return ret;
2777 /* Return the result of sizeof applied to T, a structure for the type
2778 name passed to sizeof (rather than the type itself). LOC is the
2779 location of the original expression. */
2781 struct c_expr
2782 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2784 tree type;
2785 struct c_expr ret;
2786 tree type_expr = NULL_TREE;
2787 bool type_expr_const = true;
2788 type = groktypename (t, &type_expr, &type_expr_const);
2789 ret.value = c_sizeof (loc, type);
2790 c_last_sizeof_arg = type;
2791 ret.original_code = SIZEOF_EXPR;
2792 ret.original_type = NULL;
2793 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2794 && c_vla_type_p (type))
2796 /* If the type is a [*] array, it is a VLA but is represented as
2797 having a size of zero. In such a case we must ensure that
2798 the result of sizeof does not get folded to a constant by
2799 c_fully_fold, because if the size is evaluated the result is
2800 not constant and so constraints on zero or negative size
2801 arrays must not be applied when this sizeof call is inside
2802 another array declarator. */
2803 if (!type_expr)
2804 type_expr = integer_zero_node;
2805 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2806 type_expr, ret.value);
2807 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2809 pop_maybe_used (type != error_mark_node
2810 ? C_TYPE_VARIABLE_SIZE (type) : false);
2811 return ret;
2814 /* Build a function call to function FUNCTION with parameters PARAMS.
2815 The function call is at LOC.
2816 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2817 TREE_VALUE of each node is a parameter-expression.
2818 FUNCTION's data type may be a function type or a pointer-to-function. */
2820 tree
2821 build_function_call (location_t loc, tree function, tree params)
2823 vec<tree, va_gc> *v;
2824 tree ret;
2826 vec_alloc (v, list_length (params));
2827 for (; params; params = TREE_CHAIN (params))
2828 v->quick_push (TREE_VALUE (params));
2829 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2830 vec_free (v);
2831 return ret;
2834 /* Give a note about the location of the declaration of DECL. */
2836 static void inform_declaration (tree decl)
2838 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2839 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2842 /* Build a function call to function FUNCTION with parameters PARAMS.
2843 ORIGTYPES, if not NULL, is a vector of types; each element is
2844 either NULL or the original type of the corresponding element in
2845 PARAMS. The original type may differ from TREE_TYPE of the
2846 parameter for enums. FUNCTION's data type may be a function type
2847 or pointer-to-function. This function changes the elements of
2848 PARAMS. */
2850 tree
2851 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2852 tree function, vec<tree, va_gc> *params,
2853 vec<tree, va_gc> *origtypes)
2855 tree fntype, fundecl = 0;
2856 tree name = NULL_TREE, result;
2857 tree tem;
2858 int nargs;
2859 tree *argarray;
2862 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2863 STRIP_TYPE_NOPS (function);
2865 /* Convert anything with function type to a pointer-to-function. */
2866 if (TREE_CODE (function) == FUNCTION_DECL)
2868 name = DECL_NAME (function);
2870 if (flag_tm)
2871 tm_malloc_replacement (function);
2872 fundecl = function;
2873 /* Atomic functions have type checking/casting already done. They are
2874 often rewritten and don't match the original parameter list. */
2875 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2876 origtypes = NULL;
2878 if (flag_cilkplus
2879 && is_cilkplus_reduce_builtin (function))
2880 origtypes = NULL;
2882 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2883 function = function_to_pointer_conversion (loc, function);
2885 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2886 expressions, like those used for ObjC messenger dispatches. */
2887 if (params && !params->is_empty ())
2888 function = objc_rewrite_function_call (function, (*params)[0]);
2890 function = c_fully_fold (function, false, NULL);
2892 fntype = TREE_TYPE (function);
2894 if (TREE_CODE (fntype) == ERROR_MARK)
2895 return error_mark_node;
2897 if (!(TREE_CODE (fntype) == POINTER_TYPE
2898 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2900 if (!flag_diagnostics_show_caret)
2901 error_at (loc,
2902 "called object %qE is not a function or function pointer",
2903 function);
2904 else if (DECL_P (function))
2906 error_at (loc,
2907 "called object %qD is not a function or function pointer",
2908 function);
2909 inform_declaration (function);
2911 else
2912 error_at (loc,
2913 "called object is not a function or function pointer");
2914 return error_mark_node;
2917 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2918 current_function_returns_abnormally = 1;
2920 /* fntype now gets the type of function pointed to. */
2921 fntype = TREE_TYPE (fntype);
2923 /* Convert the parameters to the types declared in the
2924 function prototype, or apply default promotions. */
2926 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2927 origtypes, function, fundecl);
2928 if (nargs < 0)
2929 return error_mark_node;
2931 /* Check that the function is called through a compatible prototype.
2932 If it is not, warn. */
2933 if (CONVERT_EXPR_P (function)
2934 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2935 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2936 && !comptypes (fntype, TREE_TYPE (tem)))
2938 tree return_type = TREE_TYPE (fntype);
2940 /* This situation leads to run-time undefined behavior. We can't,
2941 therefore, simply error unless we can prove that all possible
2942 executions of the program must execute the code. */
2943 warning_at (loc, 0, "function called through a non-compatible type");
2945 if (VOID_TYPE_P (return_type)
2946 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2947 pedwarn (loc, 0,
2948 "function with qualified void return type called");
2951 argarray = vec_safe_address (params);
2953 /* Check that arguments to builtin functions match the expectations. */
2954 if (fundecl
2955 && DECL_BUILT_IN (fundecl)
2956 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2957 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2958 return error_mark_node;
2960 /* Check that the arguments to the function are valid. */
2961 check_function_arguments (fntype, nargs, argarray);
2963 if (name != NULL_TREE
2964 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2966 if (require_constant_value)
2967 result =
2968 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2969 function, nargs, argarray);
2970 else
2971 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2972 function, nargs, argarray);
2973 if (TREE_CODE (result) == NOP_EXPR
2974 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2975 STRIP_TYPE_NOPS (result);
2977 else
2978 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2979 function, nargs, argarray);
2981 if (VOID_TYPE_P (TREE_TYPE (result)))
2983 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2984 pedwarn (loc, 0,
2985 "function with qualified void return type called");
2986 return result;
2988 return require_complete_type (result);
2991 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2993 tree
2994 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2995 tree function, vec<tree, va_gc> *params,
2996 vec<tree, va_gc> *origtypes)
2998 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2999 STRIP_TYPE_NOPS (function);
3001 /* Convert anything with function type to a pointer-to-function. */
3002 if (TREE_CODE (function) == FUNCTION_DECL)
3004 /* Implement type-directed function overloading for builtins.
3005 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3006 handle all the type checking. The result is a complete expression
3007 that implements this function call. */
3008 tree tem = resolve_overloaded_builtin (loc, function, params);
3009 if (tem)
3010 return tem;
3012 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3015 /* Convert the argument expressions in the vector VALUES
3016 to the types in the list TYPELIST.
3018 If TYPELIST is exhausted, or when an element has NULL as its type,
3019 perform the default conversions.
3021 ORIGTYPES is the original types of the expressions in VALUES. This
3022 holds the type of enum values which have been converted to integral
3023 types. It may be NULL.
3025 FUNCTION is a tree for the called function. It is used only for
3026 error messages, where it is formatted with %qE.
3028 This is also where warnings about wrong number of args are generated.
3030 ARG_LOC are locations of function arguments (if any).
3032 Returns the actual number of arguments processed (which may be less
3033 than the length of VALUES in some error situations), or -1 on
3034 failure. */
3036 static int
3037 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3038 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3039 tree function, tree fundecl)
3041 tree typetail, val;
3042 unsigned int parmnum;
3043 bool error_args = false;
3044 const bool type_generic = fundecl
3045 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3046 bool type_generic_remove_excess_precision = false;
3047 tree selector;
3049 /* Change pointer to function to the function itself for
3050 diagnostics. */
3051 if (TREE_CODE (function) == ADDR_EXPR
3052 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3053 function = TREE_OPERAND (function, 0);
3055 /* Handle an ObjC selector specially for diagnostics. */
3056 selector = objc_message_selector ();
3058 /* For type-generic built-in functions, determine whether excess
3059 precision should be removed (classification) or not
3060 (comparison). */
3061 if (type_generic
3062 && DECL_BUILT_IN (fundecl)
3063 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3065 switch (DECL_FUNCTION_CODE (fundecl))
3067 case BUILT_IN_ISFINITE:
3068 case BUILT_IN_ISINF:
3069 case BUILT_IN_ISINF_SIGN:
3070 case BUILT_IN_ISNAN:
3071 case BUILT_IN_ISNORMAL:
3072 case BUILT_IN_FPCLASSIFY:
3073 type_generic_remove_excess_precision = true;
3074 break;
3076 default:
3077 type_generic_remove_excess_precision = false;
3078 break;
3081 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3082 return vec_safe_length (values);
3084 /* Scan the given expressions and types, producing individual
3085 converted arguments. */
3087 for (typetail = typelist, parmnum = 0;
3088 values && values->iterate (parmnum, &val);
3089 ++parmnum)
3091 tree type = typetail ? TREE_VALUE (typetail) : 0;
3092 tree valtype = TREE_TYPE (val);
3093 tree rname = function;
3094 int argnum = parmnum + 1;
3095 const char *invalid_func_diag;
3096 bool excess_precision = false;
3097 bool npc;
3098 tree parmval;
3099 /* Some __atomic_* builtins have additional hidden argument at
3100 position 0. */
3101 location_t ploc
3102 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3103 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3104 : input_location;
3106 if (type == void_type_node)
3108 if (selector)
3109 error_at (loc, "too many arguments to method %qE", selector);
3110 else
3111 error_at (loc, "too many arguments to function %qE", function);
3112 inform_declaration (fundecl);
3113 return parmnum;
3116 if (selector && argnum > 2)
3118 rname = selector;
3119 argnum -= 2;
3122 npc = null_pointer_constant_p (val);
3124 /* If there is excess precision and a prototype, convert once to
3125 the required type rather than converting via the semantic
3126 type. Likewise without a prototype a float value represented
3127 as long double should be converted once to double. But for
3128 type-generic classification functions excess precision must
3129 be removed here. */
3130 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3131 && (type || !type_generic || !type_generic_remove_excess_precision))
3133 val = TREE_OPERAND (val, 0);
3134 excess_precision = true;
3136 val = c_fully_fold (val, false, NULL);
3137 STRIP_TYPE_NOPS (val);
3139 val = require_complete_type (val);
3141 if (type != 0)
3143 /* Formal parm type is specified by a function prototype. */
3145 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3147 error_at (ploc, "type of formal parameter %d is incomplete",
3148 parmnum + 1);
3149 parmval = val;
3151 else
3153 tree origtype;
3155 /* Optionally warn about conversions that
3156 differ from the default conversions. */
3157 if (warn_traditional_conversion || warn_traditional)
3159 unsigned int formal_prec = TYPE_PRECISION (type);
3161 if (INTEGRAL_TYPE_P (type)
3162 && TREE_CODE (valtype) == REAL_TYPE)
3163 warning_at (ploc, OPT_Wtraditional_conversion,
3164 "passing argument %d of %qE as integer rather "
3165 "than floating due to prototype",
3166 argnum, rname);
3167 if (INTEGRAL_TYPE_P (type)
3168 && TREE_CODE (valtype) == COMPLEX_TYPE)
3169 warning_at (ploc, OPT_Wtraditional_conversion,
3170 "passing argument %d of %qE as integer rather "
3171 "than complex due to prototype",
3172 argnum, rname);
3173 else if (TREE_CODE (type) == COMPLEX_TYPE
3174 && TREE_CODE (valtype) == REAL_TYPE)
3175 warning_at (ploc, OPT_Wtraditional_conversion,
3176 "passing argument %d of %qE as complex rather "
3177 "than floating due to prototype",
3178 argnum, rname);
3179 else if (TREE_CODE (type) == REAL_TYPE
3180 && INTEGRAL_TYPE_P (valtype))
3181 warning_at (ploc, OPT_Wtraditional_conversion,
3182 "passing argument %d of %qE as floating rather "
3183 "than integer due to prototype",
3184 argnum, rname);
3185 else if (TREE_CODE (type) == COMPLEX_TYPE
3186 && INTEGRAL_TYPE_P (valtype))
3187 warning_at (ploc, OPT_Wtraditional_conversion,
3188 "passing argument %d of %qE as complex rather "
3189 "than integer due to prototype",
3190 argnum, rname);
3191 else if (TREE_CODE (type) == REAL_TYPE
3192 && TREE_CODE (valtype) == COMPLEX_TYPE)
3193 warning_at (ploc, OPT_Wtraditional_conversion,
3194 "passing argument %d of %qE as floating rather "
3195 "than complex due to prototype",
3196 argnum, rname);
3197 /* ??? At some point, messages should be written about
3198 conversions between complex types, but that's too messy
3199 to do now. */
3200 else if (TREE_CODE (type) == REAL_TYPE
3201 && TREE_CODE (valtype) == REAL_TYPE)
3203 /* Warn if any argument is passed as `float',
3204 since without a prototype it would be `double'. */
3205 if (formal_prec == TYPE_PRECISION (float_type_node)
3206 && type != dfloat32_type_node)
3207 warning_at (ploc, 0,
3208 "passing argument %d of %qE as %<float%> "
3209 "rather than %<double%> due to prototype",
3210 argnum, rname);
3212 /* Warn if mismatch between argument and prototype
3213 for decimal float types. Warn of conversions with
3214 binary float types and of precision narrowing due to
3215 prototype. */
3216 else if (type != valtype
3217 && (type == dfloat32_type_node
3218 || type == dfloat64_type_node
3219 || type == dfloat128_type_node
3220 || valtype == dfloat32_type_node
3221 || valtype == dfloat64_type_node
3222 || valtype == dfloat128_type_node)
3223 && (formal_prec
3224 <= TYPE_PRECISION (valtype)
3225 || (type == dfloat128_type_node
3226 && (valtype
3227 != dfloat64_type_node
3228 && (valtype
3229 != dfloat32_type_node)))
3230 || (type == dfloat64_type_node
3231 && (valtype
3232 != dfloat32_type_node))))
3233 warning_at (ploc, 0,
3234 "passing argument %d of %qE as %qT "
3235 "rather than %qT due to prototype",
3236 argnum, rname, type, valtype);
3239 /* Detect integer changing in width or signedness.
3240 These warnings are only activated with
3241 -Wtraditional-conversion, not with -Wtraditional. */
3242 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3243 && INTEGRAL_TYPE_P (valtype))
3245 tree would_have_been = default_conversion (val);
3246 tree type1 = TREE_TYPE (would_have_been);
3248 if (TREE_CODE (type) == ENUMERAL_TYPE
3249 && (TYPE_MAIN_VARIANT (type)
3250 == TYPE_MAIN_VARIANT (valtype)))
3251 /* No warning if function asks for enum
3252 and the actual arg is that enum type. */
3254 else if (formal_prec != TYPE_PRECISION (type1))
3255 warning_at (ploc, OPT_Wtraditional_conversion,
3256 "passing argument %d of %qE "
3257 "with different width due to prototype",
3258 argnum, rname);
3259 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3261 /* Don't complain if the formal parameter type
3262 is an enum, because we can't tell now whether
3263 the value was an enum--even the same enum. */
3264 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3266 else if (TREE_CODE (val) == INTEGER_CST
3267 && int_fits_type_p (val, type))
3268 /* Change in signedness doesn't matter
3269 if a constant value is unaffected. */
3271 /* If the value is extended from a narrower
3272 unsigned type, it doesn't matter whether we
3273 pass it as signed or unsigned; the value
3274 certainly is the same either way. */
3275 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3276 && TYPE_UNSIGNED (valtype))
3278 else if (TYPE_UNSIGNED (type))
3279 warning_at (ploc, OPT_Wtraditional_conversion,
3280 "passing argument %d of %qE "
3281 "as unsigned due to prototype",
3282 argnum, rname);
3283 else
3284 warning_at (ploc, OPT_Wtraditional_conversion,
3285 "passing argument %d of %qE "
3286 "as signed due to prototype",
3287 argnum, rname);
3291 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3292 sake of better warnings from convert_and_check. */
3293 if (excess_precision)
3294 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3295 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3296 parmval = convert_for_assignment (loc, ploc, type,
3297 val, origtype, ic_argpass,
3298 npc, fundecl, function,
3299 parmnum + 1);
3301 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3302 && INTEGRAL_TYPE_P (type)
3303 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3304 parmval = default_conversion (parmval);
3307 else if (TREE_CODE (valtype) == REAL_TYPE
3308 && (TYPE_PRECISION (valtype)
3309 <= TYPE_PRECISION (double_type_node))
3310 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3311 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3312 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3314 if (type_generic)
3315 parmval = val;
3316 else
3318 /* Convert `float' to `double'. */
3319 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3320 warning_at (ploc, OPT_Wdouble_promotion,
3321 "implicit conversion from %qT to %qT when passing "
3322 "argument to function",
3323 valtype, double_type_node);
3324 parmval = convert (double_type_node, val);
3327 else if (excess_precision && !type_generic)
3328 /* A "double" argument with excess precision being passed
3329 without a prototype or in variable arguments. */
3330 parmval = convert (valtype, val);
3331 else if ((invalid_func_diag =
3332 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3334 error (invalid_func_diag);
3335 return -1;
3337 else
3338 /* Convert `short' and `char' to full-size `int'. */
3339 parmval = default_conversion (val);
3341 (*values)[parmnum] = parmval;
3342 if (parmval == error_mark_node)
3343 error_args = true;
3345 if (typetail)
3346 typetail = TREE_CHAIN (typetail);
3349 gcc_assert (parmnum == vec_safe_length (values));
3351 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3353 error_at (loc, "too few arguments to function %qE", function);
3354 inform_declaration (fundecl);
3355 return -1;
3358 return error_args ? -1 : (int) parmnum;
3361 /* This is the entry point used by the parser to build unary operators
3362 in the input. CODE, a tree_code, specifies the unary operator, and
3363 ARG is the operand. For unary plus, the C parser currently uses
3364 CONVERT_EXPR for code.
3366 LOC is the location to use for the tree generated.
3369 struct c_expr
3370 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3372 struct c_expr result;
3374 result.value = build_unary_op (loc, code, arg.value, 0);
3375 result.original_code = code;
3376 result.original_type = NULL;
3378 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3379 overflow_warning (loc, result.value);
3381 return result;
3384 /* This is the entry point used by the parser to build binary operators
3385 in the input. CODE, a tree_code, specifies the binary operator, and
3386 ARG1 and ARG2 are the operands. In addition to constructing the
3387 expression, we check for operands that were written with other binary
3388 operators in a way that is likely to confuse the user.
3390 LOCATION is the location of the binary operator. */
3392 struct c_expr
3393 parser_build_binary_op (location_t location, enum tree_code code,
3394 struct c_expr arg1, struct c_expr arg2)
3396 struct c_expr result;
3398 enum tree_code code1 = arg1.original_code;
3399 enum tree_code code2 = arg2.original_code;
3400 tree type1 = (arg1.original_type
3401 ? arg1.original_type
3402 : TREE_TYPE (arg1.value));
3403 tree type2 = (arg2.original_type
3404 ? arg2.original_type
3405 : TREE_TYPE (arg2.value));
3407 result.value = build_binary_op (location, code,
3408 arg1.value, arg2.value, 1);
3409 result.original_code = code;
3410 result.original_type = NULL;
3412 if (TREE_CODE (result.value) == ERROR_MARK)
3413 return result;
3415 if (location != UNKNOWN_LOCATION)
3416 protected_set_expr_location (result.value, location);
3418 /* Check for cases such as x+y<<z which users are likely
3419 to misinterpret. */
3420 if (warn_parentheses)
3421 warn_about_parentheses (location, code, code1, arg1.value, code2,
3422 arg2.value);
3424 if (warn_logical_op)
3425 warn_logical_operator (location, code, TREE_TYPE (result.value),
3426 code1, arg1.value, code2, arg2.value);
3428 if (warn_logical_not_paren
3429 && code1 == TRUTH_NOT_EXPR
3430 && code2 != TRUTH_NOT_EXPR)
3431 warn_logical_not_parentheses (location, code, arg2.value);
3433 /* Warn about comparisons against string literals, with the exception
3434 of testing for equality or inequality of a string literal with NULL. */
3435 if (code == EQ_EXPR || code == NE_EXPR)
3437 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3438 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3439 warning_at (location, OPT_Waddress,
3440 "comparison with string literal results in unspecified behavior");
3442 else if (TREE_CODE_CLASS (code) == tcc_comparison
3443 && (code1 == STRING_CST || code2 == STRING_CST))
3444 warning_at (location, OPT_Waddress,
3445 "comparison with string literal results in unspecified behavior");
3447 if (TREE_OVERFLOW_P (result.value)
3448 && !TREE_OVERFLOW_P (arg1.value)
3449 && !TREE_OVERFLOW_P (arg2.value))
3450 overflow_warning (location, result.value);
3452 /* Warn about comparisons of different enum types. */
3453 if (warn_enum_compare
3454 && TREE_CODE_CLASS (code) == tcc_comparison
3455 && TREE_CODE (type1) == ENUMERAL_TYPE
3456 && TREE_CODE (type2) == ENUMERAL_TYPE
3457 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3458 warning_at (location, OPT_Wenum_compare,
3459 "comparison between %qT and %qT",
3460 type1, type2);
3462 return result;
3465 /* Return a tree for the difference of pointers OP0 and OP1.
3466 The resulting tree has type int. */
3468 static tree
3469 pointer_diff (location_t loc, tree op0, tree op1)
3471 tree restype = ptrdiff_type_node;
3472 tree result, inttype;
3474 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3475 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3476 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3477 tree orig_op1 = op1;
3479 /* If the operands point into different address spaces, we need to
3480 explicitly convert them to pointers into the common address space
3481 before we can subtract the numerical address values. */
3482 if (as0 != as1)
3484 addr_space_t as_common;
3485 tree common_type;
3487 /* Determine the common superset address space. This is guaranteed
3488 to exist because the caller verified that comp_target_types
3489 returned non-zero. */
3490 if (!addr_space_superset (as0, as1, &as_common))
3491 gcc_unreachable ();
3493 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3494 op0 = convert (common_type, op0);
3495 op1 = convert (common_type, op1);
3498 /* Determine integer type to perform computations in. This will usually
3499 be the same as the result type (ptrdiff_t), but may need to be a wider
3500 type if pointers for the address space are wider than ptrdiff_t. */
3501 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3502 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3503 else
3504 inttype = restype;
3506 if (TREE_CODE (target_type) == VOID_TYPE)
3507 pedwarn (loc, OPT_Wpointer_arith,
3508 "pointer of type %<void *%> used in subtraction");
3509 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3510 pedwarn (loc, OPT_Wpointer_arith,
3511 "pointer to a function used in subtraction");
3513 /* First do the subtraction as integers;
3514 then drop through to build the divide operator.
3515 Do not do default conversions on the minus operator
3516 in case restype is a short type. */
3518 op0 = build_binary_op (loc,
3519 MINUS_EXPR, convert (inttype, op0),
3520 convert (inttype, op1), 0);
3521 /* This generates an error if op1 is pointer to incomplete type. */
3522 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3523 error_at (loc, "arithmetic on pointer to an incomplete type");
3525 op1 = c_size_in_bytes (target_type);
3527 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3528 error_at (loc, "arithmetic on pointer to an empty aggregate");
3530 /* Divide by the size, in easiest possible way. */
3531 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3532 op0, convert (inttype, op1));
3534 /* Convert to final result type if necessary. */
3535 return convert (restype, result);
3538 /* Expand atomic compound assignments into an approriate sequence as
3539 specified by the C11 standard section 6.5.16.2.
3540 given
3541 _Atomic T1 E1
3542 T2 E2
3543 E1 op= E2
3545 This sequence is used for all types for which these operations are
3546 supported.
3548 In addition, built-in versions of the 'fe' prefixed routines may
3549 need to be invoked for floating point (real, complex or vector) when
3550 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3552 T1 newval;
3553 T1 old;
3554 T1 *addr
3555 T2 val
3556 fenv_t fenv
3558 addr = &E1;
3559 val = (E2);
3560 __atomic_load (addr, &old, SEQ_CST);
3561 feholdexcept (&fenv);
3562 loop:
3563 newval = old op val;
3564 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3565 SEQ_CST))
3566 goto done;
3567 feclearexcept (FE_ALL_EXCEPT);
3568 goto loop:
3569 done:
3570 feupdateenv (&fenv);
3572 Also note that the compiler is simply issuing the generic form of
3573 the atomic operations. This requires temp(s) and has their address
3574 taken. The atomic processing is smart enough to figure out when the
3575 size of an object can utilize a lock-free version, and convert the
3576 built-in call to the appropriate lock-free routine. The optimizers
3577 will then dispose of any temps that are no longer required, and
3578 lock-free implementations are utilized as long as there is target
3579 support for the required size.
3581 If the operator is NOP_EXPR, then this is a simple assignment, and
3582 an __atomic_store is issued to perform the assignment rather than
3583 the above loop.
3587 /* Build an atomic assignment at LOC, expanding into the proper
3588 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3589 the result of the operation, unless RETURN_OLD_P in which case
3590 return the old value of LHS (this is only for postincrement and
3591 postdecrement). */
3592 static tree
3593 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3594 tree rhs, bool return_old_p)
3596 tree fndecl, func_call;
3597 vec<tree, va_gc> *params;
3598 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3599 tree old, old_addr;
3600 tree compound_stmt;
3601 tree stmt, goto_stmt;
3602 tree loop_label, loop_decl, done_label, done_decl;
3604 tree lhs_type = TREE_TYPE (lhs);
3605 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3606 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3607 tree rhs_type = TREE_TYPE (rhs);
3609 gcc_assert (TYPE_ATOMIC (lhs_type));
3611 if (return_old_p)
3612 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3614 /* Allocate enough vector items for a compare_exchange. */
3615 vec_alloc (params, 6);
3617 /* Create a compound statement to hold the sequence of statements
3618 with a loop. */
3619 compound_stmt = c_begin_compound_stmt (false);
3621 /* Fold the RHS if it hasn't already been folded. */
3622 if (modifycode != NOP_EXPR)
3623 rhs = c_fully_fold (rhs, false, NULL);
3625 /* Remove the qualifiers for the rest of the expressions and create
3626 the VAL temp variable to hold the RHS. */
3627 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3628 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3629 val = create_tmp_var (nonatomic_rhs_type, NULL);
3630 TREE_ADDRESSABLE (val) = 1;
3631 TREE_NO_WARNING (val) = 1;
3632 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3633 SET_EXPR_LOCATION (rhs, loc);
3634 add_stmt (rhs);
3636 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3637 an atomic_store. */
3638 if (modifycode == NOP_EXPR)
3640 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3641 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3642 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3643 params->quick_push (lhs_addr);
3644 params->quick_push (rhs);
3645 params->quick_push (seq_cst);
3646 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3647 add_stmt (func_call);
3649 /* Finish the compound statement. */
3650 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3652 /* VAL is the value which was stored, return a COMPOUND_STMT of
3653 the statement and that value. */
3654 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3657 /* Create the variables and labels required for the op= form. */
3658 old = create_tmp_var (nonatomic_lhs_type, NULL);
3659 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3660 TREE_ADDRESSABLE (old) = 1;
3661 TREE_NO_WARNING (old) = 1;
3663 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3664 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3665 TREE_ADDRESSABLE (newval) = 1;
3667 loop_decl = create_artificial_label (loc);
3668 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3670 done_decl = create_artificial_label (loc);
3671 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3673 /* __atomic_load (addr, &old, SEQ_CST). */
3674 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3675 params->quick_push (lhs_addr);
3676 params->quick_push (old_addr);
3677 params->quick_push (seq_cst);
3678 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3679 add_stmt (func_call);
3680 params->truncate (0);
3682 /* Create the expressions for floating-point environment
3683 manipulation, if required. */
3684 bool need_fenv = (flag_trapping_math
3685 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3686 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3687 if (need_fenv)
3688 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3690 if (hold_call)
3691 add_stmt (hold_call);
3693 /* loop: */
3694 add_stmt (loop_label);
3696 /* newval = old + val; */
3697 rhs = build_binary_op (loc, modifycode, old, val, 1);
3698 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3699 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3700 NULL_TREE, 0);
3701 if (rhs != error_mark_node)
3703 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3704 SET_EXPR_LOCATION (rhs, loc);
3705 add_stmt (rhs);
3708 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3709 goto done; */
3710 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3711 params->quick_push (lhs_addr);
3712 params->quick_push (old_addr);
3713 params->quick_push (newval_addr);
3714 params->quick_push (integer_zero_node);
3715 params->quick_push (seq_cst);
3716 params->quick_push (seq_cst);
3717 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3719 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3720 SET_EXPR_LOCATION (goto_stmt, loc);
3722 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3723 SET_EXPR_LOCATION (stmt, loc);
3724 add_stmt (stmt);
3726 if (clear_call)
3727 add_stmt (clear_call);
3729 /* goto loop; */
3730 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3731 SET_EXPR_LOCATION (goto_stmt, loc);
3732 add_stmt (goto_stmt);
3734 /* done: */
3735 add_stmt (done_label);
3737 if (update_call)
3738 add_stmt (update_call);
3740 /* Finish the compound statement. */
3741 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3743 /* NEWVAL is the value that was successfully stored, return a
3744 COMPOUND_EXPR of the statement and the appropriate value. */
3745 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3746 return_old_p ? old : newval);
3749 /* Construct and perhaps optimize a tree representation
3750 for a unary operation. CODE, a tree_code, specifies the operation
3751 and XARG is the operand.
3752 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3753 the default promotions (such as from short to int).
3754 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3755 allows non-lvalues; this is only used to handle conversion of non-lvalue
3756 arrays to pointers in C99.
3758 LOCATION is the location of the operator. */
3760 tree
3761 build_unary_op (location_t location,
3762 enum tree_code code, tree xarg, int flag)
3764 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3765 tree arg = xarg;
3766 tree argtype = 0;
3767 enum tree_code typecode;
3768 tree val;
3769 tree ret = error_mark_node;
3770 tree eptype = NULL_TREE;
3771 int noconvert = flag;
3772 const char *invalid_op_diag;
3773 bool int_operands;
3775 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3776 if (int_operands)
3777 arg = remove_c_maybe_const_expr (arg);
3779 if (code != ADDR_EXPR)
3780 arg = require_complete_type (arg);
3782 typecode = TREE_CODE (TREE_TYPE (arg));
3783 if (typecode == ERROR_MARK)
3784 return error_mark_node;
3785 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3786 typecode = INTEGER_TYPE;
3788 if ((invalid_op_diag
3789 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3791 error_at (location, invalid_op_diag);
3792 return error_mark_node;
3795 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3797 eptype = TREE_TYPE (arg);
3798 arg = TREE_OPERAND (arg, 0);
3801 switch (code)
3803 case CONVERT_EXPR:
3804 /* This is used for unary plus, because a CONVERT_EXPR
3805 is enough to prevent anybody from looking inside for
3806 associativity, but won't generate any code. */
3807 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3808 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3809 || typecode == VECTOR_TYPE))
3811 error_at (location, "wrong type argument to unary plus");
3812 return error_mark_node;
3814 else if (!noconvert)
3815 arg = default_conversion (arg);
3816 arg = non_lvalue_loc (location, arg);
3817 break;
3819 case NEGATE_EXPR:
3820 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3821 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3822 || typecode == VECTOR_TYPE))
3824 error_at (location, "wrong type argument to unary minus");
3825 return error_mark_node;
3827 else if (!noconvert)
3828 arg = default_conversion (arg);
3829 break;
3831 case BIT_NOT_EXPR:
3832 /* ~ works on integer types and non float vectors. */
3833 if (typecode == INTEGER_TYPE
3834 || (typecode == VECTOR_TYPE
3835 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3837 if (!noconvert)
3838 arg = default_conversion (arg);
3840 else if (typecode == COMPLEX_TYPE)
3842 code = CONJ_EXPR;
3843 pedwarn (location, OPT_Wpedantic,
3844 "ISO C does not support %<~%> for complex conjugation");
3845 if (!noconvert)
3846 arg = default_conversion (arg);
3848 else
3850 error_at (location, "wrong type argument to bit-complement");
3851 return error_mark_node;
3853 break;
3855 case ABS_EXPR:
3856 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3858 error_at (location, "wrong type argument to abs");
3859 return error_mark_node;
3861 else if (!noconvert)
3862 arg = default_conversion (arg);
3863 break;
3865 case CONJ_EXPR:
3866 /* Conjugating a real value is a no-op, but allow it anyway. */
3867 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3868 || typecode == COMPLEX_TYPE))
3870 error_at (location, "wrong type argument to conjugation");
3871 return error_mark_node;
3873 else if (!noconvert)
3874 arg = default_conversion (arg);
3875 break;
3877 case TRUTH_NOT_EXPR:
3878 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3879 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3880 && typecode != COMPLEX_TYPE)
3882 error_at (location,
3883 "wrong type argument to unary exclamation mark");
3884 return error_mark_node;
3886 if (int_operands)
3888 arg = c_objc_common_truthvalue_conversion (location, xarg);
3889 arg = remove_c_maybe_const_expr (arg);
3891 else
3892 arg = c_objc_common_truthvalue_conversion (location, arg);
3893 ret = invert_truthvalue_loc (location, arg);
3894 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3895 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3896 location = EXPR_LOCATION (ret);
3897 goto return_build_unary_op;
3899 case REALPART_EXPR:
3900 case IMAGPART_EXPR:
3901 ret = build_real_imag_expr (location, code, arg);
3902 if (ret == error_mark_node)
3903 return error_mark_node;
3904 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3905 eptype = TREE_TYPE (eptype);
3906 goto return_build_unary_op;
3908 case PREINCREMENT_EXPR:
3909 case POSTINCREMENT_EXPR:
3910 case PREDECREMENT_EXPR:
3911 case POSTDECREMENT_EXPR:
3913 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3915 tree inner = build_unary_op (location, code,
3916 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3917 if (inner == error_mark_node)
3918 return error_mark_node;
3919 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3920 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3921 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3922 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3923 goto return_build_unary_op;
3926 /* Complain about anything that is not a true lvalue. In
3927 Objective-C, skip this check for property_refs. */
3928 if (!objc_is_property_ref (arg)
3929 && !lvalue_or_else (location,
3930 arg, ((code == PREINCREMENT_EXPR
3931 || code == POSTINCREMENT_EXPR)
3932 ? lv_increment
3933 : lv_decrement)))
3934 return error_mark_node;
3936 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3938 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3939 warning_at (location, OPT_Wc___compat,
3940 "increment of enumeration value is invalid in C++");
3941 else
3942 warning_at (location, OPT_Wc___compat,
3943 "decrement of enumeration value is invalid in C++");
3946 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3947 arg = c_fully_fold (arg, false, NULL);
3949 bool atomic_op;
3950 atomic_op = really_atomic_lvalue (arg);
3952 /* Increment or decrement the real part of the value,
3953 and don't change the imaginary part. */
3954 if (typecode == COMPLEX_TYPE)
3956 tree real, imag;
3958 pedwarn (location, OPT_Wpedantic,
3959 "ISO C does not support %<++%> and %<--%> on complex types");
3961 if (!atomic_op)
3963 arg = stabilize_reference (arg);
3964 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3965 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3966 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3967 if (real == error_mark_node || imag == error_mark_node)
3968 return error_mark_node;
3969 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3970 real, imag);
3971 goto return_build_unary_op;
3975 /* Report invalid types. */
3977 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3978 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3979 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
3981 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3982 error_at (location, "wrong type argument to increment");
3983 else
3984 error_at (location, "wrong type argument to decrement");
3986 return error_mark_node;
3990 tree inc;
3992 argtype = TREE_TYPE (arg);
3994 /* Compute the increment. */
3996 if (typecode == POINTER_TYPE)
3998 /* If pointer target is an incomplete type,
3999 we just cannot know how to do the arithmetic. */
4000 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4003 error_at (location,
4004 "increment of pointer to an incomplete type %qT",
4005 TREE_TYPE (argtype));
4006 else
4007 error_at (location,
4008 "decrement of pointer to an incomplete type %qT",
4009 TREE_TYPE (argtype));
4011 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4012 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4014 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4015 pedwarn (location, OPT_Wpointer_arith,
4016 "wrong type argument to increment");
4017 else
4018 pedwarn (location, OPT_Wpointer_arith,
4019 "wrong type argument to decrement");
4022 inc = c_size_in_bytes (TREE_TYPE (argtype));
4023 inc = convert_to_ptrofftype_loc (location, inc);
4025 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4027 /* For signed fract types, we invert ++ to -- or
4028 -- to ++, and change inc from 1 to -1, because
4029 it is not possible to represent 1 in signed fract constants.
4030 For unsigned fract types, the result always overflows and
4031 we get an undefined (original) or the maximum value. */
4032 if (code == PREINCREMENT_EXPR)
4033 code = PREDECREMENT_EXPR;
4034 else if (code == PREDECREMENT_EXPR)
4035 code = PREINCREMENT_EXPR;
4036 else if (code == POSTINCREMENT_EXPR)
4037 code = POSTDECREMENT_EXPR;
4038 else /* code == POSTDECREMENT_EXPR */
4039 code = POSTINCREMENT_EXPR;
4041 inc = integer_minus_one_node;
4042 inc = convert (argtype, inc);
4044 else
4046 inc = VECTOR_TYPE_P (argtype)
4047 ? build_one_cst (argtype)
4048 : integer_one_node;
4049 inc = convert (argtype, inc);
4052 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4053 need to ask Objective-C to build the increment or decrement
4054 expression for it. */
4055 if (objc_is_property_ref (arg))
4056 return objc_build_incr_expr_for_property_ref (location, code,
4057 arg, inc);
4059 /* Report a read-only lvalue. */
4060 if (TYPE_READONLY (argtype))
4062 readonly_error (location, arg,
4063 ((code == PREINCREMENT_EXPR
4064 || code == POSTINCREMENT_EXPR)
4065 ? lv_increment : lv_decrement));
4066 return error_mark_node;
4068 else if (TREE_READONLY (arg))
4069 readonly_warning (arg,
4070 ((code == PREINCREMENT_EXPR
4071 || code == POSTINCREMENT_EXPR)
4072 ? lv_increment : lv_decrement));
4074 /* If the argument is atomic, use the special code sequences for
4075 atomic compound assignment. */
4076 if (atomic_op)
4078 arg = stabilize_reference (arg);
4079 ret = build_atomic_assign (location, arg,
4080 ((code == PREINCREMENT_EXPR
4081 || code == POSTINCREMENT_EXPR)
4082 ? PLUS_EXPR
4083 : MINUS_EXPR),
4084 (FRACT_MODE_P (TYPE_MODE (argtype))
4085 ? inc
4086 : integer_one_node),
4087 (code == POSTINCREMENT_EXPR
4088 || code == POSTDECREMENT_EXPR));
4089 goto return_build_unary_op;
4092 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4093 val = boolean_increment (code, arg);
4094 else
4095 val = build2 (code, TREE_TYPE (arg), arg, inc);
4096 TREE_SIDE_EFFECTS (val) = 1;
4097 if (TREE_CODE (val) != code)
4098 TREE_NO_WARNING (val) = 1;
4099 ret = val;
4100 goto return_build_unary_op;
4103 case ADDR_EXPR:
4104 /* Note that this operation never does default_conversion. */
4106 /* The operand of unary '&' must be an lvalue (which excludes
4107 expressions of type void), or, in C99, the result of a [] or
4108 unary '*' operator. */
4109 if (VOID_TYPE_P (TREE_TYPE (arg))
4110 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4111 && (TREE_CODE (arg) != INDIRECT_REF
4112 || !flag_isoc99))
4113 pedwarn (location, 0, "taking address of expression of type %<void%>");
4115 /* Let &* cancel out to simplify resulting code. */
4116 if (TREE_CODE (arg) == INDIRECT_REF)
4118 /* Don't let this be an lvalue. */
4119 if (lvalue_p (TREE_OPERAND (arg, 0)))
4120 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4121 ret = TREE_OPERAND (arg, 0);
4122 goto return_build_unary_op;
4125 /* For &x[y], return x+y */
4126 if (TREE_CODE (arg) == ARRAY_REF)
4128 tree op0 = TREE_OPERAND (arg, 0);
4129 if (!c_mark_addressable (op0))
4130 return error_mark_node;
4133 /* Anything not already handled and not a true memory reference
4134 or a non-lvalue array is an error. */
4135 else if (typecode != FUNCTION_TYPE && !flag
4136 && !lvalue_or_else (location, arg, lv_addressof))
4137 return error_mark_node;
4139 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4140 folding later. */
4141 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4143 tree inner = build_unary_op (location, code,
4144 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4145 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4146 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4147 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4148 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4149 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4150 goto return_build_unary_op;
4153 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4154 argtype = TREE_TYPE (arg);
4156 /* If the lvalue is const or volatile, merge that into the type
4157 to which the address will point. This is only needed
4158 for function types. */
4159 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4160 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4161 && TREE_CODE (argtype) == FUNCTION_TYPE)
4163 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4164 int quals = orig_quals;
4166 if (TREE_READONLY (arg))
4167 quals |= TYPE_QUAL_CONST;
4168 if (TREE_THIS_VOLATILE (arg))
4169 quals |= TYPE_QUAL_VOLATILE;
4171 argtype = c_build_qualified_type (argtype, quals);
4174 if (!c_mark_addressable (arg))
4175 return error_mark_node;
4177 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4178 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4180 argtype = build_pointer_type (argtype);
4182 /* ??? Cope with user tricks that amount to offsetof. Delete this
4183 when we have proper support for integer constant expressions. */
4184 val = get_base_address (arg);
4185 if (val && TREE_CODE (val) == INDIRECT_REF
4186 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4188 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4189 goto return_build_unary_op;
4192 val = build1 (ADDR_EXPR, argtype, arg);
4194 ret = val;
4195 goto return_build_unary_op;
4197 default:
4198 gcc_unreachable ();
4201 if (argtype == 0)
4202 argtype = TREE_TYPE (arg);
4203 if (TREE_CODE (arg) == INTEGER_CST)
4204 ret = (require_constant_value
4205 ? fold_build1_initializer_loc (location, code, argtype, arg)
4206 : fold_build1_loc (location, code, argtype, arg));
4207 else
4208 ret = build1 (code, argtype, arg);
4209 return_build_unary_op:
4210 gcc_assert (ret != error_mark_node);
4211 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4212 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4213 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4214 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4215 ret = note_integer_operands (ret);
4216 if (eptype)
4217 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4218 protected_set_expr_location (ret, location);
4219 return ret;
4222 /* Return nonzero if REF is an lvalue valid for this language.
4223 Lvalues can be assigned, unless their type has TYPE_READONLY.
4224 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4226 bool
4227 lvalue_p (const_tree ref)
4229 const enum tree_code code = TREE_CODE (ref);
4231 switch (code)
4233 case REALPART_EXPR:
4234 case IMAGPART_EXPR:
4235 case COMPONENT_REF:
4236 return lvalue_p (TREE_OPERAND (ref, 0));
4238 case C_MAYBE_CONST_EXPR:
4239 return lvalue_p (TREE_OPERAND (ref, 1));
4241 case COMPOUND_LITERAL_EXPR:
4242 case STRING_CST:
4243 return 1;
4245 case INDIRECT_REF:
4246 case ARRAY_REF:
4247 case ARRAY_NOTATION_REF:
4248 case VAR_DECL:
4249 case PARM_DECL:
4250 case RESULT_DECL:
4251 case ERROR_MARK:
4252 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4253 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4255 case BIND_EXPR:
4256 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4258 default:
4259 return 0;
4263 /* Give a warning for storing in something that is read-only in GCC
4264 terms but not const in ISO C terms. */
4266 static void
4267 readonly_warning (tree arg, enum lvalue_use use)
4269 switch (use)
4271 case lv_assign:
4272 warning (0, "assignment of read-only location %qE", arg);
4273 break;
4274 case lv_increment:
4275 warning (0, "increment of read-only location %qE", arg);
4276 break;
4277 case lv_decrement:
4278 warning (0, "decrement of read-only location %qE", arg);
4279 break;
4280 default:
4281 gcc_unreachable ();
4283 return;
4287 /* Return nonzero if REF is an lvalue valid for this language;
4288 otherwise, print an error message and return zero. USE says
4289 how the lvalue is being used and so selects the error message.
4290 LOCATION is the location at which any error should be reported. */
4292 static int
4293 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4295 int win = lvalue_p (ref);
4297 if (!win)
4298 lvalue_error (loc, use);
4300 return win;
4303 /* Mark EXP saying that we need to be able to take the
4304 address of it; it should not be allocated in a register.
4305 Returns true if successful. */
4307 bool
4308 c_mark_addressable (tree exp)
4310 tree x = exp;
4312 while (1)
4313 switch (TREE_CODE (x))
4315 case COMPONENT_REF:
4316 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4318 error
4319 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4320 return false;
4323 /* ... fall through ... */
4325 case ADDR_EXPR:
4326 case ARRAY_REF:
4327 case REALPART_EXPR:
4328 case IMAGPART_EXPR:
4329 x = TREE_OPERAND (x, 0);
4330 break;
4332 case COMPOUND_LITERAL_EXPR:
4333 case CONSTRUCTOR:
4334 TREE_ADDRESSABLE (x) = 1;
4335 return true;
4337 case VAR_DECL:
4338 case CONST_DECL:
4339 case PARM_DECL:
4340 case RESULT_DECL:
4341 if (C_DECL_REGISTER (x)
4342 && DECL_NONLOCAL (x))
4344 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4346 error
4347 ("global register variable %qD used in nested function", x);
4348 return false;
4350 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4352 else if (C_DECL_REGISTER (x))
4354 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4355 error ("address of global register variable %qD requested", x);
4356 else
4357 error ("address of register variable %qD requested", x);
4358 return false;
4361 /* drops in */
4362 case FUNCTION_DECL:
4363 TREE_ADDRESSABLE (x) = 1;
4364 /* drops out */
4365 default:
4366 return true;
4370 /* Convert EXPR to TYPE, warning about conversion problems with
4371 constants. SEMANTIC_TYPE is the type this conversion would use
4372 without excess precision. If SEMANTIC_TYPE is NULL, this function
4373 is equivalent to convert_and_check. This function is a wrapper that
4374 handles conversions that may be different than
4375 the usual ones because of excess precision. */
4377 static tree
4378 ep_convert_and_check (location_t loc, tree type, tree expr,
4379 tree semantic_type)
4381 if (TREE_TYPE (expr) == type)
4382 return expr;
4384 if (!semantic_type)
4385 return convert_and_check (loc, type, expr);
4387 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4388 && TREE_TYPE (expr) != semantic_type)
4390 /* For integers, we need to check the real conversion, not
4391 the conversion to the excess precision type. */
4392 expr = convert_and_check (loc, semantic_type, expr);
4394 /* Result type is the excess precision type, which should be
4395 large enough, so do not check. */
4396 return convert (type, expr);
4399 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4400 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4401 if folded to an integer constant then the unselected half may
4402 contain arbitrary operations not normally permitted in constant
4403 expressions. Set the location of the expression to LOC. */
4405 tree
4406 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4407 tree op1, tree op1_original_type, tree op2,
4408 tree op2_original_type)
4410 tree type1;
4411 tree type2;
4412 enum tree_code code1;
4413 enum tree_code code2;
4414 tree result_type = NULL;
4415 tree semantic_result_type = NULL;
4416 tree orig_op1 = op1, orig_op2 = op2;
4417 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4418 bool ifexp_int_operands;
4419 tree ret;
4421 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4422 if (op1_int_operands)
4423 op1 = remove_c_maybe_const_expr (op1);
4424 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4425 if (op2_int_operands)
4426 op2 = remove_c_maybe_const_expr (op2);
4427 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4428 if (ifexp_int_operands)
4429 ifexp = remove_c_maybe_const_expr (ifexp);
4431 /* Promote both alternatives. */
4433 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4434 op1 = default_conversion (op1);
4435 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4436 op2 = default_conversion (op2);
4438 if (TREE_CODE (ifexp) == ERROR_MARK
4439 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4440 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4441 return error_mark_node;
4443 type1 = TREE_TYPE (op1);
4444 code1 = TREE_CODE (type1);
4445 type2 = TREE_TYPE (op2);
4446 code2 = TREE_CODE (type2);
4448 /* C90 does not permit non-lvalue arrays in conditional expressions.
4449 In C99 they will be pointers by now. */
4450 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4452 error_at (colon_loc, "non-lvalue array in conditional expression");
4453 return error_mark_node;
4456 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4457 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4458 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4459 || code1 == COMPLEX_TYPE)
4460 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4461 || code2 == COMPLEX_TYPE))
4463 semantic_result_type = c_common_type (type1, type2);
4464 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4466 op1 = TREE_OPERAND (op1, 0);
4467 type1 = TREE_TYPE (op1);
4468 gcc_assert (TREE_CODE (type1) == code1);
4470 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4472 op2 = TREE_OPERAND (op2, 0);
4473 type2 = TREE_TYPE (op2);
4474 gcc_assert (TREE_CODE (type2) == code2);
4478 if (warn_cxx_compat)
4480 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4481 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4483 if (TREE_CODE (t1) == ENUMERAL_TYPE
4484 && TREE_CODE (t2) == ENUMERAL_TYPE
4485 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4486 warning_at (colon_loc, OPT_Wc___compat,
4487 ("different enum types in conditional is "
4488 "invalid in C++: %qT vs %qT"),
4489 t1, t2);
4492 /* Quickly detect the usual case where op1 and op2 have the same type
4493 after promotion. */
4494 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4496 if (type1 == type2)
4497 result_type = type1;
4498 else
4499 result_type = TYPE_MAIN_VARIANT (type1);
4501 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4502 || code1 == COMPLEX_TYPE)
4503 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4504 || code2 == COMPLEX_TYPE))
4506 result_type = c_common_type (type1, type2);
4507 do_warn_double_promotion (result_type, type1, type2,
4508 "implicit conversion from %qT to %qT to "
4509 "match other result of conditional",
4510 colon_loc);
4512 /* If -Wsign-compare, warn here if type1 and type2 have
4513 different signedness. We'll promote the signed to unsigned
4514 and later code won't know it used to be different.
4515 Do this check on the original types, so that explicit casts
4516 will be considered, but default promotions won't. */
4517 if (c_inhibit_evaluation_warnings == 0)
4519 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4520 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4522 if (unsigned_op1 ^ unsigned_op2)
4524 bool ovf;
4526 /* Do not warn if the result type is signed, since the
4527 signed type will only be chosen if it can represent
4528 all the values of the unsigned type. */
4529 if (!TYPE_UNSIGNED (result_type))
4530 /* OK */;
4531 else
4533 bool op1_maybe_const = true;
4534 bool op2_maybe_const = true;
4536 /* Do not warn if the signed quantity is an
4537 unsuffixed integer literal (or some static
4538 constant expression involving such literals) and
4539 it is non-negative. This warning requires the
4540 operands to be folded for best results, so do
4541 that folding in this case even without
4542 warn_sign_compare to avoid warning options
4543 possibly affecting code generation. */
4544 c_inhibit_evaluation_warnings
4545 += (ifexp == truthvalue_false_node);
4546 op1 = c_fully_fold (op1, require_constant_value,
4547 &op1_maybe_const);
4548 c_inhibit_evaluation_warnings
4549 -= (ifexp == truthvalue_false_node);
4551 c_inhibit_evaluation_warnings
4552 += (ifexp == truthvalue_true_node);
4553 op2 = c_fully_fold (op2, require_constant_value,
4554 &op2_maybe_const);
4555 c_inhibit_evaluation_warnings
4556 -= (ifexp == truthvalue_true_node);
4558 if (warn_sign_compare)
4560 if ((unsigned_op2
4561 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4562 || (unsigned_op1
4563 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4564 /* OK */;
4565 else
4566 warning_at (colon_loc, OPT_Wsign_compare,
4567 ("signed and unsigned type in "
4568 "conditional expression"));
4570 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4571 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4572 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4573 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4578 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4580 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4581 pedwarn (colon_loc, OPT_Wpedantic,
4582 "ISO C forbids conditional expr with only one void side");
4583 result_type = void_type_node;
4585 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4587 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4588 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4589 addr_space_t as_common;
4591 if (comp_target_types (colon_loc, type1, type2))
4592 result_type = common_pointer_type (type1, type2);
4593 else if (null_pointer_constant_p (orig_op1))
4594 result_type = type2;
4595 else if (null_pointer_constant_p (orig_op2))
4596 result_type = type1;
4597 else if (!addr_space_superset (as1, as2, &as_common))
4599 error_at (colon_loc, "pointers to disjoint address spaces "
4600 "used in conditional expression");
4601 return error_mark_node;
4603 else if (VOID_TYPE_P (TREE_TYPE (type1))
4604 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4606 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4607 pedwarn (colon_loc, OPT_Wpedantic,
4608 "ISO C forbids conditional expr between "
4609 "%<void *%> and function pointer");
4610 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4611 TREE_TYPE (type2)));
4613 else if (VOID_TYPE_P (TREE_TYPE (type2))
4614 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4616 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4617 pedwarn (colon_loc, OPT_Wpedantic,
4618 "ISO C forbids conditional expr between "
4619 "%<void *%> and function pointer");
4620 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4621 TREE_TYPE (type1)));
4623 /* Objective-C pointer comparisons are a bit more lenient. */
4624 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4625 result_type = objc_common_type (type1, type2);
4626 else
4628 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4630 pedwarn (colon_loc, 0,
4631 "pointer type mismatch in conditional expression");
4632 result_type = build_pointer_type
4633 (build_qualified_type (void_type_node, qual));
4636 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4638 if (!null_pointer_constant_p (orig_op2))
4639 pedwarn (colon_loc, 0,
4640 "pointer/integer type mismatch in conditional expression");
4641 else
4643 op2 = null_pointer_node;
4645 result_type = type1;
4647 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4649 if (!null_pointer_constant_p (orig_op1))
4650 pedwarn (colon_loc, 0,
4651 "pointer/integer type mismatch in conditional expression");
4652 else
4654 op1 = null_pointer_node;
4656 result_type = type2;
4659 if (!result_type)
4661 if (flag_cond_mismatch)
4662 result_type = void_type_node;
4663 else
4665 error_at (colon_loc, "type mismatch in conditional expression");
4666 return error_mark_node;
4670 /* Merge const and volatile flags of the incoming types. */
4671 result_type
4672 = build_type_variant (result_type,
4673 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4674 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4676 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4677 semantic_result_type);
4678 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4679 semantic_result_type);
4681 if (ifexp_bcp && ifexp == truthvalue_true_node)
4683 op2_int_operands = true;
4684 op1 = c_fully_fold (op1, require_constant_value, NULL);
4686 if (ifexp_bcp && ifexp == truthvalue_false_node)
4688 op1_int_operands = true;
4689 op2 = c_fully_fold (op2, require_constant_value, NULL);
4691 int_const = int_operands = (ifexp_int_operands
4692 && op1_int_operands
4693 && op2_int_operands);
4694 if (int_operands)
4696 int_const = ((ifexp == truthvalue_true_node
4697 && TREE_CODE (orig_op1) == INTEGER_CST
4698 && !TREE_OVERFLOW (orig_op1))
4699 || (ifexp == truthvalue_false_node
4700 && TREE_CODE (orig_op2) == INTEGER_CST
4701 && !TREE_OVERFLOW (orig_op2)));
4703 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4704 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4705 else
4707 if (int_operands)
4709 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4710 nested inside of the expression. */
4711 op1 = c_fully_fold (op1, false, NULL);
4712 op2 = c_fully_fold (op2, false, NULL);
4714 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4715 if (int_operands)
4716 ret = note_integer_operands (ret);
4718 if (semantic_result_type)
4719 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4721 protected_set_expr_location (ret, colon_loc);
4722 return ret;
4725 /* Return a compound expression that performs two expressions and
4726 returns the value of the second of them.
4728 LOC is the location of the COMPOUND_EXPR. */
4730 tree
4731 build_compound_expr (location_t loc, tree expr1, tree expr2)
4733 bool expr1_int_operands, expr2_int_operands;
4734 tree eptype = NULL_TREE;
4735 tree ret;
4737 if (flag_cilkplus
4738 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4739 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4741 error_at (loc,
4742 "spawned function call cannot be part of a comma expression");
4743 return error_mark_node;
4745 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4746 if (expr1_int_operands)
4747 expr1 = remove_c_maybe_const_expr (expr1);
4748 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4749 if (expr2_int_operands)
4750 expr2 = remove_c_maybe_const_expr (expr2);
4752 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4753 expr1 = TREE_OPERAND (expr1, 0);
4754 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4756 eptype = TREE_TYPE (expr2);
4757 expr2 = TREE_OPERAND (expr2, 0);
4760 if (!TREE_SIDE_EFFECTS (expr1))
4762 /* The left-hand operand of a comma expression is like an expression
4763 statement: with -Wunused, we should warn if it doesn't have
4764 any side-effects, unless it was explicitly cast to (void). */
4765 if (warn_unused_value)
4767 if (VOID_TYPE_P (TREE_TYPE (expr1))
4768 && CONVERT_EXPR_P (expr1))
4769 ; /* (void) a, b */
4770 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4771 && TREE_CODE (expr1) == COMPOUND_EXPR
4772 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4773 ; /* (void) a, (void) b, c */
4774 else
4775 warning_at (loc, OPT_Wunused_value,
4776 "left-hand operand of comma expression has no effect");
4779 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4780 && warn_unused_value)
4782 tree r = expr1;
4783 location_t cloc = loc;
4784 while (TREE_CODE (r) == COMPOUND_EXPR)
4786 if (EXPR_HAS_LOCATION (r))
4787 cloc = EXPR_LOCATION (r);
4788 r = TREE_OPERAND (r, 1);
4790 if (!TREE_SIDE_EFFECTS (r)
4791 && !VOID_TYPE_P (TREE_TYPE (r))
4792 && !CONVERT_EXPR_P (r))
4793 warning_at (cloc, OPT_Wunused_value,
4794 "right-hand operand of comma expression has no effect");
4797 /* With -Wunused, we should also warn if the left-hand operand does have
4798 side-effects, but computes a value which is not used. For example, in
4799 `foo() + bar(), baz()' the result of the `+' operator is not used,
4800 so we should issue a warning. */
4801 else if (warn_unused_value)
4802 warn_if_unused_value (expr1, loc);
4804 if (expr2 == error_mark_node)
4805 return error_mark_node;
4807 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4809 if (flag_isoc99
4810 && expr1_int_operands
4811 && expr2_int_operands)
4812 ret = note_integer_operands (ret);
4814 if (eptype)
4815 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4817 protected_set_expr_location (ret, loc);
4818 return ret;
4821 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4822 which we are casting. OTYPE is the type of the expression being
4823 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4824 of the cast. -Wcast-qual appeared on the command line. Named
4825 address space qualifiers are not handled here, because they result
4826 in different warnings. */
4828 static void
4829 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4831 tree in_type = type;
4832 tree in_otype = otype;
4833 int added = 0;
4834 int discarded = 0;
4835 bool is_const;
4837 /* Check that the qualifiers on IN_TYPE are a superset of the
4838 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4839 nodes is uninteresting and we stop as soon as we hit a
4840 non-POINTER_TYPE node on either type. */
4843 in_otype = TREE_TYPE (in_otype);
4844 in_type = TREE_TYPE (in_type);
4846 /* GNU C allows cv-qualified function types. 'const' means the
4847 function is very pure, 'volatile' means it can't return. We
4848 need to warn when such qualifiers are added, not when they're
4849 taken away. */
4850 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4851 && TREE_CODE (in_type) == FUNCTION_TYPE)
4852 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4853 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4854 else
4855 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4856 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4858 while (TREE_CODE (in_type) == POINTER_TYPE
4859 && TREE_CODE (in_otype) == POINTER_TYPE);
4861 if (added)
4862 warning_at (loc, OPT_Wcast_qual,
4863 "cast adds %q#v qualifier to function type", added);
4865 if (discarded)
4866 /* There are qualifiers present in IN_OTYPE that are not present
4867 in IN_TYPE. */
4868 warning_at (loc, OPT_Wcast_qual,
4869 "cast discards %qv qualifier from pointer target type",
4870 discarded);
4872 if (added || discarded)
4873 return;
4875 /* A cast from **T to const **T is unsafe, because it can cause a
4876 const value to be changed with no additional warning. We only
4877 issue this warning if T is the same on both sides, and we only
4878 issue the warning if there are the same number of pointers on
4879 both sides, as otherwise the cast is clearly unsafe anyhow. A
4880 cast is unsafe when a qualifier is added at one level and const
4881 is not present at all outer levels.
4883 To issue this warning, we check at each level whether the cast
4884 adds new qualifiers not already seen. We don't need to special
4885 case function types, as they won't have the same
4886 TYPE_MAIN_VARIANT. */
4888 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4889 return;
4890 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4891 return;
4893 in_type = type;
4894 in_otype = otype;
4895 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4898 in_type = TREE_TYPE (in_type);
4899 in_otype = TREE_TYPE (in_otype);
4900 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4901 && !is_const)
4903 warning_at (loc, OPT_Wcast_qual,
4904 "to be safe all intermediate pointers in cast from "
4905 "%qT to %qT must be %<const%> qualified",
4906 otype, type);
4907 break;
4909 if (is_const)
4910 is_const = TYPE_READONLY (in_type);
4912 while (TREE_CODE (in_type) == POINTER_TYPE);
4915 /* Build an expression representing a cast to type TYPE of expression EXPR.
4916 LOC is the location of the cast-- typically the open paren of the cast. */
4918 tree
4919 build_c_cast (location_t loc, tree type, tree expr)
4921 tree value;
4923 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4924 expr = TREE_OPERAND (expr, 0);
4926 value = expr;
4928 if (type == error_mark_node || expr == error_mark_node)
4929 return error_mark_node;
4931 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4932 only in <protocol> qualifications. But when constructing cast expressions,
4933 the protocols do matter and must be kept around. */
4934 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4935 return build1 (NOP_EXPR, type, expr);
4937 type = TYPE_MAIN_VARIANT (type);
4939 if (TREE_CODE (type) == ARRAY_TYPE)
4941 error_at (loc, "cast specifies array type");
4942 return error_mark_node;
4945 if (TREE_CODE (type) == FUNCTION_TYPE)
4947 error_at (loc, "cast specifies function type");
4948 return error_mark_node;
4951 if (!VOID_TYPE_P (type))
4953 value = require_complete_type (value);
4954 if (value == error_mark_node)
4955 return error_mark_node;
4958 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4960 if (TREE_CODE (type) == RECORD_TYPE
4961 || TREE_CODE (type) == UNION_TYPE)
4962 pedwarn (loc, OPT_Wpedantic,
4963 "ISO C forbids casting nonscalar to the same type");
4965 /* Convert to remove any qualifiers from VALUE's type. */
4966 value = convert (type, value);
4968 else if (TREE_CODE (type) == UNION_TYPE)
4970 tree field;
4972 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4973 if (TREE_TYPE (field) != error_mark_node
4974 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4975 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4976 break;
4978 if (field)
4980 tree t;
4981 bool maybe_const = true;
4983 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4984 t = c_fully_fold (value, false, &maybe_const);
4985 t = build_constructor_single (type, field, t);
4986 if (!maybe_const)
4987 t = c_wrap_maybe_const (t, true);
4988 t = digest_init (loc, type, t,
4989 NULL_TREE, false, true, 0);
4990 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4991 return t;
4993 error_at (loc, "cast to union type from type not present in union");
4994 return error_mark_node;
4996 else
4998 tree otype, ovalue;
5000 if (type == void_type_node)
5002 tree t = build1 (CONVERT_EXPR, type, value);
5003 SET_EXPR_LOCATION (t, loc);
5004 return t;
5007 otype = TREE_TYPE (value);
5009 /* Optionally warn about potentially worrisome casts. */
5010 if (warn_cast_qual
5011 && TREE_CODE (type) == POINTER_TYPE
5012 && TREE_CODE (otype) == POINTER_TYPE)
5013 handle_warn_cast_qual (loc, type, otype);
5015 /* Warn about conversions between pointers to disjoint
5016 address spaces. */
5017 if (TREE_CODE (type) == POINTER_TYPE
5018 && TREE_CODE (otype) == POINTER_TYPE
5019 && !null_pointer_constant_p (value))
5021 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5022 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5023 addr_space_t as_common;
5025 if (!addr_space_superset (as_to, as_from, &as_common))
5027 if (ADDR_SPACE_GENERIC_P (as_from))
5028 warning_at (loc, 0, "cast to %s address space pointer "
5029 "from disjoint generic address space pointer",
5030 c_addr_space_name (as_to));
5032 else if (ADDR_SPACE_GENERIC_P (as_to))
5033 warning_at (loc, 0, "cast to generic address space pointer "
5034 "from disjoint %s address space pointer",
5035 c_addr_space_name (as_from));
5037 else
5038 warning_at (loc, 0, "cast to %s address space pointer "
5039 "from disjoint %s address space pointer",
5040 c_addr_space_name (as_to),
5041 c_addr_space_name (as_from));
5045 /* Warn about possible alignment problems. */
5046 if (STRICT_ALIGNMENT
5047 && TREE_CODE (type) == POINTER_TYPE
5048 && TREE_CODE (otype) == POINTER_TYPE
5049 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5050 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5051 /* Don't warn about opaque types, where the actual alignment
5052 restriction is unknown. */
5053 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5054 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5055 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5056 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5057 warning_at (loc, OPT_Wcast_align,
5058 "cast increases required alignment of target type");
5060 if (TREE_CODE (type) == INTEGER_TYPE
5061 && TREE_CODE (otype) == POINTER_TYPE
5062 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5063 /* Unlike conversion of integers to pointers, where the
5064 warning is disabled for converting constants because
5065 of cases such as SIG_*, warn about converting constant
5066 pointers to integers. In some cases it may cause unwanted
5067 sign extension, and a warning is appropriate. */
5068 warning_at (loc, OPT_Wpointer_to_int_cast,
5069 "cast from pointer to integer of different size");
5071 if (TREE_CODE (value) == CALL_EXPR
5072 && TREE_CODE (type) != TREE_CODE (otype))
5073 warning_at (loc, OPT_Wbad_function_cast,
5074 "cast from function call of type %qT "
5075 "to non-matching type %qT", otype, type);
5077 if (TREE_CODE (type) == POINTER_TYPE
5078 && TREE_CODE (otype) == INTEGER_TYPE
5079 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5080 /* Don't warn about converting any constant. */
5081 && !TREE_CONSTANT (value))
5082 warning_at (loc,
5083 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5084 "of different size");
5086 if (warn_strict_aliasing <= 2)
5087 strict_aliasing_warning (otype, type, expr);
5089 /* If pedantic, warn for conversions between function and object
5090 pointer types, except for converting a null pointer constant
5091 to function pointer type. */
5092 if (pedantic
5093 && TREE_CODE (type) == POINTER_TYPE
5094 && TREE_CODE (otype) == POINTER_TYPE
5095 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5096 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5097 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5098 "conversion of function pointer to object pointer type");
5100 if (pedantic
5101 && TREE_CODE (type) == POINTER_TYPE
5102 && TREE_CODE (otype) == POINTER_TYPE
5103 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5104 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5105 && !null_pointer_constant_p (value))
5106 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5107 "conversion of object pointer to function pointer type");
5109 ovalue = value;
5110 value = convert (type, value);
5112 /* Ignore any integer overflow caused by the cast. */
5113 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5115 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5117 if (!TREE_OVERFLOW (value))
5119 /* Avoid clobbering a shared constant. */
5120 value = copy_node (value);
5121 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5124 else if (TREE_OVERFLOW (value))
5125 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5126 value = wide_int_to_tree (TREE_TYPE (value), value);
5130 /* Don't let a cast be an lvalue. */
5131 if (value == expr)
5132 value = non_lvalue_loc (loc, value);
5134 /* Don't allow the results of casting to floating-point or complex
5135 types be confused with actual constants, or casts involving
5136 integer and pointer types other than direct integer-to-integer
5137 and integer-to-pointer be confused with integer constant
5138 expressions and null pointer constants. */
5139 if (TREE_CODE (value) == REAL_CST
5140 || TREE_CODE (value) == COMPLEX_CST
5141 || (TREE_CODE (value) == INTEGER_CST
5142 && !((TREE_CODE (expr) == INTEGER_CST
5143 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5144 || TREE_CODE (expr) == REAL_CST
5145 || TREE_CODE (expr) == COMPLEX_CST)))
5146 value = build1 (NOP_EXPR, type, value);
5148 if (CAN_HAVE_LOCATION_P (value))
5149 SET_EXPR_LOCATION (value, loc);
5150 return value;
5153 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5154 location of the open paren of the cast, or the position of the cast
5155 expr. */
5156 tree
5157 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5159 tree type;
5160 tree type_expr = NULL_TREE;
5161 bool type_expr_const = true;
5162 tree ret;
5163 int saved_wsp = warn_strict_prototypes;
5165 /* This avoids warnings about unprototyped casts on
5166 integers. E.g. "#define SIG_DFL (void(*)())0". */
5167 if (TREE_CODE (expr) == INTEGER_CST)
5168 warn_strict_prototypes = 0;
5169 type = groktypename (type_name, &type_expr, &type_expr_const);
5170 warn_strict_prototypes = saved_wsp;
5172 ret = build_c_cast (loc, type, expr);
5173 if (type_expr)
5175 bool inner_expr_const = true;
5176 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5178 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5179 && inner_expr_const);
5180 SET_EXPR_LOCATION (ret, loc);
5183 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5184 SET_EXPR_LOCATION (ret, loc);
5186 /* C++ does not permits types to be defined in a cast, but it
5187 allows references to incomplete types. */
5188 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5189 warning_at (loc, OPT_Wc___compat,
5190 "defining a type in a cast is invalid in C++");
5192 return ret;
5195 /* Build an assignment expression of lvalue LHS from value RHS.
5196 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5197 may differ from TREE_TYPE (LHS) for an enum bitfield.
5198 MODIFYCODE is the code for a binary operator that we use
5199 to combine the old value of LHS with RHS to get the new value.
5200 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5201 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5202 which may differ from TREE_TYPE (RHS) for an enum value.
5204 LOCATION is the location of the MODIFYCODE operator.
5205 RHS_LOC is the location of the RHS. */
5207 tree
5208 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5209 enum tree_code modifycode,
5210 location_t rhs_loc, tree rhs, tree rhs_origtype)
5212 tree result;
5213 tree newrhs;
5214 tree rhseval = NULL_TREE;
5215 tree rhs_semantic_type = NULL_TREE;
5216 tree lhstype = TREE_TYPE (lhs);
5217 tree olhstype = lhstype;
5218 bool npc;
5219 bool is_atomic_op;
5221 /* Types that aren't fully specified cannot be used in assignments. */
5222 lhs = require_complete_type (lhs);
5224 /* Avoid duplicate error messages from operands that had errors. */
5225 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5226 return error_mark_node;
5228 /* Ensure an error for assigning a non-lvalue array to an array in
5229 C90. */
5230 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5232 error_at (location, "assignment to expression with array type");
5233 return error_mark_node;
5236 /* For ObjC properties, defer this check. */
5237 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5238 return error_mark_node;
5240 is_atomic_op = really_atomic_lvalue (lhs);
5242 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5244 rhs_semantic_type = TREE_TYPE (rhs);
5245 rhs = TREE_OPERAND (rhs, 0);
5248 newrhs = rhs;
5250 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5252 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5253 lhs_origtype, modifycode, rhs_loc, rhs,
5254 rhs_origtype);
5255 if (inner == error_mark_node)
5256 return error_mark_node;
5257 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5258 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5259 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5260 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5261 protected_set_expr_location (result, location);
5262 return result;
5265 /* If a binary op has been requested, combine the old LHS value with the RHS
5266 producing the value we should actually store into the LHS. */
5268 if (modifycode != NOP_EXPR)
5270 lhs = c_fully_fold (lhs, false, NULL);
5271 lhs = stabilize_reference (lhs);
5273 /* Construct the RHS for any non-atomic compound assignemnt. */
5274 if (!is_atomic_op)
5276 /* If in LHS op= RHS the RHS has side-effects, ensure they
5277 are preevaluated before the rest of the assignment expression's
5278 side-effects, because RHS could contain e.g. function calls
5279 that modify LHS. */
5280 if (TREE_SIDE_EFFECTS (rhs))
5282 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5283 rhseval = newrhs;
5285 newrhs = build_binary_op (location,
5286 modifycode, lhs, newrhs, 1);
5288 /* The original type of the right hand side is no longer
5289 meaningful. */
5290 rhs_origtype = NULL_TREE;
5294 if (c_dialect_objc ())
5296 /* Check if we are modifying an Objective-C property reference;
5297 if so, we need to generate setter calls. */
5298 result = objc_maybe_build_modify_expr (lhs, newrhs);
5299 if (result)
5300 goto return_result;
5302 /* Else, do the check that we postponed for Objective-C. */
5303 if (!lvalue_or_else (location, lhs, lv_assign))
5304 return error_mark_node;
5307 /* Give an error for storing in something that is 'const'. */
5309 if (TYPE_READONLY (lhstype)
5310 || ((TREE_CODE (lhstype) == RECORD_TYPE
5311 || TREE_CODE (lhstype) == UNION_TYPE)
5312 && C_TYPE_FIELDS_READONLY (lhstype)))
5314 readonly_error (location, lhs, lv_assign);
5315 return error_mark_node;
5317 else if (TREE_READONLY (lhs))
5318 readonly_warning (lhs, lv_assign);
5320 /* If storing into a structure or union member,
5321 it has probably been given type `int'.
5322 Compute the type that would go with
5323 the actual amount of storage the member occupies. */
5325 if (TREE_CODE (lhs) == COMPONENT_REF
5326 && (TREE_CODE (lhstype) == INTEGER_TYPE
5327 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5328 || TREE_CODE (lhstype) == REAL_TYPE
5329 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5330 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5332 /* If storing in a field that is in actuality a short or narrower than one,
5333 we must store in the field in its actual type. */
5335 if (lhstype != TREE_TYPE (lhs))
5337 lhs = copy_node (lhs);
5338 TREE_TYPE (lhs) = lhstype;
5341 /* Issue -Wc++-compat warnings about an assignment to an enum type
5342 when LHS does not have its original type. This happens for,
5343 e.g., an enum bitfield in a struct. */
5344 if (warn_cxx_compat
5345 && lhs_origtype != NULL_TREE
5346 && lhs_origtype != lhstype
5347 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5349 tree checktype = (rhs_origtype != NULL_TREE
5350 ? rhs_origtype
5351 : TREE_TYPE (rhs));
5352 if (checktype != error_mark_node
5353 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5354 || (is_atomic_op && modifycode != NOP_EXPR)))
5355 warning_at (location, OPT_Wc___compat,
5356 "enum conversion in assignment is invalid in C++");
5359 /* If the lhs is atomic, remove that qualifier. */
5360 if (is_atomic_op)
5362 lhstype = build_qualified_type (lhstype,
5363 (TYPE_QUALS (lhstype)
5364 & ~TYPE_QUAL_ATOMIC));
5365 olhstype = build_qualified_type (olhstype,
5366 (TYPE_QUALS (lhstype)
5367 & ~TYPE_QUAL_ATOMIC));
5370 /* Convert new value to destination type. Fold it first, then
5371 restore any excess precision information, for the sake of
5372 conversion warnings. */
5374 if (!(is_atomic_op && modifycode != NOP_EXPR))
5376 npc = null_pointer_constant_p (newrhs);
5377 newrhs = c_fully_fold (newrhs, false, NULL);
5378 if (rhs_semantic_type)
5379 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5380 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5381 rhs_origtype, ic_assign, npc,
5382 NULL_TREE, NULL_TREE, 0);
5383 if (TREE_CODE (newrhs) == ERROR_MARK)
5384 return error_mark_node;
5387 /* Emit ObjC write barrier, if necessary. */
5388 if (c_dialect_objc () && flag_objc_gc)
5390 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5391 if (result)
5393 protected_set_expr_location (result, location);
5394 goto return_result;
5398 /* Scan operands. */
5400 if (is_atomic_op)
5401 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5402 else
5404 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5405 TREE_SIDE_EFFECTS (result) = 1;
5406 protected_set_expr_location (result, location);
5409 /* If we got the LHS in a different type for storing in,
5410 convert the result back to the nominal type of LHS
5411 so that the value we return always has the same type
5412 as the LHS argument. */
5414 if (olhstype == TREE_TYPE (result))
5415 goto return_result;
5417 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5418 rhs_origtype, ic_assign, false, NULL_TREE,
5419 NULL_TREE, 0);
5420 protected_set_expr_location (result, location);
5422 return_result:
5423 if (rhseval)
5424 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5425 return result;
5428 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5429 This is used to implement -fplan9-extensions. */
5431 static bool
5432 find_anonymous_field_with_type (tree struct_type, tree type)
5434 tree field;
5435 bool found;
5437 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5438 || TREE_CODE (struct_type) == UNION_TYPE);
5439 found = false;
5440 for (field = TYPE_FIELDS (struct_type);
5441 field != NULL_TREE;
5442 field = TREE_CHAIN (field))
5444 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5445 ? c_build_qualified_type (TREE_TYPE (field),
5446 TYPE_QUAL_ATOMIC)
5447 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5448 if (DECL_NAME (field) == NULL
5449 && comptypes (type, fieldtype))
5451 if (found)
5452 return false;
5453 found = true;
5455 else if (DECL_NAME (field) == NULL
5456 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5457 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5458 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5460 if (found)
5461 return false;
5462 found = true;
5465 return found;
5468 /* RHS is an expression whose type is pointer to struct. If there is
5469 an anonymous field in RHS with type TYPE, then return a pointer to
5470 that field in RHS. This is used with -fplan9-extensions. This
5471 returns NULL if no conversion could be found. */
5473 static tree
5474 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5476 tree rhs_struct_type, lhs_main_type;
5477 tree field, found_field;
5478 bool found_sub_field;
5479 tree ret;
5481 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5482 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5483 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5484 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5486 gcc_assert (POINTER_TYPE_P (type));
5487 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5488 ? c_build_qualified_type (TREE_TYPE (type),
5489 TYPE_QUAL_ATOMIC)
5490 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5492 found_field = NULL_TREE;
5493 found_sub_field = false;
5494 for (field = TYPE_FIELDS (rhs_struct_type);
5495 field != NULL_TREE;
5496 field = TREE_CHAIN (field))
5498 if (DECL_NAME (field) != NULL_TREE
5499 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5500 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5501 continue;
5502 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5503 ? c_build_qualified_type (TREE_TYPE (field),
5504 TYPE_QUAL_ATOMIC)
5505 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5506 if (comptypes (lhs_main_type, fieldtype))
5508 if (found_field != NULL_TREE)
5509 return NULL_TREE;
5510 found_field = field;
5512 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5513 lhs_main_type))
5515 if (found_field != NULL_TREE)
5516 return NULL_TREE;
5517 found_field = field;
5518 found_sub_field = true;
5522 if (found_field == NULL_TREE)
5523 return NULL_TREE;
5525 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5526 build_fold_indirect_ref (rhs), found_field,
5527 NULL_TREE);
5528 ret = build_fold_addr_expr_loc (location, ret);
5530 if (found_sub_field)
5532 ret = convert_to_anonymous_field (location, type, ret);
5533 gcc_assert (ret != NULL_TREE);
5536 return ret;
5539 /* Issue an error message for a bad initializer component.
5540 GMSGID identifies the message.
5541 The component name is taken from the spelling stack. */
5543 static void
5544 error_init (location_t loc, const char *gmsgid)
5546 char *ofwhat;
5548 /* The gmsgid may be a format string with %< and %>. */
5549 error_at (loc, gmsgid);
5550 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5551 if (*ofwhat)
5552 inform (loc, "(near initialization for %qs)", ofwhat);
5555 /* Issue a pedantic warning for a bad initializer component. OPT is
5556 the option OPT_* (from options.h) controlling this warning or 0 if
5557 it is unconditionally given. GMSGID identifies the message. The
5558 component name is taken from the spelling stack. */
5560 static void
5561 pedwarn_init (location_t location, int opt, const char *gmsgid)
5563 char *ofwhat;
5564 bool warned;
5566 /* The gmsgid may be a format string with %< and %>. */
5567 warned = pedwarn (location, opt, gmsgid);
5568 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5569 if (*ofwhat && warned)
5570 inform (location, "(near initialization for %qs)", ofwhat);
5573 /* Issue a warning for a bad initializer component.
5575 OPT is the OPT_W* value corresponding to the warning option that
5576 controls this warning. GMSGID identifies the message. The
5577 component name is taken from the spelling stack. */
5579 static void
5580 warning_init (location_t loc, int opt, const char *gmsgid)
5582 char *ofwhat;
5583 bool warned;
5585 /* The gmsgid may be a format string with %< and %>. */
5586 warned = warning_at (loc, opt, gmsgid);
5587 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5588 if (*ofwhat && warned)
5589 inform (loc, "(near initialization for %qs)", ofwhat);
5592 /* If TYPE is an array type and EXPR is a parenthesized string
5593 constant, warn if pedantic that EXPR is being used to initialize an
5594 object of type TYPE. */
5596 void
5597 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5599 if (pedantic
5600 && TREE_CODE (type) == ARRAY_TYPE
5601 && TREE_CODE (expr.value) == STRING_CST
5602 && expr.original_code != STRING_CST)
5603 pedwarn_init (loc, OPT_Wpedantic,
5604 "array initialized from parenthesized string constant");
5607 /* Convert value RHS to type TYPE as preparation for an assignment to
5608 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5609 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5610 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5611 constant before any folding.
5612 The real work of conversion is done by `convert'.
5613 The purpose of this function is to generate error messages
5614 for assignments that are not allowed in C.
5615 ERRTYPE says whether it is argument passing, assignment,
5616 initialization or return.
5618 LOCATION is the location of the assignment, EXPR_LOC is the location of
5619 the RHS or, for a function, location of an argument.
5620 FUNCTION is a tree for the function being called.
5621 PARMNUM is the number of the argument, for printing in error messages. */
5623 static tree
5624 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5625 tree rhs, tree origtype, enum impl_conv errtype,
5626 bool null_pointer_constant, tree fundecl,
5627 tree function, int parmnum)
5629 enum tree_code codel = TREE_CODE (type);
5630 tree orig_rhs = rhs;
5631 tree rhstype;
5632 enum tree_code coder;
5633 tree rname = NULL_TREE;
5634 bool objc_ok = false;
5636 if (errtype == ic_argpass)
5638 tree selector;
5639 /* Change pointer to function to the function itself for
5640 diagnostics. */
5641 if (TREE_CODE (function) == ADDR_EXPR
5642 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5643 function = TREE_OPERAND (function, 0);
5645 /* Handle an ObjC selector specially for diagnostics. */
5646 selector = objc_message_selector ();
5647 rname = function;
5648 if (selector && parmnum > 2)
5650 rname = selector;
5651 parmnum -= 2;
5655 /* This macro is used to emit diagnostics to ensure that all format
5656 strings are complete sentences, visible to gettext and checked at
5657 compile time. */
5658 #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5659 do { \
5660 switch (errtype) \
5662 case ic_argpass: \
5663 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5664 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5665 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5666 "expected %qT but argument is of type %qT", \
5667 type, rhstype); \
5668 break; \
5669 case ic_assign: \
5670 pedwarn (LOCATION, OPT, AS); \
5671 break; \
5672 case ic_init: \
5673 pedwarn_init (LOCATION, OPT, IN); \
5674 break; \
5675 case ic_return: \
5676 pedwarn (LOCATION, OPT, RE); \
5677 break; \
5678 default: \
5679 gcc_unreachable (); \
5681 } while (0)
5683 /* This macro is used to emit diagnostics to ensure that all format
5684 strings are complete sentences, visible to gettext and checked at
5685 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5686 extra parameter to enumerate qualifiers. */
5688 #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5689 do { \
5690 switch (errtype) \
5692 case ic_argpass: \
5693 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5694 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5695 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5696 "expected %qT but argument is of type %qT", \
5697 type, rhstype); \
5698 break; \
5699 case ic_assign: \
5700 pedwarn (LOCATION, OPT, AS, QUALS); \
5701 break; \
5702 case ic_init: \
5703 pedwarn (LOCATION, OPT, IN, QUALS); \
5704 break; \
5705 case ic_return: \
5706 pedwarn (LOCATION, OPT, RE, QUALS); \
5707 break; \
5708 default: \
5709 gcc_unreachable (); \
5711 } while (0)
5713 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5714 rhs = TREE_OPERAND (rhs, 0);
5716 rhstype = TREE_TYPE (rhs);
5717 coder = TREE_CODE (rhstype);
5719 if (coder == ERROR_MARK)
5720 return error_mark_node;
5722 if (c_dialect_objc ())
5724 int parmno;
5726 switch (errtype)
5728 case ic_return:
5729 parmno = 0;
5730 break;
5732 case ic_assign:
5733 parmno = -1;
5734 break;
5736 case ic_init:
5737 parmno = -2;
5738 break;
5740 default:
5741 parmno = parmnum;
5742 break;
5745 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5748 if (warn_cxx_compat)
5750 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5751 if (checktype != error_mark_node
5752 && TREE_CODE (type) == ENUMERAL_TYPE
5753 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5755 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5756 G_("enum conversion when passing argument "
5757 "%d of %qE is invalid in C++"),
5758 G_("enum conversion in assignment is "
5759 "invalid in C++"),
5760 G_("enum conversion in initialization is "
5761 "invalid in C++"),
5762 G_("enum conversion in return is "
5763 "invalid in C++"));
5767 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5768 return rhs;
5770 if (coder == VOID_TYPE)
5772 /* Except for passing an argument to an unprototyped function,
5773 this is a constraint violation. When passing an argument to
5774 an unprototyped function, it is compile-time undefined;
5775 making it a constraint in that case was rejected in
5776 DR#252. */
5777 error_at (location, "void value not ignored as it ought to be");
5778 return error_mark_node;
5780 rhs = require_complete_type (rhs);
5781 if (rhs == error_mark_node)
5782 return error_mark_node;
5783 /* A non-reference type can convert to a reference. This handles
5784 va_start, va_copy and possibly port built-ins. */
5785 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5787 if (!lvalue_p (rhs))
5789 error_at (location, "cannot pass rvalue to reference parameter");
5790 return error_mark_node;
5792 if (!c_mark_addressable (rhs))
5793 return error_mark_node;
5794 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5795 SET_EXPR_LOCATION (rhs, location);
5797 rhs = convert_for_assignment (location, expr_loc,
5798 build_pointer_type (TREE_TYPE (type)),
5799 rhs, origtype, errtype,
5800 null_pointer_constant, fundecl, function,
5801 parmnum);
5802 if (rhs == error_mark_node)
5803 return error_mark_node;
5805 rhs = build1 (NOP_EXPR, type, rhs);
5806 SET_EXPR_LOCATION (rhs, location);
5807 return rhs;
5809 /* Some types can interconvert without explicit casts. */
5810 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5811 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5812 return convert (type, rhs);
5813 /* Arithmetic types all interconvert, and enum is treated like int. */
5814 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5815 || codel == FIXED_POINT_TYPE
5816 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5817 || codel == BOOLEAN_TYPE)
5818 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5819 || coder == FIXED_POINT_TYPE
5820 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5821 || coder == BOOLEAN_TYPE))
5823 tree ret;
5824 bool save = in_late_binary_op;
5825 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5826 in_late_binary_op = true;
5827 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5828 ? expr_loc : location, type, orig_rhs);
5829 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5830 in_late_binary_op = save;
5831 return ret;
5834 /* Aggregates in different TUs might need conversion. */
5835 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5836 && codel == coder
5837 && comptypes (type, rhstype))
5838 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5839 ? expr_loc : location, type, rhs);
5841 /* Conversion to a transparent union or record from its member types.
5842 This applies only to function arguments. */
5843 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5844 && TYPE_TRANSPARENT_AGGR (type))
5845 && errtype == ic_argpass)
5847 tree memb, marginal_memb = NULL_TREE;
5849 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5851 tree memb_type = TREE_TYPE (memb);
5853 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5854 TYPE_MAIN_VARIANT (rhstype)))
5855 break;
5857 if (TREE_CODE (memb_type) != POINTER_TYPE)
5858 continue;
5860 if (coder == POINTER_TYPE)
5862 tree ttl = TREE_TYPE (memb_type);
5863 tree ttr = TREE_TYPE (rhstype);
5865 /* Any non-function converts to a [const][volatile] void *
5866 and vice versa; otherwise, targets must be the same.
5867 Meanwhile, the lhs target must have all the qualifiers of
5868 the rhs. */
5869 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5870 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5871 || comp_target_types (location, memb_type, rhstype))
5873 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5874 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5875 /* If this type won't generate any warnings, use it. */
5876 if (lquals == rquals
5877 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5878 && TREE_CODE (ttl) == FUNCTION_TYPE)
5879 ? ((lquals | rquals) == rquals)
5880 : ((lquals | rquals) == lquals)))
5881 break;
5883 /* Keep looking for a better type, but remember this one. */
5884 if (!marginal_memb)
5885 marginal_memb = memb;
5889 /* Can convert integer zero to any pointer type. */
5890 if (null_pointer_constant)
5892 rhs = null_pointer_node;
5893 break;
5897 if (memb || marginal_memb)
5899 if (!memb)
5901 /* We have only a marginally acceptable member type;
5902 it needs a warning. */
5903 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5904 tree ttr = TREE_TYPE (rhstype);
5906 /* Const and volatile mean something different for function
5907 types, so the usual warnings are not appropriate. */
5908 if (TREE_CODE (ttr) == FUNCTION_TYPE
5909 && TREE_CODE (ttl) == FUNCTION_TYPE)
5911 /* Because const and volatile on functions are
5912 restrictions that say the function will not do
5913 certain things, it is okay to use a const or volatile
5914 function where an ordinary one is wanted, but not
5915 vice-versa. */
5916 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5917 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5918 WARN_FOR_QUALIFIERS (location, expr_loc,
5919 OPT_Wdiscarded_qualifiers,
5920 G_("passing argument %d of %qE "
5921 "makes %q#v qualified function "
5922 "pointer from unqualified"),
5923 G_("assignment makes %q#v qualified "
5924 "function pointer from "
5925 "unqualified"),
5926 G_("initialization makes %q#v qualified "
5927 "function pointer from "
5928 "unqualified"),
5929 G_("return makes %q#v qualified function "
5930 "pointer from unqualified"),
5931 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5933 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5934 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5935 WARN_FOR_QUALIFIERS (location, expr_loc,
5936 OPT_Wdiscarded_qualifiers,
5937 G_("passing argument %d of %qE discards "
5938 "%qv qualifier from pointer target type"),
5939 G_("assignment discards %qv qualifier "
5940 "from pointer target type"),
5941 G_("initialization discards %qv qualifier "
5942 "from pointer target type"),
5943 G_("return discards %qv qualifier from "
5944 "pointer target type"),
5945 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5947 memb = marginal_memb;
5950 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5951 pedwarn (location, OPT_Wpedantic,
5952 "ISO C prohibits argument conversion to union type");
5954 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5955 return build_constructor_single (type, memb, rhs);
5959 /* Conversions among pointers */
5960 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5961 && (coder == codel))
5963 tree ttl = TREE_TYPE (type);
5964 tree ttr = TREE_TYPE (rhstype);
5965 tree mvl = ttl;
5966 tree mvr = ttr;
5967 bool is_opaque_pointer;
5968 int target_cmp = 0; /* Cache comp_target_types () result. */
5969 addr_space_t asl;
5970 addr_space_t asr;
5972 if (TREE_CODE (mvl) != ARRAY_TYPE)
5973 mvl = (TYPE_ATOMIC (mvl)
5974 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5975 TYPE_QUAL_ATOMIC)
5976 : TYPE_MAIN_VARIANT (mvl));
5977 if (TREE_CODE (mvr) != ARRAY_TYPE)
5978 mvr = (TYPE_ATOMIC (mvr)
5979 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5980 TYPE_QUAL_ATOMIC)
5981 : TYPE_MAIN_VARIANT (mvr));
5982 /* Opaque pointers are treated like void pointers. */
5983 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5985 /* The Plan 9 compiler permits a pointer to a struct to be
5986 automatically converted into a pointer to an anonymous field
5987 within the struct. */
5988 if (flag_plan9_extensions
5989 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5990 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5991 && mvl != mvr)
5993 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5994 if (new_rhs != NULL_TREE)
5996 rhs = new_rhs;
5997 rhstype = TREE_TYPE (rhs);
5998 coder = TREE_CODE (rhstype);
5999 ttr = TREE_TYPE (rhstype);
6000 mvr = TYPE_MAIN_VARIANT (ttr);
6004 /* C++ does not allow the implicit conversion void* -> T*. However,
6005 for the purpose of reducing the number of false positives, we
6006 tolerate the special case of
6008 int *p = NULL;
6010 where NULL is typically defined in C to be '(void *) 0'. */
6011 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6012 warning_at (errtype == ic_argpass ? expr_loc : location,
6013 OPT_Wc___compat,
6014 "request for implicit conversion "
6015 "from %qT to %qT not permitted in C++", rhstype, type);
6017 /* See if the pointers point to incompatible address spaces. */
6018 asl = TYPE_ADDR_SPACE (ttl);
6019 asr = TYPE_ADDR_SPACE (ttr);
6020 if (!null_pointer_constant_p (rhs)
6021 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6023 switch (errtype)
6025 case ic_argpass:
6026 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6027 "non-enclosed address space", parmnum, rname);
6028 break;
6029 case ic_assign:
6030 error_at (location, "assignment from pointer to "
6031 "non-enclosed address space");
6032 break;
6033 case ic_init:
6034 error_at (location, "initialization from pointer to "
6035 "non-enclosed address space");
6036 break;
6037 case ic_return:
6038 error_at (location, "return from pointer to "
6039 "non-enclosed address space");
6040 break;
6041 default:
6042 gcc_unreachable ();
6044 return error_mark_node;
6047 /* Check if the right-hand side has a format attribute but the
6048 left-hand side doesn't. */
6049 if (warn_suggest_attribute_format
6050 && check_missing_format_attribute (type, rhstype))
6052 switch (errtype)
6054 case ic_argpass:
6055 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6056 "argument %d of %qE might be "
6057 "a candidate for a format attribute",
6058 parmnum, rname);
6059 break;
6060 case ic_assign:
6061 warning_at (location, OPT_Wsuggest_attribute_format,
6062 "assignment left-hand side might be "
6063 "a candidate for a format attribute");
6064 break;
6065 case ic_init:
6066 warning_at (location, OPT_Wsuggest_attribute_format,
6067 "initialization left-hand side might be "
6068 "a candidate for a format attribute");
6069 break;
6070 case ic_return:
6071 warning_at (location, OPT_Wsuggest_attribute_format,
6072 "return type might be "
6073 "a candidate for a format attribute");
6074 break;
6075 default:
6076 gcc_unreachable ();
6080 /* Any non-function converts to a [const][volatile] void *
6081 and vice versa; otherwise, targets must be the same.
6082 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6083 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6084 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6085 || (target_cmp = comp_target_types (location, type, rhstype))
6086 || is_opaque_pointer
6087 || ((c_common_unsigned_type (mvl)
6088 == c_common_unsigned_type (mvr))
6089 && (c_common_signed_type (mvl)
6090 == c_common_signed_type (mvr))
6091 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6093 if (pedantic
6094 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6096 (VOID_TYPE_P (ttr)
6097 && !null_pointer_constant
6098 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6099 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6100 G_("ISO C forbids passing argument %d of "
6101 "%qE between function pointer "
6102 "and %<void *%>"),
6103 G_("ISO C forbids assignment between "
6104 "function pointer and %<void *%>"),
6105 G_("ISO C forbids initialization between "
6106 "function pointer and %<void *%>"),
6107 G_("ISO C forbids return between function "
6108 "pointer and %<void *%>"));
6109 /* Const and volatile mean something different for function types,
6110 so the usual warnings are not appropriate. */
6111 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6112 && TREE_CODE (ttl) != FUNCTION_TYPE)
6114 /* Assignments between atomic and non-atomic objects are OK. */
6115 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6116 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6118 WARN_FOR_QUALIFIERS (location, expr_loc,
6119 OPT_Wdiscarded_qualifiers,
6120 G_("passing argument %d of %qE discards "
6121 "%qv qualifier from pointer target type"),
6122 G_("assignment discards %qv qualifier "
6123 "from pointer target type"),
6124 G_("initialization discards %qv qualifier "
6125 "from pointer target type"),
6126 G_("return discards %qv qualifier from "
6127 "pointer target type"),
6128 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6130 /* If this is not a case of ignoring a mismatch in signedness,
6131 no warning. */
6132 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6133 || target_cmp)
6135 /* If there is a mismatch, do warn. */
6136 else if (warn_pointer_sign)
6137 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6138 G_("pointer targets in passing argument "
6139 "%d of %qE differ in signedness"),
6140 G_("pointer targets in assignment "
6141 "differ in signedness"),
6142 G_("pointer targets in initialization "
6143 "differ in signedness"),
6144 G_("pointer targets in return differ "
6145 "in signedness"));
6147 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6148 && TREE_CODE (ttr) == FUNCTION_TYPE)
6150 /* Because const and volatile on functions are restrictions
6151 that say the function will not do certain things,
6152 it is okay to use a const or volatile function
6153 where an ordinary one is wanted, but not vice-versa. */
6154 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6155 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6156 WARN_FOR_QUALIFIERS (location, expr_loc,
6157 OPT_Wdiscarded_qualifiers,
6158 G_("passing argument %d of %qE makes "
6159 "%q#v qualified function pointer "
6160 "from unqualified"),
6161 G_("assignment makes %q#v qualified function "
6162 "pointer from unqualified"),
6163 G_("initialization makes %q#v qualified "
6164 "function pointer from unqualified"),
6165 G_("return makes %q#v qualified function "
6166 "pointer from unqualified"),
6167 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6170 else
6171 /* Avoid warning about the volatile ObjC EH puts on decls. */
6172 if (!objc_ok)
6173 WARN_FOR_ASSIGNMENT (location, expr_loc,
6174 OPT_Wincompatible_pointer_types,
6175 G_("passing argument %d of %qE from "
6176 "incompatible pointer type"),
6177 G_("assignment from incompatible pointer type"),
6178 G_("initialization from incompatible "
6179 "pointer type"),
6180 G_("return from incompatible pointer type"));
6182 return convert (type, rhs);
6184 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6186 /* ??? This should not be an error when inlining calls to
6187 unprototyped functions. */
6188 error_at (location, "invalid use of non-lvalue array");
6189 return error_mark_node;
6191 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6193 /* An explicit constant 0 can convert to a pointer,
6194 or one that results from arithmetic, even including
6195 a cast to integer type. */
6196 if (!null_pointer_constant)
6197 WARN_FOR_ASSIGNMENT (location, expr_loc,
6198 OPT_Wint_conversion,
6199 G_("passing argument %d of %qE makes "
6200 "pointer from integer without a cast"),
6201 G_("assignment makes pointer from integer "
6202 "without a cast"),
6203 G_("initialization makes pointer from "
6204 "integer without a cast"),
6205 G_("return makes pointer from integer "
6206 "without a cast"));
6208 return convert (type, rhs);
6210 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6212 WARN_FOR_ASSIGNMENT (location, expr_loc,
6213 OPT_Wint_conversion,
6214 G_("passing argument %d of %qE makes integer "
6215 "from pointer without a cast"),
6216 G_("assignment makes integer from pointer "
6217 "without a cast"),
6218 G_("initialization makes integer from pointer "
6219 "without a cast"),
6220 G_("return makes integer from pointer "
6221 "without a cast"));
6222 return convert (type, rhs);
6224 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6226 tree ret;
6227 bool save = in_late_binary_op;
6228 in_late_binary_op = true;
6229 ret = convert (type, rhs);
6230 in_late_binary_op = save;
6231 return ret;
6234 switch (errtype)
6236 case ic_argpass:
6237 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6238 rname);
6239 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6240 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6241 "expected %qT but argument is of type %qT", type, rhstype);
6242 break;
6243 case ic_assign:
6244 error_at (location, "incompatible types when assigning to type %qT from "
6245 "type %qT", type, rhstype);
6246 break;
6247 case ic_init:
6248 error_at (location,
6249 "incompatible types when initializing type %qT using type %qT",
6250 type, rhstype);
6251 break;
6252 case ic_return:
6253 error_at (location,
6254 "incompatible types when returning type %qT but %qT was "
6255 "expected", rhstype, type);
6256 break;
6257 default:
6258 gcc_unreachable ();
6261 return error_mark_node;
6264 /* If VALUE is a compound expr all of whose expressions are constant, then
6265 return its value. Otherwise, return error_mark_node.
6267 This is for handling COMPOUND_EXPRs as initializer elements
6268 which is allowed with a warning when -pedantic is specified. */
6270 static tree
6271 valid_compound_expr_initializer (tree value, tree endtype)
6273 if (TREE_CODE (value) == COMPOUND_EXPR)
6275 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6276 == error_mark_node)
6277 return error_mark_node;
6278 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6279 endtype);
6281 else if (!initializer_constant_valid_p (value, endtype))
6282 return error_mark_node;
6283 else
6284 return value;
6287 /* Perform appropriate conversions on the initial value of a variable,
6288 store it in the declaration DECL,
6289 and print any error messages that are appropriate.
6290 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6291 If the init is invalid, store an ERROR_MARK.
6293 INIT_LOC is the location of the initial value. */
6295 void
6296 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6298 tree value, type;
6299 bool npc = false;
6301 /* If variable's type was invalidly declared, just ignore it. */
6303 type = TREE_TYPE (decl);
6304 if (TREE_CODE (type) == ERROR_MARK)
6305 return;
6307 /* Digest the specified initializer into an expression. */
6309 if (init)
6310 npc = null_pointer_constant_p (init);
6311 value = digest_init (init_loc, type, init, origtype, npc,
6312 true, TREE_STATIC (decl));
6314 /* Store the expression if valid; else report error. */
6316 if (!in_system_header_at (input_location)
6317 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6318 warning (OPT_Wtraditional, "traditional C rejects automatic "
6319 "aggregate initialization");
6321 DECL_INITIAL (decl) = value;
6323 /* ANSI wants warnings about out-of-range constant initializers. */
6324 STRIP_TYPE_NOPS (value);
6325 if (TREE_STATIC (decl))
6326 constant_expression_warning (value);
6328 /* Check if we need to set array size from compound literal size. */
6329 if (TREE_CODE (type) == ARRAY_TYPE
6330 && TYPE_DOMAIN (type) == 0
6331 && value != error_mark_node)
6333 tree inside_init = init;
6335 STRIP_TYPE_NOPS (inside_init);
6336 inside_init = fold (inside_init);
6338 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6340 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6342 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6344 /* For int foo[] = (int [3]){1}; we need to set array size
6345 now since later on array initializer will be just the
6346 brace enclosed list of the compound literal. */
6347 tree etype = strip_array_types (TREE_TYPE (decl));
6348 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6349 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6350 layout_type (type);
6351 layout_decl (cldecl, 0);
6352 TREE_TYPE (decl)
6353 = c_build_qualified_type (type, TYPE_QUALS (etype));
6359 /* Methods for storing and printing names for error messages. */
6361 /* Implement a spelling stack that allows components of a name to be pushed
6362 and popped. Each element on the stack is this structure. */
6364 struct spelling
6366 int kind;
6367 union
6369 unsigned HOST_WIDE_INT i;
6370 const char *s;
6371 } u;
6374 #define SPELLING_STRING 1
6375 #define SPELLING_MEMBER 2
6376 #define SPELLING_BOUNDS 3
6378 static struct spelling *spelling; /* Next stack element (unused). */
6379 static struct spelling *spelling_base; /* Spelling stack base. */
6380 static int spelling_size; /* Size of the spelling stack. */
6382 /* Macros to save and restore the spelling stack around push_... functions.
6383 Alternative to SAVE_SPELLING_STACK. */
6385 #define SPELLING_DEPTH() (spelling - spelling_base)
6386 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6388 /* Push an element on the spelling stack with type KIND and assign VALUE
6389 to MEMBER. */
6391 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6393 int depth = SPELLING_DEPTH (); \
6395 if (depth >= spelling_size) \
6397 spelling_size += 10; \
6398 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6399 spelling_size); \
6400 RESTORE_SPELLING_DEPTH (depth); \
6403 spelling->kind = (KIND); \
6404 spelling->MEMBER = (VALUE); \
6405 spelling++; \
6408 /* Push STRING on the stack. Printed literally. */
6410 static void
6411 push_string (const char *string)
6413 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6416 /* Push a member name on the stack. Printed as '.' STRING. */
6418 static void
6419 push_member_name (tree decl)
6421 const char *const string
6422 = (DECL_NAME (decl)
6423 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6424 : _("<anonymous>"));
6425 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6428 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6430 static void
6431 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6433 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6436 /* Compute the maximum size in bytes of the printed spelling. */
6438 static int
6439 spelling_length (void)
6441 int size = 0;
6442 struct spelling *p;
6444 for (p = spelling_base; p < spelling; p++)
6446 if (p->kind == SPELLING_BOUNDS)
6447 size += 25;
6448 else
6449 size += strlen (p->u.s) + 1;
6452 return size;
6455 /* Print the spelling to BUFFER and return it. */
6457 static char *
6458 print_spelling (char *buffer)
6460 char *d = buffer;
6461 struct spelling *p;
6463 for (p = spelling_base; p < spelling; p++)
6464 if (p->kind == SPELLING_BOUNDS)
6466 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6467 d += strlen (d);
6469 else
6471 const char *s;
6472 if (p->kind == SPELLING_MEMBER)
6473 *d++ = '.';
6474 for (s = p->u.s; (*d = *s++); d++)
6477 *d++ = '\0';
6478 return buffer;
6481 /* Digest the parser output INIT as an initializer for type TYPE.
6482 Return a C expression of type TYPE to represent the initial value.
6484 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6486 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6488 If INIT is a string constant, STRICT_STRING is true if it is
6489 unparenthesized or we should not warn here for it being parenthesized.
6490 For other types of INIT, STRICT_STRING is not used.
6492 INIT_LOC is the location of the INIT.
6494 REQUIRE_CONSTANT requests an error if non-constant initializers or
6495 elements are seen. */
6497 static tree
6498 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6499 bool null_pointer_constant, bool strict_string,
6500 int require_constant)
6502 enum tree_code code = TREE_CODE (type);
6503 tree inside_init = init;
6504 tree semantic_type = NULL_TREE;
6505 bool maybe_const = true;
6507 if (type == error_mark_node
6508 || !init
6509 || error_operand_p (init))
6510 return error_mark_node;
6512 STRIP_TYPE_NOPS (inside_init);
6514 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6516 semantic_type = TREE_TYPE (inside_init);
6517 inside_init = TREE_OPERAND (inside_init, 0);
6519 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6520 inside_init = decl_constant_value_for_optimization (inside_init);
6522 /* Initialization of an array of chars from a string constant
6523 optionally enclosed in braces. */
6525 if (code == ARRAY_TYPE && inside_init
6526 && TREE_CODE (inside_init) == STRING_CST)
6528 tree typ1
6529 = (TYPE_ATOMIC (TREE_TYPE (type))
6530 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6531 TYPE_QUAL_ATOMIC)
6532 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6533 /* Note that an array could be both an array of character type
6534 and an array of wchar_t if wchar_t is signed char or unsigned
6535 char. */
6536 bool char_array = (typ1 == char_type_node
6537 || typ1 == signed_char_type_node
6538 || typ1 == unsigned_char_type_node);
6539 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6540 bool char16_array = !!comptypes (typ1, char16_type_node);
6541 bool char32_array = !!comptypes (typ1, char32_type_node);
6543 if (char_array || wchar_array || char16_array || char32_array)
6545 struct c_expr expr;
6546 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6547 expr.value = inside_init;
6548 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6549 expr.original_type = NULL;
6550 maybe_warn_string_init (init_loc, type, expr);
6552 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6553 pedwarn_init (init_loc, OPT_Wpedantic,
6554 "initialization of a flexible array member");
6556 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6557 TYPE_MAIN_VARIANT (type)))
6558 return inside_init;
6560 if (char_array)
6562 if (typ2 != char_type_node)
6564 error_init (init_loc, "char-array initialized from wide "
6565 "string");
6566 return error_mark_node;
6569 else
6571 if (typ2 == char_type_node)
6573 error_init (init_loc, "wide character array initialized "
6574 "from non-wide string");
6575 return error_mark_node;
6577 else if (!comptypes(typ1, typ2))
6579 error_init (init_loc, "wide character array initialized "
6580 "from incompatible wide string");
6581 return error_mark_node;
6585 TREE_TYPE (inside_init) = type;
6586 if (TYPE_DOMAIN (type) != 0
6587 && TYPE_SIZE (type) != 0
6588 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6590 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6592 /* Subtract the size of a single (possibly wide) character
6593 because it's ok to ignore the terminating null char
6594 that is counted in the length of the constant. */
6595 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6596 (len
6597 - (TYPE_PRECISION (typ1)
6598 / BITS_PER_UNIT))))
6599 pedwarn_init (init_loc, 0,
6600 ("initializer-string for array of chars "
6601 "is too long"));
6602 else if (warn_cxx_compat
6603 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6604 warning_at (init_loc, OPT_Wc___compat,
6605 ("initializer-string for array chars "
6606 "is too long for C++"));
6609 return inside_init;
6611 else if (INTEGRAL_TYPE_P (typ1))
6613 error_init (init_loc, "array of inappropriate type initialized "
6614 "from string constant");
6615 return error_mark_node;
6619 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6620 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6621 below and handle as a constructor. */
6622 if (code == VECTOR_TYPE
6623 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6624 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6625 && TREE_CONSTANT (inside_init))
6627 if (TREE_CODE (inside_init) == VECTOR_CST
6628 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6629 TYPE_MAIN_VARIANT (type)))
6630 return inside_init;
6632 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6634 unsigned HOST_WIDE_INT ix;
6635 tree value;
6636 bool constant_p = true;
6638 /* Iterate through elements and check if all constructor
6639 elements are *_CSTs. */
6640 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6641 if (!CONSTANT_CLASS_P (value))
6643 constant_p = false;
6644 break;
6647 if (constant_p)
6648 return build_vector_from_ctor (type,
6649 CONSTRUCTOR_ELTS (inside_init));
6653 if (warn_sequence_point)
6654 verify_sequence_points (inside_init);
6656 /* Any type can be initialized
6657 from an expression of the same type, optionally with braces. */
6659 if (inside_init && TREE_TYPE (inside_init) != 0
6660 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6661 TYPE_MAIN_VARIANT (type))
6662 || (code == ARRAY_TYPE
6663 && comptypes (TREE_TYPE (inside_init), type))
6664 || (code == VECTOR_TYPE
6665 && comptypes (TREE_TYPE (inside_init), type))
6666 || (code == POINTER_TYPE
6667 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6668 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6669 TREE_TYPE (type)))))
6671 if (code == POINTER_TYPE)
6673 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6675 if (TREE_CODE (inside_init) == STRING_CST
6676 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6677 inside_init = array_to_pointer_conversion
6678 (init_loc, inside_init);
6679 else
6681 error_init (init_loc, "invalid use of non-lvalue array");
6682 return error_mark_node;
6687 if (code == VECTOR_TYPE)
6688 /* Although the types are compatible, we may require a
6689 conversion. */
6690 inside_init = convert (type, inside_init);
6692 if (require_constant
6693 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6695 /* As an extension, allow initializing objects with static storage
6696 duration with compound literals (which are then treated just as
6697 the brace enclosed list they contain). Also allow this for
6698 vectors, as we can only assign them with compound literals. */
6699 if (flag_isoc99 && code != VECTOR_TYPE)
6700 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6701 "is not constant");
6702 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6703 inside_init = DECL_INITIAL (decl);
6706 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6707 && TREE_CODE (inside_init) != CONSTRUCTOR)
6709 error_init (init_loc, "array initialized from non-constant array "
6710 "expression");
6711 return error_mark_node;
6714 /* Compound expressions can only occur here if -Wpedantic or
6715 -pedantic-errors is specified. In the later case, we always want
6716 an error. In the former case, we simply want a warning. */
6717 if (require_constant && pedantic
6718 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6720 inside_init
6721 = valid_compound_expr_initializer (inside_init,
6722 TREE_TYPE (inside_init));
6723 if (inside_init == error_mark_node)
6724 error_init (init_loc, "initializer element is not constant");
6725 else
6726 pedwarn_init (init_loc, OPT_Wpedantic,
6727 "initializer element is not constant");
6728 if (flag_pedantic_errors)
6729 inside_init = error_mark_node;
6731 else if (require_constant
6732 && !initializer_constant_valid_p (inside_init,
6733 TREE_TYPE (inside_init)))
6735 error_init (init_loc, "initializer element is not constant");
6736 inside_init = error_mark_node;
6738 else if (require_constant && !maybe_const)
6739 pedwarn_init (init_loc, 0,
6740 "initializer element is not a constant expression");
6742 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6743 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6744 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6745 type, inside_init, origtype,
6746 ic_init, null_pointer_constant,
6747 NULL_TREE, NULL_TREE, 0);
6748 return inside_init;
6751 /* Handle scalar types, including conversions. */
6753 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6754 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6755 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6757 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6758 && (TREE_CODE (init) == STRING_CST
6759 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6760 inside_init = init = array_to_pointer_conversion (init_loc, init);
6761 if (semantic_type)
6762 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6763 inside_init);
6764 inside_init
6765 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6766 inside_init, origtype, ic_init,
6767 null_pointer_constant, NULL_TREE, NULL_TREE,
6770 /* Check to see if we have already given an error message. */
6771 if (inside_init == error_mark_node)
6773 else if (require_constant && !TREE_CONSTANT (inside_init))
6775 error_init (init_loc, "initializer element is not constant");
6776 inside_init = error_mark_node;
6778 else if (require_constant
6779 && !initializer_constant_valid_p (inside_init,
6780 TREE_TYPE (inside_init)))
6782 error_init (init_loc, "initializer element is not computable at "
6783 "load time");
6784 inside_init = error_mark_node;
6786 else if (require_constant && !maybe_const)
6787 pedwarn_init (init_loc, 0,
6788 "initializer element is not a constant expression");
6790 return inside_init;
6793 /* Come here only for records and arrays. */
6795 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6797 error_init (init_loc, "variable-sized object may not be initialized");
6798 return error_mark_node;
6801 error_init (init_loc, "invalid initializer");
6802 return error_mark_node;
6805 /* Handle initializers that use braces. */
6807 /* Type of object we are accumulating a constructor for.
6808 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6809 static tree constructor_type;
6811 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6812 left to fill. */
6813 static tree constructor_fields;
6815 /* For an ARRAY_TYPE, this is the specified index
6816 at which to store the next element we get. */
6817 static tree constructor_index;
6819 /* For an ARRAY_TYPE, this is the maximum index. */
6820 static tree constructor_max_index;
6822 /* For a RECORD_TYPE, this is the first field not yet written out. */
6823 static tree constructor_unfilled_fields;
6825 /* For an ARRAY_TYPE, this is the index of the first element
6826 not yet written out. */
6827 static tree constructor_unfilled_index;
6829 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6830 This is so we can generate gaps between fields, when appropriate. */
6831 static tree constructor_bit_index;
6833 /* If we are saving up the elements rather than allocating them,
6834 this is the list of elements so far (in reverse order,
6835 most recent first). */
6836 static vec<constructor_elt, va_gc> *constructor_elements;
6838 /* 1 if constructor should be incrementally stored into a constructor chain,
6839 0 if all the elements should be kept in AVL tree. */
6840 static int constructor_incremental;
6842 /* 1 if so far this constructor's elements are all compile-time constants. */
6843 static int constructor_constant;
6845 /* 1 if so far this constructor's elements are all valid address constants. */
6846 static int constructor_simple;
6848 /* 1 if this constructor has an element that cannot be part of a
6849 constant expression. */
6850 static int constructor_nonconst;
6852 /* 1 if this constructor is erroneous so far. */
6853 static int constructor_erroneous;
6855 /* 1 if this constructor is the universal zero initializer { 0 }. */
6856 static int constructor_zeroinit;
6858 /* Structure for managing pending initializer elements, organized as an
6859 AVL tree. */
6861 struct init_node
6863 struct init_node *left, *right;
6864 struct init_node *parent;
6865 int balance;
6866 tree purpose;
6867 tree value;
6868 tree origtype;
6871 /* Tree of pending elements at this constructor level.
6872 These are elements encountered out of order
6873 which belong at places we haven't reached yet in actually
6874 writing the output.
6875 Will never hold tree nodes across GC runs. */
6876 static struct init_node *constructor_pending_elts;
6878 /* The SPELLING_DEPTH of this constructor. */
6879 static int constructor_depth;
6881 /* DECL node for which an initializer is being read.
6882 0 means we are reading a constructor expression
6883 such as (struct foo) {...}. */
6884 static tree constructor_decl;
6886 /* Nonzero if this is an initializer for a top-level decl. */
6887 static int constructor_top_level;
6889 /* Nonzero if there were any member designators in this initializer. */
6890 static int constructor_designated;
6892 /* Nesting depth of designator list. */
6893 static int designator_depth;
6895 /* Nonzero if there were diagnosed errors in this designator list. */
6896 static int designator_erroneous;
6899 /* This stack has a level for each implicit or explicit level of
6900 structuring in the initializer, including the outermost one. It
6901 saves the values of most of the variables above. */
6903 struct constructor_range_stack;
6905 struct constructor_stack
6907 struct constructor_stack *next;
6908 tree type;
6909 tree fields;
6910 tree index;
6911 tree max_index;
6912 tree unfilled_index;
6913 tree unfilled_fields;
6914 tree bit_index;
6915 vec<constructor_elt, va_gc> *elements;
6916 struct init_node *pending_elts;
6917 int offset;
6918 int depth;
6919 /* If value nonzero, this value should replace the entire
6920 constructor at this level. */
6921 struct c_expr replacement_value;
6922 struct constructor_range_stack *range_stack;
6923 char constant;
6924 char simple;
6925 char nonconst;
6926 char implicit;
6927 char erroneous;
6928 char outer;
6929 char incremental;
6930 char designated;
6931 int designator_depth;
6934 static struct constructor_stack *constructor_stack;
6936 /* This stack represents designators from some range designator up to
6937 the last designator in the list. */
6939 struct constructor_range_stack
6941 struct constructor_range_stack *next, *prev;
6942 struct constructor_stack *stack;
6943 tree range_start;
6944 tree index;
6945 tree range_end;
6946 tree fields;
6949 static struct constructor_range_stack *constructor_range_stack;
6951 /* This stack records separate initializers that are nested.
6952 Nested initializers can't happen in ANSI C, but GNU C allows them
6953 in cases like { ... (struct foo) { ... } ... }. */
6955 struct initializer_stack
6957 struct initializer_stack *next;
6958 tree decl;
6959 struct constructor_stack *constructor_stack;
6960 struct constructor_range_stack *constructor_range_stack;
6961 vec<constructor_elt, va_gc> *elements;
6962 struct spelling *spelling;
6963 struct spelling *spelling_base;
6964 int spelling_size;
6965 char top_level;
6966 char require_constant_value;
6967 char require_constant_elements;
6970 static struct initializer_stack *initializer_stack;
6972 /* Prepare to parse and output the initializer for variable DECL. */
6974 void
6975 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6977 const char *locus;
6978 struct initializer_stack *p = XNEW (struct initializer_stack);
6980 p->decl = constructor_decl;
6981 p->require_constant_value = require_constant_value;
6982 p->require_constant_elements = require_constant_elements;
6983 p->constructor_stack = constructor_stack;
6984 p->constructor_range_stack = constructor_range_stack;
6985 p->elements = constructor_elements;
6986 p->spelling = spelling;
6987 p->spelling_base = spelling_base;
6988 p->spelling_size = spelling_size;
6989 p->top_level = constructor_top_level;
6990 p->next = initializer_stack;
6991 initializer_stack = p;
6993 constructor_decl = decl;
6994 constructor_designated = 0;
6995 constructor_top_level = top_level;
6997 if (decl != 0 && decl != error_mark_node)
6999 require_constant_value = TREE_STATIC (decl);
7000 require_constant_elements
7001 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7002 /* For a scalar, you can always use any value to initialize,
7003 even within braces. */
7004 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7005 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7006 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7007 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7008 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7010 else
7012 require_constant_value = 0;
7013 require_constant_elements = 0;
7014 locus = _("(anonymous)");
7017 constructor_stack = 0;
7018 constructor_range_stack = 0;
7020 found_missing_braces = 0;
7022 spelling_base = 0;
7023 spelling_size = 0;
7024 RESTORE_SPELLING_DEPTH (0);
7026 if (locus)
7027 push_string (locus);
7030 void
7031 finish_init (void)
7033 struct initializer_stack *p = initializer_stack;
7035 /* Free the whole constructor stack of this initializer. */
7036 while (constructor_stack)
7038 struct constructor_stack *q = constructor_stack;
7039 constructor_stack = q->next;
7040 free (q);
7043 gcc_assert (!constructor_range_stack);
7045 /* Pop back to the data of the outer initializer (if any). */
7046 free (spelling_base);
7048 constructor_decl = p->decl;
7049 require_constant_value = p->require_constant_value;
7050 require_constant_elements = p->require_constant_elements;
7051 constructor_stack = p->constructor_stack;
7052 constructor_range_stack = p->constructor_range_stack;
7053 constructor_elements = p->elements;
7054 spelling = p->spelling;
7055 spelling_base = p->spelling_base;
7056 spelling_size = p->spelling_size;
7057 constructor_top_level = p->top_level;
7058 initializer_stack = p->next;
7059 free (p);
7062 /* Call here when we see the initializer is surrounded by braces.
7063 This is instead of a call to push_init_level;
7064 it is matched by a call to pop_init_level.
7066 TYPE is the type to initialize, for a constructor expression.
7067 For an initializer for a decl, TYPE is zero. */
7069 void
7070 really_start_incremental_init (tree type)
7072 struct constructor_stack *p = XNEW (struct constructor_stack);
7074 if (type == 0)
7075 type = TREE_TYPE (constructor_decl);
7077 if (TREE_CODE (type) == VECTOR_TYPE
7078 && TYPE_VECTOR_OPAQUE (type))
7079 error ("opaque vector types cannot be initialized");
7081 p->type = constructor_type;
7082 p->fields = constructor_fields;
7083 p->index = constructor_index;
7084 p->max_index = constructor_max_index;
7085 p->unfilled_index = constructor_unfilled_index;
7086 p->unfilled_fields = constructor_unfilled_fields;
7087 p->bit_index = constructor_bit_index;
7088 p->elements = constructor_elements;
7089 p->constant = constructor_constant;
7090 p->simple = constructor_simple;
7091 p->nonconst = constructor_nonconst;
7092 p->erroneous = constructor_erroneous;
7093 p->pending_elts = constructor_pending_elts;
7094 p->depth = constructor_depth;
7095 p->replacement_value.value = 0;
7096 p->replacement_value.original_code = ERROR_MARK;
7097 p->replacement_value.original_type = NULL;
7098 p->implicit = 0;
7099 p->range_stack = 0;
7100 p->outer = 0;
7101 p->incremental = constructor_incremental;
7102 p->designated = constructor_designated;
7103 p->designator_depth = designator_depth;
7104 p->next = 0;
7105 constructor_stack = p;
7107 constructor_constant = 1;
7108 constructor_simple = 1;
7109 constructor_nonconst = 0;
7110 constructor_depth = SPELLING_DEPTH ();
7111 constructor_elements = NULL;
7112 constructor_pending_elts = 0;
7113 constructor_type = type;
7114 constructor_incremental = 1;
7115 constructor_designated = 0;
7116 constructor_zeroinit = 1;
7117 designator_depth = 0;
7118 designator_erroneous = 0;
7120 if (TREE_CODE (constructor_type) == RECORD_TYPE
7121 || TREE_CODE (constructor_type) == UNION_TYPE)
7123 constructor_fields = TYPE_FIELDS (constructor_type);
7124 /* Skip any nameless bit fields at the beginning. */
7125 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7126 && DECL_NAME (constructor_fields) == 0)
7127 constructor_fields = DECL_CHAIN (constructor_fields);
7129 constructor_unfilled_fields = constructor_fields;
7130 constructor_bit_index = bitsize_zero_node;
7132 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7134 if (TYPE_DOMAIN (constructor_type))
7136 constructor_max_index
7137 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7139 /* Detect non-empty initializations of zero-length arrays. */
7140 if (constructor_max_index == NULL_TREE
7141 && TYPE_SIZE (constructor_type))
7142 constructor_max_index = integer_minus_one_node;
7144 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7145 to initialize VLAs will cause a proper error; avoid tree
7146 checking errors as well by setting a safe value. */
7147 if (constructor_max_index
7148 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7149 constructor_max_index = integer_minus_one_node;
7151 constructor_index
7152 = convert (bitsizetype,
7153 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7155 else
7157 constructor_index = bitsize_zero_node;
7158 constructor_max_index = NULL_TREE;
7161 constructor_unfilled_index = constructor_index;
7163 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7165 /* Vectors are like simple fixed-size arrays. */
7166 constructor_max_index =
7167 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7168 constructor_index = bitsize_zero_node;
7169 constructor_unfilled_index = constructor_index;
7171 else
7173 /* Handle the case of int x = {5}; */
7174 constructor_fields = constructor_type;
7175 constructor_unfilled_fields = constructor_type;
7179 /* Push down into a subobject, for initialization.
7180 If this is for an explicit set of braces, IMPLICIT is 0.
7181 If it is because the next element belongs at a lower level,
7182 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7184 void
7185 push_init_level (location_t loc, int implicit,
7186 struct obstack *braced_init_obstack)
7188 struct constructor_stack *p;
7189 tree value = NULL_TREE;
7191 /* If we've exhausted any levels that didn't have braces,
7192 pop them now. If implicit == 1, this will have been done in
7193 process_init_element; do not repeat it here because in the case
7194 of excess initializers for an empty aggregate this leads to an
7195 infinite cycle of popping a level and immediately recreating
7196 it. */
7197 if (implicit != 1)
7199 while (constructor_stack->implicit)
7201 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7202 || TREE_CODE (constructor_type) == UNION_TYPE)
7203 && constructor_fields == 0)
7204 process_init_element (input_location,
7205 pop_init_level (loc, 1, braced_init_obstack),
7206 true, braced_init_obstack);
7207 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7208 && constructor_max_index
7209 && tree_int_cst_lt (constructor_max_index,
7210 constructor_index))
7211 process_init_element (input_location,
7212 pop_init_level (loc, 1, braced_init_obstack),
7213 true, braced_init_obstack);
7214 else
7215 break;
7219 /* Unless this is an explicit brace, we need to preserve previous
7220 content if any. */
7221 if (implicit)
7223 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7224 || TREE_CODE (constructor_type) == UNION_TYPE)
7225 && constructor_fields)
7226 value = find_init_member (constructor_fields, braced_init_obstack);
7227 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7228 value = find_init_member (constructor_index, braced_init_obstack);
7231 p = XNEW (struct constructor_stack);
7232 p->type = constructor_type;
7233 p->fields = constructor_fields;
7234 p->index = constructor_index;
7235 p->max_index = constructor_max_index;
7236 p->unfilled_index = constructor_unfilled_index;
7237 p->unfilled_fields = constructor_unfilled_fields;
7238 p->bit_index = constructor_bit_index;
7239 p->elements = constructor_elements;
7240 p->constant = constructor_constant;
7241 p->simple = constructor_simple;
7242 p->nonconst = constructor_nonconst;
7243 p->erroneous = constructor_erroneous;
7244 p->pending_elts = constructor_pending_elts;
7245 p->depth = constructor_depth;
7246 p->replacement_value.value = 0;
7247 p->replacement_value.original_code = ERROR_MARK;
7248 p->replacement_value.original_type = NULL;
7249 p->implicit = implicit;
7250 p->outer = 0;
7251 p->incremental = constructor_incremental;
7252 p->designated = constructor_designated;
7253 p->designator_depth = designator_depth;
7254 p->next = constructor_stack;
7255 p->range_stack = 0;
7256 constructor_stack = p;
7258 constructor_constant = 1;
7259 constructor_simple = 1;
7260 constructor_nonconst = 0;
7261 constructor_depth = SPELLING_DEPTH ();
7262 constructor_elements = NULL;
7263 constructor_incremental = 1;
7264 constructor_designated = 0;
7265 constructor_pending_elts = 0;
7266 if (!implicit)
7268 p->range_stack = constructor_range_stack;
7269 constructor_range_stack = 0;
7270 designator_depth = 0;
7271 designator_erroneous = 0;
7274 /* Don't die if an entire brace-pair level is superfluous
7275 in the containing level. */
7276 if (constructor_type == 0)
7278 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7279 || TREE_CODE (constructor_type) == UNION_TYPE)
7281 /* Don't die if there are extra init elts at the end. */
7282 if (constructor_fields == 0)
7283 constructor_type = 0;
7284 else
7286 constructor_type = TREE_TYPE (constructor_fields);
7287 push_member_name (constructor_fields);
7288 constructor_depth++;
7290 /* If upper initializer is designated, then mark this as
7291 designated too to prevent bogus warnings. */
7292 constructor_designated = p->designated;
7294 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7296 constructor_type = TREE_TYPE (constructor_type);
7297 push_array_bounds (tree_to_uhwi (constructor_index));
7298 constructor_depth++;
7301 if (constructor_type == 0)
7303 error_init (loc, "extra brace group at end of initializer");
7304 constructor_fields = 0;
7305 constructor_unfilled_fields = 0;
7306 return;
7309 if (value && TREE_CODE (value) == CONSTRUCTOR)
7311 constructor_constant = TREE_CONSTANT (value);
7312 constructor_simple = TREE_STATIC (value);
7313 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7314 constructor_elements = CONSTRUCTOR_ELTS (value);
7315 if (!vec_safe_is_empty (constructor_elements)
7316 && (TREE_CODE (constructor_type) == RECORD_TYPE
7317 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7318 set_nonincremental_init (braced_init_obstack);
7321 if (implicit == 1)
7322 found_missing_braces = 1;
7324 if (TREE_CODE (constructor_type) == RECORD_TYPE
7325 || TREE_CODE (constructor_type) == UNION_TYPE)
7327 constructor_fields = TYPE_FIELDS (constructor_type);
7328 /* Skip any nameless bit fields at the beginning. */
7329 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7330 && DECL_NAME (constructor_fields) == 0)
7331 constructor_fields = DECL_CHAIN (constructor_fields);
7333 constructor_unfilled_fields = constructor_fields;
7334 constructor_bit_index = bitsize_zero_node;
7336 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7338 /* Vectors are like simple fixed-size arrays. */
7339 constructor_max_index =
7340 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7341 constructor_index = bitsize_int (0);
7342 constructor_unfilled_index = constructor_index;
7344 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7346 if (TYPE_DOMAIN (constructor_type))
7348 constructor_max_index
7349 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7351 /* Detect non-empty initializations of zero-length arrays. */
7352 if (constructor_max_index == NULL_TREE
7353 && TYPE_SIZE (constructor_type))
7354 constructor_max_index = integer_minus_one_node;
7356 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7357 to initialize VLAs will cause a proper error; avoid tree
7358 checking errors as well by setting a safe value. */
7359 if (constructor_max_index
7360 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7361 constructor_max_index = integer_minus_one_node;
7363 constructor_index
7364 = convert (bitsizetype,
7365 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7367 else
7368 constructor_index = bitsize_zero_node;
7370 constructor_unfilled_index = constructor_index;
7371 if (value && TREE_CODE (value) == STRING_CST)
7373 /* We need to split the char/wchar array into individual
7374 characters, so that we don't have to special case it
7375 everywhere. */
7376 set_nonincremental_init_from_string (value, braced_init_obstack);
7379 else
7381 if (constructor_type != error_mark_node)
7382 warning_init (input_location, 0, "braces around scalar initializer");
7383 constructor_fields = constructor_type;
7384 constructor_unfilled_fields = constructor_type;
7388 /* At the end of an implicit or explicit brace level,
7389 finish up that level of constructor. If a single expression
7390 with redundant braces initialized that level, return the
7391 c_expr structure for that expression. Otherwise, the original_code
7392 element is set to ERROR_MARK.
7393 If we were outputting the elements as they are read, return 0 as the value
7394 from inner levels (process_init_element ignores that),
7395 but return error_mark_node as the value from the outermost level
7396 (that's what we want to put in DECL_INITIAL).
7397 Otherwise, return a CONSTRUCTOR expression as the value. */
7399 struct c_expr
7400 pop_init_level (location_t loc, int implicit,
7401 struct obstack *braced_init_obstack)
7403 struct constructor_stack *p;
7404 struct c_expr ret;
7405 ret.value = 0;
7406 ret.original_code = ERROR_MARK;
7407 ret.original_type = NULL;
7409 if (implicit == 0)
7411 /* When we come to an explicit close brace,
7412 pop any inner levels that didn't have explicit braces. */
7413 while (constructor_stack->implicit)
7414 process_init_element (input_location,
7415 pop_init_level (loc, 1, braced_init_obstack),
7416 true, braced_init_obstack);
7417 gcc_assert (!constructor_range_stack);
7420 /* Now output all pending elements. */
7421 constructor_incremental = 1;
7422 output_pending_init_elements (1, braced_init_obstack);
7424 p = constructor_stack;
7426 /* Error for initializing a flexible array member, or a zero-length
7427 array member in an inappropriate context. */
7428 if (constructor_type && constructor_fields
7429 && TREE_CODE (constructor_type) == ARRAY_TYPE
7430 && TYPE_DOMAIN (constructor_type)
7431 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7433 /* Silently discard empty initializations. The parser will
7434 already have pedwarned for empty brackets. */
7435 if (integer_zerop (constructor_unfilled_index))
7436 constructor_type = NULL_TREE;
7437 else
7439 gcc_assert (!TYPE_SIZE (constructor_type));
7441 if (constructor_depth > 2)
7442 error_init (loc, "initialization of flexible array member in a nested context");
7443 else
7444 pedwarn_init (loc, OPT_Wpedantic,
7445 "initialization of a flexible array member");
7447 /* We have already issued an error message for the existence
7448 of a flexible array member not at the end of the structure.
7449 Discard the initializer so that we do not die later. */
7450 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7451 constructor_type = NULL_TREE;
7455 /* Initialization with { } counts as zeroinit. */
7456 if (vec_safe_length (constructor_elements) == 0)
7457 constructor_zeroinit = 1;
7458 /* If the constructor has more than one element, it can't be { 0 }. */
7459 else if (vec_safe_length (constructor_elements) != 1)
7460 constructor_zeroinit = 0;
7462 /* Warn when some structs are initialized with direct aggregation. */
7463 if (!implicit && found_missing_braces && warn_missing_braces
7464 && !constructor_zeroinit)
7466 warning_init (loc, OPT_Wmissing_braces,
7467 "missing braces around initializer");
7470 /* Warn when some struct elements are implicitly initialized to zero. */
7471 if (warn_missing_field_initializers
7472 && constructor_type
7473 && TREE_CODE (constructor_type) == RECORD_TYPE
7474 && constructor_unfilled_fields)
7476 /* Do not warn for flexible array members or zero-length arrays. */
7477 while (constructor_unfilled_fields
7478 && (!DECL_SIZE (constructor_unfilled_fields)
7479 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7480 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7482 if (constructor_unfilled_fields
7483 /* Do not warn if this level of the initializer uses member
7484 designators; it is likely to be deliberate. */
7485 && !constructor_designated
7486 /* Do not warn about initializing with { 0 } or with { }. */
7487 && !constructor_zeroinit)
7489 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7490 "missing initializer for field %qD of %qT",
7491 constructor_unfilled_fields,
7492 constructor_type))
7493 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7494 "%qD declared here", constructor_unfilled_fields);
7498 /* Pad out the end of the structure. */
7499 if (p->replacement_value.value)
7500 /* If this closes a superfluous brace pair,
7501 just pass out the element between them. */
7502 ret = p->replacement_value;
7503 else if (constructor_type == 0)
7505 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7506 && TREE_CODE (constructor_type) != UNION_TYPE
7507 && TREE_CODE (constructor_type) != ARRAY_TYPE
7508 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7510 /* A nonincremental scalar initializer--just return
7511 the element, after verifying there is just one. */
7512 if (vec_safe_is_empty (constructor_elements))
7514 if (!constructor_erroneous)
7515 error_init (loc, "empty scalar initializer");
7516 ret.value = error_mark_node;
7518 else if (vec_safe_length (constructor_elements) != 1)
7520 error_init (loc, "extra elements in scalar initializer");
7521 ret.value = (*constructor_elements)[0].value;
7523 else
7524 ret.value = (*constructor_elements)[0].value;
7526 else
7528 if (constructor_erroneous)
7529 ret.value = error_mark_node;
7530 else
7532 ret.value = build_constructor (constructor_type,
7533 constructor_elements);
7534 if (constructor_constant)
7535 TREE_CONSTANT (ret.value) = 1;
7536 if (constructor_constant && constructor_simple)
7537 TREE_STATIC (ret.value) = 1;
7538 if (constructor_nonconst)
7539 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7543 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7545 if (constructor_nonconst)
7546 ret.original_code = C_MAYBE_CONST_EXPR;
7547 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7548 ret.original_code = ERROR_MARK;
7551 constructor_type = p->type;
7552 constructor_fields = p->fields;
7553 constructor_index = p->index;
7554 constructor_max_index = p->max_index;
7555 constructor_unfilled_index = p->unfilled_index;
7556 constructor_unfilled_fields = p->unfilled_fields;
7557 constructor_bit_index = p->bit_index;
7558 constructor_elements = p->elements;
7559 constructor_constant = p->constant;
7560 constructor_simple = p->simple;
7561 constructor_nonconst = p->nonconst;
7562 constructor_erroneous = p->erroneous;
7563 constructor_incremental = p->incremental;
7564 constructor_designated = p->designated;
7565 designator_depth = p->designator_depth;
7566 constructor_pending_elts = p->pending_elts;
7567 constructor_depth = p->depth;
7568 if (!p->implicit)
7569 constructor_range_stack = p->range_stack;
7570 RESTORE_SPELLING_DEPTH (constructor_depth);
7572 constructor_stack = p->next;
7573 free (p);
7575 if (ret.value == 0 && constructor_stack == 0)
7576 ret.value = error_mark_node;
7577 return ret;
7580 /* Common handling for both array range and field name designators.
7581 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7583 static int
7584 set_designator (location_t loc, int array,
7585 struct obstack *braced_init_obstack)
7587 tree subtype;
7588 enum tree_code subcode;
7590 /* Don't die if an entire brace-pair level is superfluous
7591 in the containing level. */
7592 if (constructor_type == 0)
7593 return 1;
7595 /* If there were errors in this designator list already, bail out
7596 silently. */
7597 if (designator_erroneous)
7598 return 1;
7600 if (!designator_depth)
7602 gcc_assert (!constructor_range_stack);
7604 /* Designator list starts at the level of closest explicit
7605 braces. */
7606 while (constructor_stack->implicit)
7607 process_init_element (input_location,
7608 pop_init_level (loc, 1, braced_init_obstack),
7609 true, braced_init_obstack);
7610 constructor_designated = 1;
7611 return 0;
7614 switch (TREE_CODE (constructor_type))
7616 case RECORD_TYPE:
7617 case UNION_TYPE:
7618 subtype = TREE_TYPE (constructor_fields);
7619 if (subtype != error_mark_node)
7620 subtype = TYPE_MAIN_VARIANT (subtype);
7621 break;
7622 case ARRAY_TYPE:
7623 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7624 break;
7625 default:
7626 gcc_unreachable ();
7629 subcode = TREE_CODE (subtype);
7630 if (array && subcode != ARRAY_TYPE)
7632 error_init (loc, "array index in non-array initializer");
7633 return 1;
7635 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7637 error_init (loc, "field name not in record or union initializer");
7638 return 1;
7641 constructor_designated = 1;
7642 push_init_level (loc, 2, braced_init_obstack);
7643 return 0;
7646 /* If there are range designators in designator list, push a new designator
7647 to constructor_range_stack. RANGE_END is end of such stack range or
7648 NULL_TREE if there is no range designator at this level. */
7650 static void
7651 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7653 struct constructor_range_stack *p;
7655 p = (struct constructor_range_stack *)
7656 obstack_alloc (braced_init_obstack,
7657 sizeof (struct constructor_range_stack));
7658 p->prev = constructor_range_stack;
7659 p->next = 0;
7660 p->fields = constructor_fields;
7661 p->range_start = constructor_index;
7662 p->index = constructor_index;
7663 p->stack = constructor_stack;
7664 p->range_end = range_end;
7665 if (constructor_range_stack)
7666 constructor_range_stack->next = p;
7667 constructor_range_stack = p;
7670 /* Within an array initializer, specify the next index to be initialized.
7671 FIRST is that index. If LAST is nonzero, then initialize a range
7672 of indices, running from FIRST through LAST. */
7674 void
7675 set_init_index (location_t loc, tree first, tree last,
7676 struct obstack *braced_init_obstack)
7678 if (set_designator (loc, 1, braced_init_obstack))
7679 return;
7681 designator_erroneous = 1;
7683 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7684 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7686 error_init (loc, "array index in initializer not of integer type");
7687 return;
7690 if (TREE_CODE (first) != INTEGER_CST)
7692 first = c_fully_fold (first, false, NULL);
7693 if (TREE_CODE (first) == INTEGER_CST)
7694 pedwarn_init (loc, OPT_Wpedantic,
7695 "array index in initializer is not "
7696 "an integer constant expression");
7699 if (last && TREE_CODE (last) != INTEGER_CST)
7701 last = c_fully_fold (last, false, NULL);
7702 if (TREE_CODE (last) == INTEGER_CST)
7703 pedwarn_init (loc, OPT_Wpedantic,
7704 "array index in initializer is not "
7705 "an integer constant expression");
7708 if (TREE_CODE (first) != INTEGER_CST)
7709 error_init (loc, "nonconstant array index in initializer");
7710 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7711 error_init (loc, "nonconstant array index in initializer");
7712 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7713 error_init (loc, "array index in non-array initializer");
7714 else if (tree_int_cst_sgn (first) == -1)
7715 error_init (loc, "array index in initializer exceeds array bounds");
7716 else if (constructor_max_index
7717 && tree_int_cst_lt (constructor_max_index, first))
7718 error_init (loc, "array index in initializer exceeds array bounds");
7719 else
7721 constant_expression_warning (first);
7722 if (last)
7723 constant_expression_warning (last);
7724 constructor_index = convert (bitsizetype, first);
7725 if (tree_int_cst_lt (constructor_index, first))
7727 constructor_index = copy_node (constructor_index);
7728 TREE_OVERFLOW (constructor_index) = 1;
7731 if (last)
7733 if (tree_int_cst_equal (first, last))
7734 last = 0;
7735 else if (tree_int_cst_lt (last, first))
7737 error_init (loc, "empty index range in initializer");
7738 last = 0;
7740 else
7742 last = convert (bitsizetype, last);
7743 if (constructor_max_index != 0
7744 && tree_int_cst_lt (constructor_max_index, last))
7746 error_init (loc, "array index range in initializer exceeds "
7747 "array bounds");
7748 last = 0;
7753 designator_depth++;
7754 designator_erroneous = 0;
7755 if (constructor_range_stack || last)
7756 push_range_stack (last, braced_init_obstack);
7760 /* Within a struct initializer, specify the next field to be initialized. */
7762 void
7763 set_init_label (location_t loc, tree fieldname,
7764 struct obstack *braced_init_obstack)
7766 tree field;
7768 if (set_designator (loc, 0, braced_init_obstack))
7769 return;
7771 designator_erroneous = 1;
7773 if (TREE_CODE (constructor_type) != RECORD_TYPE
7774 && TREE_CODE (constructor_type) != UNION_TYPE)
7776 error_init (loc, "field name not in record or union initializer");
7777 return;
7780 field = lookup_field (constructor_type, fieldname);
7782 if (field == 0)
7783 error ("unknown field %qE specified in initializer", fieldname);
7784 else
7787 constructor_fields = TREE_VALUE (field);
7788 designator_depth++;
7789 designator_erroneous = 0;
7790 if (constructor_range_stack)
7791 push_range_stack (NULL_TREE, braced_init_obstack);
7792 field = TREE_CHAIN (field);
7793 if (field)
7795 if (set_designator (loc, 0, braced_init_obstack))
7796 return;
7799 while (field != NULL_TREE);
7802 /* Add a new initializer to the tree of pending initializers. PURPOSE
7803 identifies the initializer, either array index or field in a structure.
7804 VALUE is the value of that index or field. If ORIGTYPE is not
7805 NULL_TREE, it is the original type of VALUE.
7807 IMPLICIT is true if value comes from pop_init_level (1),
7808 the new initializer has been merged with the existing one
7809 and thus no warnings should be emitted about overriding an
7810 existing initializer. */
7812 static void
7813 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7814 bool implicit, struct obstack *braced_init_obstack)
7816 struct init_node *p, **q, *r;
7818 q = &constructor_pending_elts;
7819 p = 0;
7821 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7823 while (*q != 0)
7825 p = *q;
7826 if (tree_int_cst_lt (purpose, p->purpose))
7827 q = &p->left;
7828 else if (tree_int_cst_lt (p->purpose, purpose))
7829 q = &p->right;
7830 else
7832 if (!implicit)
7834 if (TREE_SIDE_EFFECTS (p->value))
7835 warning_init (loc, 0,
7836 "initialized field with side-effects "
7837 "overwritten");
7838 else if (warn_override_init)
7839 warning_init (loc, OPT_Woverride_init,
7840 "initialized field overwritten");
7842 p->value = value;
7843 p->origtype = origtype;
7844 return;
7848 else
7850 tree bitpos;
7852 bitpos = bit_position (purpose);
7853 while (*q != NULL)
7855 p = *q;
7856 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7857 q = &p->left;
7858 else if (p->purpose != purpose)
7859 q = &p->right;
7860 else
7862 if (!implicit)
7864 if (TREE_SIDE_EFFECTS (p->value))
7865 warning_init (loc, 0,
7866 "initialized field with side-effects "
7867 "overwritten");
7868 else if (warn_override_init)
7869 warning_init (loc, OPT_Woverride_init,
7870 "initialized field overwritten");
7872 p->value = value;
7873 p->origtype = origtype;
7874 return;
7879 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7880 sizeof (struct init_node));
7881 r->purpose = purpose;
7882 r->value = value;
7883 r->origtype = origtype;
7885 *q = r;
7886 r->parent = p;
7887 r->left = 0;
7888 r->right = 0;
7889 r->balance = 0;
7891 while (p)
7893 struct init_node *s;
7895 if (r == p->left)
7897 if (p->balance == 0)
7898 p->balance = -1;
7899 else if (p->balance < 0)
7901 if (r->balance < 0)
7903 /* L rotation. */
7904 p->left = r->right;
7905 if (p->left)
7906 p->left->parent = p;
7907 r->right = p;
7909 p->balance = 0;
7910 r->balance = 0;
7912 s = p->parent;
7913 p->parent = r;
7914 r->parent = s;
7915 if (s)
7917 if (s->left == p)
7918 s->left = r;
7919 else
7920 s->right = r;
7922 else
7923 constructor_pending_elts = r;
7925 else
7927 /* LR rotation. */
7928 struct init_node *t = r->right;
7930 r->right = t->left;
7931 if (r->right)
7932 r->right->parent = r;
7933 t->left = r;
7935 p->left = t->right;
7936 if (p->left)
7937 p->left->parent = p;
7938 t->right = p;
7940 p->balance = t->balance < 0;
7941 r->balance = -(t->balance > 0);
7942 t->balance = 0;
7944 s = p->parent;
7945 p->parent = t;
7946 r->parent = t;
7947 t->parent = s;
7948 if (s)
7950 if (s->left == p)
7951 s->left = t;
7952 else
7953 s->right = t;
7955 else
7956 constructor_pending_elts = t;
7958 break;
7960 else
7962 /* p->balance == +1; growth of left side balances the node. */
7963 p->balance = 0;
7964 break;
7967 else /* r == p->right */
7969 if (p->balance == 0)
7970 /* Growth propagation from right side. */
7971 p->balance++;
7972 else if (p->balance > 0)
7974 if (r->balance > 0)
7976 /* R rotation. */
7977 p->right = r->left;
7978 if (p->right)
7979 p->right->parent = p;
7980 r->left = p;
7982 p->balance = 0;
7983 r->balance = 0;
7985 s = p->parent;
7986 p->parent = r;
7987 r->parent = s;
7988 if (s)
7990 if (s->left == p)
7991 s->left = r;
7992 else
7993 s->right = r;
7995 else
7996 constructor_pending_elts = r;
7998 else /* r->balance == -1 */
8000 /* RL rotation */
8001 struct init_node *t = r->left;
8003 r->left = t->right;
8004 if (r->left)
8005 r->left->parent = r;
8006 t->right = r;
8008 p->right = t->left;
8009 if (p->right)
8010 p->right->parent = p;
8011 t->left = p;
8013 r->balance = (t->balance < 0);
8014 p->balance = -(t->balance > 0);
8015 t->balance = 0;
8017 s = p->parent;
8018 p->parent = t;
8019 r->parent = t;
8020 t->parent = s;
8021 if (s)
8023 if (s->left == p)
8024 s->left = t;
8025 else
8026 s->right = t;
8028 else
8029 constructor_pending_elts = t;
8031 break;
8033 else
8035 /* p->balance == -1; growth of right side balances the node. */
8036 p->balance = 0;
8037 break;
8041 r = p;
8042 p = p->parent;
8046 /* Build AVL tree from a sorted chain. */
8048 static void
8049 set_nonincremental_init (struct obstack * braced_init_obstack)
8051 unsigned HOST_WIDE_INT ix;
8052 tree index, value;
8054 if (TREE_CODE (constructor_type) != RECORD_TYPE
8055 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8056 return;
8058 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8059 add_pending_init (input_location, index, value, NULL_TREE, true,
8060 braced_init_obstack);
8061 constructor_elements = NULL;
8062 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8064 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8065 /* Skip any nameless bit fields at the beginning. */
8066 while (constructor_unfilled_fields != 0
8067 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8068 && DECL_NAME (constructor_unfilled_fields) == 0)
8069 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8072 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8074 if (TYPE_DOMAIN (constructor_type))
8075 constructor_unfilled_index
8076 = convert (bitsizetype,
8077 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8078 else
8079 constructor_unfilled_index = bitsize_zero_node;
8081 constructor_incremental = 0;
8084 /* Build AVL tree from a string constant. */
8086 static void
8087 set_nonincremental_init_from_string (tree str,
8088 struct obstack * braced_init_obstack)
8090 tree value, purpose, type;
8091 HOST_WIDE_INT val[2];
8092 const char *p, *end;
8093 int byte, wchar_bytes, charwidth, bitpos;
8095 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8097 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8098 charwidth = TYPE_PRECISION (char_type_node);
8099 type = TREE_TYPE (constructor_type);
8100 p = TREE_STRING_POINTER (str);
8101 end = p + TREE_STRING_LENGTH (str);
8103 for (purpose = bitsize_zero_node;
8104 p < end
8105 && !(constructor_max_index
8106 && tree_int_cst_lt (constructor_max_index, purpose));
8107 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8109 if (wchar_bytes == 1)
8111 val[0] = (unsigned char) *p++;
8112 val[1] = 0;
8114 else
8116 val[1] = 0;
8117 val[0] = 0;
8118 for (byte = 0; byte < wchar_bytes; byte++)
8120 if (BYTES_BIG_ENDIAN)
8121 bitpos = (wchar_bytes - byte - 1) * charwidth;
8122 else
8123 bitpos = byte * charwidth;
8124 val[bitpos % HOST_BITS_PER_WIDE_INT]
8125 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8126 << (bitpos % HOST_BITS_PER_WIDE_INT);
8130 if (!TYPE_UNSIGNED (type))
8132 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8133 if (bitpos < HOST_BITS_PER_WIDE_INT)
8135 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8137 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8138 val[1] = -1;
8141 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8143 if (val[0] < 0)
8144 val[1] = -1;
8146 else if (val[1] & (((HOST_WIDE_INT) 1)
8147 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8148 val[1] |= ((HOST_WIDE_INT) -1)
8149 << (bitpos - HOST_BITS_PER_WIDE_INT);
8152 value = wide_int_to_tree (type,
8153 wide_int::from_array (val, 2,
8154 HOST_BITS_PER_WIDE_INT * 2));
8155 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8156 braced_init_obstack);
8159 constructor_incremental = 0;
8162 /* Return value of FIELD in pending initializer or zero if the field was
8163 not initialized yet. */
8165 static tree
8166 find_init_member (tree field, struct obstack * braced_init_obstack)
8168 struct init_node *p;
8170 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8172 if (constructor_incremental
8173 && tree_int_cst_lt (field, constructor_unfilled_index))
8174 set_nonincremental_init (braced_init_obstack);
8176 p = constructor_pending_elts;
8177 while (p)
8179 if (tree_int_cst_lt (field, p->purpose))
8180 p = p->left;
8181 else if (tree_int_cst_lt (p->purpose, field))
8182 p = p->right;
8183 else
8184 return p->value;
8187 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8189 tree bitpos = bit_position (field);
8191 if (constructor_incremental
8192 && (!constructor_unfilled_fields
8193 || tree_int_cst_lt (bitpos,
8194 bit_position (constructor_unfilled_fields))))
8195 set_nonincremental_init (braced_init_obstack);
8197 p = constructor_pending_elts;
8198 while (p)
8200 if (field == p->purpose)
8201 return p->value;
8202 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8203 p = p->left;
8204 else
8205 p = p->right;
8208 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8210 if (!vec_safe_is_empty (constructor_elements)
8211 && (constructor_elements->last ().index == field))
8212 return constructor_elements->last ().value;
8214 return 0;
8217 /* "Output" the next constructor element.
8218 At top level, really output it to assembler code now.
8219 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8220 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8221 TYPE is the data type that the containing data type wants here.
8222 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8223 If VALUE is a string constant, STRICT_STRING is true if it is
8224 unparenthesized or we should not warn here for it being parenthesized.
8225 For other types of VALUE, STRICT_STRING is not used.
8227 PENDING if non-nil means output pending elements that belong
8228 right after this element. (PENDING is normally 1;
8229 it is 0 while outputting pending elements, to avoid recursion.)
8231 IMPLICIT is true if value comes from pop_init_level (1),
8232 the new initializer has been merged with the existing one
8233 and thus no warnings should be emitted about overriding an
8234 existing initializer. */
8236 static void
8237 output_init_element (location_t loc, tree value, tree origtype,
8238 bool strict_string, tree type, tree field, int pending,
8239 bool implicit, struct obstack * braced_init_obstack)
8241 tree semantic_type = NULL_TREE;
8242 bool maybe_const = true;
8243 bool npc;
8245 if (type == error_mark_node || value == error_mark_node)
8247 constructor_erroneous = 1;
8248 return;
8250 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8251 && (TREE_CODE (value) == STRING_CST
8252 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8253 && !(TREE_CODE (value) == STRING_CST
8254 && TREE_CODE (type) == ARRAY_TYPE
8255 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8256 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8257 TYPE_MAIN_VARIANT (type)))
8258 value = array_to_pointer_conversion (input_location, value);
8260 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8261 && require_constant_value && pending)
8263 /* As an extension, allow initializing objects with static storage
8264 duration with compound literals (which are then treated just as
8265 the brace enclosed list they contain). */
8266 if (flag_isoc99)
8267 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8268 "constant");
8269 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8270 value = DECL_INITIAL (decl);
8273 npc = null_pointer_constant_p (value);
8274 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8276 semantic_type = TREE_TYPE (value);
8277 value = TREE_OPERAND (value, 0);
8279 value = c_fully_fold (value, require_constant_value, &maybe_const);
8281 if (value == error_mark_node)
8282 constructor_erroneous = 1;
8283 else if (!TREE_CONSTANT (value))
8284 constructor_constant = 0;
8285 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8286 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8287 || TREE_CODE (constructor_type) == UNION_TYPE)
8288 && DECL_C_BIT_FIELD (field)
8289 && TREE_CODE (value) != INTEGER_CST))
8290 constructor_simple = 0;
8291 if (!maybe_const)
8292 constructor_nonconst = 1;
8294 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8296 if (require_constant_value)
8298 error_init (loc, "initializer element is not constant");
8299 value = error_mark_node;
8301 else if (require_constant_elements)
8302 pedwarn (loc, OPT_Wpedantic,
8303 "initializer element is not computable at load time");
8305 else if (!maybe_const
8306 && (require_constant_value || require_constant_elements))
8307 pedwarn_init (loc, OPT_Wpedantic,
8308 "initializer element is not a constant expression");
8310 /* Issue -Wc++-compat warnings about initializing a bitfield with
8311 enum type. */
8312 if (warn_cxx_compat
8313 && field != NULL_TREE
8314 && TREE_CODE (field) == FIELD_DECL
8315 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8316 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8317 != TYPE_MAIN_VARIANT (type))
8318 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8320 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8321 if (checktype != error_mark_node
8322 && (TYPE_MAIN_VARIANT (checktype)
8323 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8324 warning_init (loc, OPT_Wc___compat,
8325 "enum conversion in initialization is invalid in C++");
8328 /* If this field is empty (and not at the end of structure),
8329 don't do anything other than checking the initializer. */
8330 if (field
8331 && (TREE_TYPE (field) == error_mark_node
8332 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8333 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8334 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8335 || DECL_CHAIN (field)))))
8336 return;
8338 if (semantic_type)
8339 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8340 value = digest_init (loc, type, value, origtype, npc, strict_string,
8341 require_constant_value);
8342 if (value == error_mark_node)
8344 constructor_erroneous = 1;
8345 return;
8347 if (require_constant_value || require_constant_elements)
8348 constant_expression_warning (value);
8350 /* If this element doesn't come next in sequence,
8351 put it on constructor_pending_elts. */
8352 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8353 && (!constructor_incremental
8354 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8356 if (constructor_incremental
8357 && tree_int_cst_lt (field, constructor_unfilled_index))
8358 set_nonincremental_init (braced_init_obstack);
8360 add_pending_init (loc, field, value, origtype, implicit,
8361 braced_init_obstack);
8362 return;
8364 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8365 && (!constructor_incremental
8366 || field != constructor_unfilled_fields))
8368 /* We do this for records but not for unions. In a union,
8369 no matter which field is specified, it can be initialized
8370 right away since it starts at the beginning of the union. */
8371 if (constructor_incremental)
8373 if (!constructor_unfilled_fields)
8374 set_nonincremental_init (braced_init_obstack);
8375 else
8377 tree bitpos, unfillpos;
8379 bitpos = bit_position (field);
8380 unfillpos = bit_position (constructor_unfilled_fields);
8382 if (tree_int_cst_lt (bitpos, unfillpos))
8383 set_nonincremental_init (braced_init_obstack);
8387 add_pending_init (loc, field, value, origtype, implicit,
8388 braced_init_obstack);
8389 return;
8391 else if (TREE_CODE (constructor_type) == UNION_TYPE
8392 && !vec_safe_is_empty (constructor_elements))
8394 if (!implicit)
8396 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8397 warning_init (loc, 0,
8398 "initialized field with side-effects overwritten");
8399 else if (warn_override_init)
8400 warning_init (loc, OPT_Woverride_init,
8401 "initialized field overwritten");
8404 /* We can have just one union field set. */
8405 constructor_elements = NULL;
8408 /* Otherwise, output this element either to
8409 constructor_elements or to the assembler file. */
8411 constructor_elt celt = {field, value};
8412 vec_safe_push (constructor_elements, celt);
8414 /* Advance the variable that indicates sequential elements output. */
8415 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8416 constructor_unfilled_index
8417 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8418 bitsize_one_node);
8419 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8421 constructor_unfilled_fields
8422 = DECL_CHAIN (constructor_unfilled_fields);
8424 /* Skip any nameless bit fields. */
8425 while (constructor_unfilled_fields != 0
8426 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8427 && DECL_NAME (constructor_unfilled_fields) == 0)
8428 constructor_unfilled_fields =
8429 DECL_CHAIN (constructor_unfilled_fields);
8431 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8432 constructor_unfilled_fields = 0;
8434 /* Now output any pending elements which have become next. */
8435 if (pending)
8436 output_pending_init_elements (0, braced_init_obstack);
8439 /* Output any pending elements which have become next.
8440 As we output elements, constructor_unfilled_{fields,index}
8441 advances, which may cause other elements to become next;
8442 if so, they too are output.
8444 If ALL is 0, we return when there are
8445 no more pending elements to output now.
8447 If ALL is 1, we output space as necessary so that
8448 we can output all the pending elements. */
8449 static void
8450 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8452 struct init_node *elt = constructor_pending_elts;
8453 tree next;
8455 retry:
8457 /* Look through the whole pending tree.
8458 If we find an element that should be output now,
8459 output it. Otherwise, set NEXT to the element
8460 that comes first among those still pending. */
8462 next = 0;
8463 while (elt)
8465 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8467 if (tree_int_cst_equal (elt->purpose,
8468 constructor_unfilled_index))
8469 output_init_element (input_location, elt->value, elt->origtype,
8470 true, TREE_TYPE (constructor_type),
8471 constructor_unfilled_index, 0, false,
8472 braced_init_obstack);
8473 else if (tree_int_cst_lt (constructor_unfilled_index,
8474 elt->purpose))
8476 /* Advance to the next smaller node. */
8477 if (elt->left)
8478 elt = elt->left;
8479 else
8481 /* We have reached the smallest node bigger than the
8482 current unfilled index. Fill the space first. */
8483 next = elt->purpose;
8484 break;
8487 else
8489 /* Advance to the next bigger node. */
8490 if (elt->right)
8491 elt = elt->right;
8492 else
8494 /* We have reached the biggest node in a subtree. Find
8495 the parent of it, which is the next bigger node. */
8496 while (elt->parent && elt->parent->right == elt)
8497 elt = elt->parent;
8498 elt = elt->parent;
8499 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8500 elt->purpose))
8502 next = elt->purpose;
8503 break;
8508 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8509 || TREE_CODE (constructor_type) == UNION_TYPE)
8511 tree ctor_unfilled_bitpos, elt_bitpos;
8513 /* If the current record is complete we are done. */
8514 if (constructor_unfilled_fields == 0)
8515 break;
8517 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8518 elt_bitpos = bit_position (elt->purpose);
8519 /* We can't compare fields here because there might be empty
8520 fields in between. */
8521 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8523 constructor_unfilled_fields = elt->purpose;
8524 output_init_element (input_location, elt->value, elt->origtype,
8525 true, TREE_TYPE (elt->purpose),
8526 elt->purpose, 0, false,
8527 braced_init_obstack);
8529 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8531 /* Advance to the next smaller node. */
8532 if (elt->left)
8533 elt = elt->left;
8534 else
8536 /* We have reached the smallest node bigger than the
8537 current unfilled field. Fill the space first. */
8538 next = elt->purpose;
8539 break;
8542 else
8544 /* Advance to the next bigger node. */
8545 if (elt->right)
8546 elt = elt->right;
8547 else
8549 /* We have reached the biggest node in a subtree. Find
8550 the parent of it, which is the next bigger node. */
8551 while (elt->parent && elt->parent->right == elt)
8552 elt = elt->parent;
8553 elt = elt->parent;
8554 if (elt
8555 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8556 bit_position (elt->purpose))))
8558 next = elt->purpose;
8559 break;
8566 /* Ordinarily return, but not if we want to output all
8567 and there are elements left. */
8568 if (!(all && next != 0))
8569 return;
8571 /* If it's not incremental, just skip over the gap, so that after
8572 jumping to retry we will output the next successive element. */
8573 if (TREE_CODE (constructor_type) == RECORD_TYPE
8574 || TREE_CODE (constructor_type) == UNION_TYPE)
8575 constructor_unfilled_fields = next;
8576 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8577 constructor_unfilled_index = next;
8579 /* ELT now points to the node in the pending tree with the next
8580 initializer to output. */
8581 goto retry;
8584 /* Add one non-braced element to the current constructor level.
8585 This adjusts the current position within the constructor's type.
8586 This may also start or terminate implicit levels
8587 to handle a partly-braced initializer.
8589 Once this has found the correct level for the new element,
8590 it calls output_init_element.
8592 IMPLICIT is true if value comes from pop_init_level (1),
8593 the new initializer has been merged with the existing one
8594 and thus no warnings should be emitted about overriding an
8595 existing initializer. */
8597 void
8598 process_init_element (location_t loc, struct c_expr value, bool implicit,
8599 struct obstack * braced_init_obstack)
8601 tree orig_value = value.value;
8602 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8603 bool strict_string = value.original_code == STRING_CST;
8604 bool was_designated = designator_depth != 0;
8606 designator_depth = 0;
8607 designator_erroneous = 0;
8609 if (!implicit && value.value && !integer_zerop (value.value))
8610 constructor_zeroinit = 0;
8612 /* Handle superfluous braces around string cst as in
8613 char x[] = {"foo"}; */
8614 if (string_flag
8615 && constructor_type
8616 && !was_designated
8617 && TREE_CODE (constructor_type) == ARRAY_TYPE
8618 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8619 && integer_zerop (constructor_unfilled_index))
8621 if (constructor_stack->replacement_value.value)
8622 error_init (loc, "excess elements in char array initializer");
8623 constructor_stack->replacement_value = value;
8624 return;
8627 if (constructor_stack->replacement_value.value != 0)
8629 error_init (loc, "excess elements in struct initializer");
8630 return;
8633 /* Ignore elements of a brace group if it is entirely superfluous
8634 and has already been diagnosed. */
8635 if (constructor_type == 0)
8636 return;
8638 if (!implicit && warn_designated_init && !was_designated
8639 && TREE_CODE (constructor_type) == RECORD_TYPE
8640 && lookup_attribute ("designated_init",
8641 TYPE_ATTRIBUTES (constructor_type)))
8642 warning_init (loc,
8643 OPT_Wdesignated_init,
8644 "positional initialization of field "
8645 "in %<struct%> declared with %<designated_init%> attribute");
8647 /* If we've exhausted any levels that didn't have braces,
8648 pop them now. */
8649 while (constructor_stack->implicit)
8651 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8652 || TREE_CODE (constructor_type) == UNION_TYPE)
8653 && constructor_fields == 0)
8654 process_init_element (loc,
8655 pop_init_level (loc, 1, braced_init_obstack),
8656 true, braced_init_obstack);
8657 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8658 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8659 && constructor_max_index
8660 && tree_int_cst_lt (constructor_max_index,
8661 constructor_index))
8662 process_init_element (loc,
8663 pop_init_level (loc, 1, braced_init_obstack),
8664 true, braced_init_obstack);
8665 else
8666 break;
8669 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8670 if (constructor_range_stack)
8672 /* If value is a compound literal and we'll be just using its
8673 content, don't put it into a SAVE_EXPR. */
8674 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8675 || !require_constant_value
8676 || flag_isoc99)
8678 tree semantic_type = NULL_TREE;
8679 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8681 semantic_type = TREE_TYPE (value.value);
8682 value.value = TREE_OPERAND (value.value, 0);
8684 value.value = c_save_expr (value.value);
8685 if (semantic_type)
8686 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8687 value.value);
8691 while (1)
8693 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8695 tree fieldtype;
8696 enum tree_code fieldcode;
8698 if (constructor_fields == 0)
8700 pedwarn_init (loc, 0, "excess elements in struct initializer");
8701 break;
8704 fieldtype = TREE_TYPE (constructor_fields);
8705 if (fieldtype != error_mark_node)
8706 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8707 fieldcode = TREE_CODE (fieldtype);
8709 /* Error for non-static initialization of a flexible array member. */
8710 if (fieldcode == ARRAY_TYPE
8711 && !require_constant_value
8712 && TYPE_SIZE (fieldtype) == NULL_TREE
8713 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8715 error_init (loc, "non-static initialization of a flexible "
8716 "array member");
8717 break;
8720 /* Accept a string constant to initialize a subarray. */
8721 if (value.value != 0
8722 && fieldcode == ARRAY_TYPE
8723 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8724 && string_flag)
8725 value.value = orig_value;
8726 /* Otherwise, if we have come to a subaggregate,
8727 and we don't have an element of its type, push into it. */
8728 else if (value.value != 0
8729 && value.value != error_mark_node
8730 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8731 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8732 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8734 push_init_level (loc, 1, braced_init_obstack);
8735 continue;
8738 if (value.value)
8740 push_member_name (constructor_fields);
8741 output_init_element (loc, value.value, value.original_type,
8742 strict_string, fieldtype,
8743 constructor_fields, 1, implicit,
8744 braced_init_obstack);
8745 RESTORE_SPELLING_DEPTH (constructor_depth);
8747 else
8748 /* Do the bookkeeping for an element that was
8749 directly output as a constructor. */
8751 /* For a record, keep track of end position of last field. */
8752 if (DECL_SIZE (constructor_fields))
8753 constructor_bit_index
8754 = size_binop_loc (input_location, PLUS_EXPR,
8755 bit_position (constructor_fields),
8756 DECL_SIZE (constructor_fields));
8758 /* If the current field was the first one not yet written out,
8759 it isn't now, so update. */
8760 if (constructor_unfilled_fields == constructor_fields)
8762 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8763 /* Skip any nameless bit fields. */
8764 while (constructor_unfilled_fields != 0
8765 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8766 && DECL_NAME (constructor_unfilled_fields) == 0)
8767 constructor_unfilled_fields =
8768 DECL_CHAIN (constructor_unfilled_fields);
8772 constructor_fields = DECL_CHAIN (constructor_fields);
8773 /* Skip any nameless bit fields at the beginning. */
8774 while (constructor_fields != 0
8775 && DECL_C_BIT_FIELD (constructor_fields)
8776 && DECL_NAME (constructor_fields) == 0)
8777 constructor_fields = DECL_CHAIN (constructor_fields);
8779 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8781 tree fieldtype;
8782 enum tree_code fieldcode;
8784 if (constructor_fields == 0)
8786 pedwarn_init (loc, 0,
8787 "excess elements in union initializer");
8788 break;
8791 fieldtype = TREE_TYPE (constructor_fields);
8792 if (fieldtype != error_mark_node)
8793 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8794 fieldcode = TREE_CODE (fieldtype);
8796 /* Warn that traditional C rejects initialization of unions.
8797 We skip the warning if the value is zero. This is done
8798 under the assumption that the zero initializer in user
8799 code appears conditioned on e.g. __STDC__ to avoid
8800 "missing initializer" warnings and relies on default
8801 initialization to zero in the traditional C case.
8802 We also skip the warning if the initializer is designated,
8803 again on the assumption that this must be conditional on
8804 __STDC__ anyway (and we've already complained about the
8805 member-designator already). */
8806 if (!in_system_header_at (input_location) && !constructor_designated
8807 && !(value.value && (integer_zerop (value.value)
8808 || real_zerop (value.value))))
8809 warning (OPT_Wtraditional, "traditional C rejects initialization "
8810 "of unions");
8812 /* Accept a string constant to initialize a subarray. */
8813 if (value.value != 0
8814 && fieldcode == ARRAY_TYPE
8815 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8816 && string_flag)
8817 value.value = orig_value;
8818 /* Otherwise, if we have come to a subaggregate,
8819 and we don't have an element of its type, push into it. */
8820 else if (value.value != 0
8821 && value.value != error_mark_node
8822 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8823 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8824 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8826 push_init_level (loc, 1, braced_init_obstack);
8827 continue;
8830 if (value.value)
8832 push_member_name (constructor_fields);
8833 output_init_element (loc, value.value, value.original_type,
8834 strict_string, fieldtype,
8835 constructor_fields, 1, implicit,
8836 braced_init_obstack);
8837 RESTORE_SPELLING_DEPTH (constructor_depth);
8839 else
8840 /* Do the bookkeeping for an element that was
8841 directly output as a constructor. */
8843 constructor_bit_index = DECL_SIZE (constructor_fields);
8844 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8847 constructor_fields = 0;
8849 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8851 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8852 enum tree_code eltcode = TREE_CODE (elttype);
8854 /* Accept a string constant to initialize a subarray. */
8855 if (value.value != 0
8856 && eltcode == ARRAY_TYPE
8857 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8858 && string_flag)
8859 value.value = orig_value;
8860 /* Otherwise, if we have come to a subaggregate,
8861 and we don't have an element of its type, push into it. */
8862 else if (value.value != 0
8863 && value.value != error_mark_node
8864 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8865 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8866 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8868 push_init_level (loc, 1, braced_init_obstack);
8869 continue;
8872 if (constructor_max_index != 0
8873 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8874 || integer_all_onesp (constructor_max_index)))
8876 pedwarn_init (loc, 0,
8877 "excess elements in array initializer");
8878 break;
8881 /* Now output the actual element. */
8882 if (value.value)
8884 push_array_bounds (tree_to_uhwi (constructor_index));
8885 output_init_element (loc, value.value, value.original_type,
8886 strict_string, elttype,
8887 constructor_index, 1, implicit,
8888 braced_init_obstack);
8889 RESTORE_SPELLING_DEPTH (constructor_depth);
8892 constructor_index
8893 = size_binop_loc (input_location, PLUS_EXPR,
8894 constructor_index, bitsize_one_node);
8896 if (!value.value)
8897 /* If we are doing the bookkeeping for an element that was
8898 directly output as a constructor, we must update
8899 constructor_unfilled_index. */
8900 constructor_unfilled_index = constructor_index;
8902 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8904 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8906 /* Do a basic check of initializer size. Note that vectors
8907 always have a fixed size derived from their type. */
8908 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8910 pedwarn_init (loc, 0,
8911 "excess elements in vector initializer");
8912 break;
8915 /* Now output the actual element. */
8916 if (value.value)
8918 if (TREE_CODE (value.value) == VECTOR_CST)
8919 elttype = TYPE_MAIN_VARIANT (constructor_type);
8920 output_init_element (loc, value.value, value.original_type,
8921 strict_string, elttype,
8922 constructor_index, 1, implicit,
8923 braced_init_obstack);
8926 constructor_index
8927 = size_binop_loc (input_location,
8928 PLUS_EXPR, constructor_index, bitsize_one_node);
8930 if (!value.value)
8931 /* If we are doing the bookkeeping for an element that was
8932 directly output as a constructor, we must update
8933 constructor_unfilled_index. */
8934 constructor_unfilled_index = constructor_index;
8937 /* Handle the sole element allowed in a braced initializer
8938 for a scalar variable. */
8939 else if (constructor_type != error_mark_node
8940 && constructor_fields == 0)
8942 pedwarn_init (loc, 0,
8943 "excess elements in scalar initializer");
8944 break;
8946 else
8948 if (value.value)
8949 output_init_element (loc, value.value, value.original_type,
8950 strict_string, constructor_type,
8951 NULL_TREE, 1, implicit,
8952 braced_init_obstack);
8953 constructor_fields = 0;
8956 /* Handle range initializers either at this level or anywhere higher
8957 in the designator stack. */
8958 if (constructor_range_stack)
8960 struct constructor_range_stack *p, *range_stack;
8961 int finish = 0;
8963 range_stack = constructor_range_stack;
8964 constructor_range_stack = 0;
8965 while (constructor_stack != range_stack->stack)
8967 gcc_assert (constructor_stack->implicit);
8968 process_init_element (loc,
8969 pop_init_level (loc, 1,
8970 braced_init_obstack),
8971 true, braced_init_obstack);
8973 for (p = range_stack;
8974 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8975 p = p->prev)
8977 gcc_assert (constructor_stack->implicit);
8978 process_init_element (loc,
8979 pop_init_level (loc, 1,
8980 braced_init_obstack),
8981 true, braced_init_obstack);
8984 p->index = size_binop_loc (input_location,
8985 PLUS_EXPR, p->index, bitsize_one_node);
8986 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8987 finish = 1;
8989 while (1)
8991 constructor_index = p->index;
8992 constructor_fields = p->fields;
8993 if (finish && p->range_end && p->index == p->range_start)
8995 finish = 0;
8996 p->prev = 0;
8998 p = p->next;
8999 if (!p)
9000 break;
9001 push_init_level (loc, 2, braced_init_obstack);
9002 p->stack = constructor_stack;
9003 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9004 p->index = p->range_start;
9007 if (!finish)
9008 constructor_range_stack = range_stack;
9009 continue;
9012 break;
9015 constructor_range_stack = 0;
9018 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9019 (guaranteed to be 'volatile' or null) and ARGS (represented using
9020 an ASM_EXPR node). */
9021 tree
9022 build_asm_stmt (tree cv_qualifier, tree args)
9024 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9025 ASM_VOLATILE_P (args) = 1;
9026 return add_stmt (args);
9029 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9030 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9031 SIMPLE indicates whether there was anything at all after the
9032 string in the asm expression -- asm("blah") and asm("blah" : )
9033 are subtly different. We use a ASM_EXPR node to represent this. */
9034 tree
9035 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9036 tree clobbers, tree labels, bool simple)
9038 tree tail;
9039 tree args;
9040 int i;
9041 const char *constraint;
9042 const char **oconstraints;
9043 bool allows_mem, allows_reg, is_inout;
9044 int ninputs, noutputs;
9046 ninputs = list_length (inputs);
9047 noutputs = list_length (outputs);
9048 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9050 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9052 /* Remove output conversions that change the type but not the mode. */
9053 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9055 tree output = TREE_VALUE (tail);
9057 output = c_fully_fold (output, false, NULL);
9059 /* ??? Really, this should not be here. Users should be using a
9060 proper lvalue, dammit. But there's a long history of using casts
9061 in the output operands. In cases like longlong.h, this becomes a
9062 primitive form of typechecking -- if the cast can be removed, then
9063 the output operand had a type of the proper width; otherwise we'll
9064 get an error. Gross, but ... */
9065 STRIP_NOPS (output);
9067 if (!lvalue_or_else (loc, output, lv_asm))
9068 output = error_mark_node;
9070 if (output != error_mark_node
9071 && (TREE_READONLY (output)
9072 || TYPE_READONLY (TREE_TYPE (output))
9073 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9074 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9075 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9076 readonly_error (loc, output, lv_asm);
9078 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9079 oconstraints[i] = constraint;
9081 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9082 &allows_mem, &allows_reg, &is_inout))
9084 /* If the operand is going to end up in memory,
9085 mark it addressable. */
9086 if (!allows_reg && !c_mark_addressable (output))
9087 output = error_mark_node;
9088 if (!(!allows_reg && allows_mem)
9089 && output != error_mark_node
9090 && VOID_TYPE_P (TREE_TYPE (output)))
9092 error_at (loc, "invalid use of void expression");
9093 output = error_mark_node;
9096 else
9097 output = error_mark_node;
9099 TREE_VALUE (tail) = output;
9102 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9104 tree input;
9106 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9107 input = TREE_VALUE (tail);
9109 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9110 oconstraints, &allows_mem, &allows_reg))
9112 /* If the operand is going to end up in memory,
9113 mark it addressable. */
9114 if (!allows_reg && allows_mem)
9116 input = c_fully_fold (input, false, NULL);
9118 /* Strip the nops as we allow this case. FIXME, this really
9119 should be rejected or made deprecated. */
9120 STRIP_NOPS (input);
9121 if (!c_mark_addressable (input))
9122 input = error_mark_node;
9124 else
9126 struct c_expr expr;
9127 memset (&expr, 0, sizeof (expr));
9128 expr.value = input;
9129 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9130 input = c_fully_fold (expr.value, false, NULL);
9132 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9134 error_at (loc, "invalid use of void expression");
9135 input = error_mark_node;
9139 else
9140 input = error_mark_node;
9142 TREE_VALUE (tail) = input;
9145 /* ASMs with labels cannot have outputs. This should have been
9146 enforced by the parser. */
9147 gcc_assert (outputs == NULL || labels == NULL);
9149 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9151 /* asm statements without outputs, including simple ones, are treated
9152 as volatile. */
9153 ASM_INPUT_P (args) = simple;
9154 ASM_VOLATILE_P (args) = (noutputs == 0);
9156 return args;
9159 /* Generate a goto statement to LABEL. LOC is the location of the
9160 GOTO. */
9162 tree
9163 c_finish_goto_label (location_t loc, tree label)
9165 tree decl = lookup_label_for_goto (loc, label);
9166 if (!decl)
9167 return NULL_TREE;
9168 TREE_USED (decl) = 1;
9170 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9171 SET_EXPR_LOCATION (t, loc);
9172 return add_stmt (t);
9176 /* Generate a computed goto statement to EXPR. LOC is the location of
9177 the GOTO. */
9179 tree
9180 c_finish_goto_ptr (location_t loc, tree expr)
9182 tree t;
9183 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9184 expr = c_fully_fold (expr, false, NULL);
9185 expr = convert (ptr_type_node, expr);
9186 t = build1 (GOTO_EXPR, void_type_node, expr);
9187 SET_EXPR_LOCATION (t, loc);
9188 return add_stmt (t);
9191 /* Generate a C `return' statement. RETVAL is the expression for what
9192 to return, or a null pointer for `return;' with no value. LOC is
9193 the location of the return statement, or the location of the expression,
9194 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9195 is the original type of RETVAL. */
9197 tree
9198 c_finish_return (location_t loc, tree retval, tree origtype)
9200 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9201 bool no_warning = false;
9202 bool npc = false;
9203 size_t rank = 0;
9205 if (TREE_THIS_VOLATILE (current_function_decl))
9206 warning_at (loc, 0,
9207 "function declared %<noreturn%> has a %<return%> statement");
9209 if (flag_cilkplus && contains_array_notation_expr (retval))
9211 /* Array notations are allowed in a return statement if it is inside a
9212 built-in array notation reduction function. */
9213 if (!find_rank (loc, retval, retval, false, &rank))
9214 return error_mark_node;
9215 if (rank >= 1)
9217 error_at (loc, "array notation expression cannot be used as a "
9218 "return value");
9219 return error_mark_node;
9222 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9224 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9225 "allowed");
9226 return error_mark_node;
9228 if (retval)
9230 tree semantic_type = NULL_TREE;
9231 npc = null_pointer_constant_p (retval);
9232 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9234 semantic_type = TREE_TYPE (retval);
9235 retval = TREE_OPERAND (retval, 0);
9237 retval = c_fully_fold (retval, false, NULL);
9238 if (semantic_type)
9239 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9242 if (!retval)
9244 current_function_returns_null = 1;
9245 if ((warn_return_type || flag_isoc99)
9246 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9248 if (flag_isoc99)
9249 pedwarn (loc, 0, "%<return%> with no value, in "
9250 "function returning non-void");
9251 else
9252 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9253 "in function returning non-void");
9254 no_warning = true;
9257 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9259 current_function_returns_null = 1;
9260 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9261 pedwarn (loc, 0,
9262 "%<return%> with a value, in function returning void");
9263 else
9264 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9265 "%<return%> with expression, in function returning void");
9267 else
9269 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9270 retval, origtype, ic_return,
9271 npc, NULL_TREE, NULL_TREE, 0);
9272 tree res = DECL_RESULT (current_function_decl);
9273 tree inner;
9274 bool save;
9276 current_function_returns_value = 1;
9277 if (t == error_mark_node)
9278 return NULL_TREE;
9280 save = in_late_binary_op;
9281 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9282 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9283 in_late_binary_op = true;
9284 inner = t = convert (TREE_TYPE (res), t);
9285 in_late_binary_op = save;
9287 /* Strip any conversions, additions, and subtractions, and see if
9288 we are returning the address of a local variable. Warn if so. */
9289 while (1)
9291 switch (TREE_CODE (inner))
9293 CASE_CONVERT:
9294 case NON_LVALUE_EXPR:
9295 case PLUS_EXPR:
9296 case POINTER_PLUS_EXPR:
9297 inner = TREE_OPERAND (inner, 0);
9298 continue;
9300 case MINUS_EXPR:
9301 /* If the second operand of the MINUS_EXPR has a pointer
9302 type (or is converted from it), this may be valid, so
9303 don't give a warning. */
9305 tree op1 = TREE_OPERAND (inner, 1);
9307 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9308 && (CONVERT_EXPR_P (op1)
9309 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9310 op1 = TREE_OPERAND (op1, 0);
9312 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9313 break;
9315 inner = TREE_OPERAND (inner, 0);
9316 continue;
9319 case ADDR_EXPR:
9320 inner = TREE_OPERAND (inner, 0);
9322 while (REFERENCE_CLASS_P (inner)
9323 && TREE_CODE (inner) != INDIRECT_REF)
9324 inner = TREE_OPERAND (inner, 0);
9326 if (DECL_P (inner)
9327 && !DECL_EXTERNAL (inner)
9328 && !TREE_STATIC (inner)
9329 && DECL_CONTEXT (inner) == current_function_decl)
9331 if (TREE_CODE (inner) == LABEL_DECL)
9332 warning_at (loc, OPT_Wreturn_local_addr,
9333 "function returns address of label");
9334 else
9336 warning_at (loc, OPT_Wreturn_local_addr,
9337 "function returns address of local variable");
9338 tree zero = build_zero_cst (TREE_TYPE (res));
9339 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9342 break;
9344 default:
9345 break;
9348 break;
9351 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9352 SET_EXPR_LOCATION (retval, loc);
9354 if (warn_sequence_point)
9355 verify_sequence_points (retval);
9358 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9359 TREE_NO_WARNING (ret_stmt) |= no_warning;
9360 return add_stmt (ret_stmt);
9363 struct c_switch {
9364 /* The SWITCH_EXPR being built. */
9365 tree switch_expr;
9367 /* The original type of the testing expression, i.e. before the
9368 default conversion is applied. */
9369 tree orig_type;
9371 /* A splay-tree mapping the low element of a case range to the high
9372 element, or NULL_TREE if there is no high element. Used to
9373 determine whether or not a new case label duplicates an old case
9374 label. We need a tree, rather than simply a hash table, because
9375 of the GNU case range extension. */
9376 splay_tree cases;
9378 /* The bindings at the point of the switch. This is used for
9379 warnings crossing decls when branching to a case label. */
9380 struct c_spot_bindings *bindings;
9382 /* The next node on the stack. */
9383 struct c_switch *next;
9386 /* A stack of the currently active switch statements. The innermost
9387 switch statement is on the top of the stack. There is no need to
9388 mark the stack for garbage collection because it is only active
9389 during the processing of the body of a function, and we never
9390 collect at that point. */
9392 struct c_switch *c_switch_stack;
9394 /* Start a C switch statement, testing expression EXP. Return the new
9395 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9396 SWITCH_COND_LOC is the location of the switch's condition.
9397 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9399 tree
9400 c_start_case (location_t switch_loc,
9401 location_t switch_cond_loc,
9402 tree exp, bool explicit_cast_p)
9404 tree orig_type = error_mark_node;
9405 struct c_switch *cs;
9407 if (exp != error_mark_node)
9409 orig_type = TREE_TYPE (exp);
9411 if (!INTEGRAL_TYPE_P (orig_type))
9413 if (orig_type != error_mark_node)
9415 error_at (switch_cond_loc, "switch quantity not an integer");
9416 orig_type = error_mark_node;
9418 exp = integer_zero_node;
9420 else
9422 tree type = TYPE_MAIN_VARIANT (orig_type);
9423 tree e = exp;
9425 /* Warn if the condition has boolean value. */
9426 while (TREE_CODE (e) == COMPOUND_EXPR)
9427 e = TREE_OPERAND (e, 1);
9429 if ((TREE_CODE (type) == BOOLEAN_TYPE
9430 || truth_value_p (TREE_CODE (e)))
9431 /* Explicit cast to int suppresses this warning. */
9432 && !(TREE_CODE (type) == INTEGER_TYPE
9433 && explicit_cast_p))
9434 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9435 "switch condition has boolean value");
9437 if (!in_system_header_at (input_location)
9438 && (type == long_integer_type_node
9439 || type == long_unsigned_type_node))
9440 warning_at (switch_cond_loc,
9441 OPT_Wtraditional, "%<long%> switch expression not "
9442 "converted to %<int%> in ISO C");
9444 exp = c_fully_fold (exp, false, NULL);
9445 exp = default_conversion (exp);
9447 if (warn_sequence_point)
9448 verify_sequence_points (exp);
9452 /* Add this new SWITCH_EXPR to the stack. */
9453 cs = XNEW (struct c_switch);
9454 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9455 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9456 cs->orig_type = orig_type;
9457 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9458 cs->bindings = c_get_switch_bindings ();
9459 cs->next = c_switch_stack;
9460 c_switch_stack = cs;
9462 return add_stmt (cs->switch_expr);
9465 /* Process a case label at location LOC. */
9467 tree
9468 do_case (location_t loc, tree low_value, tree high_value)
9470 tree label = NULL_TREE;
9472 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9474 low_value = c_fully_fold (low_value, false, NULL);
9475 if (TREE_CODE (low_value) == INTEGER_CST)
9476 pedwarn (loc, OPT_Wpedantic,
9477 "case label is not an integer constant expression");
9480 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9482 high_value = c_fully_fold (high_value, false, NULL);
9483 if (TREE_CODE (high_value) == INTEGER_CST)
9484 pedwarn (input_location, OPT_Wpedantic,
9485 "case label is not an integer constant expression");
9488 if (c_switch_stack == NULL)
9490 if (low_value)
9491 error_at (loc, "case label not within a switch statement");
9492 else
9493 error_at (loc, "%<default%> label not within a switch statement");
9494 return NULL_TREE;
9497 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9498 EXPR_LOCATION (c_switch_stack->switch_expr),
9499 loc))
9500 return NULL_TREE;
9502 label = c_add_case_label (loc, c_switch_stack->cases,
9503 SWITCH_COND (c_switch_stack->switch_expr),
9504 c_switch_stack->orig_type,
9505 low_value, high_value);
9506 if (label == error_mark_node)
9507 label = NULL_TREE;
9508 return label;
9511 /* Finish the switch statement. TYPE is the original type of the
9512 controlling expression of the switch, or NULL_TREE. */
9514 void
9515 c_finish_case (tree body, tree type)
9517 struct c_switch *cs = c_switch_stack;
9518 location_t switch_location;
9520 SWITCH_BODY (cs->switch_expr) = body;
9522 /* Emit warnings as needed. */
9523 switch_location = EXPR_LOCATION (cs->switch_expr);
9524 c_do_switch_warnings (cs->cases, switch_location,
9525 type ? type : TREE_TYPE (cs->switch_expr),
9526 SWITCH_COND (cs->switch_expr));
9528 /* Pop the stack. */
9529 c_switch_stack = cs->next;
9530 splay_tree_delete (cs->cases);
9531 c_release_switch_bindings (cs->bindings);
9532 XDELETE (cs);
9535 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9536 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9537 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9538 statement, and was not surrounded with parenthesis. */
9540 void
9541 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9542 tree else_block, bool nested_if)
9544 tree stmt;
9546 /* If the condition has array notations, then the rank of the then_block and
9547 else_block must be either 0 or be equal to the rank of the condition. If
9548 the condition does not have array notations then break them up as it is
9549 broken up in a normal expression. */
9550 if (flag_cilkplus && contains_array_notation_expr (cond))
9552 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9553 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9554 return;
9555 if (then_block
9556 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9557 return;
9558 if (else_block
9559 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9560 return;
9561 if (cond_rank != then_rank && then_rank != 0)
9563 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9564 " and the then-block");
9565 return;
9567 else if (cond_rank != else_rank && else_rank != 0)
9569 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9570 " and the else-block");
9571 return;
9574 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9575 if (warn_parentheses && nested_if && else_block == NULL)
9577 tree inner_if = then_block;
9579 /* We know from the grammar productions that there is an IF nested
9580 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9581 it might not be exactly THEN_BLOCK, but should be the last
9582 non-container statement within. */
9583 while (1)
9584 switch (TREE_CODE (inner_if))
9586 case COND_EXPR:
9587 goto found;
9588 case BIND_EXPR:
9589 inner_if = BIND_EXPR_BODY (inner_if);
9590 break;
9591 case STATEMENT_LIST:
9592 inner_if = expr_last (then_block);
9593 break;
9594 case TRY_FINALLY_EXPR:
9595 case TRY_CATCH_EXPR:
9596 inner_if = TREE_OPERAND (inner_if, 0);
9597 break;
9598 default:
9599 gcc_unreachable ();
9601 found:
9603 if (COND_EXPR_ELSE (inner_if))
9604 warning_at (if_locus, OPT_Wparentheses,
9605 "suggest explicit braces to avoid ambiguous %<else%>");
9608 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9609 SET_EXPR_LOCATION (stmt, if_locus);
9610 add_stmt (stmt);
9613 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9614 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9615 is false for DO loops. INCR is the FOR increment expression. BODY is
9616 the statement controlled by the loop. BLAB is the break label. CLAB is
9617 the continue label. Everything is allowed to be NULL. */
9619 void
9620 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9621 tree blab, tree clab, bool cond_is_first)
9623 tree entry = NULL, exit = NULL, t;
9625 if (flag_cilkplus && contains_array_notation_expr (cond))
9627 error_at (start_locus, "array notation expression cannot be used in a "
9628 "loop%'s condition");
9629 return;
9632 /* If the condition is zero don't generate a loop construct. */
9633 if (cond && integer_zerop (cond))
9635 if (cond_is_first)
9637 t = build_and_jump (&blab);
9638 SET_EXPR_LOCATION (t, start_locus);
9639 add_stmt (t);
9642 else
9644 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9646 /* If we have an exit condition, then we build an IF with gotos either
9647 out of the loop, or to the top of it. If there's no exit condition,
9648 then we just build a jump back to the top. */
9649 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9651 if (cond && !integer_nonzerop (cond))
9653 /* Canonicalize the loop condition to the end. This means
9654 generating a branch to the loop condition. Reuse the
9655 continue label, if possible. */
9656 if (cond_is_first)
9658 if (incr || !clab)
9660 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9661 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9663 else
9664 t = build1 (GOTO_EXPR, void_type_node, clab);
9665 SET_EXPR_LOCATION (t, start_locus);
9666 add_stmt (t);
9669 t = build_and_jump (&blab);
9670 if (cond_is_first)
9671 exit = fold_build3_loc (start_locus,
9672 COND_EXPR, void_type_node, cond, exit, t);
9673 else
9674 exit = fold_build3_loc (input_location,
9675 COND_EXPR, void_type_node, cond, exit, t);
9678 add_stmt (top);
9681 if (body)
9682 add_stmt (body);
9683 if (clab)
9684 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9685 if (incr)
9686 add_stmt (incr);
9687 if (entry)
9688 add_stmt (entry);
9689 if (exit)
9690 add_stmt (exit);
9691 if (blab)
9692 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9695 tree
9696 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9698 bool skip;
9699 tree label = *label_p;
9701 /* In switch statements break is sometimes stylistically used after
9702 a return statement. This can lead to spurious warnings about
9703 control reaching the end of a non-void function when it is
9704 inlined. Note that we are calling block_may_fallthru with
9705 language specific tree nodes; this works because
9706 block_may_fallthru returns true when given something it does not
9707 understand. */
9708 skip = !block_may_fallthru (cur_stmt_list);
9710 if (!label)
9712 if (!skip)
9713 *label_p = label = create_artificial_label (loc);
9715 else if (TREE_CODE (label) == LABEL_DECL)
9717 else switch (TREE_INT_CST_LOW (label))
9719 case 0:
9720 if (is_break)
9721 error_at (loc, "break statement not within loop or switch");
9722 else
9723 error_at (loc, "continue statement not within a loop");
9724 return NULL_TREE;
9726 case 1:
9727 gcc_assert (is_break);
9728 error_at (loc, "break statement used with OpenMP for loop");
9729 return NULL_TREE;
9731 case 2:
9732 if (is_break)
9733 error ("break statement within %<#pragma simd%> loop body");
9734 else
9735 error ("continue statement within %<#pragma simd%> loop body");
9736 return NULL_TREE;
9738 default:
9739 gcc_unreachable ();
9742 if (skip)
9743 return NULL_TREE;
9745 if (!is_break)
9746 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9748 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9751 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9753 static void
9754 emit_side_effect_warnings (location_t loc, tree expr)
9756 if (expr == error_mark_node)
9758 else if (!TREE_SIDE_EFFECTS (expr))
9760 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9761 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9763 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9765 tree r = expr;
9766 location_t cloc = loc;
9767 while (TREE_CODE (r) == COMPOUND_EXPR)
9769 if (EXPR_HAS_LOCATION (r))
9770 cloc = EXPR_LOCATION (r);
9771 r = TREE_OPERAND (r, 1);
9773 if (!TREE_SIDE_EFFECTS (r)
9774 && !VOID_TYPE_P (TREE_TYPE (r))
9775 && !CONVERT_EXPR_P (r)
9776 && !TREE_NO_WARNING (r)
9777 && !TREE_NO_WARNING (expr))
9778 warning_at (cloc, OPT_Wunused_value,
9779 "right-hand operand of comma expression has no effect");
9781 else
9782 warn_if_unused_value (expr, loc);
9785 /* Process an expression as if it were a complete statement. Emit
9786 diagnostics, but do not call ADD_STMT. LOC is the location of the
9787 statement. */
9789 tree
9790 c_process_expr_stmt (location_t loc, tree expr)
9792 tree exprv;
9794 if (!expr)
9795 return NULL_TREE;
9797 expr = c_fully_fold (expr, false, NULL);
9799 if (warn_sequence_point)
9800 verify_sequence_points (expr);
9802 if (TREE_TYPE (expr) != error_mark_node
9803 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9804 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9805 error_at (loc, "expression statement has incomplete type");
9807 /* If we're not processing a statement expression, warn about unused values.
9808 Warnings for statement expressions will be emitted later, once we figure
9809 out which is the result. */
9810 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9811 && warn_unused_value)
9812 emit_side_effect_warnings (loc, expr);
9814 exprv = expr;
9815 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9816 exprv = TREE_OPERAND (exprv, 1);
9817 while (CONVERT_EXPR_P (exprv))
9818 exprv = TREE_OPERAND (exprv, 0);
9819 if (DECL_P (exprv)
9820 || handled_component_p (exprv)
9821 || TREE_CODE (exprv) == ADDR_EXPR)
9822 mark_exp_read (exprv);
9824 /* If the expression is not of a type to which we cannot assign a line
9825 number, wrap the thing in a no-op NOP_EXPR. */
9826 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9828 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9829 SET_EXPR_LOCATION (expr, loc);
9832 return expr;
9835 /* Emit an expression as a statement. LOC is the location of the
9836 expression. */
9838 tree
9839 c_finish_expr_stmt (location_t loc, tree expr)
9841 if (expr)
9842 return add_stmt (c_process_expr_stmt (loc, expr));
9843 else
9844 return NULL;
9847 /* Do the opposite and emit a statement as an expression. To begin,
9848 create a new binding level and return it. */
9850 tree
9851 c_begin_stmt_expr (void)
9853 tree ret;
9855 /* We must force a BLOCK for this level so that, if it is not expanded
9856 later, there is a way to turn off the entire subtree of blocks that
9857 are contained in it. */
9858 keep_next_level ();
9859 ret = c_begin_compound_stmt (true);
9861 c_bindings_start_stmt_expr (c_switch_stack == NULL
9862 ? NULL
9863 : c_switch_stack->bindings);
9865 /* Mark the current statement list as belonging to a statement list. */
9866 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9868 return ret;
9871 /* LOC is the location of the compound statement to which this body
9872 belongs. */
9874 tree
9875 c_finish_stmt_expr (location_t loc, tree body)
9877 tree last, type, tmp, val;
9878 tree *last_p;
9880 body = c_end_compound_stmt (loc, body, true);
9882 c_bindings_end_stmt_expr (c_switch_stack == NULL
9883 ? NULL
9884 : c_switch_stack->bindings);
9886 /* Locate the last statement in BODY. See c_end_compound_stmt
9887 about always returning a BIND_EXPR. */
9888 last_p = &BIND_EXPR_BODY (body);
9889 last = BIND_EXPR_BODY (body);
9891 continue_searching:
9892 if (TREE_CODE (last) == STATEMENT_LIST)
9894 tree_stmt_iterator i;
9896 /* This can happen with degenerate cases like ({ }). No value. */
9897 if (!TREE_SIDE_EFFECTS (last))
9898 return body;
9900 /* If we're supposed to generate side effects warnings, process
9901 all of the statements except the last. */
9902 if (warn_unused_value)
9904 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9906 location_t tloc;
9907 tree t = tsi_stmt (i);
9909 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9910 emit_side_effect_warnings (tloc, t);
9913 else
9914 i = tsi_last (last);
9915 last_p = tsi_stmt_ptr (i);
9916 last = *last_p;
9919 /* If the end of the list is exception related, then the list was split
9920 by a call to push_cleanup. Continue searching. */
9921 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9922 || TREE_CODE (last) == TRY_CATCH_EXPR)
9924 last_p = &TREE_OPERAND (last, 0);
9925 last = *last_p;
9926 goto continue_searching;
9929 if (last == error_mark_node)
9930 return last;
9932 /* In the case that the BIND_EXPR is not necessary, return the
9933 expression out from inside it. */
9934 if (last == BIND_EXPR_BODY (body)
9935 && BIND_EXPR_VARS (body) == NULL)
9937 /* Even if this looks constant, do not allow it in a constant
9938 expression. */
9939 last = c_wrap_maybe_const (last, true);
9940 /* Do not warn if the return value of a statement expression is
9941 unused. */
9942 TREE_NO_WARNING (last) = 1;
9943 return last;
9946 /* Extract the type of said expression. */
9947 type = TREE_TYPE (last);
9949 /* If we're not returning a value at all, then the BIND_EXPR that
9950 we already have is a fine expression to return. */
9951 if (!type || VOID_TYPE_P (type))
9952 return body;
9954 /* Now that we've located the expression containing the value, it seems
9955 silly to make voidify_wrapper_expr repeat the process. Create a
9956 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9957 tmp = create_tmp_var_raw (type, NULL);
9959 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9960 tree_expr_nonnegative_p giving up immediately. */
9961 val = last;
9962 if (TREE_CODE (val) == NOP_EXPR
9963 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9964 val = TREE_OPERAND (val, 0);
9966 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9967 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9970 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9971 SET_EXPR_LOCATION (t, loc);
9972 return t;
9976 /* Begin and end compound statements. This is as simple as pushing
9977 and popping new statement lists from the tree. */
9979 tree
9980 c_begin_compound_stmt (bool do_scope)
9982 tree stmt = push_stmt_list ();
9983 if (do_scope)
9984 push_scope ();
9985 return stmt;
9988 /* End a compound statement. STMT is the statement. LOC is the
9989 location of the compound statement-- this is usually the location
9990 of the opening brace. */
9992 tree
9993 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9995 tree block = NULL;
9997 if (do_scope)
9999 if (c_dialect_objc ())
10000 objc_clear_super_receiver ();
10001 block = pop_scope ();
10004 stmt = pop_stmt_list (stmt);
10005 stmt = c_build_bind_expr (loc, block, stmt);
10007 /* If this compound statement is nested immediately inside a statement
10008 expression, then force a BIND_EXPR to be created. Otherwise we'll
10009 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10010 STATEMENT_LISTs merge, and thus we can lose track of what statement
10011 was really last. */
10012 if (building_stmt_list_p ()
10013 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10014 && TREE_CODE (stmt) != BIND_EXPR)
10016 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10017 TREE_SIDE_EFFECTS (stmt) = 1;
10018 SET_EXPR_LOCATION (stmt, loc);
10021 return stmt;
10024 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10025 when the current scope is exited. EH_ONLY is true when this is not
10026 meant to apply to normal control flow transfer. */
10028 void
10029 push_cleanup (tree decl, tree cleanup, bool eh_only)
10031 enum tree_code code;
10032 tree stmt, list;
10033 bool stmt_expr;
10035 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10036 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10037 add_stmt (stmt);
10038 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10039 list = push_stmt_list ();
10040 TREE_OPERAND (stmt, 0) = list;
10041 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10044 /* Build a binary-operation expression without default conversions.
10045 CODE is the kind of expression to build.
10046 LOCATION is the operator's location.
10047 This function differs from `build' in several ways:
10048 the data type of the result is computed and recorded in it,
10049 warnings are generated if arg data types are invalid,
10050 special handling for addition and subtraction of pointers is known,
10051 and some optimization is done (operations on narrow ints
10052 are done in the narrower type when that gives the same result).
10053 Constant folding is also done before the result is returned.
10055 Note that the operands will never have enumeral types, or function
10056 or array types, because either they will have the default conversions
10057 performed or they have both just been converted to some other type in which
10058 the arithmetic is to be done. */
10060 tree
10061 build_binary_op (location_t location, enum tree_code code,
10062 tree orig_op0, tree orig_op1, int convert_p)
10064 tree type0, type1, orig_type0, orig_type1;
10065 tree eptype;
10066 enum tree_code code0, code1;
10067 tree op0, op1;
10068 tree ret = error_mark_node;
10069 const char *invalid_op_diag;
10070 bool op0_int_operands, op1_int_operands;
10071 bool int_const, int_const_or_overflow, int_operands;
10073 /* Expression code to give to the expression when it is built.
10074 Normally this is CODE, which is what the caller asked for,
10075 but in some special cases we change it. */
10076 enum tree_code resultcode = code;
10078 /* Data type in which the computation is to be performed.
10079 In the simplest cases this is the common type of the arguments. */
10080 tree result_type = NULL;
10082 /* When the computation is in excess precision, the type of the
10083 final EXCESS_PRECISION_EXPR. */
10084 tree semantic_result_type = NULL;
10086 /* Nonzero means operands have already been type-converted
10087 in whatever way is necessary.
10088 Zero means they need to be converted to RESULT_TYPE. */
10089 int converted = 0;
10091 /* Nonzero means create the expression with this type, rather than
10092 RESULT_TYPE. */
10093 tree build_type = 0;
10095 /* Nonzero means after finally constructing the expression
10096 convert it to this type. */
10097 tree final_type = 0;
10099 /* Nonzero if this is an operation like MIN or MAX which can
10100 safely be computed in short if both args are promoted shorts.
10101 Also implies COMMON.
10102 -1 indicates a bitwise operation; this makes a difference
10103 in the exact conditions for when it is safe to do the operation
10104 in a narrower mode. */
10105 int shorten = 0;
10107 /* Nonzero if this is a comparison operation;
10108 if both args are promoted shorts, compare the original shorts.
10109 Also implies COMMON. */
10110 int short_compare = 0;
10112 /* Nonzero if this is a right-shift operation, which can be computed on the
10113 original short and then promoted if the operand is a promoted short. */
10114 int short_shift = 0;
10116 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10117 int common = 0;
10119 /* True means types are compatible as far as ObjC is concerned. */
10120 bool objc_ok;
10122 /* True means this is an arithmetic operation that may need excess
10123 precision. */
10124 bool may_need_excess_precision;
10126 /* True means this is a boolean operation that converts both its
10127 operands to truth-values. */
10128 bool boolean_op = false;
10130 /* Remember whether we're doing / or %. */
10131 bool doing_div_or_mod = false;
10133 /* Remember whether we're doing << or >>. */
10134 bool doing_shift = false;
10136 /* Tree holding instrumentation expression. */
10137 tree instrument_expr = NULL;
10139 if (location == UNKNOWN_LOCATION)
10140 location = input_location;
10142 op0 = orig_op0;
10143 op1 = orig_op1;
10145 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10146 if (op0_int_operands)
10147 op0 = remove_c_maybe_const_expr (op0);
10148 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10149 if (op1_int_operands)
10150 op1 = remove_c_maybe_const_expr (op1);
10151 int_operands = (op0_int_operands && op1_int_operands);
10152 if (int_operands)
10154 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10155 && TREE_CODE (orig_op1) == INTEGER_CST);
10156 int_const = (int_const_or_overflow
10157 && !TREE_OVERFLOW (orig_op0)
10158 && !TREE_OVERFLOW (orig_op1));
10160 else
10161 int_const = int_const_or_overflow = false;
10163 /* Do not apply default conversion in mixed vector/scalar expression. */
10164 if (convert_p
10165 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10166 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10168 op0 = default_conversion (op0);
10169 op1 = default_conversion (op1);
10172 /* When Cilk Plus is enabled and there are array notations inside op0, then
10173 we check to see if there are builtin array notation functions. If
10174 so, then we take on the type of the array notation inside it. */
10175 if (flag_cilkplus && contains_array_notation_expr (op0))
10176 orig_type0 = type0 = find_correct_array_notation_type (op0);
10177 else
10178 orig_type0 = type0 = TREE_TYPE (op0);
10180 if (flag_cilkplus && contains_array_notation_expr (op1))
10181 orig_type1 = type1 = find_correct_array_notation_type (op1);
10182 else
10183 orig_type1 = type1 = TREE_TYPE (op1);
10185 /* The expression codes of the data types of the arguments tell us
10186 whether the arguments are integers, floating, pointers, etc. */
10187 code0 = TREE_CODE (type0);
10188 code1 = TREE_CODE (type1);
10190 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10191 STRIP_TYPE_NOPS (op0);
10192 STRIP_TYPE_NOPS (op1);
10194 /* If an error was already reported for one of the arguments,
10195 avoid reporting another error. */
10197 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10198 return error_mark_node;
10200 if ((invalid_op_diag
10201 = targetm.invalid_binary_op (code, type0, type1)))
10203 error_at (location, invalid_op_diag);
10204 return error_mark_node;
10207 switch (code)
10209 case PLUS_EXPR:
10210 case MINUS_EXPR:
10211 case MULT_EXPR:
10212 case TRUNC_DIV_EXPR:
10213 case CEIL_DIV_EXPR:
10214 case FLOOR_DIV_EXPR:
10215 case ROUND_DIV_EXPR:
10216 case EXACT_DIV_EXPR:
10217 may_need_excess_precision = true;
10218 break;
10219 default:
10220 may_need_excess_precision = false;
10221 break;
10223 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10225 op0 = TREE_OPERAND (op0, 0);
10226 type0 = TREE_TYPE (op0);
10228 else if (may_need_excess_precision
10229 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10231 type0 = eptype;
10232 op0 = convert (eptype, op0);
10234 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10236 op1 = TREE_OPERAND (op1, 0);
10237 type1 = TREE_TYPE (op1);
10239 else if (may_need_excess_precision
10240 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10242 type1 = eptype;
10243 op1 = convert (eptype, op1);
10246 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10248 /* In case when one of the operands of the binary operation is
10249 a vector and another is a scalar -- convert scalar to vector. */
10250 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10252 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10253 true);
10255 switch (convert_flag)
10257 case stv_error:
10258 return error_mark_node;
10259 case stv_firstarg:
10261 bool maybe_const = true;
10262 tree sc;
10263 sc = c_fully_fold (op0, false, &maybe_const);
10264 sc = save_expr (sc);
10265 sc = convert (TREE_TYPE (type1), sc);
10266 op0 = build_vector_from_val (type1, sc);
10267 if (!maybe_const)
10268 op0 = c_wrap_maybe_const (op0, true);
10269 orig_type0 = type0 = TREE_TYPE (op0);
10270 code0 = TREE_CODE (type0);
10271 converted = 1;
10272 break;
10274 case stv_secondarg:
10276 bool maybe_const = true;
10277 tree sc;
10278 sc = c_fully_fold (op1, false, &maybe_const);
10279 sc = save_expr (sc);
10280 sc = convert (TREE_TYPE (type0), sc);
10281 op1 = build_vector_from_val (type0, sc);
10282 if (!maybe_const)
10283 op1 = c_wrap_maybe_const (op1, true);
10284 orig_type1 = type1 = TREE_TYPE (op1);
10285 code1 = TREE_CODE (type1);
10286 converted = 1;
10287 break;
10289 default:
10290 break;
10294 switch (code)
10296 case PLUS_EXPR:
10297 /* Handle the pointer + int case. */
10298 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10300 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10301 goto return_build_binary_op;
10303 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10305 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10306 goto return_build_binary_op;
10308 else
10309 common = 1;
10310 break;
10312 case MINUS_EXPR:
10313 /* Subtraction of two similar pointers.
10314 We must subtract them as integers, then divide by object size. */
10315 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10316 && comp_target_types (location, type0, type1))
10318 ret = pointer_diff (location, op0, op1);
10319 goto return_build_binary_op;
10321 /* Handle pointer minus int. Just like pointer plus int. */
10322 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10324 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10325 goto return_build_binary_op;
10327 else
10328 common = 1;
10329 break;
10331 case MULT_EXPR:
10332 common = 1;
10333 break;
10335 case TRUNC_DIV_EXPR:
10336 case CEIL_DIV_EXPR:
10337 case FLOOR_DIV_EXPR:
10338 case ROUND_DIV_EXPR:
10339 case EXACT_DIV_EXPR:
10340 doing_div_or_mod = true;
10341 warn_for_div_by_zero (location, op1);
10343 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10344 || code0 == FIXED_POINT_TYPE
10345 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10346 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10347 || code1 == FIXED_POINT_TYPE
10348 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10350 enum tree_code tcode0 = code0, tcode1 = code1;
10352 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10353 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10354 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10355 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10357 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10358 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10359 resultcode = RDIV_EXPR;
10360 else
10361 /* Although it would be tempting to shorten always here, that
10362 loses on some targets, since the modulo instruction is
10363 undefined if the quotient can't be represented in the
10364 computation mode. We shorten only if unsigned or if
10365 dividing by something we know != -1. */
10366 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10367 || (TREE_CODE (op1) == INTEGER_CST
10368 && !integer_all_onesp (op1)));
10369 common = 1;
10371 break;
10373 case BIT_AND_EXPR:
10374 case BIT_IOR_EXPR:
10375 case BIT_XOR_EXPR:
10376 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10377 shorten = -1;
10378 /* Allow vector types which are not floating point types. */
10379 else if (code0 == VECTOR_TYPE
10380 && code1 == VECTOR_TYPE
10381 && !VECTOR_FLOAT_TYPE_P (type0)
10382 && !VECTOR_FLOAT_TYPE_P (type1))
10383 common = 1;
10384 break;
10386 case TRUNC_MOD_EXPR:
10387 case FLOOR_MOD_EXPR:
10388 doing_div_or_mod = true;
10389 warn_for_div_by_zero (location, op1);
10391 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10392 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10393 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10394 common = 1;
10395 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10397 /* Although it would be tempting to shorten always here, that loses
10398 on some targets, since the modulo instruction is undefined if the
10399 quotient can't be represented in the computation mode. We shorten
10400 only if unsigned or if dividing by something we know != -1. */
10401 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10402 || (TREE_CODE (op1) == INTEGER_CST
10403 && !integer_all_onesp (op1)));
10404 common = 1;
10406 break;
10408 case TRUTH_ANDIF_EXPR:
10409 case TRUTH_ORIF_EXPR:
10410 case TRUTH_AND_EXPR:
10411 case TRUTH_OR_EXPR:
10412 case TRUTH_XOR_EXPR:
10413 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10414 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10415 || code0 == FIXED_POINT_TYPE)
10416 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10417 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10418 || code1 == FIXED_POINT_TYPE))
10420 /* Result of these operations is always an int,
10421 but that does not mean the operands should be
10422 converted to ints! */
10423 result_type = integer_type_node;
10424 if (op0_int_operands)
10426 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10427 op0 = remove_c_maybe_const_expr (op0);
10429 else
10430 op0 = c_objc_common_truthvalue_conversion (location, op0);
10431 if (op1_int_operands)
10433 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10434 op1 = remove_c_maybe_const_expr (op1);
10436 else
10437 op1 = c_objc_common_truthvalue_conversion (location, op1);
10438 converted = 1;
10439 boolean_op = true;
10441 if (code == TRUTH_ANDIF_EXPR)
10443 int_const_or_overflow = (int_operands
10444 && TREE_CODE (orig_op0) == INTEGER_CST
10445 && (op0 == truthvalue_false_node
10446 || TREE_CODE (orig_op1) == INTEGER_CST));
10447 int_const = (int_const_or_overflow
10448 && !TREE_OVERFLOW (orig_op0)
10449 && (op0 == truthvalue_false_node
10450 || !TREE_OVERFLOW (orig_op1)));
10452 else if (code == TRUTH_ORIF_EXPR)
10454 int_const_or_overflow = (int_operands
10455 && TREE_CODE (orig_op0) == INTEGER_CST
10456 && (op0 == truthvalue_true_node
10457 || TREE_CODE (orig_op1) == INTEGER_CST));
10458 int_const = (int_const_or_overflow
10459 && !TREE_OVERFLOW (orig_op0)
10460 && (op0 == truthvalue_true_node
10461 || !TREE_OVERFLOW (orig_op1)));
10463 break;
10465 /* Shift operations: result has same type as first operand;
10466 always convert second operand to int.
10467 Also set SHORT_SHIFT if shifting rightward. */
10469 case RSHIFT_EXPR:
10470 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10471 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10473 result_type = type0;
10474 converted = 1;
10476 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10477 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10478 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10479 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10481 result_type = type0;
10482 converted = 1;
10484 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10485 && code1 == INTEGER_TYPE)
10487 doing_shift = true;
10488 if (TREE_CODE (op1) == INTEGER_CST)
10490 if (tree_int_cst_sgn (op1) < 0)
10492 int_const = false;
10493 if (c_inhibit_evaluation_warnings == 0)
10494 warning_at (location, 0, "right shift count is negative");
10496 else
10498 if (!integer_zerop (op1))
10499 short_shift = 1;
10501 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10503 int_const = false;
10504 if (c_inhibit_evaluation_warnings == 0)
10505 warning_at (location, 0, "right shift count >= width "
10506 "of type");
10511 /* Use the type of the value to be shifted. */
10512 result_type = type0;
10513 /* Convert the non vector shift-count to an integer, regardless
10514 of size of value being shifted. */
10515 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10516 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10517 op1 = convert (integer_type_node, op1);
10518 /* Avoid converting op1 to result_type later. */
10519 converted = 1;
10521 break;
10523 case LSHIFT_EXPR:
10524 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10525 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10527 result_type = type0;
10528 converted = 1;
10530 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10531 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10532 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10533 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10535 result_type = type0;
10536 converted = 1;
10538 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10539 && code1 == INTEGER_TYPE)
10541 doing_shift = true;
10542 if (TREE_CODE (op1) == INTEGER_CST)
10544 if (tree_int_cst_sgn (op1) < 0)
10546 int_const = false;
10547 if (c_inhibit_evaluation_warnings == 0)
10548 warning_at (location, 0, "left shift count is negative");
10551 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10553 int_const = false;
10554 if (c_inhibit_evaluation_warnings == 0)
10555 warning_at (location, 0, "left shift count >= width of "
10556 "type");
10560 /* Use the type of the value to be shifted. */
10561 result_type = type0;
10562 /* Convert the non vector shift-count to an integer, regardless
10563 of size of value being shifted. */
10564 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10565 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10566 op1 = convert (integer_type_node, op1);
10567 /* Avoid converting op1 to result_type later. */
10568 converted = 1;
10570 break;
10572 case EQ_EXPR:
10573 case NE_EXPR:
10574 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10576 tree intt;
10577 if (!vector_types_compatible_elements_p (type0, type1))
10579 error_at (location, "comparing vectors with different "
10580 "element types");
10581 return error_mark_node;
10584 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10586 error_at (location, "comparing vectors with different "
10587 "number of elements");
10588 return error_mark_node;
10591 /* Always construct signed integer vector type. */
10592 intt = c_common_type_for_size (GET_MODE_BITSIZE
10593 (TYPE_MODE (TREE_TYPE (type0))), 0);
10594 result_type = build_opaque_vector_type (intt,
10595 TYPE_VECTOR_SUBPARTS (type0));
10596 converted = 1;
10597 break;
10599 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10600 warning_at (location,
10601 OPT_Wfloat_equal,
10602 "comparing floating point with == or != is unsafe");
10603 /* Result of comparison is always int,
10604 but don't convert the args to int! */
10605 build_type = integer_type_node;
10606 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10607 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10608 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10609 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10610 short_compare = 1;
10611 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10613 if (TREE_CODE (op0) == ADDR_EXPR
10614 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10616 if (code == EQ_EXPR)
10617 warning_at (location,
10618 OPT_Waddress,
10619 "the comparison will always evaluate as %<false%> "
10620 "for the address of %qD will never be NULL",
10621 TREE_OPERAND (op0, 0));
10622 else
10623 warning_at (location,
10624 OPT_Waddress,
10625 "the comparison will always evaluate as %<true%> "
10626 "for the address of %qD will never be NULL",
10627 TREE_OPERAND (op0, 0));
10629 result_type = type0;
10631 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10633 if (TREE_CODE (op1) == ADDR_EXPR
10634 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10636 if (code == EQ_EXPR)
10637 warning_at (location,
10638 OPT_Waddress,
10639 "the comparison will always evaluate as %<false%> "
10640 "for the address of %qD will never be NULL",
10641 TREE_OPERAND (op1, 0));
10642 else
10643 warning_at (location,
10644 OPT_Waddress,
10645 "the comparison will always evaluate as %<true%> "
10646 "for the address of %qD will never be NULL",
10647 TREE_OPERAND (op1, 0));
10649 result_type = type1;
10651 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10653 tree tt0 = TREE_TYPE (type0);
10654 tree tt1 = TREE_TYPE (type1);
10655 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10656 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10657 addr_space_t as_common = ADDR_SPACE_GENERIC;
10659 /* Anything compares with void *. void * compares with anything.
10660 Otherwise, the targets must be compatible
10661 and both must be object or both incomplete. */
10662 if (comp_target_types (location, type0, type1))
10663 result_type = common_pointer_type (type0, type1);
10664 else if (!addr_space_superset (as0, as1, &as_common))
10666 error_at (location, "comparison of pointers to "
10667 "disjoint address spaces");
10668 return error_mark_node;
10670 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10672 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10673 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10674 "comparison of %<void *%> with function pointer");
10676 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10678 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10679 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10680 "comparison of %<void *%> with function pointer");
10682 else
10683 /* Avoid warning about the volatile ObjC EH puts on decls. */
10684 if (!objc_ok)
10685 pedwarn (location, 0,
10686 "comparison of distinct pointer types lacks a cast");
10688 if (result_type == NULL_TREE)
10690 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10691 result_type = build_pointer_type
10692 (build_qualified_type (void_type_node, qual));
10695 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10697 result_type = type0;
10698 pedwarn (location, 0, "comparison between pointer and integer");
10700 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10702 result_type = type1;
10703 pedwarn (location, 0, "comparison between pointer and integer");
10705 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10706 || truth_value_p (TREE_CODE (orig_op0)))
10707 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10708 || truth_value_p (TREE_CODE (orig_op1))))
10709 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10710 break;
10712 case LE_EXPR:
10713 case GE_EXPR:
10714 case LT_EXPR:
10715 case GT_EXPR:
10716 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10718 tree intt;
10719 if (!vector_types_compatible_elements_p (type0, type1))
10721 error_at (location, "comparing vectors with different "
10722 "element types");
10723 return error_mark_node;
10726 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10728 error_at (location, "comparing vectors with different "
10729 "number of elements");
10730 return error_mark_node;
10733 /* Always construct signed integer vector type. */
10734 intt = c_common_type_for_size (GET_MODE_BITSIZE
10735 (TYPE_MODE (TREE_TYPE (type0))), 0);
10736 result_type = build_opaque_vector_type (intt,
10737 TYPE_VECTOR_SUBPARTS (type0));
10738 converted = 1;
10739 break;
10741 build_type = integer_type_node;
10742 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10743 || code0 == FIXED_POINT_TYPE)
10744 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10745 || code1 == FIXED_POINT_TYPE))
10746 short_compare = 1;
10747 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10749 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10750 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10751 addr_space_t as_common;
10753 if (comp_target_types (location, type0, type1))
10755 result_type = common_pointer_type (type0, type1);
10756 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10757 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10758 pedwarn (location, 0,
10759 "comparison of complete and incomplete pointers");
10760 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10761 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10762 "ordered comparisons of pointers to functions");
10763 else if (null_pointer_constant_p (orig_op0)
10764 || null_pointer_constant_p (orig_op1))
10765 warning_at (location, OPT_Wextra,
10766 "ordered comparison of pointer with null pointer");
10769 else if (!addr_space_superset (as0, as1, &as_common))
10771 error_at (location, "comparison of pointers to "
10772 "disjoint address spaces");
10773 return error_mark_node;
10775 else
10777 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10778 result_type = build_pointer_type
10779 (build_qualified_type (void_type_node, qual));
10780 pedwarn (location, 0,
10781 "comparison of distinct pointer types lacks a cast");
10784 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10786 result_type = type0;
10787 if (pedantic)
10788 pedwarn (location, OPT_Wpedantic,
10789 "ordered comparison of pointer with integer zero");
10790 else if (extra_warnings)
10791 warning_at (location, OPT_Wextra,
10792 "ordered comparison of pointer with integer zero");
10794 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10796 result_type = type1;
10797 if (pedantic)
10798 pedwarn (location, OPT_Wpedantic,
10799 "ordered comparison of pointer with integer zero");
10800 else if (extra_warnings)
10801 warning_at (location, OPT_Wextra,
10802 "ordered comparison of pointer with integer zero");
10804 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10806 result_type = type0;
10807 pedwarn (location, 0, "comparison between pointer and integer");
10809 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10811 result_type = type1;
10812 pedwarn (location, 0, "comparison between pointer and integer");
10814 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10815 || truth_value_p (TREE_CODE (orig_op0)))
10816 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10817 || truth_value_p (TREE_CODE (orig_op1))))
10818 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10819 break;
10821 default:
10822 gcc_unreachable ();
10825 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10826 return error_mark_node;
10828 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10829 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10830 || !vector_types_compatible_elements_p (type0, type1)))
10832 binary_op_error (location, code, type0, type1);
10833 return error_mark_node;
10836 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10837 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10839 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10840 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10842 bool first_complex = (code0 == COMPLEX_TYPE);
10843 bool second_complex = (code1 == COMPLEX_TYPE);
10844 int none_complex = (!first_complex && !second_complex);
10846 if (shorten || common || short_compare)
10848 result_type = c_common_type (type0, type1);
10849 do_warn_double_promotion (result_type, type0, type1,
10850 "implicit conversion from %qT to %qT "
10851 "to match other operand of binary "
10852 "expression",
10853 location);
10854 if (result_type == error_mark_node)
10855 return error_mark_node;
10858 if (first_complex != second_complex
10859 && (code == PLUS_EXPR
10860 || code == MINUS_EXPR
10861 || code == MULT_EXPR
10862 || (code == TRUNC_DIV_EXPR && first_complex))
10863 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10864 && flag_signed_zeros)
10866 /* An operation on mixed real/complex operands must be
10867 handled specially, but the language-independent code can
10868 more easily optimize the plain complex arithmetic if
10869 -fno-signed-zeros. */
10870 tree real_type = TREE_TYPE (result_type);
10871 tree real, imag;
10872 if (type0 != orig_type0 || type1 != orig_type1)
10874 gcc_assert (may_need_excess_precision && common);
10875 semantic_result_type = c_common_type (orig_type0, orig_type1);
10877 if (first_complex)
10879 if (TREE_TYPE (op0) != result_type)
10880 op0 = convert_and_check (location, result_type, op0);
10881 if (TREE_TYPE (op1) != real_type)
10882 op1 = convert_and_check (location, real_type, op1);
10884 else
10886 if (TREE_TYPE (op0) != real_type)
10887 op0 = convert_and_check (location, real_type, op0);
10888 if (TREE_TYPE (op1) != result_type)
10889 op1 = convert_and_check (location, result_type, op1);
10891 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10892 return error_mark_node;
10893 if (first_complex)
10895 op0 = c_save_expr (op0);
10896 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10897 op0, 1);
10898 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10899 op0, 1);
10900 switch (code)
10902 case MULT_EXPR:
10903 case TRUNC_DIV_EXPR:
10904 op1 = c_save_expr (op1);
10905 imag = build2 (resultcode, real_type, imag, op1);
10906 /* Fall through. */
10907 case PLUS_EXPR:
10908 case MINUS_EXPR:
10909 real = build2 (resultcode, real_type, real, op1);
10910 break;
10911 default:
10912 gcc_unreachable();
10915 else
10917 op1 = c_save_expr (op1);
10918 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10919 op1, 1);
10920 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10921 op1, 1);
10922 switch (code)
10924 case MULT_EXPR:
10925 op0 = c_save_expr (op0);
10926 imag = build2 (resultcode, real_type, op0, imag);
10927 /* Fall through. */
10928 case PLUS_EXPR:
10929 real = build2 (resultcode, real_type, op0, real);
10930 break;
10931 case MINUS_EXPR:
10932 real = build2 (resultcode, real_type, op0, real);
10933 imag = build1 (NEGATE_EXPR, real_type, imag);
10934 break;
10935 default:
10936 gcc_unreachable();
10939 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10940 goto return_build_binary_op;
10943 /* For certain operations (which identify themselves by shorten != 0)
10944 if both args were extended from the same smaller type,
10945 do the arithmetic in that type and then extend.
10947 shorten !=0 and !=1 indicates a bitwise operation.
10948 For them, this optimization is safe only if
10949 both args are zero-extended or both are sign-extended.
10950 Otherwise, we might change the result.
10951 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10952 but calculated in (unsigned short) it would be (unsigned short)-1. */
10954 if (shorten && none_complex)
10956 final_type = result_type;
10957 result_type = shorten_binary_op (result_type, op0, op1,
10958 shorten == -1);
10961 /* Shifts can be shortened if shifting right. */
10963 if (short_shift)
10965 int unsigned_arg;
10966 tree arg0 = get_narrower (op0, &unsigned_arg);
10968 final_type = result_type;
10970 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10971 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10973 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10974 && tree_int_cst_sgn (op1) > 0
10975 /* We can shorten only if the shift count is less than the
10976 number of bits in the smaller type size. */
10977 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10978 /* We cannot drop an unsigned shift after sign-extension. */
10979 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10981 /* Do an unsigned shift if the operand was zero-extended. */
10982 result_type
10983 = c_common_signed_or_unsigned_type (unsigned_arg,
10984 TREE_TYPE (arg0));
10985 /* Convert value-to-be-shifted to that type. */
10986 if (TREE_TYPE (op0) != result_type)
10987 op0 = convert (result_type, op0);
10988 converted = 1;
10992 /* Comparison operations are shortened too but differently.
10993 They identify themselves by setting short_compare = 1. */
10995 if (short_compare)
10997 /* Don't write &op0, etc., because that would prevent op0
10998 from being kept in a register.
10999 Instead, make copies of the our local variables and
11000 pass the copies by reference, then copy them back afterward. */
11001 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11002 enum tree_code xresultcode = resultcode;
11003 tree val
11004 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11005 &xresultcode);
11007 if (val != 0)
11009 ret = val;
11010 goto return_build_binary_op;
11013 op0 = xop0, op1 = xop1;
11014 converted = 1;
11015 resultcode = xresultcode;
11017 if (c_inhibit_evaluation_warnings == 0)
11019 bool op0_maybe_const = true;
11020 bool op1_maybe_const = true;
11021 tree orig_op0_folded, orig_op1_folded;
11023 if (in_late_binary_op)
11025 orig_op0_folded = orig_op0;
11026 orig_op1_folded = orig_op1;
11028 else
11030 /* Fold for the sake of possible warnings, as in
11031 build_conditional_expr. This requires the
11032 "original" values to be folded, not just op0 and
11033 op1. */
11034 c_inhibit_evaluation_warnings++;
11035 op0 = c_fully_fold (op0, require_constant_value,
11036 &op0_maybe_const);
11037 op1 = c_fully_fold (op1, require_constant_value,
11038 &op1_maybe_const);
11039 c_inhibit_evaluation_warnings--;
11040 orig_op0_folded = c_fully_fold (orig_op0,
11041 require_constant_value,
11042 NULL);
11043 orig_op1_folded = c_fully_fold (orig_op1,
11044 require_constant_value,
11045 NULL);
11048 if (warn_sign_compare)
11049 warn_for_sign_compare (location, orig_op0_folded,
11050 orig_op1_folded, op0, op1,
11051 result_type, resultcode);
11052 if (!in_late_binary_op && !int_operands)
11054 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11055 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11056 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11057 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11063 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11064 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11065 Then the expression will be built.
11066 It will be given type FINAL_TYPE if that is nonzero;
11067 otherwise, it will be given type RESULT_TYPE. */
11069 if (!result_type)
11071 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11072 return error_mark_node;
11075 if (build_type == NULL_TREE)
11077 build_type = result_type;
11078 if ((type0 != orig_type0 || type1 != orig_type1)
11079 && !boolean_op)
11081 gcc_assert (may_need_excess_precision && common);
11082 semantic_result_type = c_common_type (orig_type0, orig_type1);
11086 if (!converted)
11088 op0 = ep_convert_and_check (location, result_type, op0,
11089 semantic_result_type);
11090 op1 = ep_convert_and_check (location, result_type, op1,
11091 semantic_result_type);
11093 /* This can happen if one operand has a vector type, and the other
11094 has a different type. */
11095 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11096 return error_mark_node;
11099 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11100 | SANITIZE_FLOAT_DIVIDE))
11101 && current_function_decl != 0
11102 && !lookup_attribute ("no_sanitize_undefined",
11103 DECL_ATTRIBUTES (current_function_decl))
11104 && (doing_div_or_mod || doing_shift))
11106 /* OP0 and/or OP1 might have side-effects. */
11107 op0 = c_save_expr (op0);
11108 op1 = c_save_expr (op1);
11109 op0 = c_fully_fold (op0, false, NULL);
11110 op1 = c_fully_fold (op1, false, NULL);
11111 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11112 | SANITIZE_FLOAT_DIVIDE)))
11113 instrument_expr = ubsan_instrument_division (location, op0, op1);
11114 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11115 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11118 /* Treat expressions in initializers specially as they can't trap. */
11119 if (int_const_or_overflow)
11120 ret = (require_constant_value
11121 ? fold_build2_initializer_loc (location, resultcode, build_type,
11122 op0, op1)
11123 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11124 else
11125 ret = build2 (resultcode, build_type, op0, op1);
11126 if (final_type != 0)
11127 ret = convert (final_type, ret);
11129 return_build_binary_op:
11130 gcc_assert (ret != error_mark_node);
11131 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11132 ret = (int_operands
11133 ? note_integer_operands (ret)
11134 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11135 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11136 && !in_late_binary_op)
11137 ret = note_integer_operands (ret);
11138 if (semantic_result_type)
11139 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11140 protected_set_expr_location (ret, location);
11142 if (instrument_expr != NULL)
11143 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11144 instrument_expr, ret);
11146 return ret;
11150 /* Convert EXPR to be a truth-value, validating its type for this
11151 purpose. LOCATION is the source location for the expression. */
11153 tree
11154 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11156 bool int_const, int_operands;
11158 switch (TREE_CODE (TREE_TYPE (expr)))
11160 case ARRAY_TYPE:
11161 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11162 return error_mark_node;
11164 case RECORD_TYPE:
11165 error_at (location, "used struct type value where scalar is required");
11166 return error_mark_node;
11168 case UNION_TYPE:
11169 error_at (location, "used union type value where scalar is required");
11170 return error_mark_node;
11172 case VOID_TYPE:
11173 error_at (location, "void value not ignored as it ought to be");
11174 return error_mark_node;
11176 case FUNCTION_TYPE:
11177 gcc_unreachable ();
11179 case VECTOR_TYPE:
11180 error_at (location, "used vector type where scalar is required");
11181 return error_mark_node;
11183 default:
11184 break;
11187 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11188 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11189 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11191 expr = remove_c_maybe_const_expr (expr);
11192 expr = build2 (NE_EXPR, integer_type_node, expr,
11193 convert (TREE_TYPE (expr), integer_zero_node));
11194 expr = note_integer_operands (expr);
11196 else
11197 /* ??? Should we also give an error for vectors rather than leaving
11198 those to give errors later? */
11199 expr = c_common_truthvalue_conversion (location, expr);
11201 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11203 if (TREE_OVERFLOW (expr))
11204 return expr;
11205 else
11206 return note_integer_operands (expr);
11208 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11209 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11210 return expr;
11214 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11215 required. */
11217 tree
11218 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11220 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11222 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11223 /* Executing a compound literal inside a function reinitializes
11224 it. */
11225 if (!TREE_STATIC (decl))
11226 *se = true;
11227 return decl;
11229 else
11230 return expr;
11233 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11235 tree
11236 c_begin_omp_parallel (void)
11238 tree block;
11240 keep_next_level ();
11241 block = c_begin_compound_stmt (true);
11243 return block;
11246 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11247 statement. LOC is the location of the OMP_PARALLEL. */
11249 tree
11250 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11252 tree stmt;
11254 block = c_end_compound_stmt (loc, block, true);
11256 stmt = make_node (OMP_PARALLEL);
11257 TREE_TYPE (stmt) = void_type_node;
11258 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11259 OMP_PARALLEL_BODY (stmt) = block;
11260 SET_EXPR_LOCATION (stmt, loc);
11262 return add_stmt (stmt);
11265 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11267 tree
11268 c_begin_omp_task (void)
11270 tree block;
11272 keep_next_level ();
11273 block = c_begin_compound_stmt (true);
11275 return block;
11278 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11279 statement. LOC is the location of the #pragma. */
11281 tree
11282 c_finish_omp_task (location_t loc, tree clauses, tree block)
11284 tree stmt;
11286 block = c_end_compound_stmt (loc, block, true);
11288 stmt = make_node (OMP_TASK);
11289 TREE_TYPE (stmt) = void_type_node;
11290 OMP_TASK_CLAUSES (stmt) = clauses;
11291 OMP_TASK_BODY (stmt) = block;
11292 SET_EXPR_LOCATION (stmt, loc);
11294 return add_stmt (stmt);
11297 /* Generate GOMP_cancel call for #pragma omp cancel. */
11299 void
11300 c_finish_omp_cancel (location_t loc, tree clauses)
11302 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11303 int mask = 0;
11304 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11305 mask = 1;
11306 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11307 mask = 2;
11308 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11309 mask = 4;
11310 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11311 mask = 8;
11312 else
11314 error_at (loc, "%<#pragma omp cancel must specify one of "
11315 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11316 "clauses");
11317 return;
11319 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11320 if (ifc != NULL_TREE)
11322 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11323 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11324 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11325 build_zero_cst (type));
11327 else
11328 ifc = boolean_true_node;
11329 tree stmt = build_call_expr_loc (loc, fn, 2,
11330 build_int_cst (integer_type_node, mask),
11331 ifc);
11332 add_stmt (stmt);
11335 /* Generate GOMP_cancellation_point call for
11336 #pragma omp cancellation point. */
11338 void
11339 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11341 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11342 int mask = 0;
11343 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11344 mask = 1;
11345 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11346 mask = 2;
11347 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11348 mask = 4;
11349 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11350 mask = 8;
11351 else
11353 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11354 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11355 "clauses");
11356 return;
11358 tree stmt = build_call_expr_loc (loc, fn, 1,
11359 build_int_cst (integer_type_node, mask));
11360 add_stmt (stmt);
11363 /* Helper function for handle_omp_array_sections. Called recursively
11364 to handle multiple array-section-subscripts. C is the clause,
11365 T current expression (initially OMP_CLAUSE_DECL), which is either
11366 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11367 expression if specified, TREE_VALUE length expression if specified,
11368 TREE_CHAIN is what it has been specified after, or some decl.
11369 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11370 set to true if any of the array-section-subscript could have length
11371 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11372 first array-section-subscript which is known not to have length
11373 of one. Given say:
11374 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11375 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11376 all are or may have length of 1, array-section-subscript [:2] is the
11377 first one knonwn not to have length 1. For array-section-subscript
11378 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11379 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11380 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11381 case though, as some lengths could be zero. */
11383 static tree
11384 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11385 bool &maybe_zero_len, unsigned int &first_non_one)
11387 tree ret, low_bound, length, type;
11388 if (TREE_CODE (t) != TREE_LIST)
11390 if (error_operand_p (t))
11391 return error_mark_node;
11392 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11394 if (DECL_P (t))
11395 error_at (OMP_CLAUSE_LOCATION (c),
11396 "%qD is not a variable in %qs clause", t,
11397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11398 else
11399 error_at (OMP_CLAUSE_LOCATION (c),
11400 "%qE is not a variable in %qs clause", t,
11401 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11402 return error_mark_node;
11404 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11405 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11407 error_at (OMP_CLAUSE_LOCATION (c),
11408 "%qD is threadprivate variable in %qs clause", t,
11409 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11410 return error_mark_node;
11412 return t;
11415 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11416 maybe_zero_len, first_non_one);
11417 if (ret == error_mark_node || ret == NULL_TREE)
11418 return ret;
11420 type = TREE_TYPE (ret);
11421 low_bound = TREE_PURPOSE (t);
11422 length = TREE_VALUE (t);
11424 if (low_bound == error_mark_node || length == error_mark_node)
11425 return error_mark_node;
11427 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11429 error_at (OMP_CLAUSE_LOCATION (c),
11430 "low bound %qE of array section does not have integral type",
11431 low_bound);
11432 return error_mark_node;
11434 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11436 error_at (OMP_CLAUSE_LOCATION (c),
11437 "length %qE of array section does not have integral type",
11438 length);
11439 return error_mark_node;
11441 if (low_bound
11442 && TREE_CODE (low_bound) == INTEGER_CST
11443 && TYPE_PRECISION (TREE_TYPE (low_bound))
11444 > TYPE_PRECISION (sizetype))
11445 low_bound = fold_convert (sizetype, low_bound);
11446 if (length
11447 && TREE_CODE (length) == INTEGER_CST
11448 && TYPE_PRECISION (TREE_TYPE (length))
11449 > TYPE_PRECISION (sizetype))
11450 length = fold_convert (sizetype, length);
11451 if (low_bound == NULL_TREE)
11452 low_bound = integer_zero_node;
11454 if (length != NULL_TREE)
11456 if (!integer_nonzerop (length))
11457 maybe_zero_len = true;
11458 if (first_non_one == types.length ()
11459 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11460 first_non_one++;
11462 if (TREE_CODE (type) == ARRAY_TYPE)
11464 if (length == NULL_TREE
11465 && (TYPE_DOMAIN (type) == NULL_TREE
11466 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11468 error_at (OMP_CLAUSE_LOCATION (c),
11469 "for unknown bound array type length expression must "
11470 "be specified");
11471 return error_mark_node;
11473 if (TREE_CODE (low_bound) == INTEGER_CST
11474 && tree_int_cst_sgn (low_bound) == -1)
11476 error_at (OMP_CLAUSE_LOCATION (c),
11477 "negative low bound in array section in %qs clause",
11478 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11479 return error_mark_node;
11481 if (length != NULL_TREE
11482 && TREE_CODE (length) == INTEGER_CST
11483 && tree_int_cst_sgn (length) == -1)
11485 error_at (OMP_CLAUSE_LOCATION (c),
11486 "negative length in array section in %qs clause",
11487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11488 return error_mark_node;
11490 if (TYPE_DOMAIN (type)
11491 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11492 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11493 == INTEGER_CST)
11495 tree size = size_binop (PLUS_EXPR,
11496 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11497 size_one_node);
11498 if (TREE_CODE (low_bound) == INTEGER_CST)
11500 if (tree_int_cst_lt (size, low_bound))
11502 error_at (OMP_CLAUSE_LOCATION (c),
11503 "low bound %qE above array section size "
11504 "in %qs clause", low_bound,
11505 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11506 return error_mark_node;
11508 if (tree_int_cst_equal (size, low_bound))
11509 maybe_zero_len = true;
11510 else if (length == NULL_TREE
11511 && first_non_one == types.length ()
11512 && tree_int_cst_equal
11513 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11514 low_bound))
11515 first_non_one++;
11517 else if (length == NULL_TREE)
11519 maybe_zero_len = true;
11520 if (first_non_one == types.length ())
11521 first_non_one++;
11523 if (length && TREE_CODE (length) == INTEGER_CST)
11525 if (tree_int_cst_lt (size, length))
11527 error_at (OMP_CLAUSE_LOCATION (c),
11528 "length %qE above array section size "
11529 "in %qs clause", length,
11530 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11531 return error_mark_node;
11533 if (TREE_CODE (low_bound) == INTEGER_CST)
11535 tree lbpluslen
11536 = size_binop (PLUS_EXPR,
11537 fold_convert (sizetype, low_bound),
11538 fold_convert (sizetype, length));
11539 if (TREE_CODE (lbpluslen) == INTEGER_CST
11540 && tree_int_cst_lt (size, lbpluslen))
11542 error_at (OMP_CLAUSE_LOCATION (c),
11543 "high bound %qE above array section size "
11544 "in %qs clause", lbpluslen,
11545 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11546 return error_mark_node;
11551 else if (length == NULL_TREE)
11553 maybe_zero_len = true;
11554 if (first_non_one == types.length ())
11555 first_non_one++;
11558 /* For [lb:] we will need to evaluate lb more than once. */
11559 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11561 tree lb = c_save_expr (low_bound);
11562 if (lb != low_bound)
11564 TREE_PURPOSE (t) = lb;
11565 low_bound = lb;
11569 else if (TREE_CODE (type) == POINTER_TYPE)
11571 if (length == NULL_TREE)
11573 error_at (OMP_CLAUSE_LOCATION (c),
11574 "for pointer type length expression must be specified");
11575 return error_mark_node;
11577 /* If there is a pointer type anywhere but in the very first
11578 array-section-subscript, the array section can't be contiguous. */
11579 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11580 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11582 error_at (OMP_CLAUSE_LOCATION (c),
11583 "array section is not contiguous in %qs clause",
11584 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11585 return error_mark_node;
11588 else
11590 error_at (OMP_CLAUSE_LOCATION (c),
11591 "%qE does not have pointer or array type", ret);
11592 return error_mark_node;
11594 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11595 types.safe_push (TREE_TYPE (ret));
11596 /* We will need to evaluate lb more than once. */
11597 tree lb = c_save_expr (low_bound);
11598 if (lb != low_bound)
11600 TREE_PURPOSE (t) = lb;
11601 low_bound = lb;
11603 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11604 return ret;
11607 /* Handle array sections for clause C. */
11609 static bool
11610 handle_omp_array_sections (tree c)
11612 bool maybe_zero_len = false;
11613 unsigned int first_non_one = 0;
11614 vec<tree> types = vNULL;
11615 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11616 maybe_zero_len, first_non_one);
11617 if (first == error_mark_node)
11619 types.release ();
11620 return true;
11622 if (first == NULL_TREE)
11624 types.release ();
11625 return false;
11627 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11629 tree t = OMP_CLAUSE_DECL (c);
11630 tree tem = NULL_TREE;
11631 types.release ();
11632 /* Need to evaluate side effects in the length expressions
11633 if any. */
11634 while (TREE_CODE (t) == TREE_LIST)
11636 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11638 if (tem == NULL_TREE)
11639 tem = TREE_VALUE (t);
11640 else
11641 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11642 TREE_VALUE (t), tem);
11644 t = TREE_CHAIN (t);
11646 if (tem)
11647 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11648 first = c_fully_fold (first, false, NULL);
11649 OMP_CLAUSE_DECL (c) = first;
11651 else
11653 unsigned int num = types.length (), i;
11654 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11655 tree condition = NULL_TREE;
11657 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11658 maybe_zero_len = true;
11660 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11661 t = TREE_CHAIN (t))
11663 tree low_bound = TREE_PURPOSE (t);
11664 tree length = TREE_VALUE (t);
11666 i--;
11667 if (low_bound
11668 && TREE_CODE (low_bound) == INTEGER_CST
11669 && TYPE_PRECISION (TREE_TYPE (low_bound))
11670 > TYPE_PRECISION (sizetype))
11671 low_bound = fold_convert (sizetype, low_bound);
11672 if (length
11673 && TREE_CODE (length) == INTEGER_CST
11674 && TYPE_PRECISION (TREE_TYPE (length))
11675 > TYPE_PRECISION (sizetype))
11676 length = fold_convert (sizetype, length);
11677 if (low_bound == NULL_TREE)
11678 low_bound = integer_zero_node;
11679 if (!maybe_zero_len && i > first_non_one)
11681 if (integer_nonzerop (low_bound))
11682 goto do_warn_noncontiguous;
11683 if (length != NULL_TREE
11684 && TREE_CODE (length) == INTEGER_CST
11685 && TYPE_DOMAIN (types[i])
11686 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11687 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11688 == INTEGER_CST)
11690 tree size;
11691 size = size_binop (PLUS_EXPR,
11692 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11693 size_one_node);
11694 if (!tree_int_cst_equal (length, size))
11696 do_warn_noncontiguous:
11697 error_at (OMP_CLAUSE_LOCATION (c),
11698 "array section is not contiguous in %qs "
11699 "clause",
11700 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11701 types.release ();
11702 return true;
11705 if (length != NULL_TREE
11706 && TREE_SIDE_EFFECTS (length))
11708 if (side_effects == NULL_TREE)
11709 side_effects = length;
11710 else
11711 side_effects = build2 (COMPOUND_EXPR,
11712 TREE_TYPE (side_effects),
11713 length, side_effects);
11716 else
11718 tree l;
11720 if (i > first_non_one && length && integer_nonzerop (length))
11721 continue;
11722 if (length)
11723 l = fold_convert (sizetype, length);
11724 else
11726 l = size_binop (PLUS_EXPR,
11727 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11728 size_one_node);
11729 l = size_binop (MINUS_EXPR, l,
11730 fold_convert (sizetype, low_bound));
11732 if (i > first_non_one)
11734 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11735 size_zero_node);
11736 if (condition == NULL_TREE)
11737 condition = l;
11738 else
11739 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11740 l, condition);
11742 else if (size == NULL_TREE)
11744 size = size_in_bytes (TREE_TYPE (types[i]));
11745 size = size_binop (MULT_EXPR, size, l);
11746 if (condition)
11747 size = fold_build3 (COND_EXPR, sizetype, condition,
11748 size, size_zero_node);
11750 else
11751 size = size_binop (MULT_EXPR, size, l);
11754 types.release ();
11755 if (side_effects)
11756 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11757 first = c_fully_fold (first, false, NULL);
11758 OMP_CLAUSE_DECL (c) = first;
11759 if (size)
11760 size = c_fully_fold (size, false, NULL);
11761 OMP_CLAUSE_SIZE (c) = size;
11762 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11763 return false;
11764 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11765 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11766 if (!c_mark_addressable (t))
11767 return false;
11768 OMP_CLAUSE_DECL (c2) = t;
11769 t = build_fold_addr_expr (first);
11770 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11771 tree ptr = OMP_CLAUSE_DECL (c2);
11772 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11773 ptr = build_fold_addr_expr (ptr);
11774 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11775 ptrdiff_type_node, t,
11776 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11777 ptrdiff_type_node, ptr));
11778 t = c_fully_fold (t, false, NULL);
11779 OMP_CLAUSE_SIZE (c2) = t;
11780 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11781 OMP_CLAUSE_CHAIN (c) = c2;
11783 return false;
11786 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11787 an inline call. But, remap
11788 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11789 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11791 static tree
11792 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11793 tree decl, tree placeholder)
11795 copy_body_data id;
11796 hash_map<tree, tree> decl_map;
11798 decl_map.put (omp_decl1, placeholder);
11799 decl_map.put (omp_decl2, decl);
11800 memset (&id, 0, sizeof (id));
11801 id.src_fn = DECL_CONTEXT (omp_decl1);
11802 id.dst_fn = current_function_decl;
11803 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11804 id.decl_map = &decl_map;
11806 id.copy_decl = copy_decl_no_change;
11807 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11808 id.transform_new_cfg = true;
11809 id.transform_return_to_modify = false;
11810 id.transform_lang_insert_block = NULL;
11811 id.eh_lp_nr = 0;
11812 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11813 return stmt;
11816 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11817 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11819 static tree
11820 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11822 if (*tp == (tree) data)
11823 return *tp;
11824 return NULL_TREE;
11827 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11828 Remove any elements from the list that are invalid. */
11830 tree
11831 c_finish_omp_clauses (tree clauses)
11833 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11834 bitmap_head aligned_head;
11835 tree c, t, *pc;
11836 bool branch_seen = false;
11837 bool copyprivate_seen = false;
11838 tree *nowait_clause = NULL;
11840 bitmap_obstack_initialize (NULL);
11841 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11842 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11843 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11844 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11846 for (pc = &clauses, c = clauses; c ; c = *pc)
11848 bool remove = false;
11849 bool need_complete = false;
11850 bool need_implicitly_determined = false;
11852 switch (OMP_CLAUSE_CODE (c))
11854 case OMP_CLAUSE_SHARED:
11855 need_implicitly_determined = true;
11856 goto check_dup_generic;
11858 case OMP_CLAUSE_PRIVATE:
11859 need_complete = true;
11860 need_implicitly_determined = true;
11861 goto check_dup_generic;
11863 case OMP_CLAUSE_REDUCTION:
11864 need_implicitly_determined = true;
11865 t = OMP_CLAUSE_DECL (c);
11866 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11867 && (FLOAT_TYPE_P (TREE_TYPE (t))
11868 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11870 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11871 const char *r_name = NULL;
11873 switch (r_code)
11875 case PLUS_EXPR:
11876 case MULT_EXPR:
11877 case MINUS_EXPR:
11878 break;
11879 case MIN_EXPR:
11880 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11881 r_name = "min";
11882 break;
11883 case MAX_EXPR:
11884 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11885 r_name = "max";
11886 break;
11887 case BIT_AND_EXPR:
11888 r_name = "&";
11889 break;
11890 case BIT_XOR_EXPR:
11891 r_name = "^";
11892 break;
11893 case BIT_IOR_EXPR:
11894 r_name = "|";
11895 break;
11896 case TRUTH_ANDIF_EXPR:
11897 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11898 r_name = "&&";
11899 break;
11900 case TRUTH_ORIF_EXPR:
11901 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11902 r_name = "||";
11903 break;
11904 default:
11905 gcc_unreachable ();
11907 if (r_name)
11909 error_at (OMP_CLAUSE_LOCATION (c),
11910 "%qE has invalid type for %<reduction(%s)%>",
11911 t, r_name);
11912 remove = true;
11913 break;
11916 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11918 error_at (OMP_CLAUSE_LOCATION (c),
11919 "user defined reduction not found for %qD", t);
11920 remove = true;
11921 break;
11923 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11925 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11926 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11927 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11928 VAR_DECL, NULL_TREE, type);
11929 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11930 DECL_ARTIFICIAL (placeholder) = 1;
11931 DECL_IGNORED_P (placeholder) = 1;
11932 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11933 c_mark_addressable (placeholder);
11934 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11935 c_mark_addressable (OMP_CLAUSE_DECL (c));
11936 OMP_CLAUSE_REDUCTION_MERGE (c)
11937 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11938 TREE_VEC_ELT (list, 0),
11939 TREE_VEC_ELT (list, 1),
11940 OMP_CLAUSE_DECL (c), placeholder);
11941 OMP_CLAUSE_REDUCTION_MERGE (c)
11942 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11943 void_type_node, NULL_TREE,
11944 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11945 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11946 if (TREE_VEC_LENGTH (list) == 6)
11948 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11949 c_mark_addressable (OMP_CLAUSE_DECL (c));
11950 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11951 c_mark_addressable (placeholder);
11952 tree init = TREE_VEC_ELT (list, 5);
11953 if (init == error_mark_node)
11954 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11955 OMP_CLAUSE_REDUCTION_INIT (c)
11956 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11957 TREE_VEC_ELT (list, 3),
11958 OMP_CLAUSE_DECL (c), placeholder);
11959 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11960 OMP_CLAUSE_REDUCTION_INIT (c)
11961 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11962 OMP_CLAUSE_REDUCTION_INIT (c));
11963 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11964 c_find_omp_placeholder_r,
11965 placeholder, NULL))
11966 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11968 else
11970 tree init;
11971 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11972 init = build_constructor (TREE_TYPE (t), NULL);
11973 else
11974 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11975 OMP_CLAUSE_REDUCTION_INIT (c)
11976 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11978 OMP_CLAUSE_REDUCTION_INIT (c)
11979 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11980 void_type_node, NULL_TREE,
11981 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11982 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11984 goto check_dup_generic;
11986 case OMP_CLAUSE_COPYPRIVATE:
11987 copyprivate_seen = true;
11988 if (nowait_clause)
11990 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11991 "%<nowait%> clause must not be used together "
11992 "with %<copyprivate%>");
11993 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11994 nowait_clause = NULL;
11996 goto check_dup_generic;
11998 case OMP_CLAUSE_COPYIN:
11999 t = OMP_CLAUSE_DECL (c);
12000 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12002 error_at (OMP_CLAUSE_LOCATION (c),
12003 "%qE must be %<threadprivate%> for %<copyin%>", t);
12004 remove = true;
12005 break;
12007 goto check_dup_generic;
12009 case OMP_CLAUSE_LINEAR:
12010 t = OMP_CLAUSE_DECL (c);
12011 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12012 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12014 error_at (OMP_CLAUSE_LOCATION (c),
12015 "linear clause applied to non-integral non-pointer "
12016 "variable with type %qT", TREE_TYPE (t));
12017 remove = true;
12018 break;
12020 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12022 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12023 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12024 OMP_CLAUSE_DECL (c), s);
12025 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12026 sizetype, s, OMP_CLAUSE_DECL (c));
12027 if (s == error_mark_node)
12028 s = size_one_node;
12029 OMP_CLAUSE_LINEAR_STEP (c) = s;
12031 else
12032 OMP_CLAUSE_LINEAR_STEP (c)
12033 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12034 goto check_dup_generic;
12036 check_dup_generic:
12037 t = OMP_CLAUSE_DECL (c);
12038 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12040 error_at (OMP_CLAUSE_LOCATION (c),
12041 "%qE is not a variable in clause %qs", t,
12042 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12043 remove = true;
12045 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12046 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12047 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12049 error_at (OMP_CLAUSE_LOCATION (c),
12050 "%qE appears more than once in data clauses", t);
12051 remove = true;
12053 else
12054 bitmap_set_bit (&generic_head, DECL_UID (t));
12055 break;
12057 case OMP_CLAUSE_FIRSTPRIVATE:
12058 t = OMP_CLAUSE_DECL (c);
12059 need_complete = true;
12060 need_implicitly_determined = true;
12061 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12063 error_at (OMP_CLAUSE_LOCATION (c),
12064 "%qE is not a variable in clause %<firstprivate%>", t);
12065 remove = true;
12067 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12068 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12070 error_at (OMP_CLAUSE_LOCATION (c),
12071 "%qE appears more than once in data clauses", t);
12072 remove = true;
12074 else
12075 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12076 break;
12078 case OMP_CLAUSE_LASTPRIVATE:
12079 t = OMP_CLAUSE_DECL (c);
12080 need_complete = true;
12081 need_implicitly_determined = true;
12082 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12084 error_at (OMP_CLAUSE_LOCATION (c),
12085 "%qE is not a variable in clause %<lastprivate%>", t);
12086 remove = true;
12088 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12089 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12091 error_at (OMP_CLAUSE_LOCATION (c),
12092 "%qE appears more than once in data clauses", t);
12093 remove = true;
12095 else
12096 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12097 break;
12099 case OMP_CLAUSE_ALIGNED:
12100 t = OMP_CLAUSE_DECL (c);
12101 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12103 error_at (OMP_CLAUSE_LOCATION (c),
12104 "%qE is not a variable in %<aligned%> clause", t);
12105 remove = true;
12107 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12108 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12110 error_at (OMP_CLAUSE_LOCATION (c),
12111 "%qE in %<aligned%> clause is neither a pointer nor "
12112 "an array", t);
12113 remove = true;
12115 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12117 error_at (OMP_CLAUSE_LOCATION (c),
12118 "%qE appears more than once in %<aligned%> clauses",
12120 remove = true;
12122 else
12123 bitmap_set_bit (&aligned_head, DECL_UID (t));
12124 break;
12126 case OMP_CLAUSE_DEPEND:
12127 t = OMP_CLAUSE_DECL (c);
12128 if (TREE_CODE (t) == TREE_LIST)
12130 if (handle_omp_array_sections (c))
12131 remove = true;
12132 break;
12134 if (t == error_mark_node)
12135 remove = true;
12136 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12138 error_at (OMP_CLAUSE_LOCATION (c),
12139 "%qE is not a variable in %<depend%> clause", t);
12140 remove = true;
12142 else if (!c_mark_addressable (t))
12143 remove = true;
12144 break;
12146 case OMP_CLAUSE_MAP:
12147 case OMP_CLAUSE_TO:
12148 case OMP_CLAUSE_FROM:
12149 t = OMP_CLAUSE_DECL (c);
12150 if (TREE_CODE (t) == TREE_LIST)
12152 if (handle_omp_array_sections (c))
12153 remove = true;
12154 else
12156 t = OMP_CLAUSE_DECL (c);
12157 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12159 error_at (OMP_CLAUSE_LOCATION (c),
12160 "array section does not have mappable type "
12161 "in %qs clause",
12162 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12163 remove = true;
12166 break;
12168 if (t == error_mark_node)
12169 remove = true;
12170 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12172 error_at (OMP_CLAUSE_LOCATION (c),
12173 "%qE is not a variable in %qs clause", t,
12174 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12175 remove = true;
12177 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12179 error_at (OMP_CLAUSE_LOCATION (c),
12180 "%qD is threadprivate variable in %qs clause", t,
12181 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12182 remove = true;
12184 else if (!c_mark_addressable (t))
12185 remove = true;
12186 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12187 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12188 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12190 error_at (OMP_CLAUSE_LOCATION (c),
12191 "%qD does not have a mappable type in %qs clause", t,
12192 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12193 remove = true;
12195 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12197 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12198 error ("%qD appears more than once in motion clauses", t);
12199 else
12200 error ("%qD appears more than once in map clauses", t);
12201 remove = true;
12203 else
12204 bitmap_set_bit (&generic_head, DECL_UID (t));
12205 break;
12207 case OMP_CLAUSE_UNIFORM:
12208 t = OMP_CLAUSE_DECL (c);
12209 if (TREE_CODE (t) != PARM_DECL)
12211 if (DECL_P (t))
12212 error_at (OMP_CLAUSE_LOCATION (c),
12213 "%qD is not an argument in %<uniform%> clause", t);
12214 else
12215 error_at (OMP_CLAUSE_LOCATION (c),
12216 "%qE is not an argument in %<uniform%> clause", t);
12217 remove = true;
12218 break;
12220 goto check_dup_generic;
12222 case OMP_CLAUSE_NOWAIT:
12223 if (copyprivate_seen)
12225 error_at (OMP_CLAUSE_LOCATION (c),
12226 "%<nowait%> clause must not be used together "
12227 "with %<copyprivate%>");
12228 remove = true;
12229 break;
12231 nowait_clause = pc;
12232 pc = &OMP_CLAUSE_CHAIN (c);
12233 continue;
12235 case OMP_CLAUSE_IF:
12236 case OMP_CLAUSE_NUM_THREADS:
12237 case OMP_CLAUSE_NUM_TEAMS:
12238 case OMP_CLAUSE_THREAD_LIMIT:
12239 case OMP_CLAUSE_SCHEDULE:
12240 case OMP_CLAUSE_ORDERED:
12241 case OMP_CLAUSE_DEFAULT:
12242 case OMP_CLAUSE_UNTIED:
12243 case OMP_CLAUSE_COLLAPSE:
12244 case OMP_CLAUSE_FINAL:
12245 case OMP_CLAUSE_MERGEABLE:
12246 case OMP_CLAUSE_SAFELEN:
12247 case OMP_CLAUSE_SIMDLEN:
12248 case OMP_CLAUSE_DEVICE:
12249 case OMP_CLAUSE_DIST_SCHEDULE:
12250 case OMP_CLAUSE_PARALLEL:
12251 case OMP_CLAUSE_FOR:
12252 case OMP_CLAUSE_SECTIONS:
12253 case OMP_CLAUSE_TASKGROUP:
12254 case OMP_CLAUSE_PROC_BIND:
12255 case OMP_CLAUSE__CILK_FOR_COUNT_:
12256 pc = &OMP_CLAUSE_CHAIN (c);
12257 continue;
12259 case OMP_CLAUSE_INBRANCH:
12260 case OMP_CLAUSE_NOTINBRANCH:
12261 if (branch_seen)
12263 error_at (OMP_CLAUSE_LOCATION (c),
12264 "%<inbranch%> clause is incompatible with "
12265 "%<notinbranch%>");
12266 remove = true;
12267 break;
12269 branch_seen = true;
12270 pc = &OMP_CLAUSE_CHAIN (c);
12271 continue;
12273 default:
12274 gcc_unreachable ();
12277 if (!remove)
12279 t = OMP_CLAUSE_DECL (c);
12281 if (need_complete)
12283 t = require_complete_type (t);
12284 if (t == error_mark_node)
12285 remove = true;
12288 if (need_implicitly_determined)
12290 const char *share_name = NULL;
12292 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12293 share_name = "threadprivate";
12294 else switch (c_omp_predetermined_sharing (t))
12296 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12297 break;
12298 case OMP_CLAUSE_DEFAULT_SHARED:
12299 /* const vars may be specified in firstprivate clause. */
12300 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12301 && TREE_READONLY (t))
12302 break;
12303 share_name = "shared";
12304 break;
12305 case OMP_CLAUSE_DEFAULT_PRIVATE:
12306 share_name = "private";
12307 break;
12308 default:
12309 gcc_unreachable ();
12311 if (share_name)
12313 error_at (OMP_CLAUSE_LOCATION (c),
12314 "%qE is predetermined %qs for %qs",
12315 t, share_name,
12316 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12317 remove = true;
12322 if (remove)
12323 *pc = OMP_CLAUSE_CHAIN (c);
12324 else
12325 pc = &OMP_CLAUSE_CHAIN (c);
12328 bitmap_obstack_release (NULL);
12329 return clauses;
12332 /* Create a transaction node. */
12334 tree
12335 c_finish_transaction (location_t loc, tree block, int flags)
12337 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12338 if (flags & TM_STMT_ATTR_OUTER)
12339 TRANSACTION_EXPR_OUTER (stmt) = 1;
12340 if (flags & TM_STMT_ATTR_RELAXED)
12341 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12342 return add_stmt (stmt);
12345 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12346 down to the element type of an array. */
12348 tree
12349 c_build_qualified_type (tree type, int type_quals)
12351 if (type == error_mark_node)
12352 return type;
12354 if (TREE_CODE (type) == ARRAY_TYPE)
12356 tree t;
12357 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12358 type_quals);
12360 /* See if we already have an identically qualified type. */
12361 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12363 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12364 && TYPE_NAME (t) == TYPE_NAME (type)
12365 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12366 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12367 TYPE_ATTRIBUTES (type)))
12368 break;
12370 if (!t)
12372 tree domain = TYPE_DOMAIN (type);
12374 t = build_variant_type_copy (type);
12375 TREE_TYPE (t) = element_type;
12377 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12378 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12379 SET_TYPE_STRUCTURAL_EQUALITY (t);
12380 else if (TYPE_CANONICAL (element_type) != element_type
12381 || (domain && TYPE_CANONICAL (domain) != domain))
12383 tree unqualified_canon
12384 = build_array_type (TYPE_CANONICAL (element_type),
12385 domain? TYPE_CANONICAL (domain)
12386 : NULL_TREE);
12387 TYPE_CANONICAL (t)
12388 = c_build_qualified_type (unqualified_canon, type_quals);
12390 else
12391 TYPE_CANONICAL (t) = t;
12393 return t;
12396 /* A restrict-qualified pointer type must be a pointer to object or
12397 incomplete type. Note that the use of POINTER_TYPE_P also allows
12398 REFERENCE_TYPEs, which is appropriate for C++. */
12399 if ((type_quals & TYPE_QUAL_RESTRICT)
12400 && (!POINTER_TYPE_P (type)
12401 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12403 error ("invalid use of %<restrict%>");
12404 type_quals &= ~TYPE_QUAL_RESTRICT;
12407 return build_qualified_type (type, type_quals);
12410 /* Build a VA_ARG_EXPR for the C parser. */
12412 tree
12413 c_build_va_arg (location_t loc, tree expr, tree type)
12415 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12416 warning_at (loc, OPT_Wc___compat,
12417 "C++ requires promoted type, not enum type, in %<va_arg%>");
12418 return build_va_arg (loc, expr, type);
12421 /* Return truthvalue of whether T1 is the same tree structure as T2.
12422 Return 1 if they are the same. Return 0 if they are different. */
12424 bool
12425 c_tree_equal (tree t1, tree t2)
12427 enum tree_code code1, code2;
12429 if (t1 == t2)
12430 return true;
12431 if (!t1 || !t2)
12432 return false;
12434 for (code1 = TREE_CODE (t1);
12435 CONVERT_EXPR_CODE_P (code1)
12436 || code1 == NON_LVALUE_EXPR;
12437 code1 = TREE_CODE (t1))
12438 t1 = TREE_OPERAND (t1, 0);
12439 for (code2 = TREE_CODE (t2);
12440 CONVERT_EXPR_CODE_P (code2)
12441 || code2 == NON_LVALUE_EXPR;
12442 code2 = TREE_CODE (t2))
12443 t2 = TREE_OPERAND (t2, 0);
12445 /* They might have become equal now. */
12446 if (t1 == t2)
12447 return true;
12449 if (code1 != code2)
12450 return false;
12452 switch (code1)
12454 case INTEGER_CST:
12455 return wi::eq_p (t1, t2);
12457 case REAL_CST:
12458 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12460 case STRING_CST:
12461 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12462 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12463 TREE_STRING_LENGTH (t1));
12465 case FIXED_CST:
12466 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12467 TREE_FIXED_CST (t2));
12469 case COMPLEX_CST:
12470 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12471 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12473 case VECTOR_CST:
12474 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12476 case CONSTRUCTOR:
12477 /* We need to do this when determining whether or not two
12478 non-type pointer to member function template arguments
12479 are the same. */
12480 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12481 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12482 return false;
12484 tree field, value;
12485 unsigned int i;
12486 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12488 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12489 if (!c_tree_equal (field, elt2->index)
12490 || !c_tree_equal (value, elt2->value))
12491 return false;
12494 return true;
12496 case TREE_LIST:
12497 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12498 return false;
12499 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12500 return false;
12501 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12503 case SAVE_EXPR:
12504 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12506 case CALL_EXPR:
12508 tree arg1, arg2;
12509 call_expr_arg_iterator iter1, iter2;
12510 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12511 return false;
12512 for (arg1 = first_call_expr_arg (t1, &iter1),
12513 arg2 = first_call_expr_arg (t2, &iter2);
12514 arg1 && arg2;
12515 arg1 = next_call_expr_arg (&iter1),
12516 arg2 = next_call_expr_arg (&iter2))
12517 if (!c_tree_equal (arg1, arg2))
12518 return false;
12519 if (arg1 || arg2)
12520 return false;
12521 return true;
12524 case TARGET_EXPR:
12526 tree o1 = TREE_OPERAND (t1, 0);
12527 tree o2 = TREE_OPERAND (t2, 0);
12529 /* Special case: if either target is an unallocated VAR_DECL,
12530 it means that it's going to be unified with whatever the
12531 TARGET_EXPR is really supposed to initialize, so treat it
12532 as being equivalent to anything. */
12533 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12534 && !DECL_RTL_SET_P (o1))
12535 /*Nop*/;
12536 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12537 && !DECL_RTL_SET_P (o2))
12538 /*Nop*/;
12539 else if (!c_tree_equal (o1, o2))
12540 return false;
12542 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12545 case COMPONENT_REF:
12546 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12547 return false;
12548 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12550 case PARM_DECL:
12551 case VAR_DECL:
12552 case CONST_DECL:
12553 case FIELD_DECL:
12554 case FUNCTION_DECL:
12555 case IDENTIFIER_NODE:
12556 case SSA_NAME:
12557 return false;
12559 case TREE_VEC:
12561 unsigned ix;
12562 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12563 return false;
12564 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12565 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12566 TREE_VEC_ELT (t2, ix)))
12567 return false;
12568 return true;
12571 default:
12572 break;
12575 switch (TREE_CODE_CLASS (code1))
12577 case tcc_unary:
12578 case tcc_binary:
12579 case tcc_comparison:
12580 case tcc_expression:
12581 case tcc_vl_exp:
12582 case tcc_reference:
12583 case tcc_statement:
12585 int i, n = TREE_OPERAND_LENGTH (t1);
12587 switch (code1)
12589 case PREINCREMENT_EXPR:
12590 case PREDECREMENT_EXPR:
12591 case POSTINCREMENT_EXPR:
12592 case POSTDECREMENT_EXPR:
12593 n = 1;
12594 break;
12595 case ARRAY_REF:
12596 n = 2;
12597 break;
12598 default:
12599 break;
12602 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12603 && n != TREE_OPERAND_LENGTH (t2))
12604 return false;
12606 for (i = 0; i < n; ++i)
12607 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12608 return false;
12610 return true;
12613 case tcc_type:
12614 return comptypes (t1, t2);
12615 default:
12616 gcc_unreachable ();
12618 /* We can get here with --disable-checking. */
12619 return false;
12622 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12623 spawn-helper and BODY is the newly created body for FNDECL. */
12625 void
12626 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12628 tree list = alloc_stmt_list ();
12629 tree frame = make_cilk_frame (fndecl);
12630 tree dtor = create_cilk_function_exit (frame, false, true);
12631 add_local_decl (cfun, frame);
12633 DECL_SAVED_TREE (fndecl) = list;
12634 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12635 frame);
12636 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12637 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12639 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12640 append_to_statement_list (detach_expr, &body_list);
12642 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12643 body = fold_build_cleanup_point_expr (void_type_node, body);
12645 append_to_statement_list (body, &body_list);
12646 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12647 body_list, dtor), &list);