C++ FE: offer suggestions for misspelled field names
[official-gcc.git] / gcc / c / c-typeck.c
blob9284bfcea66e464a679609792ffb59f348b76439
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 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 "target.h"
30 #include "function.h"
31 #include "bitmap.h"
32 #include "c-tree.h"
33 #include "gimple-expr.h"
34 #include "predict.h"
35 #include "stor-layout.h"
36 #include "trans-mem.h"
37 #include "varasm.h"
38 #include "stmt.h"
39 #include "langhooks.h"
40 #include "c-lang.h"
41 #include "intl.h"
42 #include "tree-iterator.h"
43 #include "gimplify.h"
44 #include "tree-inline.h"
45 #include "omp-low.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-ubsan.h"
48 #include "cilk.h"
49 #include "gomp-constants.h"
50 #include "spellcheck.h"
52 /* Possible cases of implicit bad conversions. Used to select
53 diagnostic messages in convert_for_assignment. */
54 enum impl_conv {
55 ic_argpass,
56 ic_assign,
57 ic_init,
58 ic_return
61 /* The level of nesting inside "__alignof__". */
62 int in_alignof;
64 /* The level of nesting inside "sizeof". */
65 int in_sizeof;
67 /* The level of nesting inside "typeof". */
68 int in_typeof;
70 /* The argument of last parsed sizeof expression, only to be tested
71 if expr.original_code == SIZEOF_EXPR. */
72 tree c_last_sizeof_arg;
74 /* Nonzero if we might need to print a "missing braces around
75 initializer" message within this initializer. */
76 static int found_missing_braces;
78 static int require_constant_value;
79 static int require_constant_elements;
81 static bool null_pointer_constant_p (const_tree);
82 static tree qualify_type (tree, tree);
83 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
84 bool *);
85 static int comp_target_types (location_t, tree, tree);
86 static int function_types_compatible_p (const_tree, const_tree, bool *,
87 bool *);
88 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
89 static tree lookup_field (tree, tree);
90 static int convert_arguments (location_t, vec<location_t>, tree,
91 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
92 tree);
93 static tree pointer_diff (location_t, tree, tree);
94 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
95 enum impl_conv, bool, tree, tree, int);
96 static tree valid_compound_expr_initializer (tree, tree);
97 static void push_string (const char *);
98 static void push_member_name (tree);
99 static int spelling_length (void);
100 static char *print_spelling (char *);
101 static void warning_init (location_t, int, const char *);
102 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
103 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
104 bool, struct obstack *);
105 static void output_pending_init_elements (int, struct obstack *);
106 static int set_designator (location_t, int, struct obstack *);
107 static void push_range_stack (tree, struct obstack *);
108 static void add_pending_init (location_t, tree, tree, tree, bool,
109 struct obstack *);
110 static void set_nonincremental_init (struct obstack *);
111 static void set_nonincremental_init_from_string (tree, struct obstack *);
112 static tree find_init_member (tree, struct obstack *);
113 static void readonly_warning (tree, enum lvalue_use);
114 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
115 static void record_maybe_used_decl (tree);
116 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
118 /* Return true if EXP is a null pointer constant, false otherwise. */
120 static bool
121 null_pointer_constant_p (const_tree expr)
123 /* This should really operate on c_expr structures, but they aren't
124 yet available everywhere required. */
125 tree type = TREE_TYPE (expr);
126 return (TREE_CODE (expr) == INTEGER_CST
127 && !TREE_OVERFLOW (expr)
128 && integer_zerop (expr)
129 && (INTEGRAL_TYPE_P (type)
130 || (TREE_CODE (type) == POINTER_TYPE
131 && VOID_TYPE_P (TREE_TYPE (type))
132 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
135 /* EXPR may appear in an unevaluated part of an integer constant
136 expression, but not in an evaluated part. Wrap it in a
137 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
138 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
140 static tree
141 note_integer_operands (tree expr)
143 tree ret;
144 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
146 ret = copy_node (expr);
147 TREE_OVERFLOW (ret) = 1;
149 else
151 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
152 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
154 return ret;
157 /* Having checked whether EXPR may appear in an unevaluated part of an
158 integer constant expression and found that it may, remove any
159 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 expression. */
162 static inline tree
163 remove_c_maybe_const_expr (tree expr)
165 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
166 return C_MAYBE_CONST_EXPR_EXPR (expr);
167 else
168 return expr;
171 \f/* This is a cache to hold if two types are compatible or not. */
173 struct tagged_tu_seen_cache {
174 const struct tagged_tu_seen_cache * next;
175 const_tree t1;
176 const_tree t2;
177 /* The return value of tagged_types_tu_compatible_p if we had seen
178 these two types already. */
179 int val;
182 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
183 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
185 /* Do `exp = require_complete_type (exp);' to make sure exp
186 does not have an incomplete type. (That includes void types.) */
188 tree
189 require_complete_type (tree value)
191 tree type = TREE_TYPE (value);
193 if (error_operand_p (value))
194 return error_mark_node;
196 /* First, detect a valid value with a complete type. */
197 if (COMPLETE_TYPE_P (type))
198 return value;
200 c_incomplete_type_error (value, type);
201 return error_mark_node;
204 /* Print an error message for invalid use of an incomplete type.
205 VALUE is the expression that was used (or 0 if that isn't known)
206 and TYPE is the type that was invalid. */
208 void
209 c_incomplete_type_error (const_tree value, const_tree type)
211 /* Avoid duplicate error message. */
212 if (TREE_CODE (type) == ERROR_MARK)
213 return;
215 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
216 error ("%qD has an incomplete type %qT", value, type);
217 else
219 retry:
220 /* We must print an error message. Be clever about what it says. */
222 switch (TREE_CODE (type))
224 case RECORD_TYPE:
225 case UNION_TYPE:
226 case ENUMERAL_TYPE:
227 break;
229 case VOID_TYPE:
230 error ("invalid use of void expression");
231 return;
233 case ARRAY_TYPE:
234 if (TYPE_DOMAIN (type))
236 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
238 error ("invalid use of flexible array member");
239 return;
241 type = TREE_TYPE (type);
242 goto retry;
244 error ("invalid use of array with unspecified bounds");
245 return;
247 default:
248 gcc_unreachable ();
251 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
252 error ("invalid use of undefined type %qT", type);
253 else
254 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
255 error ("invalid use of incomplete typedef %qT", type);
259 /* Given a type, apply default promotions wrt unnamed function
260 arguments and return the new type. */
262 tree
263 c_type_promotes_to (tree type)
265 tree ret = NULL_TREE;
267 if (TYPE_MAIN_VARIANT (type) == float_type_node)
268 ret = double_type_node;
269 else if (c_promoting_integer_type_p (type))
271 /* Preserve unsignedness if not really getting any wider. */
272 if (TYPE_UNSIGNED (type)
273 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
274 ret = unsigned_type_node;
275 else
276 ret = integer_type_node;
279 if (ret != NULL_TREE)
280 return (TYPE_ATOMIC (type)
281 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
282 : ret);
284 return type;
287 /* Return true if between two named address spaces, whether there is a superset
288 named address space that encompasses both address spaces. If there is a
289 superset, return which address space is the superset. */
291 static bool
292 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
294 if (as1 == as2)
296 *common = as1;
297 return true;
299 else if (targetm.addr_space.subset_p (as1, as2))
301 *common = as2;
302 return true;
304 else if (targetm.addr_space.subset_p (as2, as1))
306 *common = as1;
307 return true;
309 else
310 return false;
313 /* Return a variant of TYPE which has all the type qualifiers of LIKE
314 as well as those of TYPE. */
316 static tree
317 qualify_type (tree type, tree like)
319 addr_space_t as_type = TYPE_ADDR_SPACE (type);
320 addr_space_t as_like = TYPE_ADDR_SPACE (like);
321 addr_space_t as_common;
323 /* If the two named address spaces are different, determine the common
324 superset address space. If there isn't one, raise an error. */
325 if (!addr_space_superset (as_type, as_like, &as_common))
327 as_common = as_type;
328 error ("%qT and %qT are in disjoint named address spaces",
329 type, like);
332 return c_build_qualified_type (type,
333 TYPE_QUALS_NO_ADDR_SPACE (type)
334 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
335 | ENCODE_QUAL_ADDR_SPACE (as_common));
338 /* Return true iff the given tree T is a variable length array. */
340 bool
341 c_vla_type_p (const_tree t)
343 if (TREE_CODE (t) == ARRAY_TYPE
344 && C_TYPE_VARIABLE_SIZE (t))
345 return true;
346 return false;
349 /* Return the composite type of two compatible types.
351 We assume that comptypes has already been done and returned
352 nonzero; if that isn't so, this may crash. In particular, we
353 assume that qualifiers match. */
355 tree
356 composite_type (tree t1, tree t2)
358 enum tree_code code1;
359 enum tree_code code2;
360 tree attributes;
362 /* Save time if the two types are the same. */
364 if (t1 == t2) return t1;
366 /* If one type is nonsense, use the other. */
367 if (t1 == error_mark_node)
368 return t2;
369 if (t2 == error_mark_node)
370 return t1;
372 code1 = TREE_CODE (t1);
373 code2 = TREE_CODE (t2);
375 /* Merge the attributes. */
376 attributes = targetm.merge_type_attributes (t1, t2);
378 /* If one is an enumerated type and the other is the compatible
379 integer type, the composite type might be either of the two
380 (DR#013 question 3). For consistency, use the enumerated type as
381 the composite type. */
383 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
384 return t1;
385 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
386 return t2;
388 gcc_assert (code1 == code2);
390 switch (code1)
392 case POINTER_TYPE:
393 /* For two pointers, do this recursively on the target type. */
395 tree pointed_to_1 = TREE_TYPE (t1);
396 tree pointed_to_2 = TREE_TYPE (t2);
397 tree target = composite_type (pointed_to_1, pointed_to_2);
398 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
399 t1 = build_type_attribute_variant (t1, attributes);
400 return qualify_type (t1, t2);
403 case ARRAY_TYPE:
405 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
406 int quals;
407 tree unqual_elt;
408 tree d1 = TYPE_DOMAIN (t1);
409 tree d2 = TYPE_DOMAIN (t2);
410 bool d1_variable, d2_variable;
411 bool d1_zero, d2_zero;
412 bool t1_complete, t2_complete;
414 /* We should not have any type quals on arrays at all. */
415 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
416 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
418 t1_complete = COMPLETE_TYPE_P (t1);
419 t2_complete = COMPLETE_TYPE_P (t2);
421 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
422 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
424 d1_variable = (!d1_zero
425 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
426 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
427 d2_variable = (!d2_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
430 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
431 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
433 /* Save space: see if the result is identical to one of the args. */
434 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
435 && (d2_variable || d2_zero || !d1_variable))
436 return build_type_attribute_variant (t1, attributes);
437 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
438 && (d1_variable || d1_zero || !d2_variable))
439 return build_type_attribute_variant (t2, attributes);
441 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
442 return build_type_attribute_variant (t1, attributes);
443 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
444 return build_type_attribute_variant (t2, attributes);
446 /* Merge the element types, and have a size if either arg has
447 one. We may have qualifiers on the element types. To set
448 up TYPE_MAIN_VARIANT correctly, we need to form the
449 composite of the unqualified types and add the qualifiers
450 back at the end. */
451 quals = TYPE_QUALS (strip_array_types (elt));
452 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
453 t1 = build_array_type (unqual_elt,
454 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
455 && (d2_variable
456 || d2_zero
457 || !d1_variable))
458 ? t1
459 : t2));
460 /* Ensure a composite type involving a zero-length array type
461 is a zero-length type not an incomplete type. */
462 if (d1_zero && d2_zero
463 && (t1_complete || t2_complete)
464 && !COMPLETE_TYPE_P (t1))
466 TYPE_SIZE (t1) = bitsize_zero_node;
467 TYPE_SIZE_UNIT (t1) = size_zero_node;
469 t1 = c_build_qualified_type (t1, quals);
470 return build_type_attribute_variant (t1, attributes);
473 case ENUMERAL_TYPE:
474 case RECORD_TYPE:
475 case UNION_TYPE:
476 if (attributes != NULL)
478 /* Try harder not to create a new aggregate type. */
479 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
480 return t1;
481 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
482 return t2;
484 return build_type_attribute_variant (t1, attributes);
486 case FUNCTION_TYPE:
487 /* Function types: prefer the one that specified arg types.
488 If both do, merge the arg types. Also merge the return types. */
490 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
491 tree p1 = TYPE_ARG_TYPES (t1);
492 tree p2 = TYPE_ARG_TYPES (t2);
493 int len;
494 tree newargs, n;
495 int i;
497 /* Save space: see if the result is identical to one of the args. */
498 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
499 return build_type_attribute_variant (t1, attributes);
500 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
501 return build_type_attribute_variant (t2, attributes);
503 /* Simple way if one arg fails to specify argument types. */
504 if (TYPE_ARG_TYPES (t1) == 0)
506 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
507 t1 = build_type_attribute_variant (t1, attributes);
508 return qualify_type (t1, t2);
510 if (TYPE_ARG_TYPES (t2) == 0)
512 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
513 t1 = build_type_attribute_variant (t1, attributes);
514 return qualify_type (t1, t2);
517 /* If both args specify argument types, we must merge the two
518 lists, argument by argument. */
520 len = list_length (p1);
521 newargs = 0;
523 for (i = 0; i < len; i++)
524 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
526 n = newargs;
528 for (; p1;
529 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
531 /* A null type means arg type is not specified.
532 Take whatever the other function type has. */
533 if (TREE_VALUE (p1) == 0)
535 TREE_VALUE (n) = TREE_VALUE (p2);
536 goto parm_done;
538 if (TREE_VALUE (p2) == 0)
540 TREE_VALUE (n) = TREE_VALUE (p1);
541 goto parm_done;
544 /* Given wait (union {union wait *u; int *i} *)
545 and wait (union wait *),
546 prefer union wait * as type of parm. */
547 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
548 && TREE_VALUE (p1) != TREE_VALUE (p2))
550 tree memb;
551 tree mv2 = TREE_VALUE (p2);
552 if (mv2 && mv2 != error_mark_node
553 && TREE_CODE (mv2) != ARRAY_TYPE)
554 mv2 = TYPE_MAIN_VARIANT (mv2);
555 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
556 memb; memb = DECL_CHAIN (memb))
558 tree mv3 = TREE_TYPE (memb);
559 if (mv3 && mv3 != error_mark_node
560 && TREE_CODE (mv3) != ARRAY_TYPE)
561 mv3 = TYPE_MAIN_VARIANT (mv3);
562 if (comptypes (mv3, mv2))
564 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
565 TREE_VALUE (p2));
566 pedwarn (input_location, OPT_Wpedantic,
567 "function types not truly compatible in ISO C");
568 goto parm_done;
572 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
573 && TREE_VALUE (p2) != TREE_VALUE (p1))
575 tree memb;
576 tree mv1 = TREE_VALUE (p1);
577 if (mv1 && mv1 != error_mark_node
578 && TREE_CODE (mv1) != ARRAY_TYPE)
579 mv1 = TYPE_MAIN_VARIANT (mv1);
580 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
581 memb; memb = DECL_CHAIN (memb))
583 tree mv3 = TREE_TYPE (memb);
584 if (mv3 && mv3 != error_mark_node
585 && TREE_CODE (mv3) != ARRAY_TYPE)
586 mv3 = TYPE_MAIN_VARIANT (mv3);
587 if (comptypes (mv3, mv1))
589 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
590 TREE_VALUE (p1));
591 pedwarn (input_location, OPT_Wpedantic,
592 "function types not truly compatible in ISO C");
593 goto parm_done;
597 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
598 parm_done: ;
601 t1 = build_function_type (valtype, newargs);
602 t1 = qualify_type (t1, t2);
603 /* ... falls through ... */
606 default:
607 return build_type_attribute_variant (t1, attributes);
612 /* Return the type of a conditional expression between pointers to
613 possibly differently qualified versions of compatible types.
615 We assume that comp_target_types has already been done and returned
616 nonzero; if that isn't so, this may crash. */
618 static tree
619 common_pointer_type (tree t1, tree t2)
621 tree attributes;
622 tree pointed_to_1, mv1;
623 tree pointed_to_2, mv2;
624 tree target;
625 unsigned target_quals;
626 addr_space_t as1, as2, as_common;
627 int quals1, quals2;
629 /* Save time if the two types are the same. */
631 if (t1 == t2) return t1;
633 /* If one type is nonsense, use the other. */
634 if (t1 == error_mark_node)
635 return t2;
636 if (t2 == error_mark_node)
637 return t1;
639 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
640 && TREE_CODE (t2) == POINTER_TYPE);
642 /* Merge the attributes. */
643 attributes = targetm.merge_type_attributes (t1, t2);
645 /* Find the composite type of the target types, and combine the
646 qualifiers of the two types' targets. Do not lose qualifiers on
647 array element types by taking the TYPE_MAIN_VARIANT. */
648 mv1 = pointed_to_1 = TREE_TYPE (t1);
649 mv2 = pointed_to_2 = TREE_TYPE (t2);
650 if (TREE_CODE (mv1) != ARRAY_TYPE)
651 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
652 if (TREE_CODE (mv2) != ARRAY_TYPE)
653 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
654 target = composite_type (mv1, mv2);
656 /* Strip array types to get correct qualifier for pointers to arrays */
657 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
658 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
660 /* For function types do not merge const qualifiers, but drop them
661 if used inconsistently. The middle-end uses these to mark const
662 and noreturn functions. */
663 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
664 target_quals = (quals1 & quals2);
665 else
666 target_quals = (quals1 | quals2);
668 /* If the two named address spaces are different, determine the common
669 superset address space. This is guaranteed to exist due to the
670 assumption that comp_target_type returned non-zero. */
671 as1 = TYPE_ADDR_SPACE (pointed_to_1);
672 as2 = TYPE_ADDR_SPACE (pointed_to_2);
673 if (!addr_space_superset (as1, as2, &as_common))
674 gcc_unreachable ();
676 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
678 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
679 return build_type_attribute_variant (t1, attributes);
682 /* Return the common type for two arithmetic types under the usual
683 arithmetic conversions. The default conversions have already been
684 applied, and enumerated types converted to their compatible integer
685 types. The resulting type is unqualified and has no attributes.
687 This is the type for the result of most arithmetic operations
688 if the operands have the given two types. */
690 static tree
691 c_common_type (tree t1, tree t2)
693 enum tree_code code1;
694 enum tree_code code2;
696 /* If one type is nonsense, use the other. */
697 if (t1 == error_mark_node)
698 return t2;
699 if (t2 == error_mark_node)
700 return t1;
702 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
703 t1 = TYPE_MAIN_VARIANT (t1);
705 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
706 t2 = TYPE_MAIN_VARIANT (t2);
708 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
709 t1 = build_type_attribute_variant (t1, NULL_TREE);
711 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
712 t2 = build_type_attribute_variant (t2, NULL_TREE);
714 /* Save time if the two types are the same. */
716 if (t1 == t2) return t1;
718 code1 = TREE_CODE (t1);
719 code2 = TREE_CODE (t2);
721 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
722 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
723 || code1 == INTEGER_TYPE);
724 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
725 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
726 || code2 == INTEGER_TYPE);
728 /* When one operand is a decimal float type, the other operand cannot be
729 a generic float type or a complex type. We also disallow vector types
730 here. */
731 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
732 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
734 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
736 error ("can%'t mix operands of decimal float and vector types");
737 return error_mark_node;
739 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
741 error ("can%'t mix operands of decimal float and complex types");
742 return error_mark_node;
744 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
746 error ("can%'t mix operands of decimal float and other float types");
747 return error_mark_node;
751 /* If one type is a vector type, return that type. (How the usual
752 arithmetic conversions apply to the vector types extension is not
753 precisely specified.) */
754 if (code1 == VECTOR_TYPE)
755 return t1;
757 if (code2 == VECTOR_TYPE)
758 return t2;
760 /* If one type is complex, form the common type of the non-complex
761 components, then make that complex. Use T1 or T2 if it is the
762 required type. */
763 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
765 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
766 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
767 tree subtype = c_common_type (subtype1, subtype2);
769 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
770 return t1;
771 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
772 return t2;
773 else
774 return build_complex_type (subtype);
777 /* If only one is real, use it as the result. */
779 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
780 return t1;
782 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
783 return t2;
785 /* If both are real and either are decimal floating point types, use
786 the decimal floating point type with the greater precision. */
788 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
790 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
791 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
792 return dfloat128_type_node;
793 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
794 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
795 return dfloat64_type_node;
796 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
798 return dfloat32_type_node;
801 /* Deal with fixed-point types. */
802 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
804 unsigned int unsignedp = 0, satp = 0;
805 machine_mode m1, m2;
806 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
808 m1 = TYPE_MODE (t1);
809 m2 = TYPE_MODE (t2);
811 /* If one input type is saturating, the result type is saturating. */
812 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
813 satp = 1;
815 /* If both fixed-point types are unsigned, the result type is unsigned.
816 When mixing fixed-point and integer types, follow the sign of the
817 fixed-point type.
818 Otherwise, the result type is signed. */
819 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
820 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
821 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t1))
823 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t2)))
825 unsignedp = 1;
827 /* The result type is signed. */
828 if (unsignedp == 0)
830 /* If the input type is unsigned, we need to convert to the
831 signed type. */
832 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
834 enum mode_class mclass = (enum mode_class) 0;
835 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
836 mclass = MODE_FRACT;
837 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
838 mclass = MODE_ACCUM;
839 else
840 gcc_unreachable ();
841 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
843 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
845 enum mode_class mclass = (enum mode_class) 0;
846 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
856 if (code1 == FIXED_POINT_TYPE)
858 fbit1 = GET_MODE_FBIT (m1);
859 ibit1 = GET_MODE_IBIT (m1);
861 else
863 fbit1 = 0;
864 /* Signed integers need to subtract one sign bit. */
865 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
868 if (code2 == FIXED_POINT_TYPE)
870 fbit2 = GET_MODE_FBIT (m2);
871 ibit2 = GET_MODE_IBIT (m2);
873 else
875 fbit2 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
880 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
881 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
882 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
883 satp);
886 /* Both real or both integers; use the one with greater precision. */
888 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
889 return t1;
890 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
891 return t2;
893 /* Same precision. Prefer long longs to longs to ints when the
894 same precision, following the C99 rules on integer type rank
895 (which are equivalent to the C90 rules for C90 types). */
897 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
898 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
899 return long_long_unsigned_type_node;
901 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
902 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
904 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
905 return long_long_unsigned_type_node;
906 else
907 return long_long_integer_type_node;
910 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
911 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
912 return long_unsigned_type_node;
914 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
917 /* But preserve unsignedness from the other type,
918 since long cannot hold all the values of an unsigned int. */
919 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
920 return long_unsigned_type_node;
921 else
922 return long_integer_type_node;
925 /* Likewise, prefer long double to double even if same size. */
926 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
928 return long_double_type_node;
930 /* Likewise, prefer double to float even if same size.
931 We got a couple of embedded targets with 32 bit doubles, and the
932 pdp11 might have 64 bit floats. */
933 if (TYPE_MAIN_VARIANT (t1) == double_type_node
934 || TYPE_MAIN_VARIANT (t2) == double_type_node)
935 return double_type_node;
937 /* Otherwise prefer the unsigned one. */
939 if (TYPE_UNSIGNED (t1))
940 return t1;
941 else
942 return t2;
945 /* Wrapper around c_common_type that is used by c-common.c and other
946 front end optimizations that remove promotions. ENUMERAL_TYPEs
947 are allowed here and are converted to their compatible integer types.
948 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
949 preferably a non-Boolean type as the common type. */
950 tree
951 common_type (tree t1, tree t2)
953 if (TREE_CODE (t1) == ENUMERAL_TYPE)
954 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
955 if (TREE_CODE (t2) == ENUMERAL_TYPE)
956 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
958 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE
960 && TREE_CODE (t2) == BOOLEAN_TYPE)
961 return boolean_type_node;
963 /* If either type is BOOLEAN_TYPE, then return the other. */
964 if (TREE_CODE (t1) == BOOLEAN_TYPE)
965 return t2;
966 if (TREE_CODE (t2) == BOOLEAN_TYPE)
967 return t1;
969 return c_common_type (t1, t2);
972 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
973 or various other operations. Return 2 if they are compatible
974 but a warning may be needed if you use them together. */
977 comptypes (tree type1, tree type2)
979 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
980 int val;
982 val = comptypes_internal (type1, type2, NULL, NULL);
983 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
985 return val;
988 /* Like comptypes, but if it returns non-zero because enum and int are
989 compatible, it sets *ENUM_AND_INT_P to true. */
991 static int
992 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
994 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
995 int val;
997 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
998 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1000 return val;
1003 /* Like comptypes, but if it returns nonzero for different types, it
1004 sets *DIFFERENT_TYPES_P to true. */
1007 comptypes_check_different_types (tree type1, tree type2,
1008 bool *different_types_p)
1010 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1011 int val;
1013 val = comptypes_internal (type1, type2, NULL, different_types_p);
1014 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1016 return val;
1019 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1020 or various other operations. Return 2 if they are compatible
1021 but a warning may be needed if you use them together. If
1022 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1023 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1024 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1025 NULL, and the types are compatible but different enough not to be
1026 permitted in C11 typedef redeclarations, then this sets
1027 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1028 false, but may or may not be set if the types are incompatible.
1029 This differs from comptypes, in that we don't free the seen
1030 types. */
1032 static int
1033 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1034 bool *different_types_p)
1036 const_tree t1 = type1;
1037 const_tree t2 = type2;
1038 int attrval, val;
1040 /* Suppress errors caused by previously reported errors. */
1042 if (t1 == t2 || !t1 || !t2
1043 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1044 return 1;
1046 /* Enumerated types are compatible with integer types, but this is
1047 not transitive: two enumerated types in the same translation unit
1048 are compatible with each other only if they are the same type. */
1050 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1052 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1053 if (TREE_CODE (t2) != VOID_TYPE)
1055 if (enum_and_int_p != NULL)
1056 *enum_and_int_p = true;
1057 if (different_types_p != NULL)
1058 *different_types_p = true;
1061 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1063 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1064 if (TREE_CODE (t1) != VOID_TYPE)
1066 if (enum_and_int_p != NULL)
1067 *enum_and_int_p = true;
1068 if (different_types_p != NULL)
1069 *different_types_p = true;
1073 if (t1 == t2)
1074 return 1;
1076 /* Different classes of types can't be compatible. */
1078 if (TREE_CODE (t1) != TREE_CODE (t2))
1079 return 0;
1081 /* Qualifiers must match. C99 6.7.3p9 */
1083 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1084 return 0;
1086 /* Allow for two different type nodes which have essentially the same
1087 definition. Note that we already checked for equality of the type
1088 qualifiers (just above). */
1090 if (TREE_CODE (t1) != ARRAY_TYPE
1091 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1092 return 1;
1094 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1095 if (!(attrval = comp_type_attributes (t1, t2)))
1096 return 0;
1098 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1099 val = 0;
1101 switch (TREE_CODE (t1))
1103 case POINTER_TYPE:
1104 /* Do not remove mode or aliasing information. */
1105 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1106 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1107 break;
1108 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1109 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1110 enum_and_int_p, different_types_p));
1111 break;
1113 case FUNCTION_TYPE:
1114 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1115 different_types_p);
1116 break;
1118 case ARRAY_TYPE:
1120 tree d1 = TYPE_DOMAIN (t1);
1121 tree d2 = TYPE_DOMAIN (t2);
1122 bool d1_variable, d2_variable;
1123 bool d1_zero, d2_zero;
1124 val = 1;
1126 /* Target types must match incl. qualifiers. */
1127 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1128 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1129 enum_and_int_p,
1130 different_types_p)))
1131 return 0;
1133 if (different_types_p != NULL
1134 && (d1 == 0) != (d2 == 0))
1135 *different_types_p = true;
1136 /* Sizes must match unless one is missing or variable. */
1137 if (d1 == 0 || d2 == 0 || d1 == d2)
1138 break;
1140 d1_zero = !TYPE_MAX_VALUE (d1);
1141 d2_zero = !TYPE_MAX_VALUE (d2);
1143 d1_variable = (!d1_zero
1144 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1145 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1146 d2_variable = (!d2_zero
1147 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1148 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1149 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1150 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1152 if (different_types_p != NULL
1153 && d1_variable != d2_variable)
1154 *different_types_p = true;
1155 if (d1_variable || d2_variable)
1156 break;
1157 if (d1_zero && d2_zero)
1158 break;
1159 if (d1_zero || d2_zero
1160 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1161 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1162 val = 0;
1164 break;
1167 case ENUMERAL_TYPE:
1168 case RECORD_TYPE:
1169 case UNION_TYPE:
1170 if (val != 1 && !same_translation_unit_p (t1, t2))
1172 tree a1 = TYPE_ATTRIBUTES (t1);
1173 tree a2 = TYPE_ATTRIBUTES (t2);
1175 if (! attribute_list_contained (a1, a2)
1176 && ! attribute_list_contained (a2, a1))
1177 break;
1179 if (attrval != 2)
1180 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1181 different_types_p);
1182 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1183 different_types_p);
1185 break;
1187 case VECTOR_TYPE:
1188 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1189 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1190 enum_and_int_p, different_types_p));
1191 break;
1193 default:
1194 break;
1196 return attrval == 2 && val == 1 ? 2 : val;
1199 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1200 their qualifiers, except for named address spaces. If the pointers point to
1201 different named addresses, then we must determine if one address space is a
1202 subset of the other. */
1204 static int
1205 comp_target_types (location_t location, tree ttl, tree ttr)
1207 int val;
1208 int val_ped;
1209 tree mvl = TREE_TYPE (ttl);
1210 tree mvr = TREE_TYPE (ttr);
1211 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1212 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1213 addr_space_t as_common;
1214 bool enum_and_int_p;
1216 /* Fail if pointers point to incompatible address spaces. */
1217 if (!addr_space_superset (asl, asr, &as_common))
1218 return 0;
1220 /* For pedantic record result of comptypes on arrays before losing
1221 qualifiers on the element type below. */
1222 val_ped = 1;
1224 if (TREE_CODE (mvl) == ARRAY_TYPE
1225 && TREE_CODE (mvr) == ARRAY_TYPE)
1226 val_ped = comptypes (mvl, mvr);
1228 /* Qualifiers on element types of array types that are
1229 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1231 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1232 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1233 : TYPE_MAIN_VARIANT (mvl));
1235 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1236 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1237 : TYPE_MAIN_VARIANT (mvr));
1239 enum_and_int_p = false;
1240 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1242 if (val == 1 && val_ped != 1)
1243 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1244 "are incompatible in ISO C");
1246 if (val == 2)
1247 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1249 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1250 warning_at (location, OPT_Wc___compat,
1251 "pointer target types incompatible in C++");
1253 return val;
1256 /* Subroutines of `comptypes'. */
1258 /* Determine whether two trees derive from the same translation unit.
1259 If the CONTEXT chain ends in a null, that tree's context is still
1260 being parsed, so if two trees have context chains ending in null,
1261 they're in the same translation unit. */
1263 same_translation_unit_p (const_tree t1, const_tree t2)
1265 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1266 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1268 case tcc_declaration:
1269 t1 = DECL_CONTEXT (t1); break;
1270 case tcc_type:
1271 t1 = TYPE_CONTEXT (t1); break;
1272 case tcc_exceptional:
1273 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1274 default: gcc_unreachable ();
1277 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1278 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1280 case tcc_declaration:
1281 t2 = DECL_CONTEXT (t2); break;
1282 case tcc_type:
1283 t2 = TYPE_CONTEXT (t2); break;
1284 case tcc_exceptional:
1285 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1286 default: gcc_unreachable ();
1289 return t1 == t2;
1292 /* Allocate the seen two types, assuming that they are compatible. */
1294 static struct tagged_tu_seen_cache *
1295 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1297 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1298 tu->next = tagged_tu_seen_base;
1299 tu->t1 = t1;
1300 tu->t2 = t2;
1302 tagged_tu_seen_base = tu;
1304 /* The C standard says that two structures in different translation
1305 units are compatible with each other only if the types of their
1306 fields are compatible (among other things). We assume that they
1307 are compatible until proven otherwise when building the cache.
1308 An example where this can occur is:
1309 struct a
1311 struct a *next;
1313 If we are comparing this against a similar struct in another TU,
1314 and did not assume they were compatible, we end up with an infinite
1315 loop. */
1316 tu->val = 1;
1317 return tu;
1320 /* Free the seen types until we get to TU_TIL. */
1322 static void
1323 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1325 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1326 while (tu != tu_til)
1328 const struct tagged_tu_seen_cache *const tu1
1329 = (const struct tagged_tu_seen_cache *) tu;
1330 tu = tu1->next;
1331 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1333 tagged_tu_seen_base = tu_til;
1336 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1337 compatible. If the two types are not the same (which has been
1338 checked earlier), this can only happen when multiple translation
1339 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1340 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1341 comptypes_internal. */
1343 static int
1344 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1345 bool *enum_and_int_p, bool *different_types_p)
1347 tree s1, s2;
1348 bool needs_warning = false;
1350 /* We have to verify that the tags of the types are the same. This
1351 is harder than it looks because this may be a typedef, so we have
1352 to go look at the original type. It may even be a typedef of a
1353 typedef...
1354 In the case of compiler-created builtin structs the TYPE_DECL
1355 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1356 while (TYPE_NAME (t1)
1357 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1358 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1359 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1361 while (TYPE_NAME (t2)
1362 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1363 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1364 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1366 /* C90 didn't have the requirement that the two tags be the same. */
1367 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1368 return 0;
1370 /* C90 didn't say what happened if one or both of the types were
1371 incomplete; we choose to follow C99 rules here, which is that they
1372 are compatible. */
1373 if (TYPE_SIZE (t1) == NULL
1374 || TYPE_SIZE (t2) == NULL)
1375 return 1;
1378 const struct tagged_tu_seen_cache * tts_i;
1379 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1380 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1381 return tts_i->val;
1384 switch (TREE_CODE (t1))
1386 case ENUMERAL_TYPE:
1388 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1389 /* Speed up the case where the type values are in the same order. */
1390 tree tv1 = TYPE_VALUES (t1);
1391 tree tv2 = TYPE_VALUES (t2);
1393 if (tv1 == tv2)
1395 return 1;
1398 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1400 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1401 break;
1402 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1404 tu->val = 0;
1405 return 0;
1409 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1411 return 1;
1413 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1415 tu->val = 0;
1416 return 0;
1419 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1421 tu->val = 0;
1422 return 0;
1425 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1427 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1428 if (s2 == NULL
1429 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1431 tu->val = 0;
1432 return 0;
1435 return 1;
1438 case UNION_TYPE:
1440 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1441 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1443 tu->val = 0;
1444 return 0;
1447 /* Speed up the common case where the fields are in the same order. */
1448 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1449 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1451 int result;
1453 if (DECL_NAME (s1) != DECL_NAME (s2))
1454 break;
1455 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1456 enum_and_int_p, different_types_p);
1458 if (result != 1 && !DECL_NAME (s1))
1459 break;
1460 if (result == 0)
1462 tu->val = 0;
1463 return 0;
1465 if (result == 2)
1466 needs_warning = true;
1468 if (TREE_CODE (s1) == FIELD_DECL
1469 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1470 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1472 tu->val = 0;
1473 return 0;
1476 if (!s1 && !s2)
1478 tu->val = needs_warning ? 2 : 1;
1479 return tu->val;
1482 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1484 bool ok = false;
1486 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1487 if (DECL_NAME (s1) == DECL_NAME (s2))
1489 int result;
1491 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1492 enum_and_int_p,
1493 different_types_p);
1495 if (result != 1 && !DECL_NAME (s1))
1496 continue;
1497 if (result == 0)
1499 tu->val = 0;
1500 return 0;
1502 if (result == 2)
1503 needs_warning = true;
1505 if (TREE_CODE (s1) == FIELD_DECL
1506 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1507 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1508 break;
1510 ok = true;
1511 break;
1513 if (!ok)
1515 tu->val = 0;
1516 return 0;
1519 tu->val = needs_warning ? 2 : 10;
1520 return tu->val;
1523 case RECORD_TYPE:
1525 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1527 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1528 s1 && s2;
1529 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1531 int result;
1532 if (TREE_CODE (s1) != TREE_CODE (s2)
1533 || DECL_NAME (s1) != DECL_NAME (s2))
1534 break;
1535 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1536 enum_and_int_p, different_types_p);
1537 if (result == 0)
1538 break;
1539 if (result == 2)
1540 needs_warning = true;
1542 if (TREE_CODE (s1) == FIELD_DECL
1543 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1544 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1545 break;
1547 if (s1 && s2)
1548 tu->val = 0;
1549 else
1550 tu->val = needs_warning ? 2 : 1;
1551 return tu->val;
1554 default:
1555 gcc_unreachable ();
1559 /* Return 1 if two function types F1 and F2 are compatible.
1560 If either type specifies no argument types,
1561 the other must specify a fixed number of self-promoting arg types.
1562 Otherwise, if one type specifies only the number of arguments,
1563 the other must specify that number of self-promoting arg types.
1564 Otherwise, the argument types must match.
1565 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1567 static int
1568 function_types_compatible_p (const_tree f1, const_tree f2,
1569 bool *enum_and_int_p, bool *different_types_p)
1571 tree args1, args2;
1572 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1573 int val = 1;
1574 int val1;
1575 tree ret1, ret2;
1577 ret1 = TREE_TYPE (f1);
1578 ret2 = TREE_TYPE (f2);
1580 /* 'volatile' qualifiers on a function's return type used to mean
1581 the function is noreturn. */
1582 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1583 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1584 if (TYPE_VOLATILE (ret1))
1585 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1586 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1587 if (TYPE_VOLATILE (ret2))
1588 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1589 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1590 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1591 if (val == 0)
1592 return 0;
1594 args1 = TYPE_ARG_TYPES (f1);
1595 args2 = TYPE_ARG_TYPES (f2);
1597 if (different_types_p != NULL
1598 && (args1 == 0) != (args2 == 0))
1599 *different_types_p = true;
1601 /* An unspecified parmlist matches any specified parmlist
1602 whose argument types don't need default promotions. */
1604 if (args1 == 0)
1606 if (!self_promoting_args_p (args2))
1607 return 0;
1608 /* If one of these types comes from a non-prototype fn definition,
1609 compare that with the other type's arglist.
1610 If they don't match, ask for a warning (but no error). */
1611 if (TYPE_ACTUAL_ARG_TYPES (f1)
1612 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1613 enum_and_int_p, different_types_p))
1614 val = 2;
1615 return val;
1617 if (args2 == 0)
1619 if (!self_promoting_args_p (args1))
1620 return 0;
1621 if (TYPE_ACTUAL_ARG_TYPES (f2)
1622 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1623 enum_and_int_p, different_types_p))
1624 val = 2;
1625 return val;
1628 /* Both types have argument lists: compare them and propagate results. */
1629 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1630 different_types_p);
1631 return val1 != 1 ? val1 : val;
1634 /* Check two lists of types for compatibility, returning 0 for
1635 incompatible, 1 for compatible, or 2 for compatible with
1636 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1637 comptypes_internal. */
1639 static int
1640 type_lists_compatible_p (const_tree args1, const_tree args2,
1641 bool *enum_and_int_p, bool *different_types_p)
1643 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1644 int val = 1;
1645 int newval = 0;
1647 while (1)
1649 tree a1, mv1, a2, mv2;
1650 if (args1 == 0 && args2 == 0)
1651 return val;
1652 /* If one list is shorter than the other,
1653 they fail to match. */
1654 if (args1 == 0 || args2 == 0)
1655 return 0;
1656 mv1 = a1 = TREE_VALUE (args1);
1657 mv2 = a2 = TREE_VALUE (args2);
1658 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1659 mv1 = (TYPE_ATOMIC (mv1)
1660 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1661 TYPE_QUAL_ATOMIC)
1662 : TYPE_MAIN_VARIANT (mv1));
1663 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1664 mv2 = (TYPE_ATOMIC (mv2)
1665 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1666 TYPE_QUAL_ATOMIC)
1667 : TYPE_MAIN_VARIANT (mv2));
1668 /* A null pointer instead of a type
1669 means there is supposed to be an argument
1670 but nothing is specified about what type it has.
1671 So match anything that self-promotes. */
1672 if (different_types_p != NULL
1673 && (a1 == 0) != (a2 == 0))
1674 *different_types_p = true;
1675 if (a1 == 0)
1677 if (c_type_promotes_to (a2) != a2)
1678 return 0;
1680 else if (a2 == 0)
1682 if (c_type_promotes_to (a1) != a1)
1683 return 0;
1685 /* If one of the lists has an error marker, ignore this arg. */
1686 else if (TREE_CODE (a1) == ERROR_MARK
1687 || TREE_CODE (a2) == ERROR_MARK)
1689 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1690 different_types_p)))
1692 if (different_types_p != NULL)
1693 *different_types_p = true;
1694 /* Allow wait (union {union wait *u; int *i} *)
1695 and wait (union wait *) to be compatible. */
1696 if (TREE_CODE (a1) == UNION_TYPE
1697 && (TYPE_NAME (a1) == 0
1698 || TYPE_TRANSPARENT_AGGR (a1))
1699 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1700 && tree_int_cst_equal (TYPE_SIZE (a1),
1701 TYPE_SIZE (a2)))
1703 tree memb;
1704 for (memb = TYPE_FIELDS (a1);
1705 memb; memb = DECL_CHAIN (memb))
1707 tree mv3 = TREE_TYPE (memb);
1708 if (mv3 && mv3 != error_mark_node
1709 && TREE_CODE (mv3) != ARRAY_TYPE)
1710 mv3 = (TYPE_ATOMIC (mv3)
1711 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1712 TYPE_QUAL_ATOMIC)
1713 : TYPE_MAIN_VARIANT (mv3));
1714 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1715 different_types_p))
1716 break;
1718 if (memb == 0)
1719 return 0;
1721 else if (TREE_CODE (a2) == UNION_TYPE
1722 && (TYPE_NAME (a2) == 0
1723 || TYPE_TRANSPARENT_AGGR (a2))
1724 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1725 && tree_int_cst_equal (TYPE_SIZE (a2),
1726 TYPE_SIZE (a1)))
1728 tree memb;
1729 for (memb = TYPE_FIELDS (a2);
1730 memb; memb = DECL_CHAIN (memb))
1732 tree mv3 = TREE_TYPE (memb);
1733 if (mv3 && mv3 != error_mark_node
1734 && TREE_CODE (mv3) != ARRAY_TYPE)
1735 mv3 = (TYPE_ATOMIC (mv3)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (mv3));
1739 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1740 different_types_p))
1741 break;
1743 if (memb == 0)
1744 return 0;
1746 else
1747 return 0;
1750 /* comptypes said ok, but record if it said to warn. */
1751 if (newval > val)
1752 val = newval;
1754 args1 = TREE_CHAIN (args1);
1755 args2 = TREE_CHAIN (args2);
1759 /* Compute the size to increment a pointer by. When a function type or void
1760 type or incomplete type is passed, size_one_node is returned.
1761 This function does not emit any diagnostics; the caller is responsible
1762 for that. */
1764 static tree
1765 c_size_in_bytes (const_tree type)
1767 enum tree_code code = TREE_CODE (type);
1769 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1770 || !COMPLETE_TYPE_P (type))
1771 return size_one_node;
1773 /* Convert in case a char is more than one unit. */
1774 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1775 size_int (TYPE_PRECISION (char_type_node)
1776 / BITS_PER_UNIT));
1779 /* Return either DECL or its known constant value (if it has one). */
1781 tree
1782 decl_constant_value (tree decl)
1784 if (/* Don't change a variable array bound or initial value to a constant
1785 in a place where a variable is invalid. Note that DECL_INITIAL
1786 isn't valid for a PARM_DECL. */
1787 current_function_decl != 0
1788 && TREE_CODE (decl) != PARM_DECL
1789 && !TREE_THIS_VOLATILE (decl)
1790 && TREE_READONLY (decl)
1791 && DECL_INITIAL (decl) != 0
1792 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1793 /* This is invalid if initial value is not constant.
1794 If it has either a function call, a memory reference,
1795 or a variable, then re-evaluating it could give different results. */
1796 && TREE_CONSTANT (DECL_INITIAL (decl))
1797 /* Check for cases where this is sub-optimal, even though valid. */
1798 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1799 return DECL_INITIAL (decl);
1800 return decl;
1803 /* Convert the array expression EXP to a pointer. */
1804 static tree
1805 array_to_pointer_conversion (location_t loc, tree exp)
1807 tree orig_exp = exp;
1808 tree type = TREE_TYPE (exp);
1809 tree adr;
1810 tree restype = TREE_TYPE (type);
1811 tree ptrtype;
1813 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1815 STRIP_TYPE_NOPS (exp);
1817 if (TREE_NO_WARNING (orig_exp))
1818 TREE_NO_WARNING (exp) = 1;
1820 ptrtype = build_pointer_type (restype);
1822 if (INDIRECT_REF_P (exp))
1823 return convert (ptrtype, TREE_OPERAND (exp, 0));
1825 /* In C++ array compound literals are temporary objects unless they are
1826 const or appear in namespace scope, so they are destroyed too soon
1827 to use them for much of anything (c++/53220). */
1828 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1830 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1831 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1832 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1833 "converting an array compound literal to a pointer "
1834 "is ill-formed in C++");
1837 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1838 return convert (ptrtype, adr);
1841 /* Convert the function expression EXP to a pointer. */
1842 static tree
1843 function_to_pointer_conversion (location_t loc, tree exp)
1845 tree orig_exp = exp;
1847 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1849 STRIP_TYPE_NOPS (exp);
1851 if (TREE_NO_WARNING (orig_exp))
1852 TREE_NO_WARNING (exp) = 1;
1854 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1857 /* Mark EXP as read, not just set, for set but not used -Wunused
1858 warning purposes. */
1860 void
1861 mark_exp_read (tree exp)
1863 switch (TREE_CODE (exp))
1865 case VAR_DECL:
1866 case PARM_DECL:
1867 DECL_READ_P (exp) = 1;
1868 break;
1869 case ARRAY_REF:
1870 case COMPONENT_REF:
1871 case MODIFY_EXPR:
1872 case REALPART_EXPR:
1873 case IMAGPART_EXPR:
1874 CASE_CONVERT:
1875 case ADDR_EXPR:
1876 mark_exp_read (TREE_OPERAND (exp, 0));
1877 break;
1878 case COMPOUND_EXPR:
1879 case C_MAYBE_CONST_EXPR:
1880 mark_exp_read (TREE_OPERAND (exp, 1));
1881 break;
1882 default:
1883 break;
1887 /* Perform the default conversion of arrays and functions to pointers.
1888 Return the result of converting EXP. For any other expression, just
1889 return EXP.
1891 LOC is the location of the expression. */
1893 struct c_expr
1894 default_function_array_conversion (location_t loc, struct c_expr exp)
1896 tree orig_exp = exp.value;
1897 tree type = TREE_TYPE (exp.value);
1898 enum tree_code code = TREE_CODE (type);
1900 switch (code)
1902 case ARRAY_TYPE:
1904 bool not_lvalue = false;
1905 bool lvalue_array_p;
1907 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1908 || CONVERT_EXPR_P (exp.value))
1909 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1911 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1912 not_lvalue = true;
1913 exp.value = TREE_OPERAND (exp.value, 0);
1916 if (TREE_NO_WARNING (orig_exp))
1917 TREE_NO_WARNING (exp.value) = 1;
1919 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1920 if (!flag_isoc99 && !lvalue_array_p)
1922 /* Before C99, non-lvalue arrays do not decay to pointers.
1923 Normally, using such an array would be invalid; but it can
1924 be used correctly inside sizeof or as a statement expression.
1925 Thus, do not give an error here; an error will result later. */
1926 return exp;
1929 exp.value = array_to_pointer_conversion (loc, exp.value);
1931 break;
1932 case FUNCTION_TYPE:
1933 exp.value = function_to_pointer_conversion (loc, exp.value);
1934 break;
1935 default:
1936 break;
1939 return exp;
1942 struct c_expr
1943 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1945 mark_exp_read (exp.value);
1946 return default_function_array_conversion (loc, exp);
1949 /* Return whether EXPR should be treated as an atomic lvalue for the
1950 purposes of load and store handling. */
1952 static bool
1953 really_atomic_lvalue (tree expr)
1955 if (error_operand_p (expr))
1956 return false;
1957 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1958 return false;
1959 if (!lvalue_p (expr))
1960 return false;
1962 /* Ignore _Atomic on register variables, since their addresses can't
1963 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1964 sequences wouldn't work. Ignore _Atomic on structures containing
1965 bit-fields, since accessing elements of atomic structures or
1966 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1967 it's undefined at translation time or execution time, and the
1968 normal atomic sequences again wouldn't work. */
1969 while (handled_component_p (expr))
1971 if (TREE_CODE (expr) == COMPONENT_REF
1972 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1973 return false;
1974 expr = TREE_OPERAND (expr, 0);
1976 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1977 return false;
1978 return true;
1981 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1982 including converting functions and arrays to pointers if CONVERT_P.
1983 If READ_P, also mark the expression as having been read. */
1985 struct c_expr
1986 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1987 bool convert_p, bool read_p)
1989 if (read_p)
1990 mark_exp_read (exp.value);
1991 if (convert_p)
1992 exp = default_function_array_conversion (loc, exp);
1993 if (really_atomic_lvalue (exp.value))
1995 vec<tree, va_gc> *params;
1996 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1997 tree expr_type = TREE_TYPE (exp.value);
1998 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1999 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2001 gcc_assert (TYPE_ATOMIC (expr_type));
2003 /* Expansion of a generic atomic load may require an addition
2004 element, so allocate enough to prevent a resize. */
2005 vec_alloc (params, 4);
2007 /* Remove the qualifiers for the rest of the expressions and
2008 create the VAL temp variable to hold the RHS. */
2009 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2010 tmp = create_tmp_var_raw (nonatomic_type);
2011 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2012 TREE_ADDRESSABLE (tmp) = 1;
2013 TREE_NO_WARNING (tmp) = 1;
2015 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2016 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2017 params->quick_push (expr_addr);
2018 params->quick_push (tmp_addr);
2019 params->quick_push (seq_cst);
2020 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2022 /* EXPR is always read. */
2023 mark_exp_read (exp.value);
2025 /* Return tmp which contains the value loaded. */
2026 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2027 NULL_TREE, NULL_TREE);
2029 return exp;
2032 /* EXP is an expression of integer type. Apply the integer promotions
2033 to it and return the promoted value. */
2035 tree
2036 perform_integral_promotions (tree exp)
2038 tree type = TREE_TYPE (exp);
2039 enum tree_code code = TREE_CODE (type);
2041 gcc_assert (INTEGRAL_TYPE_P (type));
2043 /* Normally convert enums to int,
2044 but convert wide enums to something wider. */
2045 if (code == ENUMERAL_TYPE)
2047 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2048 TYPE_PRECISION (integer_type_node)),
2049 ((TYPE_PRECISION (type)
2050 >= TYPE_PRECISION (integer_type_node))
2051 && TYPE_UNSIGNED (type)));
2053 return convert (type, exp);
2056 /* ??? This should no longer be needed now bit-fields have their
2057 proper types. */
2058 if (TREE_CODE (exp) == COMPONENT_REF
2059 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2060 /* If it's thinner than an int, promote it like a
2061 c_promoting_integer_type_p, otherwise leave it alone. */
2062 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2063 TYPE_PRECISION (integer_type_node)))
2064 return convert (integer_type_node, exp);
2066 if (c_promoting_integer_type_p (type))
2068 /* Preserve unsignedness if not really getting any wider. */
2069 if (TYPE_UNSIGNED (type)
2070 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2071 return convert (unsigned_type_node, exp);
2073 return convert (integer_type_node, exp);
2076 return exp;
2080 /* Perform default promotions for C data used in expressions.
2081 Enumeral types or short or char are converted to int.
2082 In addition, manifest constants symbols are replaced by their values. */
2084 tree
2085 default_conversion (tree exp)
2087 tree orig_exp;
2088 tree type = TREE_TYPE (exp);
2089 enum tree_code code = TREE_CODE (type);
2090 tree promoted_type;
2092 mark_exp_read (exp);
2094 /* Functions and arrays have been converted during parsing. */
2095 gcc_assert (code != FUNCTION_TYPE);
2096 if (code == ARRAY_TYPE)
2097 return exp;
2099 /* Constants can be used directly unless they're not loadable. */
2100 if (TREE_CODE (exp) == CONST_DECL)
2101 exp = DECL_INITIAL (exp);
2103 /* Strip no-op conversions. */
2104 orig_exp = exp;
2105 STRIP_TYPE_NOPS (exp);
2107 if (TREE_NO_WARNING (orig_exp))
2108 TREE_NO_WARNING (exp) = 1;
2110 if (code == VOID_TYPE)
2112 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2113 "void value not ignored as it ought to be");
2114 return error_mark_node;
2117 exp = require_complete_type (exp);
2118 if (exp == error_mark_node)
2119 return error_mark_node;
2121 promoted_type = targetm.promoted_type (type);
2122 if (promoted_type)
2123 return convert (promoted_type, exp);
2125 if (INTEGRAL_TYPE_P (type))
2126 return perform_integral_promotions (exp);
2128 return exp;
2131 /* Look up COMPONENT in a structure or union TYPE.
2133 If the component name is not found, returns NULL_TREE. Otherwise,
2134 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2135 stepping down the chain to the component, which is in the last
2136 TREE_VALUE of the list. Normally the list is of length one, but if
2137 the component is embedded within (nested) anonymous structures or
2138 unions, the list steps down the chain to the component. */
2140 static tree
2141 lookup_field (tree type, tree component)
2143 tree field;
2145 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2146 to the field elements. Use a binary search on this array to quickly
2147 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2148 will always be set for structures which have many elements. */
2150 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2152 int bot, top, half;
2153 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2155 field = TYPE_FIELDS (type);
2156 bot = 0;
2157 top = TYPE_LANG_SPECIFIC (type)->s->len;
2158 while (top - bot > 1)
2160 half = (top - bot + 1) >> 1;
2161 field = field_array[bot+half];
2163 if (DECL_NAME (field) == NULL_TREE)
2165 /* Step through all anon unions in linear fashion. */
2166 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2168 field = field_array[bot++];
2169 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2171 tree anon = lookup_field (TREE_TYPE (field), component);
2173 if (anon)
2174 return tree_cons (NULL_TREE, field, anon);
2176 /* The Plan 9 compiler permits referring
2177 directly to an anonymous struct/union field
2178 using a typedef name. */
2179 if (flag_plan9_extensions
2180 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2181 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2182 == TYPE_DECL)
2183 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2184 == component))
2185 break;
2189 /* Entire record is only anon unions. */
2190 if (bot > top)
2191 return NULL_TREE;
2193 /* Restart the binary search, with new lower bound. */
2194 continue;
2197 if (DECL_NAME (field) == component)
2198 break;
2199 if (DECL_NAME (field) < component)
2200 bot += half;
2201 else
2202 top = bot + half;
2205 if (DECL_NAME (field_array[bot]) == component)
2206 field = field_array[bot];
2207 else if (DECL_NAME (field) != component)
2208 return NULL_TREE;
2210 else
2212 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2214 if (DECL_NAME (field) == NULL_TREE
2215 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2217 tree anon = lookup_field (TREE_TYPE (field), component);
2219 if (anon)
2220 return tree_cons (NULL_TREE, field, anon);
2222 /* The Plan 9 compiler permits referring directly to an
2223 anonymous struct/union field using a typedef
2224 name. */
2225 if (flag_plan9_extensions
2226 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2227 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2228 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2229 == component))
2230 break;
2233 if (DECL_NAME (field) == component)
2234 break;
2237 if (field == NULL_TREE)
2238 return NULL_TREE;
2241 return tree_cons (NULL_TREE, field, NULL_TREE);
2244 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2246 static void
2247 lookup_field_fuzzy_find_candidates (tree type, tree component,
2248 vec<tree> *candidates)
2250 tree field;
2251 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2253 if (DECL_NAME (field) == NULL_TREE
2254 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2255 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2256 candidates);
2258 if (DECL_NAME (field))
2259 candidates->safe_push (DECL_NAME (field));
2263 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2264 rather than returning a TREE_LIST for an exact match. */
2266 static tree
2267 lookup_field_fuzzy (tree type, tree component)
2269 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2271 /* First, gather a list of candidates. */
2272 auto_vec <tree> candidates;
2274 lookup_field_fuzzy_find_candidates (type, component,
2275 &candidates);
2277 return find_closest_identifier (component, &candidates);
2280 /* Make an expression to refer to the COMPONENT field of structure or
2281 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2282 location of the COMPONENT_REF. */
2284 tree
2285 build_component_ref (location_t loc, tree datum, tree component)
2287 tree type = TREE_TYPE (datum);
2288 enum tree_code code = TREE_CODE (type);
2289 tree field = NULL;
2290 tree ref;
2291 bool datum_lvalue = lvalue_p (datum);
2293 if (!objc_is_public (datum, component))
2294 return error_mark_node;
2296 /* Detect Objective-C property syntax object.property. */
2297 if (c_dialect_objc ()
2298 && (ref = objc_maybe_build_component_ref (datum, component)))
2299 return ref;
2301 /* See if there is a field or component with name COMPONENT. */
2303 if (code == RECORD_TYPE || code == UNION_TYPE)
2305 if (!COMPLETE_TYPE_P (type))
2307 c_incomplete_type_error (NULL_TREE, type);
2308 return error_mark_node;
2311 field = lookup_field (type, component);
2313 if (!field)
2315 tree guessed_id = lookup_field_fuzzy (type, component);
2316 if (guessed_id)
2317 error_at (loc, "%qT has no member named %qE; did you mean %qE?",
2318 type, component, guessed_id);
2319 else
2320 error_at (loc, "%qT has no member named %qE", type, component);
2321 return error_mark_node;
2324 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2325 This might be better solved in future the way the C++ front
2326 end does it - by giving the anonymous entities each a
2327 separate name and type, and then have build_component_ref
2328 recursively call itself. We can't do that here. */
2331 tree subdatum = TREE_VALUE (field);
2332 int quals;
2333 tree subtype;
2334 bool use_datum_quals;
2336 if (TREE_TYPE (subdatum) == error_mark_node)
2337 return error_mark_node;
2339 /* If this is an rvalue, it does not have qualifiers in C
2340 standard terms and we must avoid propagating such
2341 qualifiers down to a non-lvalue array that is then
2342 converted to a pointer. */
2343 use_datum_quals = (datum_lvalue
2344 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2346 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2347 if (use_datum_quals)
2348 quals |= TYPE_QUALS (TREE_TYPE (datum));
2349 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2351 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2352 NULL_TREE);
2353 SET_EXPR_LOCATION (ref, loc);
2354 if (TREE_READONLY (subdatum)
2355 || (use_datum_quals && TREE_READONLY (datum)))
2356 TREE_READONLY (ref) = 1;
2357 if (TREE_THIS_VOLATILE (subdatum)
2358 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2359 TREE_THIS_VOLATILE (ref) = 1;
2361 if (TREE_DEPRECATED (subdatum))
2362 warn_deprecated_use (subdatum, NULL_TREE);
2364 datum = ref;
2366 field = TREE_CHAIN (field);
2368 while (field);
2370 return ref;
2372 else if (code != ERROR_MARK)
2373 error_at (loc,
2374 "request for member %qE in something not a structure or union",
2375 component);
2377 return error_mark_node;
2380 /* Given an expression PTR for a pointer, return an expression
2381 for the value pointed to.
2382 ERRORSTRING is the name of the operator to appear in error messages.
2384 LOC is the location to use for the generated tree. */
2386 tree
2387 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2389 tree pointer = default_conversion (ptr);
2390 tree type = TREE_TYPE (pointer);
2391 tree ref;
2393 if (TREE_CODE (type) == POINTER_TYPE)
2395 if (CONVERT_EXPR_P (pointer)
2396 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2398 /* If a warning is issued, mark it to avoid duplicates from
2399 the backend. This only needs to be done at
2400 warn_strict_aliasing > 2. */
2401 if (warn_strict_aliasing > 2)
2402 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2403 type, TREE_OPERAND (pointer, 0)))
2404 TREE_NO_WARNING (pointer) = 1;
2407 if (TREE_CODE (pointer) == ADDR_EXPR
2408 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2409 == TREE_TYPE (type)))
2411 ref = TREE_OPERAND (pointer, 0);
2412 protected_set_expr_location (ref, loc);
2413 return ref;
2415 else
2417 tree t = TREE_TYPE (type);
2419 ref = build1 (INDIRECT_REF, t, pointer);
2421 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2423 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2425 error_at (loc, "dereferencing pointer to incomplete type "
2426 "%qT", t);
2427 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2429 return error_mark_node;
2431 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2432 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2434 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2435 so that we get the proper error message if the result is used
2436 to assign to. Also, &* is supposed to be a no-op.
2437 And ANSI C seems to specify that the type of the result
2438 should be the const type. */
2439 /* A de-reference of a pointer to const is not a const. It is valid
2440 to change it via some other pointer. */
2441 TREE_READONLY (ref) = TYPE_READONLY (t);
2442 TREE_SIDE_EFFECTS (ref)
2443 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2444 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2445 protected_set_expr_location (ref, loc);
2446 return ref;
2449 else if (TREE_CODE (pointer) != ERROR_MARK)
2450 invalid_indirection_error (loc, type, errstring);
2452 return error_mark_node;
2455 /* This handles expressions of the form "a[i]", which denotes
2456 an array reference.
2458 This is logically equivalent in C to *(a+i), but we may do it differently.
2459 If A is a variable or a member, we generate a primitive ARRAY_REF.
2460 This avoids forcing the array out of registers, and can work on
2461 arrays that are not lvalues (for example, members of structures returned
2462 by functions).
2464 For vector types, allow vector[i] but not i[vector], and create
2465 *(((type*)&vectortype) + i) for the expression.
2467 LOC is the location to use for the returned expression. */
2469 tree
2470 build_array_ref (location_t loc, tree array, tree index)
2472 tree ret;
2473 bool swapped = false;
2474 if (TREE_TYPE (array) == error_mark_node
2475 || TREE_TYPE (index) == error_mark_node)
2476 return error_mark_node;
2478 if (flag_cilkplus && contains_array_notation_expr (index))
2480 size_t rank = 0;
2481 if (!find_rank (loc, index, index, true, &rank))
2482 return error_mark_node;
2483 if (rank > 1)
2485 error_at (loc, "rank of the array's index is greater than 1");
2486 return error_mark_node;
2489 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2490 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2491 /* Allow vector[index] but not index[vector]. */
2492 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2494 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2495 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2497 error_at (loc,
2498 "subscripted value is neither array nor pointer nor vector");
2500 return error_mark_node;
2502 std::swap (array, index);
2503 swapped = true;
2506 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2508 error_at (loc, "array subscript is not an integer");
2509 return error_mark_node;
2512 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2514 error_at (loc, "subscripted value is pointer to function");
2515 return error_mark_node;
2518 /* ??? Existing practice has been to warn only when the char
2519 index is syntactically the index, not for char[array]. */
2520 if (!swapped)
2521 warn_array_subscript_with_type_char (loc, index);
2523 /* Apply default promotions *after* noticing character types. */
2524 index = default_conversion (index);
2525 if (index == error_mark_node)
2526 return error_mark_node;
2528 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2530 bool non_lvalue
2531 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2533 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2535 tree rval, type;
2537 /* An array that is indexed by a non-constant
2538 cannot be stored in a register; we must be able to do
2539 address arithmetic on its address.
2540 Likewise an array of elements of variable size. */
2541 if (TREE_CODE (index) != INTEGER_CST
2542 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2543 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2545 if (!c_mark_addressable (array))
2546 return error_mark_node;
2548 /* An array that is indexed by a constant value which is not within
2549 the array bounds cannot be stored in a register either; because we
2550 would get a crash in store_bit_field/extract_bit_field when trying
2551 to access a non-existent part of the register. */
2552 if (TREE_CODE (index) == INTEGER_CST
2553 && TYPE_DOMAIN (TREE_TYPE (array))
2554 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2556 if (!c_mark_addressable (array))
2557 return error_mark_node;
2560 if (pedantic || warn_c90_c99_compat)
2562 tree foo = array;
2563 while (TREE_CODE (foo) == COMPONENT_REF)
2564 foo = TREE_OPERAND (foo, 0);
2565 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2566 pedwarn (loc, OPT_Wpedantic,
2567 "ISO C forbids subscripting %<register%> array");
2568 else if (!lvalue_p (foo))
2569 pedwarn_c90 (loc, OPT_Wpedantic,
2570 "ISO C90 forbids subscripting non-lvalue "
2571 "array");
2574 type = TREE_TYPE (TREE_TYPE (array));
2575 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2576 /* Array ref is const/volatile if the array elements are
2577 or if the array is. */
2578 TREE_READONLY (rval)
2579 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2580 | TREE_READONLY (array));
2581 TREE_SIDE_EFFECTS (rval)
2582 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2583 | TREE_SIDE_EFFECTS (array));
2584 TREE_THIS_VOLATILE (rval)
2585 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2586 /* This was added by rms on 16 Nov 91.
2587 It fixes vol struct foo *a; a->elts[1]
2588 in an inline function.
2589 Hope it doesn't break something else. */
2590 | TREE_THIS_VOLATILE (array));
2591 ret = require_complete_type (rval);
2592 protected_set_expr_location (ret, loc);
2593 if (non_lvalue)
2594 ret = non_lvalue_loc (loc, ret);
2595 return ret;
2597 else
2599 tree ar = default_conversion (array);
2601 if (ar == error_mark_node)
2602 return ar;
2604 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2605 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2607 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2608 index, 0),
2609 RO_ARRAY_INDEXING);
2610 if (non_lvalue)
2611 ret = non_lvalue_loc (loc, ret);
2612 return ret;
2616 /* Build an external reference to identifier ID. FUN indicates
2617 whether this will be used for a function call. LOC is the source
2618 location of the identifier. This sets *TYPE to the type of the
2619 identifier, which is not the same as the type of the returned value
2620 for CONST_DECLs defined as enum constants. If the type of the
2621 identifier is not available, *TYPE is set to NULL. */
2622 tree
2623 build_external_ref (location_t loc, tree id, int fun, tree *type)
2625 tree ref;
2626 tree decl = lookup_name (id);
2628 /* In Objective-C, an instance variable (ivar) may be preferred to
2629 whatever lookup_name() found. */
2630 decl = objc_lookup_ivar (decl, id);
2632 *type = NULL;
2633 if (decl && decl != error_mark_node)
2635 ref = decl;
2636 *type = TREE_TYPE (ref);
2638 else if (fun)
2639 /* Implicit function declaration. */
2640 ref = implicitly_declare (loc, id);
2641 else if (decl == error_mark_node)
2642 /* Don't complain about something that's already been
2643 complained about. */
2644 return error_mark_node;
2645 else
2647 undeclared_variable (loc, id);
2648 return error_mark_node;
2651 if (TREE_TYPE (ref) == error_mark_node)
2652 return error_mark_node;
2654 if (TREE_DEPRECATED (ref))
2655 warn_deprecated_use (ref, NULL_TREE);
2657 /* Recursive call does not count as usage. */
2658 if (ref != current_function_decl)
2660 TREE_USED (ref) = 1;
2663 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2665 if (!in_sizeof && !in_typeof)
2666 C_DECL_USED (ref) = 1;
2667 else if (DECL_INITIAL (ref) == 0
2668 && DECL_EXTERNAL (ref)
2669 && !TREE_PUBLIC (ref))
2670 record_maybe_used_decl (ref);
2673 if (TREE_CODE (ref) == CONST_DECL)
2675 used_types_insert (TREE_TYPE (ref));
2677 if (warn_cxx_compat
2678 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2679 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2681 warning_at (loc, OPT_Wc___compat,
2682 ("enum constant defined in struct or union "
2683 "is not visible in C++"));
2684 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2687 ref = DECL_INITIAL (ref);
2688 TREE_CONSTANT (ref) = 1;
2690 else if (current_function_decl != 0
2691 && !DECL_FILE_SCOPE_P (current_function_decl)
2692 && (VAR_OR_FUNCTION_DECL_P (ref)
2693 || TREE_CODE (ref) == PARM_DECL))
2695 tree context = decl_function_context (ref);
2697 if (context != 0 && context != current_function_decl)
2698 DECL_NONLOCAL (ref) = 1;
2700 /* C99 6.7.4p3: An inline definition of a function with external
2701 linkage ... shall not contain a reference to an identifier with
2702 internal linkage. */
2703 else if (current_function_decl != 0
2704 && DECL_DECLARED_INLINE_P (current_function_decl)
2705 && DECL_EXTERNAL (current_function_decl)
2706 && VAR_OR_FUNCTION_DECL_P (ref)
2707 && (!VAR_P (ref) || TREE_STATIC (ref))
2708 && ! TREE_PUBLIC (ref)
2709 && DECL_CONTEXT (ref) != current_function_decl)
2710 record_inline_static (loc, current_function_decl, ref,
2711 csi_internal);
2713 return ref;
2716 /* Record details of decls possibly used inside sizeof or typeof. */
2717 struct maybe_used_decl
2719 /* The decl. */
2720 tree decl;
2721 /* The level seen at (in_sizeof + in_typeof). */
2722 int level;
2723 /* The next one at this level or above, or NULL. */
2724 struct maybe_used_decl *next;
2727 static struct maybe_used_decl *maybe_used_decls;
2729 /* Record that DECL, an undefined static function reference seen
2730 inside sizeof or typeof, might be used if the operand of sizeof is
2731 a VLA type or the operand of typeof is a variably modified
2732 type. */
2734 static void
2735 record_maybe_used_decl (tree decl)
2737 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2738 t->decl = decl;
2739 t->level = in_sizeof + in_typeof;
2740 t->next = maybe_used_decls;
2741 maybe_used_decls = t;
2744 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2745 USED is false, just discard them. If it is true, mark them used
2746 (if no longer inside sizeof or typeof) or move them to the next
2747 level up (if still inside sizeof or typeof). */
2749 void
2750 pop_maybe_used (bool used)
2752 struct maybe_used_decl *p = maybe_used_decls;
2753 int cur_level = in_sizeof + in_typeof;
2754 while (p && p->level > cur_level)
2756 if (used)
2758 if (cur_level == 0)
2759 C_DECL_USED (p->decl) = 1;
2760 else
2761 p->level = cur_level;
2763 p = p->next;
2765 if (!used || cur_level == 0)
2766 maybe_used_decls = p;
2769 /* Return the result of sizeof applied to EXPR. */
2771 struct c_expr
2772 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2774 struct c_expr ret;
2775 if (expr.value == error_mark_node)
2777 ret.value = error_mark_node;
2778 ret.original_code = ERROR_MARK;
2779 ret.original_type = NULL;
2780 pop_maybe_used (false);
2782 else
2784 bool expr_const_operands = true;
2786 if (TREE_CODE (expr.value) == PARM_DECL
2787 && C_ARRAY_PARAMETER (expr.value))
2789 if (warning_at (loc, OPT_Wsizeof_array_argument,
2790 "%<sizeof%> on array function parameter %qE will "
2791 "return size of %qT", expr.value,
2792 expr.original_type))
2793 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2795 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2796 &expr_const_operands);
2797 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2798 c_last_sizeof_arg = expr.value;
2799 ret.original_code = SIZEOF_EXPR;
2800 ret.original_type = NULL;
2801 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2803 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2804 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2805 folded_expr, ret.value);
2806 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2807 SET_EXPR_LOCATION (ret.value, loc);
2809 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2811 return ret;
2814 /* Return the result of sizeof applied to T, a structure for the type
2815 name passed to sizeof (rather than the type itself). LOC is the
2816 location of the original expression. */
2818 struct c_expr
2819 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2821 tree type;
2822 struct c_expr ret;
2823 tree type_expr = NULL_TREE;
2824 bool type_expr_const = true;
2825 type = groktypename (t, &type_expr, &type_expr_const);
2826 ret.value = c_sizeof (loc, type);
2827 c_last_sizeof_arg = type;
2828 ret.original_code = SIZEOF_EXPR;
2829 ret.original_type = NULL;
2830 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2831 && c_vla_type_p (type))
2833 /* If the type is a [*] array, it is a VLA but is represented as
2834 having a size of zero. In such a case we must ensure that
2835 the result of sizeof does not get folded to a constant by
2836 c_fully_fold, because if the size is evaluated the result is
2837 not constant and so constraints on zero or negative size
2838 arrays must not be applied when this sizeof call is inside
2839 another array declarator. */
2840 if (!type_expr)
2841 type_expr = integer_zero_node;
2842 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2843 type_expr, ret.value);
2844 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2846 pop_maybe_used (type != error_mark_node
2847 ? C_TYPE_VARIABLE_SIZE (type) : false);
2848 return ret;
2851 /* Build a function call to function FUNCTION with parameters PARAMS.
2852 The function call is at LOC.
2853 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2854 TREE_VALUE of each node is a parameter-expression.
2855 FUNCTION's data type may be a function type or a pointer-to-function. */
2857 tree
2858 build_function_call (location_t loc, tree function, tree params)
2860 vec<tree, va_gc> *v;
2861 tree ret;
2863 vec_alloc (v, list_length (params));
2864 for (; params; params = TREE_CHAIN (params))
2865 v->quick_push (TREE_VALUE (params));
2866 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2867 vec_free (v);
2868 return ret;
2871 /* Give a note about the location of the declaration of DECL. */
2873 static void
2874 inform_declaration (tree decl)
2876 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2877 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2880 /* Build a function call to function FUNCTION with parameters PARAMS.
2881 ORIGTYPES, if not NULL, is a vector of types; each element is
2882 either NULL or the original type of the corresponding element in
2883 PARAMS. The original type may differ from TREE_TYPE of the
2884 parameter for enums. FUNCTION's data type may be a function type
2885 or pointer-to-function. This function changes the elements of
2886 PARAMS. */
2888 tree
2889 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2890 tree function, vec<tree, va_gc> *params,
2891 vec<tree, va_gc> *origtypes)
2893 tree fntype, fundecl = 0;
2894 tree name = NULL_TREE, result;
2895 tree tem;
2896 int nargs;
2897 tree *argarray;
2900 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2901 STRIP_TYPE_NOPS (function);
2903 /* Convert anything with function type to a pointer-to-function. */
2904 if (TREE_CODE (function) == FUNCTION_DECL)
2906 name = DECL_NAME (function);
2908 if (flag_tm)
2909 tm_malloc_replacement (function);
2910 fundecl = function;
2911 /* Atomic functions have type checking/casting already done. They are
2912 often rewritten and don't match the original parameter list. */
2913 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2914 origtypes = NULL;
2916 if (flag_cilkplus
2917 && is_cilkplus_reduce_builtin (function))
2918 origtypes = NULL;
2920 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2921 function = function_to_pointer_conversion (loc, function);
2923 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2924 expressions, like those used for ObjC messenger dispatches. */
2925 if (params && !params->is_empty ())
2926 function = objc_rewrite_function_call (function, (*params)[0]);
2928 function = c_fully_fold (function, false, NULL);
2930 fntype = TREE_TYPE (function);
2932 if (TREE_CODE (fntype) == ERROR_MARK)
2933 return error_mark_node;
2935 if (!(TREE_CODE (fntype) == POINTER_TYPE
2936 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2938 if (!flag_diagnostics_show_caret)
2939 error_at (loc,
2940 "called object %qE is not a function or function pointer",
2941 function);
2942 else if (DECL_P (function))
2944 error_at (loc,
2945 "called object %qD is not a function or function pointer",
2946 function);
2947 inform_declaration (function);
2949 else
2950 error_at (loc,
2951 "called object is not a function or function pointer");
2952 return error_mark_node;
2955 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2956 current_function_returns_abnormally = 1;
2958 /* fntype now gets the type of function pointed to. */
2959 fntype = TREE_TYPE (fntype);
2961 /* Convert the parameters to the types declared in the
2962 function prototype, or apply default promotions. */
2964 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2965 origtypes, function, fundecl);
2966 if (nargs < 0)
2967 return error_mark_node;
2969 /* Check that the function is called through a compatible prototype.
2970 If it is not, warn. */
2971 if (CONVERT_EXPR_P (function)
2972 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2973 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2974 && !comptypes (fntype, TREE_TYPE (tem)))
2976 tree return_type = TREE_TYPE (fntype);
2978 /* This situation leads to run-time undefined behavior. We can't,
2979 therefore, simply error unless we can prove that all possible
2980 executions of the program must execute the code. */
2981 warning_at (loc, 0, "function called through a non-compatible type");
2983 if (VOID_TYPE_P (return_type)
2984 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2985 pedwarn (loc, 0,
2986 "function with qualified void return type called");
2989 argarray = vec_safe_address (params);
2991 /* Check that arguments to builtin functions match the expectations. */
2992 if (fundecl
2993 && DECL_BUILT_IN (fundecl)
2994 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2995 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2996 return error_mark_node;
2998 /* Check that the arguments to the function are valid. */
2999 check_function_arguments (fntype, nargs, argarray);
3001 if (name != NULL_TREE
3002 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3004 if (require_constant_value)
3005 result =
3006 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3007 function, nargs, argarray);
3008 else
3009 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3010 function, nargs, argarray);
3011 if (TREE_CODE (result) == NOP_EXPR
3012 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3013 STRIP_TYPE_NOPS (result);
3015 else
3016 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3017 function, nargs, argarray);
3019 if (VOID_TYPE_P (TREE_TYPE (result)))
3021 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3022 pedwarn (loc, 0,
3023 "function with qualified void return type called");
3024 return result;
3026 return require_complete_type (result);
3029 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3031 tree
3032 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3033 tree function, vec<tree, va_gc> *params,
3034 vec<tree, va_gc> *origtypes)
3036 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3037 STRIP_TYPE_NOPS (function);
3039 /* Convert anything with function type to a pointer-to-function. */
3040 if (TREE_CODE (function) == FUNCTION_DECL)
3042 /* Implement type-directed function overloading for builtins.
3043 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3044 handle all the type checking. The result is a complete expression
3045 that implements this function call. */
3046 tree tem = resolve_overloaded_builtin (loc, function, params);
3047 if (tem)
3048 return tem;
3050 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3053 /* Convert the argument expressions in the vector VALUES
3054 to the types in the list TYPELIST.
3056 If TYPELIST is exhausted, or when an element has NULL as its type,
3057 perform the default conversions.
3059 ORIGTYPES is the original types of the expressions in VALUES. This
3060 holds the type of enum values which have been converted to integral
3061 types. It may be NULL.
3063 FUNCTION is a tree for the called function. It is used only for
3064 error messages, where it is formatted with %qE.
3066 This is also where warnings about wrong number of args are generated.
3068 ARG_LOC are locations of function arguments (if any).
3070 Returns the actual number of arguments processed (which may be less
3071 than the length of VALUES in some error situations), or -1 on
3072 failure. */
3074 static int
3075 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3076 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3077 tree function, tree fundecl)
3079 tree typetail, val;
3080 unsigned int parmnum;
3081 bool error_args = false;
3082 const bool type_generic = fundecl
3083 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3084 bool type_generic_remove_excess_precision = false;
3085 tree selector;
3087 /* Change pointer to function to the function itself for
3088 diagnostics. */
3089 if (TREE_CODE (function) == ADDR_EXPR
3090 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3091 function = TREE_OPERAND (function, 0);
3093 /* Handle an ObjC selector specially for diagnostics. */
3094 selector = objc_message_selector ();
3096 /* For type-generic built-in functions, determine whether excess
3097 precision should be removed (classification) or not
3098 (comparison). */
3099 if (type_generic
3100 && DECL_BUILT_IN (fundecl)
3101 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3103 switch (DECL_FUNCTION_CODE (fundecl))
3105 case BUILT_IN_ISFINITE:
3106 case BUILT_IN_ISINF:
3107 case BUILT_IN_ISINF_SIGN:
3108 case BUILT_IN_ISNAN:
3109 case BUILT_IN_ISNORMAL:
3110 case BUILT_IN_FPCLASSIFY:
3111 type_generic_remove_excess_precision = true;
3112 break;
3114 default:
3115 type_generic_remove_excess_precision = false;
3116 break;
3119 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3120 return vec_safe_length (values);
3122 /* Scan the given expressions and types, producing individual
3123 converted arguments. */
3125 for (typetail = typelist, parmnum = 0;
3126 values && values->iterate (parmnum, &val);
3127 ++parmnum)
3129 tree type = typetail ? TREE_VALUE (typetail) : 0;
3130 tree valtype = TREE_TYPE (val);
3131 tree rname = function;
3132 int argnum = parmnum + 1;
3133 const char *invalid_func_diag;
3134 bool excess_precision = false;
3135 bool npc;
3136 tree parmval;
3137 /* Some __atomic_* builtins have additional hidden argument at
3138 position 0. */
3139 location_t ploc
3140 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3141 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3142 : input_location;
3144 if (type == void_type_node)
3146 if (selector)
3147 error_at (loc, "too many arguments to method %qE", selector);
3148 else
3149 error_at (loc, "too many arguments to function %qE", function);
3150 inform_declaration (fundecl);
3151 return error_args ? -1 : (int) parmnum;
3154 if (selector && argnum > 2)
3156 rname = selector;
3157 argnum -= 2;
3160 npc = null_pointer_constant_p (val);
3162 /* If there is excess precision and a prototype, convert once to
3163 the required type rather than converting via the semantic
3164 type. Likewise without a prototype a float value represented
3165 as long double should be converted once to double. But for
3166 type-generic classification functions excess precision must
3167 be removed here. */
3168 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3169 && (type || !type_generic || !type_generic_remove_excess_precision))
3171 val = TREE_OPERAND (val, 0);
3172 excess_precision = true;
3174 val = c_fully_fold (val, false, NULL);
3175 STRIP_TYPE_NOPS (val);
3177 val = require_complete_type (val);
3179 if (type != 0)
3181 /* Formal parm type is specified by a function prototype. */
3183 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3185 error_at (ploc, "type of formal parameter %d is incomplete",
3186 parmnum + 1);
3187 parmval = val;
3189 else
3191 tree origtype;
3193 /* Optionally warn about conversions that
3194 differ from the default conversions. */
3195 if (warn_traditional_conversion || warn_traditional)
3197 unsigned int formal_prec = TYPE_PRECISION (type);
3199 if (INTEGRAL_TYPE_P (type)
3200 && TREE_CODE (valtype) == REAL_TYPE)
3201 warning_at (ploc, OPT_Wtraditional_conversion,
3202 "passing argument %d of %qE as integer rather "
3203 "than floating due to prototype",
3204 argnum, rname);
3205 if (INTEGRAL_TYPE_P (type)
3206 && TREE_CODE (valtype) == COMPLEX_TYPE)
3207 warning_at (ploc, OPT_Wtraditional_conversion,
3208 "passing argument %d of %qE as integer rather "
3209 "than complex due to prototype",
3210 argnum, rname);
3211 else if (TREE_CODE (type) == COMPLEX_TYPE
3212 && TREE_CODE (valtype) == REAL_TYPE)
3213 warning_at (ploc, OPT_Wtraditional_conversion,
3214 "passing argument %d of %qE as complex rather "
3215 "than floating due to prototype",
3216 argnum, rname);
3217 else if (TREE_CODE (type) == REAL_TYPE
3218 && INTEGRAL_TYPE_P (valtype))
3219 warning_at (ploc, OPT_Wtraditional_conversion,
3220 "passing argument %d of %qE as floating rather "
3221 "than integer due to prototype",
3222 argnum, rname);
3223 else if (TREE_CODE (type) == COMPLEX_TYPE
3224 && INTEGRAL_TYPE_P (valtype))
3225 warning_at (ploc, OPT_Wtraditional_conversion,
3226 "passing argument %d of %qE as complex rather "
3227 "than integer due to prototype",
3228 argnum, rname);
3229 else if (TREE_CODE (type) == REAL_TYPE
3230 && TREE_CODE (valtype) == COMPLEX_TYPE)
3231 warning_at (ploc, OPT_Wtraditional_conversion,
3232 "passing argument %d of %qE as floating rather "
3233 "than complex due to prototype",
3234 argnum, rname);
3235 /* ??? At some point, messages should be written about
3236 conversions between complex types, but that's too messy
3237 to do now. */
3238 else if (TREE_CODE (type) == REAL_TYPE
3239 && TREE_CODE (valtype) == REAL_TYPE)
3241 /* Warn if any argument is passed as `float',
3242 since without a prototype it would be `double'. */
3243 if (formal_prec == TYPE_PRECISION (float_type_node)
3244 && type != dfloat32_type_node)
3245 warning_at (ploc, 0,
3246 "passing argument %d of %qE as %<float%> "
3247 "rather than %<double%> due to prototype",
3248 argnum, rname);
3250 /* Warn if mismatch between argument and prototype
3251 for decimal float types. Warn of conversions with
3252 binary float types and of precision narrowing due to
3253 prototype. */
3254 else if (type != valtype
3255 && (type == dfloat32_type_node
3256 || type == dfloat64_type_node
3257 || type == dfloat128_type_node
3258 || valtype == dfloat32_type_node
3259 || valtype == dfloat64_type_node
3260 || valtype == dfloat128_type_node)
3261 && (formal_prec
3262 <= TYPE_PRECISION (valtype)
3263 || (type == dfloat128_type_node
3264 && (valtype
3265 != dfloat64_type_node
3266 && (valtype
3267 != dfloat32_type_node)))
3268 || (type == dfloat64_type_node
3269 && (valtype
3270 != dfloat32_type_node))))
3271 warning_at (ploc, 0,
3272 "passing argument %d of %qE as %qT "
3273 "rather than %qT due to prototype",
3274 argnum, rname, type, valtype);
3277 /* Detect integer changing in width or signedness.
3278 These warnings are only activated with
3279 -Wtraditional-conversion, not with -Wtraditional. */
3280 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3281 && INTEGRAL_TYPE_P (valtype))
3283 tree would_have_been = default_conversion (val);
3284 tree type1 = TREE_TYPE (would_have_been);
3286 if (TREE_CODE (type) == ENUMERAL_TYPE
3287 && (TYPE_MAIN_VARIANT (type)
3288 == TYPE_MAIN_VARIANT (valtype)))
3289 /* No warning if function asks for enum
3290 and the actual arg is that enum type. */
3292 else if (formal_prec != TYPE_PRECISION (type1))
3293 warning_at (ploc, OPT_Wtraditional_conversion,
3294 "passing argument %d of %qE "
3295 "with different width due to prototype",
3296 argnum, rname);
3297 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3299 /* Don't complain if the formal parameter type
3300 is an enum, because we can't tell now whether
3301 the value was an enum--even the same enum. */
3302 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3304 else if (TREE_CODE (val) == INTEGER_CST
3305 && int_fits_type_p (val, type))
3306 /* Change in signedness doesn't matter
3307 if a constant value is unaffected. */
3309 /* If the value is extended from a narrower
3310 unsigned type, it doesn't matter whether we
3311 pass it as signed or unsigned; the value
3312 certainly is the same either way. */
3313 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3314 && TYPE_UNSIGNED (valtype))
3316 else if (TYPE_UNSIGNED (type))
3317 warning_at (ploc, OPT_Wtraditional_conversion,
3318 "passing argument %d of %qE "
3319 "as unsigned due to prototype",
3320 argnum, rname);
3321 else
3322 warning_at (ploc, OPT_Wtraditional_conversion,
3323 "passing argument %d of %qE "
3324 "as signed due to prototype",
3325 argnum, rname);
3329 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3330 sake of better warnings from convert_and_check. */
3331 if (excess_precision)
3332 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3333 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3334 parmval = convert_for_assignment (loc, ploc, type,
3335 val, origtype, ic_argpass,
3336 npc, fundecl, function,
3337 parmnum + 1);
3339 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3340 && INTEGRAL_TYPE_P (type)
3341 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3342 parmval = default_conversion (parmval);
3345 else if (TREE_CODE (valtype) == REAL_TYPE
3346 && (TYPE_PRECISION (valtype)
3347 <= TYPE_PRECISION (double_type_node))
3348 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3349 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3350 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3352 if (type_generic)
3353 parmval = val;
3354 else
3356 /* Convert `float' to `double'. */
3357 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3358 warning_at (ploc, OPT_Wdouble_promotion,
3359 "implicit conversion from %qT to %qT when passing "
3360 "argument to function",
3361 valtype, double_type_node);
3362 parmval = convert (double_type_node, val);
3365 else if (excess_precision && !type_generic)
3366 /* A "double" argument with excess precision being passed
3367 without a prototype or in variable arguments. */
3368 parmval = convert (valtype, val);
3369 else if ((invalid_func_diag =
3370 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3372 error (invalid_func_diag);
3373 return -1;
3375 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3377 return -1;
3379 else
3380 /* Convert `short' and `char' to full-size `int'. */
3381 parmval = default_conversion (val);
3383 (*values)[parmnum] = parmval;
3384 if (parmval == error_mark_node)
3385 error_args = true;
3387 if (typetail)
3388 typetail = TREE_CHAIN (typetail);
3391 gcc_assert (parmnum == vec_safe_length (values));
3393 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3395 error_at (loc, "too few arguments to function %qE", function);
3396 inform_declaration (fundecl);
3397 return -1;
3400 return error_args ? -1 : (int) parmnum;
3403 /* This is the entry point used by the parser to build unary operators
3404 in the input. CODE, a tree_code, specifies the unary operator, and
3405 ARG is the operand. For unary plus, the C parser currently uses
3406 CONVERT_EXPR for code.
3408 LOC is the location to use for the tree generated.
3411 struct c_expr
3412 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3414 struct c_expr result;
3416 result.original_code = code;
3417 result.original_type = NULL;
3419 if (reject_gcc_builtin (arg.value))
3421 result.value = error_mark_node;
3423 else
3425 result.value = build_unary_op (loc, code, arg.value, 0);
3427 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3428 overflow_warning (loc, result.value);
3431 /* We are typically called when parsing a prefix token at LOC acting on
3432 ARG. Reflect this by updating the source range of the result to
3433 start at LOC and end at the end of ARG. */
3434 set_c_expr_source_range (&result,
3435 loc, arg.get_finish ());
3437 return result;
3440 /* This is the entry point used by the parser to build binary operators
3441 in the input. CODE, a tree_code, specifies the binary operator, and
3442 ARG1 and ARG2 are the operands. In addition to constructing the
3443 expression, we check for operands that were written with other binary
3444 operators in a way that is likely to confuse the user.
3446 LOCATION is the location of the binary operator. */
3448 struct c_expr
3449 parser_build_binary_op (location_t location, enum tree_code code,
3450 struct c_expr arg1, struct c_expr arg2)
3452 struct c_expr result;
3454 enum tree_code code1 = arg1.original_code;
3455 enum tree_code code2 = arg2.original_code;
3456 tree type1 = (arg1.original_type
3457 ? arg1.original_type
3458 : TREE_TYPE (arg1.value));
3459 tree type2 = (arg2.original_type
3460 ? arg2.original_type
3461 : TREE_TYPE (arg2.value));
3463 result.value = build_binary_op (location, code,
3464 arg1.value, arg2.value, 1);
3465 result.original_code = code;
3466 result.original_type = NULL;
3468 if (TREE_CODE (result.value) == ERROR_MARK)
3469 return result;
3471 if (location != UNKNOWN_LOCATION)
3472 protected_set_expr_location (result.value, location);
3474 set_c_expr_source_range (&result,
3475 arg1.get_start (),
3476 arg2.get_finish ());
3478 /* Check for cases such as x+y<<z which users are likely
3479 to misinterpret. */
3480 if (warn_parentheses)
3481 warn_about_parentheses (location, code, code1, arg1.value, code2,
3482 arg2.value);
3484 if (warn_logical_op)
3485 warn_logical_operator (location, code, TREE_TYPE (result.value),
3486 code1, arg1.value, code2, arg2.value);
3488 if (warn_tautological_compare)
3490 tree lhs = arg1.value;
3491 tree rhs = arg2.value;
3492 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3494 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3495 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3496 lhs = NULL_TREE;
3497 else
3498 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3500 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3502 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3503 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3504 rhs = NULL_TREE;
3505 else
3506 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3508 if (lhs != NULL_TREE && rhs != NULL_TREE)
3509 warn_tautological_cmp (location, code, lhs, rhs);
3512 if (warn_logical_not_paren
3513 && TREE_CODE_CLASS (code) == tcc_comparison
3514 && code1 == TRUTH_NOT_EXPR
3515 && code2 != TRUTH_NOT_EXPR
3516 /* Avoid warning for !!x == y. */
3517 && (TREE_CODE (arg1.value) != NE_EXPR
3518 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3520 /* Avoid warning for !b == y where b has _Bool type. */
3521 tree t = integer_zero_node;
3522 if (TREE_CODE (arg1.value) == EQ_EXPR
3523 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3524 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3526 t = TREE_OPERAND (arg1.value, 0);
3529 if (TREE_TYPE (t) != integer_type_node)
3530 break;
3531 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3532 t = C_MAYBE_CONST_EXPR_EXPR (t);
3533 else if (CONVERT_EXPR_P (t))
3534 t = TREE_OPERAND (t, 0);
3535 else
3536 break;
3538 while (1);
3540 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3541 warn_logical_not_parentheses (location, code, arg2.value);
3544 /* Warn about comparisons against string literals, with the exception
3545 of testing for equality or inequality of a string literal with NULL. */
3546 if (code == EQ_EXPR || code == NE_EXPR)
3548 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3549 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3550 warning_at (location, OPT_Waddress,
3551 "comparison with string literal results in unspecified behavior");
3553 else if (TREE_CODE_CLASS (code) == tcc_comparison
3554 && (code1 == STRING_CST || code2 == STRING_CST))
3555 warning_at (location, OPT_Waddress,
3556 "comparison with string literal results in unspecified behavior");
3558 if (TREE_OVERFLOW_P (result.value)
3559 && !TREE_OVERFLOW_P (arg1.value)
3560 && !TREE_OVERFLOW_P (arg2.value))
3561 overflow_warning (location, result.value);
3563 /* Warn about comparisons of different enum types. */
3564 if (warn_enum_compare
3565 && TREE_CODE_CLASS (code) == tcc_comparison
3566 && TREE_CODE (type1) == ENUMERAL_TYPE
3567 && TREE_CODE (type2) == ENUMERAL_TYPE
3568 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3569 warning_at (location, OPT_Wenum_compare,
3570 "comparison between %qT and %qT",
3571 type1, type2);
3573 return result;
3576 /* Return a tree for the difference of pointers OP0 and OP1.
3577 The resulting tree has type int. */
3579 static tree
3580 pointer_diff (location_t loc, tree op0, tree op1)
3582 tree restype = ptrdiff_type_node;
3583 tree result, inttype;
3585 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3586 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3587 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3588 tree orig_op1 = op1;
3590 /* If the operands point into different address spaces, we need to
3591 explicitly convert them to pointers into the common address space
3592 before we can subtract the numerical address values. */
3593 if (as0 != as1)
3595 addr_space_t as_common;
3596 tree common_type;
3598 /* Determine the common superset address space. This is guaranteed
3599 to exist because the caller verified that comp_target_types
3600 returned non-zero. */
3601 if (!addr_space_superset (as0, as1, &as_common))
3602 gcc_unreachable ();
3604 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3605 op0 = convert (common_type, op0);
3606 op1 = convert (common_type, op1);
3609 /* Determine integer type to perform computations in. This will usually
3610 be the same as the result type (ptrdiff_t), but may need to be a wider
3611 type if pointers for the address space are wider than ptrdiff_t. */
3612 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3613 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3614 else
3615 inttype = restype;
3617 if (TREE_CODE (target_type) == VOID_TYPE)
3618 pedwarn (loc, OPT_Wpointer_arith,
3619 "pointer of type %<void *%> used in subtraction");
3620 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3621 pedwarn (loc, OPT_Wpointer_arith,
3622 "pointer to a function used in subtraction");
3624 /* First do the subtraction as integers;
3625 then drop through to build the divide operator.
3626 Do not do default conversions on the minus operator
3627 in case restype is a short type. */
3629 op0 = build_binary_op (loc,
3630 MINUS_EXPR, convert (inttype, op0),
3631 convert (inttype, op1), 0);
3632 /* This generates an error if op1 is pointer to incomplete type. */
3633 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3634 error_at (loc, "arithmetic on pointer to an incomplete type");
3636 op1 = c_size_in_bytes (target_type);
3638 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3639 error_at (loc, "arithmetic on pointer to an empty aggregate");
3641 /* Divide by the size, in easiest possible way. */
3642 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3643 op0, convert (inttype, op1));
3645 /* Convert to final result type if necessary. */
3646 return convert (restype, result);
3649 /* Expand atomic compound assignments into an approriate sequence as
3650 specified by the C11 standard section 6.5.16.2.
3651 given
3652 _Atomic T1 E1
3653 T2 E2
3654 E1 op= E2
3656 This sequence is used for all types for which these operations are
3657 supported.
3659 In addition, built-in versions of the 'fe' prefixed routines may
3660 need to be invoked for floating point (real, complex or vector) when
3661 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3663 T1 newval;
3664 T1 old;
3665 T1 *addr
3666 T2 val
3667 fenv_t fenv
3669 addr = &E1;
3670 val = (E2);
3671 __atomic_load (addr, &old, SEQ_CST);
3672 feholdexcept (&fenv);
3673 loop:
3674 newval = old op val;
3675 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3676 SEQ_CST))
3677 goto done;
3678 feclearexcept (FE_ALL_EXCEPT);
3679 goto loop:
3680 done:
3681 feupdateenv (&fenv);
3683 Also note that the compiler is simply issuing the generic form of
3684 the atomic operations. This requires temp(s) and has their address
3685 taken. The atomic processing is smart enough to figure out when the
3686 size of an object can utilize a lock-free version, and convert the
3687 built-in call to the appropriate lock-free routine. The optimizers
3688 will then dispose of any temps that are no longer required, and
3689 lock-free implementations are utilized as long as there is target
3690 support for the required size.
3692 If the operator is NOP_EXPR, then this is a simple assignment, and
3693 an __atomic_store is issued to perform the assignment rather than
3694 the above loop.
3698 /* Build an atomic assignment at LOC, expanding into the proper
3699 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3700 the result of the operation, unless RETURN_OLD_P in which case
3701 return the old value of LHS (this is only for postincrement and
3702 postdecrement). */
3703 static tree
3704 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3705 tree rhs, bool return_old_p)
3707 tree fndecl, func_call;
3708 vec<tree, va_gc> *params;
3709 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3710 tree old, old_addr;
3711 tree compound_stmt;
3712 tree stmt, goto_stmt;
3713 tree loop_label, loop_decl, done_label, done_decl;
3715 tree lhs_type = TREE_TYPE (lhs);
3716 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3717 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3718 tree rhs_type = TREE_TYPE (rhs);
3720 gcc_assert (TYPE_ATOMIC (lhs_type));
3722 if (return_old_p)
3723 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3725 /* Allocate enough vector items for a compare_exchange. */
3726 vec_alloc (params, 6);
3728 /* Create a compound statement to hold the sequence of statements
3729 with a loop. */
3730 compound_stmt = c_begin_compound_stmt (false);
3732 /* Fold the RHS if it hasn't already been folded. */
3733 if (modifycode != NOP_EXPR)
3734 rhs = c_fully_fold (rhs, false, NULL);
3736 /* Remove the qualifiers for the rest of the expressions and create
3737 the VAL temp variable to hold the RHS. */
3738 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3739 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3740 val = create_tmp_var_raw (nonatomic_rhs_type);
3741 TREE_ADDRESSABLE (val) = 1;
3742 TREE_NO_WARNING (val) = 1;
3743 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3744 NULL_TREE);
3745 SET_EXPR_LOCATION (rhs, loc);
3746 add_stmt (rhs);
3748 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3749 an atomic_store. */
3750 if (modifycode == NOP_EXPR)
3752 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3753 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3754 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3755 params->quick_push (lhs_addr);
3756 params->quick_push (rhs);
3757 params->quick_push (seq_cst);
3758 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3759 add_stmt (func_call);
3761 /* Finish the compound statement. */
3762 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3764 /* VAL is the value which was stored, return a COMPOUND_STMT of
3765 the statement and that value. */
3766 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3769 /* Create the variables and labels required for the op= form. */
3770 old = create_tmp_var_raw (nonatomic_lhs_type);
3771 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3772 TREE_ADDRESSABLE (old) = 1;
3773 TREE_NO_WARNING (old) = 1;
3775 newval = create_tmp_var_raw (nonatomic_lhs_type);
3776 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3777 TREE_ADDRESSABLE (newval) = 1;
3779 loop_decl = create_artificial_label (loc);
3780 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3782 done_decl = create_artificial_label (loc);
3783 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3785 /* __atomic_load (addr, &old, SEQ_CST). */
3786 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3787 params->quick_push (lhs_addr);
3788 params->quick_push (old_addr);
3789 params->quick_push (seq_cst);
3790 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3791 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3792 NULL_TREE);
3793 add_stmt (old);
3794 params->truncate (0);
3796 /* Create the expressions for floating-point environment
3797 manipulation, if required. */
3798 bool need_fenv = (flag_trapping_math
3799 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3800 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3801 if (need_fenv)
3802 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3804 if (hold_call)
3805 add_stmt (hold_call);
3807 /* loop: */
3808 add_stmt (loop_label);
3810 /* newval = old + val; */
3811 rhs = build_binary_op (loc, modifycode, old, val, 1);
3812 rhs = c_fully_fold (rhs, false, NULL);
3813 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3814 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3815 NULL_TREE, 0);
3816 if (rhs != error_mark_node)
3818 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3819 NULL_TREE);
3820 SET_EXPR_LOCATION (rhs, loc);
3821 add_stmt (rhs);
3824 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3825 goto done; */
3826 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3827 params->quick_push (lhs_addr);
3828 params->quick_push (old_addr);
3829 params->quick_push (newval_addr);
3830 params->quick_push (integer_zero_node);
3831 params->quick_push (seq_cst);
3832 params->quick_push (seq_cst);
3833 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3835 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3836 SET_EXPR_LOCATION (goto_stmt, loc);
3838 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3839 SET_EXPR_LOCATION (stmt, loc);
3840 add_stmt (stmt);
3842 if (clear_call)
3843 add_stmt (clear_call);
3845 /* goto loop; */
3846 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3847 SET_EXPR_LOCATION (goto_stmt, loc);
3848 add_stmt (goto_stmt);
3850 /* done: */
3851 add_stmt (done_label);
3853 if (update_call)
3854 add_stmt (update_call);
3856 /* Finish the compound statement. */
3857 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3859 /* NEWVAL is the value that was successfully stored, return a
3860 COMPOUND_EXPR of the statement and the appropriate value. */
3861 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3862 return_old_p ? old : newval);
3865 /* Construct and perhaps optimize a tree representation
3866 for a unary operation. CODE, a tree_code, specifies the operation
3867 and XARG is the operand.
3868 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3869 the default promotions (such as from short to int).
3870 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3871 allows non-lvalues; this is only used to handle conversion of non-lvalue
3872 arrays to pointers in C99.
3874 LOCATION is the location of the operator. */
3876 tree
3877 build_unary_op (location_t location,
3878 enum tree_code code, tree xarg, int flag)
3880 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3881 tree arg = xarg;
3882 tree argtype = 0;
3883 enum tree_code typecode;
3884 tree val;
3885 tree ret = error_mark_node;
3886 tree eptype = NULL_TREE;
3887 int noconvert = flag;
3888 const char *invalid_op_diag;
3889 bool int_operands;
3891 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3892 if (int_operands)
3893 arg = remove_c_maybe_const_expr (arg);
3895 if (code != ADDR_EXPR)
3896 arg = require_complete_type (arg);
3898 typecode = TREE_CODE (TREE_TYPE (arg));
3899 if (typecode == ERROR_MARK)
3900 return error_mark_node;
3901 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3902 typecode = INTEGER_TYPE;
3904 if ((invalid_op_diag
3905 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3907 error_at (location, invalid_op_diag);
3908 return error_mark_node;
3911 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3913 eptype = TREE_TYPE (arg);
3914 arg = TREE_OPERAND (arg, 0);
3917 switch (code)
3919 case CONVERT_EXPR:
3920 /* This is used for unary plus, because a CONVERT_EXPR
3921 is enough to prevent anybody from looking inside for
3922 associativity, but won't generate any code. */
3923 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3924 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3925 || typecode == VECTOR_TYPE))
3927 error_at (location, "wrong type argument to unary plus");
3928 return error_mark_node;
3930 else if (!noconvert)
3931 arg = default_conversion (arg);
3932 arg = non_lvalue_loc (location, arg);
3933 break;
3935 case NEGATE_EXPR:
3936 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3937 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3938 || typecode == VECTOR_TYPE))
3940 error_at (location, "wrong type argument to unary minus");
3941 return error_mark_node;
3943 else if (!noconvert)
3944 arg = default_conversion (arg);
3945 break;
3947 case BIT_NOT_EXPR:
3948 /* ~ works on integer types and non float vectors. */
3949 if (typecode == INTEGER_TYPE
3950 || (typecode == VECTOR_TYPE
3951 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3953 if (!noconvert)
3954 arg = default_conversion (arg);
3956 else if (typecode == COMPLEX_TYPE)
3958 code = CONJ_EXPR;
3959 pedwarn (location, OPT_Wpedantic,
3960 "ISO C does not support %<~%> for complex conjugation");
3961 if (!noconvert)
3962 arg = default_conversion (arg);
3964 else
3966 error_at (location, "wrong type argument to bit-complement");
3967 return error_mark_node;
3969 break;
3971 case ABS_EXPR:
3972 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3974 error_at (location, "wrong type argument to abs");
3975 return error_mark_node;
3977 else if (!noconvert)
3978 arg = default_conversion (arg);
3979 break;
3981 case CONJ_EXPR:
3982 /* Conjugating a real value is a no-op, but allow it anyway. */
3983 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3984 || typecode == COMPLEX_TYPE))
3986 error_at (location, "wrong type argument to conjugation");
3987 return error_mark_node;
3989 else if (!noconvert)
3990 arg = default_conversion (arg);
3991 break;
3993 case TRUTH_NOT_EXPR:
3994 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3995 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3996 && typecode != COMPLEX_TYPE)
3998 error_at (location,
3999 "wrong type argument to unary exclamation mark");
4000 return error_mark_node;
4002 if (int_operands)
4004 arg = c_objc_common_truthvalue_conversion (location, xarg);
4005 arg = remove_c_maybe_const_expr (arg);
4007 else
4008 arg = c_objc_common_truthvalue_conversion (location, arg);
4009 ret = invert_truthvalue_loc (location, arg);
4010 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4011 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4012 location = EXPR_LOCATION (ret);
4013 goto return_build_unary_op;
4015 case REALPART_EXPR:
4016 case IMAGPART_EXPR:
4017 ret = build_real_imag_expr (location, code, arg);
4018 if (ret == error_mark_node)
4019 return error_mark_node;
4020 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4021 eptype = TREE_TYPE (eptype);
4022 goto return_build_unary_op;
4024 case PREINCREMENT_EXPR:
4025 case POSTINCREMENT_EXPR:
4026 case PREDECREMENT_EXPR:
4027 case POSTDECREMENT_EXPR:
4029 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4031 tree inner = build_unary_op (location, code,
4032 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4033 if (inner == error_mark_node)
4034 return error_mark_node;
4035 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4036 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4037 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4038 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4039 goto return_build_unary_op;
4042 /* Complain about anything that is not a true lvalue. In
4043 Objective-C, skip this check for property_refs. */
4044 if (!objc_is_property_ref (arg)
4045 && !lvalue_or_else (location,
4046 arg, ((code == PREINCREMENT_EXPR
4047 || code == POSTINCREMENT_EXPR)
4048 ? lv_increment
4049 : lv_decrement)))
4050 return error_mark_node;
4052 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4054 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4055 warning_at (location, OPT_Wc___compat,
4056 "increment of enumeration value is invalid in C++");
4057 else
4058 warning_at (location, OPT_Wc___compat,
4059 "decrement of enumeration value is invalid in C++");
4062 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4063 arg = c_fully_fold (arg, false, NULL);
4065 bool atomic_op;
4066 atomic_op = really_atomic_lvalue (arg);
4068 /* Increment or decrement the real part of the value,
4069 and don't change the imaginary part. */
4070 if (typecode == COMPLEX_TYPE)
4072 tree real, imag;
4074 pedwarn (location, OPT_Wpedantic,
4075 "ISO C does not support %<++%> and %<--%> on complex types");
4077 if (!atomic_op)
4079 arg = stabilize_reference (arg);
4080 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4081 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4082 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4083 if (real == error_mark_node || imag == error_mark_node)
4084 return error_mark_node;
4085 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4086 real, imag);
4087 goto return_build_unary_op;
4091 /* Report invalid types. */
4093 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4094 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4095 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4097 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4098 error_at (location, "wrong type argument to increment");
4099 else
4100 error_at (location, "wrong type argument to decrement");
4102 return error_mark_node;
4106 tree inc;
4108 argtype = TREE_TYPE (arg);
4110 /* Compute the increment. */
4112 if (typecode == POINTER_TYPE)
4114 /* If pointer target is an incomplete type,
4115 we just cannot know how to do the arithmetic. */
4116 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4118 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4119 error_at (location,
4120 "increment of pointer to an incomplete type %qT",
4121 TREE_TYPE (argtype));
4122 else
4123 error_at (location,
4124 "decrement of pointer to an incomplete type %qT",
4125 TREE_TYPE (argtype));
4127 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4128 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4130 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4131 pedwarn (location, OPT_Wpointer_arith,
4132 "wrong type argument to increment");
4133 else
4134 pedwarn (location, OPT_Wpointer_arith,
4135 "wrong type argument to decrement");
4138 inc = c_size_in_bytes (TREE_TYPE (argtype));
4139 inc = convert_to_ptrofftype_loc (location, inc);
4141 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4143 /* For signed fract types, we invert ++ to -- or
4144 -- to ++, and change inc from 1 to -1, because
4145 it is not possible to represent 1 in signed fract constants.
4146 For unsigned fract types, the result always overflows and
4147 we get an undefined (original) or the maximum value. */
4148 if (code == PREINCREMENT_EXPR)
4149 code = PREDECREMENT_EXPR;
4150 else if (code == PREDECREMENT_EXPR)
4151 code = PREINCREMENT_EXPR;
4152 else if (code == POSTINCREMENT_EXPR)
4153 code = POSTDECREMENT_EXPR;
4154 else /* code == POSTDECREMENT_EXPR */
4155 code = POSTINCREMENT_EXPR;
4157 inc = integer_minus_one_node;
4158 inc = convert (argtype, inc);
4160 else
4162 inc = VECTOR_TYPE_P (argtype)
4163 ? build_one_cst (argtype)
4164 : integer_one_node;
4165 inc = convert (argtype, inc);
4168 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4169 need to ask Objective-C to build the increment or decrement
4170 expression for it. */
4171 if (objc_is_property_ref (arg))
4172 return objc_build_incr_expr_for_property_ref (location, code,
4173 arg, inc);
4175 /* Report a read-only lvalue. */
4176 if (TYPE_READONLY (argtype))
4178 readonly_error (location, arg,
4179 ((code == PREINCREMENT_EXPR
4180 || code == POSTINCREMENT_EXPR)
4181 ? lv_increment : lv_decrement));
4182 return error_mark_node;
4184 else if (TREE_READONLY (arg))
4185 readonly_warning (arg,
4186 ((code == PREINCREMENT_EXPR
4187 || code == POSTINCREMENT_EXPR)
4188 ? lv_increment : lv_decrement));
4190 /* If the argument is atomic, use the special code sequences for
4191 atomic compound assignment. */
4192 if (atomic_op)
4194 arg = stabilize_reference (arg);
4195 ret = build_atomic_assign (location, arg,
4196 ((code == PREINCREMENT_EXPR
4197 || code == POSTINCREMENT_EXPR)
4198 ? PLUS_EXPR
4199 : MINUS_EXPR),
4200 (FRACT_MODE_P (TYPE_MODE (argtype))
4201 ? inc
4202 : integer_one_node),
4203 (code == POSTINCREMENT_EXPR
4204 || code == POSTDECREMENT_EXPR));
4205 goto return_build_unary_op;
4208 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4209 val = boolean_increment (code, arg);
4210 else
4211 val = build2 (code, TREE_TYPE (arg), arg, inc);
4212 TREE_SIDE_EFFECTS (val) = 1;
4213 if (TREE_CODE (val) != code)
4214 TREE_NO_WARNING (val) = 1;
4215 ret = val;
4216 goto return_build_unary_op;
4219 case ADDR_EXPR:
4220 /* Note that this operation never does default_conversion. */
4222 /* The operand of unary '&' must be an lvalue (which excludes
4223 expressions of type void), or, in C99, the result of a [] or
4224 unary '*' operator. */
4225 if (VOID_TYPE_P (TREE_TYPE (arg))
4226 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4227 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4228 pedwarn (location, 0, "taking address of expression of type %<void%>");
4230 /* Let &* cancel out to simplify resulting code. */
4231 if (INDIRECT_REF_P (arg))
4233 /* Don't let this be an lvalue. */
4234 if (lvalue_p (TREE_OPERAND (arg, 0)))
4235 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4236 ret = TREE_OPERAND (arg, 0);
4237 goto return_build_unary_op;
4240 /* Anything not already handled and not a true memory reference
4241 or a non-lvalue array is an error. */
4242 if (typecode != FUNCTION_TYPE && !flag
4243 && !lvalue_or_else (location, arg, lv_addressof))
4244 return error_mark_node;
4246 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4247 folding later. */
4248 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4250 tree inner = build_unary_op (location, code,
4251 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4252 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4253 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4254 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4255 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4256 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4257 goto return_build_unary_op;
4260 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4261 argtype = TREE_TYPE (arg);
4263 /* If the lvalue is const or volatile, merge that into the type
4264 to which the address will point. This is only needed
4265 for function types. */
4266 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4267 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4268 && TREE_CODE (argtype) == FUNCTION_TYPE)
4270 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4271 int quals = orig_quals;
4273 if (TREE_READONLY (arg))
4274 quals |= TYPE_QUAL_CONST;
4275 if (TREE_THIS_VOLATILE (arg))
4276 quals |= TYPE_QUAL_VOLATILE;
4278 argtype = c_build_qualified_type (argtype, quals);
4281 switch (TREE_CODE (arg))
4283 case COMPONENT_REF:
4284 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4286 error ("cannot take address of bit-field %qD",
4287 TREE_OPERAND (arg, 1));
4288 return error_mark_node;
4291 /* ... fall through ... */
4293 case ARRAY_REF:
4294 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4296 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4297 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4299 error ("cannot take address of scalar with reverse storage "
4300 "order");
4301 return error_mark_node;
4304 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4305 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4306 warning (OPT_Wscalar_storage_order, "address of array with "
4307 "reverse scalar storage order requested");
4310 default:
4311 break;
4314 if (!c_mark_addressable (arg))
4315 return error_mark_node;
4317 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4318 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4320 argtype = build_pointer_type (argtype);
4322 /* ??? Cope with user tricks that amount to offsetof. Delete this
4323 when we have proper support for integer constant expressions. */
4324 val = get_base_address (arg);
4325 if (val && INDIRECT_REF_P (val)
4326 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4328 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4329 goto return_build_unary_op;
4332 val = build1 (ADDR_EXPR, argtype, arg);
4334 ret = val;
4335 goto return_build_unary_op;
4337 default:
4338 gcc_unreachable ();
4341 if (argtype == 0)
4342 argtype = TREE_TYPE (arg);
4343 if (TREE_CODE (arg) == INTEGER_CST)
4344 ret = (require_constant_value
4345 ? fold_build1_initializer_loc (location, code, argtype, arg)
4346 : fold_build1_loc (location, code, argtype, arg));
4347 else
4348 ret = build1 (code, argtype, arg);
4349 return_build_unary_op:
4350 gcc_assert (ret != error_mark_node);
4351 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4352 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4353 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4354 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4355 ret = note_integer_operands (ret);
4356 if (eptype)
4357 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4358 protected_set_expr_location (ret, location);
4359 return ret;
4362 /* Return nonzero if REF is an lvalue valid for this language.
4363 Lvalues can be assigned, unless their type has TYPE_READONLY.
4364 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4366 bool
4367 lvalue_p (const_tree ref)
4369 const enum tree_code code = TREE_CODE (ref);
4371 switch (code)
4373 case REALPART_EXPR:
4374 case IMAGPART_EXPR:
4375 case COMPONENT_REF:
4376 return lvalue_p (TREE_OPERAND (ref, 0));
4378 case C_MAYBE_CONST_EXPR:
4379 return lvalue_p (TREE_OPERAND (ref, 1));
4381 case COMPOUND_LITERAL_EXPR:
4382 case STRING_CST:
4383 return 1;
4385 case INDIRECT_REF:
4386 case ARRAY_REF:
4387 case ARRAY_NOTATION_REF:
4388 case VAR_DECL:
4389 case PARM_DECL:
4390 case RESULT_DECL:
4391 case ERROR_MARK:
4392 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4393 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4395 case BIND_EXPR:
4396 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4398 default:
4399 return 0;
4403 /* Give a warning for storing in something that is read-only in GCC
4404 terms but not const in ISO C terms. */
4406 static void
4407 readonly_warning (tree arg, enum lvalue_use use)
4409 switch (use)
4411 case lv_assign:
4412 warning (0, "assignment of read-only location %qE", arg);
4413 break;
4414 case lv_increment:
4415 warning (0, "increment of read-only location %qE", arg);
4416 break;
4417 case lv_decrement:
4418 warning (0, "decrement of read-only location %qE", arg);
4419 break;
4420 default:
4421 gcc_unreachable ();
4423 return;
4427 /* Return nonzero if REF is an lvalue valid for this language;
4428 otherwise, print an error message and return zero. USE says
4429 how the lvalue is being used and so selects the error message.
4430 LOCATION is the location at which any error should be reported. */
4432 static int
4433 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4435 int win = lvalue_p (ref);
4437 if (!win)
4438 lvalue_error (loc, use);
4440 return win;
4443 /* Mark EXP saying that we need to be able to take the
4444 address of it; it should not be allocated in a register.
4445 Returns true if successful. */
4447 bool
4448 c_mark_addressable (tree exp)
4450 tree x = exp;
4452 while (1)
4453 switch (TREE_CODE (x))
4455 case COMPONENT_REF:
4456 case ADDR_EXPR:
4457 case ARRAY_REF:
4458 case REALPART_EXPR:
4459 case IMAGPART_EXPR:
4460 x = TREE_OPERAND (x, 0);
4461 break;
4463 case COMPOUND_LITERAL_EXPR:
4464 case CONSTRUCTOR:
4465 TREE_ADDRESSABLE (x) = 1;
4466 return true;
4468 case VAR_DECL:
4469 case CONST_DECL:
4470 case PARM_DECL:
4471 case RESULT_DECL:
4472 if (C_DECL_REGISTER (x)
4473 && DECL_NONLOCAL (x))
4475 if (TREE_PUBLIC (x) || is_global_var (x))
4477 error
4478 ("global register variable %qD used in nested function", x);
4479 return false;
4481 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4483 else if (C_DECL_REGISTER (x))
4485 if (TREE_PUBLIC (x) || is_global_var (x))
4486 error ("address of global register variable %qD requested", x);
4487 else
4488 error ("address of register variable %qD requested", x);
4489 return false;
4492 /* drops in */
4493 case FUNCTION_DECL:
4494 TREE_ADDRESSABLE (x) = 1;
4495 /* drops out */
4496 default:
4497 return true;
4501 /* Convert EXPR to TYPE, warning about conversion problems with
4502 constants. SEMANTIC_TYPE is the type this conversion would use
4503 without excess precision. If SEMANTIC_TYPE is NULL, this function
4504 is equivalent to convert_and_check. This function is a wrapper that
4505 handles conversions that may be different than
4506 the usual ones because of excess precision. */
4508 static tree
4509 ep_convert_and_check (location_t loc, tree type, tree expr,
4510 tree semantic_type)
4512 if (TREE_TYPE (expr) == type)
4513 return expr;
4515 if (!semantic_type)
4516 return convert_and_check (loc, type, expr);
4518 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4519 && TREE_TYPE (expr) != semantic_type)
4521 /* For integers, we need to check the real conversion, not
4522 the conversion to the excess precision type. */
4523 expr = convert_and_check (loc, semantic_type, expr);
4525 /* Result type is the excess precision type, which should be
4526 large enough, so do not check. */
4527 return convert (type, expr);
4530 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4531 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4532 if folded to an integer constant then the unselected half may
4533 contain arbitrary operations not normally permitted in constant
4534 expressions. Set the location of the expression to LOC. */
4536 tree
4537 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4538 tree op1, tree op1_original_type, tree op2,
4539 tree op2_original_type)
4541 tree type1;
4542 tree type2;
4543 enum tree_code code1;
4544 enum tree_code code2;
4545 tree result_type = NULL;
4546 tree semantic_result_type = NULL;
4547 tree orig_op1 = op1, orig_op2 = op2;
4548 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4549 bool ifexp_int_operands;
4550 tree ret;
4552 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4553 if (op1_int_operands)
4554 op1 = remove_c_maybe_const_expr (op1);
4555 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4556 if (op2_int_operands)
4557 op2 = remove_c_maybe_const_expr (op2);
4558 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4559 if (ifexp_int_operands)
4560 ifexp = remove_c_maybe_const_expr (ifexp);
4562 /* Promote both alternatives. */
4564 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4565 op1 = default_conversion (op1);
4566 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4567 op2 = default_conversion (op2);
4569 if (TREE_CODE (ifexp) == ERROR_MARK
4570 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4571 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4572 return error_mark_node;
4574 type1 = TREE_TYPE (op1);
4575 code1 = TREE_CODE (type1);
4576 type2 = TREE_TYPE (op2);
4577 code2 = TREE_CODE (type2);
4579 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4580 return error_mark_node;
4582 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4583 return error_mark_node;
4585 /* C90 does not permit non-lvalue arrays in conditional expressions.
4586 In C99 they will be pointers by now. */
4587 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4589 error_at (colon_loc, "non-lvalue array in conditional expression");
4590 return error_mark_node;
4593 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4594 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4595 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4596 || code1 == COMPLEX_TYPE)
4597 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4598 || code2 == COMPLEX_TYPE))
4600 semantic_result_type = c_common_type (type1, type2);
4601 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4603 op1 = TREE_OPERAND (op1, 0);
4604 type1 = TREE_TYPE (op1);
4605 gcc_assert (TREE_CODE (type1) == code1);
4607 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4609 op2 = TREE_OPERAND (op2, 0);
4610 type2 = TREE_TYPE (op2);
4611 gcc_assert (TREE_CODE (type2) == code2);
4615 if (warn_cxx_compat)
4617 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4618 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4620 if (TREE_CODE (t1) == ENUMERAL_TYPE
4621 && TREE_CODE (t2) == ENUMERAL_TYPE
4622 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4623 warning_at (colon_loc, OPT_Wc___compat,
4624 ("different enum types in conditional is "
4625 "invalid in C++: %qT vs %qT"),
4626 t1, t2);
4629 /* Quickly detect the usual case where op1 and op2 have the same type
4630 after promotion. */
4631 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4633 if (type1 == type2)
4634 result_type = type1;
4635 else
4636 result_type = TYPE_MAIN_VARIANT (type1);
4638 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4639 || code1 == COMPLEX_TYPE)
4640 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4641 || code2 == COMPLEX_TYPE))
4643 result_type = c_common_type (type1, type2);
4644 do_warn_double_promotion (result_type, type1, type2,
4645 "implicit conversion from %qT to %qT to "
4646 "match other result of conditional",
4647 colon_loc);
4649 /* If -Wsign-compare, warn here if type1 and type2 have
4650 different signedness. We'll promote the signed to unsigned
4651 and later code won't know it used to be different.
4652 Do this check on the original types, so that explicit casts
4653 will be considered, but default promotions won't. */
4654 if (c_inhibit_evaluation_warnings == 0)
4656 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4657 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4659 if (unsigned_op1 ^ unsigned_op2)
4661 bool ovf;
4663 /* Do not warn if the result type is signed, since the
4664 signed type will only be chosen if it can represent
4665 all the values of the unsigned type. */
4666 if (!TYPE_UNSIGNED (result_type))
4667 /* OK */;
4668 else
4670 bool op1_maybe_const = true;
4671 bool op2_maybe_const = true;
4673 /* Do not warn if the signed quantity is an
4674 unsuffixed integer literal (or some static
4675 constant expression involving such literals) and
4676 it is non-negative. This warning requires the
4677 operands to be folded for best results, so do
4678 that folding in this case even without
4679 warn_sign_compare to avoid warning options
4680 possibly affecting code generation. */
4681 c_inhibit_evaluation_warnings
4682 += (ifexp == truthvalue_false_node);
4683 op1 = c_fully_fold (op1, require_constant_value,
4684 &op1_maybe_const);
4685 c_inhibit_evaluation_warnings
4686 -= (ifexp == truthvalue_false_node);
4688 c_inhibit_evaluation_warnings
4689 += (ifexp == truthvalue_true_node);
4690 op2 = c_fully_fold (op2, require_constant_value,
4691 &op2_maybe_const);
4692 c_inhibit_evaluation_warnings
4693 -= (ifexp == truthvalue_true_node);
4695 if (warn_sign_compare)
4697 if ((unsigned_op2
4698 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4699 || (unsigned_op1
4700 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4701 /* OK */;
4702 else
4703 warning_at (colon_loc, OPT_Wsign_compare,
4704 ("signed and unsigned type in "
4705 "conditional expression"));
4707 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4708 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4709 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4710 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4715 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4717 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4718 pedwarn (colon_loc, OPT_Wpedantic,
4719 "ISO C forbids conditional expr with only one void side");
4720 result_type = void_type_node;
4722 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4724 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4725 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4726 addr_space_t as_common;
4728 if (comp_target_types (colon_loc, type1, type2))
4729 result_type = common_pointer_type (type1, type2);
4730 else if (null_pointer_constant_p (orig_op1))
4731 result_type = type2;
4732 else if (null_pointer_constant_p (orig_op2))
4733 result_type = type1;
4734 else if (!addr_space_superset (as1, as2, &as_common))
4736 error_at (colon_loc, "pointers to disjoint address spaces "
4737 "used in conditional expression");
4738 return error_mark_node;
4740 else if (VOID_TYPE_P (TREE_TYPE (type1))
4741 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4743 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4744 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4745 & ~TYPE_QUALS (TREE_TYPE (type1))))
4746 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4747 "pointer to array loses qualifier "
4748 "in conditional expression");
4750 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4751 pedwarn (colon_loc, OPT_Wpedantic,
4752 "ISO C forbids conditional expr between "
4753 "%<void *%> and function pointer");
4754 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4755 TREE_TYPE (type2)));
4757 else if (VOID_TYPE_P (TREE_TYPE (type2))
4758 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4760 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4761 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4762 & ~TYPE_QUALS (TREE_TYPE (type2))))
4763 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4764 "pointer to array loses qualifier "
4765 "in conditional expression");
4767 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4768 pedwarn (colon_loc, OPT_Wpedantic,
4769 "ISO C forbids conditional expr between "
4770 "%<void *%> and function pointer");
4771 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4772 TREE_TYPE (type1)));
4774 /* Objective-C pointer comparisons are a bit more lenient. */
4775 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4776 result_type = objc_common_type (type1, type2);
4777 else
4779 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4781 pedwarn (colon_loc, 0,
4782 "pointer type mismatch in conditional expression");
4783 result_type = build_pointer_type
4784 (build_qualified_type (void_type_node, qual));
4787 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4789 if (!null_pointer_constant_p (orig_op2))
4790 pedwarn (colon_loc, 0,
4791 "pointer/integer type mismatch in conditional expression");
4792 else
4794 op2 = null_pointer_node;
4796 result_type = type1;
4798 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4800 if (!null_pointer_constant_p (orig_op1))
4801 pedwarn (colon_loc, 0,
4802 "pointer/integer type mismatch in conditional expression");
4803 else
4805 op1 = null_pointer_node;
4807 result_type = type2;
4810 if (!result_type)
4812 if (flag_cond_mismatch)
4813 result_type = void_type_node;
4814 else
4816 error_at (colon_loc, "type mismatch in conditional expression");
4817 return error_mark_node;
4821 /* Merge const and volatile flags of the incoming types. */
4822 result_type
4823 = build_type_variant (result_type,
4824 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4825 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4827 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4828 semantic_result_type);
4829 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4830 semantic_result_type);
4832 if (ifexp_bcp && ifexp == truthvalue_true_node)
4834 op2_int_operands = true;
4835 op1 = c_fully_fold (op1, require_constant_value, NULL);
4837 if (ifexp_bcp && ifexp == truthvalue_false_node)
4839 op1_int_operands = true;
4840 op2 = c_fully_fold (op2, require_constant_value, NULL);
4842 int_const = int_operands = (ifexp_int_operands
4843 && op1_int_operands
4844 && op2_int_operands);
4845 if (int_operands)
4847 int_const = ((ifexp == truthvalue_true_node
4848 && TREE_CODE (orig_op1) == INTEGER_CST
4849 && !TREE_OVERFLOW (orig_op1))
4850 || (ifexp == truthvalue_false_node
4851 && TREE_CODE (orig_op2) == INTEGER_CST
4852 && !TREE_OVERFLOW (orig_op2)));
4855 /* Need to convert condition operand into a vector mask. */
4856 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
4858 tree vectype = TREE_TYPE (ifexp);
4859 tree elem_type = TREE_TYPE (vectype);
4860 tree zero = build_int_cst (elem_type, 0);
4861 tree zero_vec = build_vector_from_val (vectype, zero);
4862 tree cmp_type = build_same_sized_truth_vector_type (vectype);
4863 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
4866 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4867 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4868 else
4870 if (int_operands)
4872 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4873 nested inside of the expression. */
4874 op1 = c_fully_fold (op1, false, NULL);
4875 op2 = c_fully_fold (op2, false, NULL);
4877 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4878 if (int_operands)
4879 ret = note_integer_operands (ret);
4881 if (semantic_result_type)
4882 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4884 protected_set_expr_location (ret, colon_loc);
4885 return ret;
4888 /* Return a compound expression that performs two expressions and
4889 returns the value of the second of them.
4891 LOC is the location of the COMPOUND_EXPR. */
4893 tree
4894 build_compound_expr (location_t loc, tree expr1, tree expr2)
4896 bool expr1_int_operands, expr2_int_operands;
4897 tree eptype = NULL_TREE;
4898 tree ret;
4900 if (flag_cilkplus
4901 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4902 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4904 error_at (loc,
4905 "spawned function call cannot be part of a comma expression");
4906 return error_mark_node;
4908 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4909 if (expr1_int_operands)
4910 expr1 = remove_c_maybe_const_expr (expr1);
4911 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4912 if (expr2_int_operands)
4913 expr2 = remove_c_maybe_const_expr (expr2);
4915 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4916 expr1 = TREE_OPERAND (expr1, 0);
4917 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4919 eptype = TREE_TYPE (expr2);
4920 expr2 = TREE_OPERAND (expr2, 0);
4923 if (!TREE_SIDE_EFFECTS (expr1))
4925 /* The left-hand operand of a comma expression is like an expression
4926 statement: with -Wunused, we should warn if it doesn't have
4927 any side-effects, unless it was explicitly cast to (void). */
4928 if (warn_unused_value)
4930 if (VOID_TYPE_P (TREE_TYPE (expr1))
4931 && CONVERT_EXPR_P (expr1))
4932 ; /* (void) a, b */
4933 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4934 && TREE_CODE (expr1) == COMPOUND_EXPR
4935 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4936 ; /* (void) a, (void) b, c */
4937 else
4938 warning_at (loc, OPT_Wunused_value,
4939 "left-hand operand of comma expression has no effect");
4942 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4943 && warn_unused_value)
4945 tree r = expr1;
4946 location_t cloc = loc;
4947 while (TREE_CODE (r) == COMPOUND_EXPR)
4949 if (EXPR_HAS_LOCATION (r))
4950 cloc = EXPR_LOCATION (r);
4951 r = TREE_OPERAND (r, 1);
4953 if (!TREE_SIDE_EFFECTS (r)
4954 && !VOID_TYPE_P (TREE_TYPE (r))
4955 && !CONVERT_EXPR_P (r))
4956 warning_at (cloc, OPT_Wunused_value,
4957 "right-hand operand of comma expression has no effect");
4960 /* With -Wunused, we should also warn if the left-hand operand does have
4961 side-effects, but computes a value which is not used. For example, in
4962 `foo() + bar(), baz()' the result of the `+' operator is not used,
4963 so we should issue a warning. */
4964 else if (warn_unused_value)
4965 warn_if_unused_value (expr1, loc);
4967 if (expr2 == error_mark_node)
4968 return error_mark_node;
4970 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4972 if (flag_isoc99
4973 && expr1_int_operands
4974 && expr2_int_operands)
4975 ret = note_integer_operands (ret);
4977 if (eptype)
4978 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4980 protected_set_expr_location (ret, loc);
4981 return ret;
4984 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4985 which we are casting. OTYPE is the type of the expression being
4986 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4987 of the cast. -Wcast-qual appeared on the command line. Named
4988 address space qualifiers are not handled here, because they result
4989 in different warnings. */
4991 static void
4992 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4994 tree in_type = type;
4995 tree in_otype = otype;
4996 int added = 0;
4997 int discarded = 0;
4998 bool is_const;
5000 /* Check that the qualifiers on IN_TYPE are a superset of the
5001 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5002 nodes is uninteresting and we stop as soon as we hit a
5003 non-POINTER_TYPE node on either type. */
5006 in_otype = TREE_TYPE (in_otype);
5007 in_type = TREE_TYPE (in_type);
5009 /* GNU C allows cv-qualified function types. 'const' means the
5010 function is very pure, 'volatile' means it can't return. We
5011 need to warn when such qualifiers are added, not when they're
5012 taken away. */
5013 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5014 && TREE_CODE (in_type) == FUNCTION_TYPE)
5015 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5016 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5017 else
5018 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5019 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5021 while (TREE_CODE (in_type) == POINTER_TYPE
5022 && TREE_CODE (in_otype) == POINTER_TYPE);
5024 if (added)
5025 warning_at (loc, OPT_Wcast_qual,
5026 "cast adds %q#v qualifier to function type", added);
5028 if (discarded)
5029 /* There are qualifiers present in IN_OTYPE that are not present
5030 in IN_TYPE. */
5031 warning_at (loc, OPT_Wcast_qual,
5032 "cast discards %qv qualifier from pointer target type",
5033 discarded);
5035 if (added || discarded)
5036 return;
5038 /* A cast from **T to const **T is unsafe, because it can cause a
5039 const value to be changed with no additional warning. We only
5040 issue this warning if T is the same on both sides, and we only
5041 issue the warning if there are the same number of pointers on
5042 both sides, as otherwise the cast is clearly unsafe anyhow. A
5043 cast is unsafe when a qualifier is added at one level and const
5044 is not present at all outer levels.
5046 To issue this warning, we check at each level whether the cast
5047 adds new qualifiers not already seen. We don't need to special
5048 case function types, as they won't have the same
5049 TYPE_MAIN_VARIANT. */
5051 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5052 return;
5053 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5054 return;
5056 in_type = type;
5057 in_otype = otype;
5058 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5061 in_type = TREE_TYPE (in_type);
5062 in_otype = TREE_TYPE (in_otype);
5063 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5064 && !is_const)
5066 warning_at (loc, OPT_Wcast_qual,
5067 "to be safe all intermediate pointers in cast from "
5068 "%qT to %qT must be %<const%> qualified",
5069 otype, type);
5070 break;
5072 if (is_const)
5073 is_const = TYPE_READONLY (in_type);
5075 while (TREE_CODE (in_type) == POINTER_TYPE);
5078 /* Build an expression representing a cast to type TYPE of expression EXPR.
5079 LOC is the location of the cast-- typically the open paren of the cast. */
5081 tree
5082 build_c_cast (location_t loc, tree type, tree expr)
5084 tree value;
5086 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5087 expr = TREE_OPERAND (expr, 0);
5089 value = expr;
5091 if (type == error_mark_node || expr == error_mark_node)
5092 return error_mark_node;
5094 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5095 only in <protocol> qualifications. But when constructing cast expressions,
5096 the protocols do matter and must be kept around. */
5097 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5098 return build1 (NOP_EXPR, type, expr);
5100 type = TYPE_MAIN_VARIANT (type);
5102 if (TREE_CODE (type) == ARRAY_TYPE)
5104 error_at (loc, "cast specifies array type");
5105 return error_mark_node;
5108 if (TREE_CODE (type) == FUNCTION_TYPE)
5110 error_at (loc, "cast specifies function type");
5111 return error_mark_node;
5114 if (!VOID_TYPE_P (type))
5116 value = require_complete_type (value);
5117 if (value == error_mark_node)
5118 return error_mark_node;
5121 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5123 if (RECORD_OR_UNION_TYPE_P (type))
5124 pedwarn (loc, OPT_Wpedantic,
5125 "ISO C forbids casting nonscalar to the same type");
5127 /* Convert to remove any qualifiers from VALUE's type. */
5128 value = convert (type, value);
5130 else if (TREE_CODE (type) == UNION_TYPE)
5132 tree field;
5134 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5135 if (TREE_TYPE (field) != error_mark_node
5136 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5137 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5138 break;
5140 if (field)
5142 tree t;
5143 bool maybe_const = true;
5145 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5146 t = c_fully_fold (value, false, &maybe_const);
5147 t = build_constructor_single (type, field, t);
5148 if (!maybe_const)
5149 t = c_wrap_maybe_const (t, true);
5150 t = digest_init (loc, type, t,
5151 NULL_TREE, false, true, 0);
5152 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5153 return t;
5155 error_at (loc, "cast to union type from type not present in union");
5156 return error_mark_node;
5158 else
5160 tree otype, ovalue;
5162 if (type == void_type_node)
5164 tree t = build1 (CONVERT_EXPR, type, value);
5165 SET_EXPR_LOCATION (t, loc);
5166 return t;
5169 otype = TREE_TYPE (value);
5171 /* Optionally warn about potentially worrisome casts. */
5172 if (warn_cast_qual
5173 && TREE_CODE (type) == POINTER_TYPE
5174 && TREE_CODE (otype) == POINTER_TYPE)
5175 handle_warn_cast_qual (loc, type, otype);
5177 /* Warn about conversions between pointers to disjoint
5178 address spaces. */
5179 if (TREE_CODE (type) == POINTER_TYPE
5180 && TREE_CODE (otype) == POINTER_TYPE
5181 && !null_pointer_constant_p (value))
5183 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5184 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5185 addr_space_t as_common;
5187 if (!addr_space_superset (as_to, as_from, &as_common))
5189 if (ADDR_SPACE_GENERIC_P (as_from))
5190 warning_at (loc, 0, "cast to %s address space pointer "
5191 "from disjoint generic address space pointer",
5192 c_addr_space_name (as_to));
5194 else if (ADDR_SPACE_GENERIC_P (as_to))
5195 warning_at (loc, 0, "cast to generic address space pointer "
5196 "from disjoint %s address space pointer",
5197 c_addr_space_name (as_from));
5199 else
5200 warning_at (loc, 0, "cast to %s address space pointer "
5201 "from disjoint %s address space pointer",
5202 c_addr_space_name (as_to),
5203 c_addr_space_name (as_from));
5207 /* Warn about possible alignment problems. */
5208 if (STRICT_ALIGNMENT
5209 && TREE_CODE (type) == POINTER_TYPE
5210 && TREE_CODE (otype) == POINTER_TYPE
5211 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5212 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5213 /* Don't warn about opaque types, where the actual alignment
5214 restriction is unknown. */
5215 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5216 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5217 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5218 warning_at (loc, OPT_Wcast_align,
5219 "cast increases required alignment of target type");
5221 if (TREE_CODE (type) == INTEGER_TYPE
5222 && TREE_CODE (otype) == POINTER_TYPE
5223 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5224 /* Unlike conversion of integers to pointers, where the
5225 warning is disabled for converting constants because
5226 of cases such as SIG_*, warn about converting constant
5227 pointers to integers. In some cases it may cause unwanted
5228 sign extension, and a warning is appropriate. */
5229 warning_at (loc, OPT_Wpointer_to_int_cast,
5230 "cast from pointer to integer of different size");
5232 if (TREE_CODE (value) == CALL_EXPR
5233 && TREE_CODE (type) != TREE_CODE (otype))
5234 warning_at (loc, OPT_Wbad_function_cast,
5235 "cast from function call of type %qT "
5236 "to non-matching type %qT", otype, type);
5238 if (TREE_CODE (type) == POINTER_TYPE
5239 && TREE_CODE (otype) == INTEGER_TYPE
5240 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5241 /* Don't warn about converting any constant. */
5242 && !TREE_CONSTANT (value))
5243 warning_at (loc,
5244 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5245 "of different size");
5247 if (warn_strict_aliasing <= 2)
5248 strict_aliasing_warning (otype, type, expr);
5250 /* If pedantic, warn for conversions between function and object
5251 pointer types, except for converting a null pointer constant
5252 to function pointer type. */
5253 if (pedantic
5254 && TREE_CODE (type) == POINTER_TYPE
5255 && TREE_CODE (otype) == POINTER_TYPE
5256 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5257 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5258 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5259 "conversion of function pointer to object pointer type");
5261 if (pedantic
5262 && TREE_CODE (type) == POINTER_TYPE
5263 && TREE_CODE (otype) == POINTER_TYPE
5264 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5265 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5266 && !null_pointer_constant_p (value))
5267 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5268 "conversion of object pointer to function pointer type");
5270 ovalue = value;
5271 value = convert (type, value);
5273 /* Ignore any integer overflow caused by the cast. */
5274 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5276 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5278 if (!TREE_OVERFLOW (value))
5280 /* Avoid clobbering a shared constant. */
5281 value = copy_node (value);
5282 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5285 else if (TREE_OVERFLOW (value))
5286 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5287 value = wide_int_to_tree (TREE_TYPE (value), value);
5291 /* Don't let a cast be an lvalue. */
5292 if (lvalue_p (value))
5293 value = non_lvalue_loc (loc, value);
5295 /* Don't allow the results of casting to floating-point or complex
5296 types be confused with actual constants, or casts involving
5297 integer and pointer types other than direct integer-to-integer
5298 and integer-to-pointer be confused with integer constant
5299 expressions and null pointer constants. */
5300 if (TREE_CODE (value) == REAL_CST
5301 || TREE_CODE (value) == COMPLEX_CST
5302 || (TREE_CODE (value) == INTEGER_CST
5303 && !((TREE_CODE (expr) == INTEGER_CST
5304 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5305 || TREE_CODE (expr) == REAL_CST
5306 || TREE_CODE (expr) == COMPLEX_CST)))
5307 value = build1 (NOP_EXPR, type, value);
5309 protected_set_expr_location (value, loc);
5310 return value;
5313 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5314 location of the open paren of the cast, or the position of the cast
5315 expr. */
5316 tree
5317 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5319 tree type;
5320 tree type_expr = NULL_TREE;
5321 bool type_expr_const = true;
5322 tree ret;
5323 int saved_wsp = warn_strict_prototypes;
5325 /* This avoids warnings about unprototyped casts on
5326 integers. E.g. "#define SIG_DFL (void(*)())0". */
5327 if (TREE_CODE (expr) == INTEGER_CST)
5328 warn_strict_prototypes = 0;
5329 type = groktypename (type_name, &type_expr, &type_expr_const);
5330 warn_strict_prototypes = saved_wsp;
5332 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5333 && reject_gcc_builtin (expr))
5334 return error_mark_node;
5336 ret = build_c_cast (loc, type, expr);
5337 if (type_expr)
5339 bool inner_expr_const = true;
5340 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5341 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5342 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5343 && inner_expr_const);
5344 SET_EXPR_LOCATION (ret, loc);
5347 if (!EXPR_HAS_LOCATION (ret))
5348 protected_set_expr_location (ret, loc);
5350 /* C++ does not permits types to be defined in a cast, but it
5351 allows references to incomplete types. */
5352 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5353 warning_at (loc, OPT_Wc___compat,
5354 "defining a type in a cast is invalid in C++");
5356 return ret;
5359 /* Build an assignment expression of lvalue LHS from value RHS.
5360 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5361 may differ from TREE_TYPE (LHS) for an enum bitfield.
5362 MODIFYCODE is the code for a binary operator that we use
5363 to combine the old value of LHS with RHS to get the new value.
5364 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5365 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5366 which may differ from TREE_TYPE (RHS) for an enum value.
5368 LOCATION is the location of the MODIFYCODE operator.
5369 RHS_LOC is the location of the RHS. */
5371 tree
5372 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5373 enum tree_code modifycode,
5374 location_t rhs_loc, tree rhs, tree rhs_origtype)
5376 tree result;
5377 tree newrhs;
5378 tree rhseval = NULL_TREE;
5379 tree rhs_semantic_type = NULL_TREE;
5380 tree lhstype = TREE_TYPE (lhs);
5381 tree olhstype = lhstype;
5382 bool npc;
5383 bool is_atomic_op;
5385 /* Types that aren't fully specified cannot be used in assignments. */
5386 lhs = require_complete_type (lhs);
5388 /* Avoid duplicate error messages from operands that had errors. */
5389 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5390 return error_mark_node;
5392 /* Ensure an error for assigning a non-lvalue array to an array in
5393 C90. */
5394 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5396 error_at (location, "assignment to expression with array type");
5397 return error_mark_node;
5400 /* For ObjC properties, defer this check. */
5401 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5402 return error_mark_node;
5404 is_atomic_op = really_atomic_lvalue (lhs);
5406 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5408 rhs_semantic_type = TREE_TYPE (rhs);
5409 rhs = TREE_OPERAND (rhs, 0);
5412 newrhs = rhs;
5414 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5416 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5417 lhs_origtype, modifycode, rhs_loc, rhs,
5418 rhs_origtype);
5419 if (inner == error_mark_node)
5420 return error_mark_node;
5421 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5422 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5423 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5424 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5425 protected_set_expr_location (result, location);
5426 return result;
5429 /* If a binary op has been requested, combine the old LHS value with the RHS
5430 producing the value we should actually store into the LHS. */
5432 if (modifycode != NOP_EXPR)
5434 lhs = c_fully_fold (lhs, false, NULL);
5435 lhs = stabilize_reference (lhs);
5437 /* Construct the RHS for any non-atomic compound assignemnt. */
5438 if (!is_atomic_op)
5440 /* If in LHS op= RHS the RHS has side-effects, ensure they
5441 are preevaluated before the rest of the assignment expression's
5442 side-effects, because RHS could contain e.g. function calls
5443 that modify LHS. */
5444 if (TREE_SIDE_EFFECTS (rhs))
5446 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5447 rhseval = newrhs;
5449 newrhs = build_binary_op (location,
5450 modifycode, lhs, newrhs, 1);
5452 /* The original type of the right hand side is no longer
5453 meaningful. */
5454 rhs_origtype = NULL_TREE;
5458 if (c_dialect_objc ())
5460 /* Check if we are modifying an Objective-C property reference;
5461 if so, we need to generate setter calls. */
5462 result = objc_maybe_build_modify_expr (lhs, newrhs);
5463 if (result)
5464 goto return_result;
5466 /* Else, do the check that we postponed for Objective-C. */
5467 if (!lvalue_or_else (location, lhs, lv_assign))
5468 return error_mark_node;
5471 /* Give an error for storing in something that is 'const'. */
5473 if (TYPE_READONLY (lhstype)
5474 || (RECORD_OR_UNION_TYPE_P (lhstype)
5475 && C_TYPE_FIELDS_READONLY (lhstype)))
5477 readonly_error (location, lhs, lv_assign);
5478 return error_mark_node;
5480 else if (TREE_READONLY (lhs))
5481 readonly_warning (lhs, lv_assign);
5483 /* If storing into a structure or union member,
5484 it has probably been given type `int'.
5485 Compute the type that would go with
5486 the actual amount of storage the member occupies. */
5488 if (TREE_CODE (lhs) == COMPONENT_REF
5489 && (TREE_CODE (lhstype) == INTEGER_TYPE
5490 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5491 || TREE_CODE (lhstype) == REAL_TYPE
5492 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5493 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5495 /* If storing in a field that is in actuality a short or narrower than one,
5496 we must store in the field in its actual type. */
5498 if (lhstype != TREE_TYPE (lhs))
5500 lhs = copy_node (lhs);
5501 TREE_TYPE (lhs) = lhstype;
5504 /* Issue -Wc++-compat warnings about an assignment to an enum type
5505 when LHS does not have its original type. This happens for,
5506 e.g., an enum bitfield in a struct. */
5507 if (warn_cxx_compat
5508 && lhs_origtype != NULL_TREE
5509 && lhs_origtype != lhstype
5510 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5512 tree checktype = (rhs_origtype != NULL_TREE
5513 ? rhs_origtype
5514 : TREE_TYPE (rhs));
5515 if (checktype != error_mark_node
5516 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5517 || (is_atomic_op && modifycode != NOP_EXPR)))
5518 warning_at (location, OPT_Wc___compat,
5519 "enum conversion in assignment is invalid in C++");
5522 /* If the lhs is atomic, remove that qualifier. */
5523 if (is_atomic_op)
5525 lhstype = build_qualified_type (lhstype,
5526 (TYPE_QUALS (lhstype)
5527 & ~TYPE_QUAL_ATOMIC));
5528 olhstype = build_qualified_type (olhstype,
5529 (TYPE_QUALS (lhstype)
5530 & ~TYPE_QUAL_ATOMIC));
5533 /* Convert new value to destination type. Fold it first, then
5534 restore any excess precision information, for the sake of
5535 conversion warnings. */
5537 if (!(is_atomic_op && modifycode != NOP_EXPR))
5539 npc = null_pointer_constant_p (newrhs);
5540 newrhs = c_fully_fold (newrhs, false, NULL);
5541 if (rhs_semantic_type)
5542 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5543 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5544 rhs_origtype, ic_assign, npc,
5545 NULL_TREE, NULL_TREE, 0);
5546 if (TREE_CODE (newrhs) == ERROR_MARK)
5547 return error_mark_node;
5550 /* Emit ObjC write barrier, if necessary. */
5551 if (c_dialect_objc () && flag_objc_gc)
5553 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5554 if (result)
5556 protected_set_expr_location (result, location);
5557 goto return_result;
5561 /* Scan operands. */
5563 if (is_atomic_op)
5564 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5565 else
5567 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5568 TREE_SIDE_EFFECTS (result) = 1;
5569 protected_set_expr_location (result, location);
5572 /* If we got the LHS in a different type for storing in,
5573 convert the result back to the nominal type of LHS
5574 so that the value we return always has the same type
5575 as the LHS argument. */
5577 if (olhstype == TREE_TYPE (result))
5578 goto return_result;
5580 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5581 rhs_origtype, ic_assign, false, NULL_TREE,
5582 NULL_TREE, 0);
5583 protected_set_expr_location (result, location);
5585 return_result:
5586 if (rhseval)
5587 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5588 return result;
5591 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5592 This is used to implement -fplan9-extensions. */
5594 static bool
5595 find_anonymous_field_with_type (tree struct_type, tree type)
5597 tree field;
5598 bool found;
5600 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5601 found = false;
5602 for (field = TYPE_FIELDS (struct_type);
5603 field != NULL_TREE;
5604 field = TREE_CHAIN (field))
5606 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5607 ? c_build_qualified_type (TREE_TYPE (field),
5608 TYPE_QUAL_ATOMIC)
5609 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5610 if (DECL_NAME (field) == NULL
5611 && comptypes (type, fieldtype))
5613 if (found)
5614 return false;
5615 found = true;
5617 else if (DECL_NAME (field) == NULL
5618 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5619 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5621 if (found)
5622 return false;
5623 found = true;
5626 return found;
5629 /* RHS is an expression whose type is pointer to struct. If there is
5630 an anonymous field in RHS with type TYPE, then return a pointer to
5631 that field in RHS. This is used with -fplan9-extensions. This
5632 returns NULL if no conversion could be found. */
5634 static tree
5635 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5637 tree rhs_struct_type, lhs_main_type;
5638 tree field, found_field;
5639 bool found_sub_field;
5640 tree ret;
5642 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5643 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5644 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5646 gcc_assert (POINTER_TYPE_P (type));
5647 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5648 ? c_build_qualified_type (TREE_TYPE (type),
5649 TYPE_QUAL_ATOMIC)
5650 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5652 found_field = NULL_TREE;
5653 found_sub_field = false;
5654 for (field = TYPE_FIELDS (rhs_struct_type);
5655 field != NULL_TREE;
5656 field = TREE_CHAIN (field))
5658 if (DECL_NAME (field) != NULL_TREE
5659 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5660 continue;
5661 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5662 ? c_build_qualified_type (TREE_TYPE (field),
5663 TYPE_QUAL_ATOMIC)
5664 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5665 if (comptypes (lhs_main_type, fieldtype))
5667 if (found_field != NULL_TREE)
5668 return NULL_TREE;
5669 found_field = field;
5671 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5672 lhs_main_type))
5674 if (found_field != NULL_TREE)
5675 return NULL_TREE;
5676 found_field = field;
5677 found_sub_field = true;
5681 if (found_field == NULL_TREE)
5682 return NULL_TREE;
5684 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5685 build_fold_indirect_ref (rhs), found_field,
5686 NULL_TREE);
5687 ret = build_fold_addr_expr_loc (location, ret);
5689 if (found_sub_field)
5691 ret = convert_to_anonymous_field (location, type, ret);
5692 gcc_assert (ret != NULL_TREE);
5695 return ret;
5698 /* Issue an error message for a bad initializer component.
5699 GMSGID identifies the message.
5700 The component name is taken from the spelling stack. */
5702 static void
5703 error_init (location_t loc, const char *gmsgid)
5705 char *ofwhat;
5707 /* The gmsgid may be a format string with %< and %>. */
5708 error_at (loc, gmsgid);
5709 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5710 if (*ofwhat)
5711 inform (loc, "(near initialization for %qs)", ofwhat);
5714 /* Issue a pedantic warning for a bad initializer component. OPT is
5715 the option OPT_* (from options.h) controlling this warning or 0 if
5716 it is unconditionally given. GMSGID identifies the message. The
5717 component name is taken from the spelling stack. */
5719 static void
5720 pedwarn_init (location_t location, int opt, const char *gmsgid)
5722 char *ofwhat;
5723 bool warned;
5725 /* The gmsgid may be a format string with %< and %>. */
5726 warned = pedwarn (location, opt, gmsgid);
5727 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5728 if (*ofwhat && warned)
5729 inform (location, "(near initialization for %qs)", ofwhat);
5732 /* Issue a warning for a bad initializer component.
5734 OPT is the OPT_W* value corresponding to the warning option that
5735 controls this warning. GMSGID identifies the message. The
5736 component name is taken from the spelling stack. */
5738 static void
5739 warning_init (location_t loc, int opt, const char *gmsgid)
5741 char *ofwhat;
5742 bool warned;
5744 /* The gmsgid may be a format string with %< and %>. */
5745 warned = warning_at (loc, opt, gmsgid);
5746 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5747 if (*ofwhat && warned)
5748 inform (loc, "(near initialization for %qs)", ofwhat);
5751 /* If TYPE is an array type and EXPR is a parenthesized string
5752 constant, warn if pedantic that EXPR is being used to initialize an
5753 object of type TYPE. */
5755 void
5756 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5758 if (pedantic
5759 && TREE_CODE (type) == ARRAY_TYPE
5760 && TREE_CODE (expr.value) == STRING_CST
5761 && expr.original_code != STRING_CST)
5762 pedwarn_init (loc, OPT_Wpedantic,
5763 "array initialized from parenthesized string constant");
5766 /* Convert value RHS to type TYPE as preparation for an assignment to
5767 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5768 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5769 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5770 constant before any folding.
5771 The real work of conversion is done by `convert'.
5772 The purpose of this function is to generate error messages
5773 for assignments that are not allowed in C.
5774 ERRTYPE says whether it is argument passing, assignment,
5775 initialization or return.
5777 In the following example, '~' denotes where EXPR_LOC and '^' where
5778 LOCATION point to:
5780 f (var); [ic_argpass]
5781 ^ ~~~
5782 x = var; [ic_assign]
5783 ^ ~~~;
5784 int x = var; [ic_init]
5786 return x; [ic_return]
5789 FUNCTION is a tree for the function being called.
5790 PARMNUM is the number of the argument, for printing in error messages. */
5792 static tree
5793 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5794 tree rhs, tree origtype, enum impl_conv errtype,
5795 bool null_pointer_constant, tree fundecl,
5796 tree function, int parmnum)
5798 enum tree_code codel = TREE_CODE (type);
5799 tree orig_rhs = rhs;
5800 tree rhstype;
5801 enum tree_code coder;
5802 tree rname = NULL_TREE;
5803 bool objc_ok = false;
5805 /* Use the expansion point location to handle cases such as user's
5806 function returning a wrong-type macro defined in a system header. */
5807 location = expansion_point_location_if_in_system_header (location);
5809 if (errtype == ic_argpass)
5811 tree selector;
5812 /* Change pointer to function to the function itself for
5813 diagnostics. */
5814 if (TREE_CODE (function) == ADDR_EXPR
5815 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5816 function = TREE_OPERAND (function, 0);
5818 /* Handle an ObjC selector specially for diagnostics. */
5819 selector = objc_message_selector ();
5820 rname = function;
5821 if (selector && parmnum > 2)
5823 rname = selector;
5824 parmnum -= 2;
5828 /* This macro is used to emit diagnostics to ensure that all format
5829 strings are complete sentences, visible to gettext and checked at
5830 compile time. */
5831 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5832 do { \
5833 switch (errtype) \
5835 case ic_argpass: \
5836 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5837 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5838 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5839 "expected %qT but argument is of type %qT", \
5840 type, rhstype); \
5841 break; \
5842 case ic_assign: \
5843 pedwarn (LOCATION, OPT, AS); \
5844 break; \
5845 case ic_init: \
5846 pedwarn_init (LOCATION, OPT, IN); \
5847 break; \
5848 case ic_return: \
5849 pedwarn (LOCATION, OPT, RE); \
5850 break; \
5851 default: \
5852 gcc_unreachable (); \
5854 } while (0)
5856 /* This macro is used to emit diagnostics to ensure that all format
5857 strings are complete sentences, visible to gettext and checked at
5858 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5859 extra parameter to enumerate qualifiers. */
5860 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5861 do { \
5862 switch (errtype) \
5864 case ic_argpass: \
5865 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5866 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5867 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5868 "expected %qT but argument is of type %qT", \
5869 type, rhstype); \
5870 break; \
5871 case ic_assign: \
5872 pedwarn (LOCATION, OPT, AS, QUALS); \
5873 break; \
5874 case ic_init: \
5875 pedwarn (LOCATION, OPT, IN, QUALS); \
5876 break; \
5877 case ic_return: \
5878 pedwarn (LOCATION, OPT, RE, QUALS); \
5879 break; \
5880 default: \
5881 gcc_unreachable (); \
5883 } while (0)
5885 /* This macro is used to emit diagnostics to ensure that all format
5886 strings are complete sentences, visible to gettext and checked at
5887 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5888 warning_at instead of pedwarn. */
5889 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5890 do { \
5891 switch (errtype) \
5893 case ic_argpass: \
5894 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5895 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5896 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5897 "expected %qT but argument is of type %qT", \
5898 type, rhstype); \
5899 break; \
5900 case ic_assign: \
5901 warning_at (LOCATION, OPT, AS, QUALS); \
5902 break; \
5903 case ic_init: \
5904 warning_at (LOCATION, OPT, IN, QUALS); \
5905 break; \
5906 case ic_return: \
5907 warning_at (LOCATION, OPT, RE, QUALS); \
5908 break; \
5909 default: \
5910 gcc_unreachable (); \
5912 } while (0)
5914 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5915 rhs = TREE_OPERAND (rhs, 0);
5917 rhstype = TREE_TYPE (rhs);
5918 coder = TREE_CODE (rhstype);
5920 if (coder == ERROR_MARK)
5921 return error_mark_node;
5923 if (c_dialect_objc ())
5925 int parmno;
5927 switch (errtype)
5929 case ic_return:
5930 parmno = 0;
5931 break;
5933 case ic_assign:
5934 parmno = -1;
5935 break;
5937 case ic_init:
5938 parmno = -2;
5939 break;
5941 default:
5942 parmno = parmnum;
5943 break;
5946 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5949 if (warn_cxx_compat)
5951 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5952 if (checktype != error_mark_node
5953 && TREE_CODE (type) == ENUMERAL_TYPE
5954 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5956 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5957 G_("enum conversion when passing argument "
5958 "%d of %qE is invalid in C++"),
5959 G_("enum conversion in assignment is "
5960 "invalid in C++"),
5961 G_("enum conversion in initialization is "
5962 "invalid in C++"),
5963 G_("enum conversion in return is "
5964 "invalid in C++"));
5968 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5969 return rhs;
5971 if (coder == VOID_TYPE)
5973 /* Except for passing an argument to an unprototyped function,
5974 this is a constraint violation. When passing an argument to
5975 an unprototyped function, it is compile-time undefined;
5976 making it a constraint in that case was rejected in
5977 DR#252. */
5978 error_at (location, "void value not ignored as it ought to be");
5979 return error_mark_node;
5981 rhs = require_complete_type (rhs);
5982 if (rhs == error_mark_node)
5983 return error_mark_node;
5985 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
5986 return error_mark_node;
5988 /* A non-reference type can convert to a reference. This handles
5989 va_start, va_copy and possibly port built-ins. */
5990 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5992 if (!lvalue_p (rhs))
5994 error_at (location, "cannot pass rvalue to reference parameter");
5995 return error_mark_node;
5997 if (!c_mark_addressable (rhs))
5998 return error_mark_node;
5999 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6000 SET_EXPR_LOCATION (rhs, location);
6002 rhs = convert_for_assignment (location, expr_loc,
6003 build_pointer_type (TREE_TYPE (type)),
6004 rhs, origtype, errtype,
6005 null_pointer_constant, fundecl, function,
6006 parmnum);
6007 if (rhs == error_mark_node)
6008 return error_mark_node;
6010 rhs = build1 (NOP_EXPR, type, rhs);
6011 SET_EXPR_LOCATION (rhs, location);
6012 return rhs;
6014 /* Some types can interconvert without explicit casts. */
6015 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6016 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6017 return convert (type, rhs);
6018 /* Arithmetic types all interconvert, and enum is treated like int. */
6019 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6020 || codel == FIXED_POINT_TYPE
6021 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6022 || codel == BOOLEAN_TYPE)
6023 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6024 || coder == FIXED_POINT_TYPE
6025 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6026 || coder == BOOLEAN_TYPE))
6028 tree ret;
6029 bool save = in_late_binary_op;
6030 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6031 || (coder == REAL_TYPE
6032 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6033 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6034 in_late_binary_op = true;
6035 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6036 ? expr_loc : location, type, orig_rhs);
6037 in_late_binary_op = save;
6038 return ret;
6041 /* Aggregates in different TUs might need conversion. */
6042 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6043 && codel == coder
6044 && comptypes (type, rhstype))
6045 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6046 ? expr_loc : location, type, rhs);
6048 /* Conversion to a transparent union or record from its member types.
6049 This applies only to function arguments. */
6050 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6051 && TYPE_TRANSPARENT_AGGR (type))
6052 && errtype == ic_argpass)
6054 tree memb, marginal_memb = NULL_TREE;
6056 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6058 tree memb_type = TREE_TYPE (memb);
6060 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6061 TYPE_MAIN_VARIANT (rhstype)))
6062 break;
6064 if (TREE_CODE (memb_type) != POINTER_TYPE)
6065 continue;
6067 if (coder == POINTER_TYPE)
6069 tree ttl = TREE_TYPE (memb_type);
6070 tree ttr = TREE_TYPE (rhstype);
6072 /* Any non-function converts to a [const][volatile] void *
6073 and vice versa; otherwise, targets must be the same.
6074 Meanwhile, the lhs target must have all the qualifiers of
6075 the rhs. */
6076 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6077 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6078 || comp_target_types (location, memb_type, rhstype))
6080 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6081 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6082 /* If this type won't generate any warnings, use it. */
6083 if (lquals == rquals
6084 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6085 && TREE_CODE (ttl) == FUNCTION_TYPE)
6086 ? ((lquals | rquals) == rquals)
6087 : ((lquals | rquals) == lquals)))
6088 break;
6090 /* Keep looking for a better type, but remember this one. */
6091 if (!marginal_memb)
6092 marginal_memb = memb;
6096 /* Can convert integer zero to any pointer type. */
6097 if (null_pointer_constant)
6099 rhs = null_pointer_node;
6100 break;
6104 if (memb || marginal_memb)
6106 if (!memb)
6108 /* We have only a marginally acceptable member type;
6109 it needs a warning. */
6110 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6111 tree ttr = TREE_TYPE (rhstype);
6113 /* Const and volatile mean something different for function
6114 types, so the usual warnings are not appropriate. */
6115 if (TREE_CODE (ttr) == FUNCTION_TYPE
6116 && TREE_CODE (ttl) == FUNCTION_TYPE)
6118 /* Because const and volatile on functions are
6119 restrictions that say the function will not do
6120 certain things, it is okay to use a const or volatile
6121 function where an ordinary one is wanted, but not
6122 vice-versa. */
6123 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6124 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6125 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6126 OPT_Wdiscarded_qualifiers,
6127 G_("passing argument %d of %qE "
6128 "makes %q#v qualified function "
6129 "pointer from unqualified"),
6130 G_("assignment makes %q#v qualified "
6131 "function pointer from "
6132 "unqualified"),
6133 G_("initialization makes %q#v qualified "
6134 "function pointer from "
6135 "unqualified"),
6136 G_("return makes %q#v qualified function "
6137 "pointer from unqualified"),
6138 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6140 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6141 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6142 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6143 OPT_Wdiscarded_qualifiers,
6144 G_("passing argument %d of %qE discards "
6145 "%qv qualifier from pointer target type"),
6146 G_("assignment discards %qv qualifier "
6147 "from pointer target type"),
6148 G_("initialization discards %qv qualifier "
6149 "from pointer target type"),
6150 G_("return discards %qv qualifier from "
6151 "pointer target type"),
6152 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6154 memb = marginal_memb;
6157 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6158 pedwarn (location, OPT_Wpedantic,
6159 "ISO C prohibits argument conversion to union type");
6161 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6162 return build_constructor_single (type, memb, rhs);
6166 /* Conversions among pointers */
6167 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6168 && (coder == codel))
6170 tree ttl = TREE_TYPE (type);
6171 tree ttr = TREE_TYPE (rhstype);
6172 tree mvl = ttl;
6173 tree mvr = ttr;
6174 bool is_opaque_pointer;
6175 int target_cmp = 0; /* Cache comp_target_types () result. */
6176 addr_space_t asl;
6177 addr_space_t asr;
6179 if (TREE_CODE (mvl) != ARRAY_TYPE)
6180 mvl = (TYPE_ATOMIC (mvl)
6181 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6182 TYPE_QUAL_ATOMIC)
6183 : TYPE_MAIN_VARIANT (mvl));
6184 if (TREE_CODE (mvr) != ARRAY_TYPE)
6185 mvr = (TYPE_ATOMIC (mvr)
6186 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6187 TYPE_QUAL_ATOMIC)
6188 : TYPE_MAIN_VARIANT (mvr));
6189 /* Opaque pointers are treated like void pointers. */
6190 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6192 /* The Plan 9 compiler permits a pointer to a struct to be
6193 automatically converted into a pointer to an anonymous field
6194 within the struct. */
6195 if (flag_plan9_extensions
6196 && RECORD_OR_UNION_TYPE_P (mvl)
6197 && RECORD_OR_UNION_TYPE_P (mvr)
6198 && mvl != mvr)
6200 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6201 if (new_rhs != NULL_TREE)
6203 rhs = new_rhs;
6204 rhstype = TREE_TYPE (rhs);
6205 coder = TREE_CODE (rhstype);
6206 ttr = TREE_TYPE (rhstype);
6207 mvr = TYPE_MAIN_VARIANT (ttr);
6211 /* C++ does not allow the implicit conversion void* -> T*. However,
6212 for the purpose of reducing the number of false positives, we
6213 tolerate the special case of
6215 int *p = NULL;
6217 where NULL is typically defined in C to be '(void *) 0'. */
6218 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6219 warning_at (errtype == ic_argpass ? expr_loc : location,
6220 OPT_Wc___compat,
6221 "request for implicit conversion "
6222 "from %qT to %qT not permitted in C++", rhstype, type);
6224 /* See if the pointers point to incompatible address spaces. */
6225 asl = TYPE_ADDR_SPACE (ttl);
6226 asr = TYPE_ADDR_SPACE (ttr);
6227 if (!null_pointer_constant_p (rhs)
6228 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6230 switch (errtype)
6232 case ic_argpass:
6233 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6234 "non-enclosed address space", parmnum, rname);
6235 break;
6236 case ic_assign:
6237 error_at (location, "assignment from pointer to "
6238 "non-enclosed address space");
6239 break;
6240 case ic_init:
6241 error_at (location, "initialization from pointer to "
6242 "non-enclosed address space");
6243 break;
6244 case ic_return:
6245 error_at (location, "return from pointer to "
6246 "non-enclosed address space");
6247 break;
6248 default:
6249 gcc_unreachable ();
6251 return error_mark_node;
6254 /* Check if the right-hand side has a format attribute but the
6255 left-hand side doesn't. */
6256 if (warn_suggest_attribute_format
6257 && check_missing_format_attribute (type, rhstype))
6259 switch (errtype)
6261 case ic_argpass:
6262 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6263 "argument %d of %qE might be "
6264 "a candidate for a format attribute",
6265 parmnum, rname);
6266 break;
6267 case ic_assign:
6268 warning_at (location, OPT_Wsuggest_attribute_format,
6269 "assignment left-hand side might be "
6270 "a candidate for a format attribute");
6271 break;
6272 case ic_init:
6273 warning_at (location, OPT_Wsuggest_attribute_format,
6274 "initialization left-hand side might be "
6275 "a candidate for a format attribute");
6276 break;
6277 case ic_return:
6278 warning_at (location, OPT_Wsuggest_attribute_format,
6279 "return type might be "
6280 "a candidate for a format attribute");
6281 break;
6282 default:
6283 gcc_unreachable ();
6287 /* Any non-function converts to a [const][volatile] void *
6288 and vice versa; otherwise, targets must be the same.
6289 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6290 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6291 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6292 || (target_cmp = comp_target_types (location, type, rhstype))
6293 || is_opaque_pointer
6294 || ((c_common_unsigned_type (mvl)
6295 == c_common_unsigned_type (mvr))
6296 && (c_common_signed_type (mvl)
6297 == c_common_signed_type (mvr))
6298 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6300 /* Warn about loss of qualifers from pointers to arrays with
6301 qualifiers on the element type. */
6302 if (TREE_CODE (ttr) == ARRAY_TYPE)
6304 ttr = strip_array_types (ttr);
6305 ttl = strip_array_types (ttl);
6307 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6308 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6309 WARNING_FOR_QUALIFIERS (location, expr_loc,
6310 OPT_Wdiscarded_array_qualifiers,
6311 G_("passing argument %d of %qE discards "
6312 "%qv qualifier from pointer target type"),
6313 G_("assignment discards %qv qualifier "
6314 "from pointer target type"),
6315 G_("initialization discards %qv qualifier "
6316 "from pointer target type"),
6317 G_("return discards %qv qualifier from "
6318 "pointer target type"),
6319 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6321 else if (pedantic
6322 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6324 (VOID_TYPE_P (ttr)
6325 && !null_pointer_constant
6326 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6327 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6328 G_("ISO C forbids passing argument %d of "
6329 "%qE between function pointer "
6330 "and %<void *%>"),
6331 G_("ISO C forbids assignment between "
6332 "function pointer and %<void *%>"),
6333 G_("ISO C forbids initialization between "
6334 "function pointer and %<void *%>"),
6335 G_("ISO C forbids return between function "
6336 "pointer and %<void *%>"));
6337 /* Const and volatile mean something different for function types,
6338 so the usual warnings are not appropriate. */
6339 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6340 && TREE_CODE (ttl) != FUNCTION_TYPE)
6342 /* Don't warn about loss of qualifier for conversions from
6343 qualified void* to pointers to arrays with corresponding
6344 qualifier on the element type. */
6345 if (!pedantic)
6346 ttl = strip_array_types (ttl);
6348 /* Assignments between atomic and non-atomic objects are OK. */
6349 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6350 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6352 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6353 OPT_Wdiscarded_qualifiers,
6354 G_("passing argument %d of %qE discards "
6355 "%qv qualifier from pointer target type"),
6356 G_("assignment discards %qv qualifier "
6357 "from pointer target type"),
6358 G_("initialization discards %qv qualifier "
6359 "from pointer target type"),
6360 G_("return discards %qv qualifier from "
6361 "pointer target type"),
6362 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6364 /* If this is not a case of ignoring a mismatch in signedness,
6365 no warning. */
6366 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6367 || target_cmp)
6369 /* If there is a mismatch, do warn. */
6370 else if (warn_pointer_sign)
6371 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6372 G_("pointer targets in passing argument "
6373 "%d of %qE differ in signedness"),
6374 G_("pointer targets in assignment "
6375 "differ in signedness"),
6376 G_("pointer targets in initialization "
6377 "differ in signedness"),
6378 G_("pointer targets in return differ "
6379 "in signedness"));
6381 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6382 && TREE_CODE (ttr) == FUNCTION_TYPE)
6384 /* Because const and volatile on functions are restrictions
6385 that say the function will not do certain things,
6386 it is okay to use a const or volatile function
6387 where an ordinary one is wanted, but not vice-versa. */
6388 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6389 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6390 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6391 OPT_Wdiscarded_qualifiers,
6392 G_("passing argument %d of %qE makes "
6393 "%q#v qualified function pointer "
6394 "from unqualified"),
6395 G_("assignment makes %q#v qualified function "
6396 "pointer from unqualified"),
6397 G_("initialization makes %q#v qualified "
6398 "function pointer from unqualified"),
6399 G_("return makes %q#v qualified function "
6400 "pointer from unqualified"),
6401 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6404 else
6405 /* Avoid warning about the volatile ObjC EH puts on decls. */
6406 if (!objc_ok)
6407 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6408 OPT_Wincompatible_pointer_types,
6409 G_("passing argument %d of %qE from "
6410 "incompatible pointer type"),
6411 G_("assignment from incompatible pointer type"),
6412 G_("initialization from incompatible "
6413 "pointer type"),
6414 G_("return from incompatible pointer type"));
6416 return convert (type, rhs);
6418 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6420 /* ??? This should not be an error when inlining calls to
6421 unprototyped functions. */
6422 error_at (location, "invalid use of non-lvalue array");
6423 return error_mark_node;
6425 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6427 /* An explicit constant 0 can convert to a pointer,
6428 or one that results from arithmetic, even including
6429 a cast to integer type. */
6430 if (!null_pointer_constant)
6431 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6432 OPT_Wint_conversion,
6433 G_("passing argument %d of %qE makes "
6434 "pointer from integer without a cast"),
6435 G_("assignment makes pointer from integer "
6436 "without a cast"),
6437 G_("initialization makes pointer from "
6438 "integer without a cast"),
6439 G_("return makes pointer from integer "
6440 "without a cast"));
6442 return convert (type, rhs);
6444 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6446 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6447 OPT_Wint_conversion,
6448 G_("passing argument %d of %qE makes integer "
6449 "from pointer without a cast"),
6450 G_("assignment makes integer from pointer "
6451 "without a cast"),
6452 G_("initialization makes integer from pointer "
6453 "without a cast"),
6454 G_("return makes integer from pointer "
6455 "without a cast"));
6456 return convert (type, rhs);
6458 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6460 tree ret;
6461 bool save = in_late_binary_op;
6462 in_late_binary_op = true;
6463 ret = convert (type, rhs);
6464 in_late_binary_op = save;
6465 return ret;
6468 switch (errtype)
6470 case ic_argpass:
6471 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6472 rname);
6473 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6474 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6475 "expected %qT but argument is of type %qT", type, rhstype);
6476 break;
6477 case ic_assign:
6478 error_at (location, "incompatible types when assigning to type %qT from "
6479 "type %qT", type, rhstype);
6480 break;
6481 case ic_init:
6482 error_at (location,
6483 "incompatible types when initializing type %qT using type %qT",
6484 type, rhstype);
6485 break;
6486 case ic_return:
6487 error_at (location,
6488 "incompatible types when returning type %qT but %qT was "
6489 "expected", rhstype, type);
6490 break;
6491 default:
6492 gcc_unreachable ();
6495 return error_mark_node;
6498 /* If VALUE is a compound expr all of whose expressions are constant, then
6499 return its value. Otherwise, return error_mark_node.
6501 This is for handling COMPOUND_EXPRs as initializer elements
6502 which is allowed with a warning when -pedantic is specified. */
6504 static tree
6505 valid_compound_expr_initializer (tree value, tree endtype)
6507 if (TREE_CODE (value) == COMPOUND_EXPR)
6509 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6510 == error_mark_node)
6511 return error_mark_node;
6512 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6513 endtype);
6515 else if (!initializer_constant_valid_p (value, endtype))
6516 return error_mark_node;
6517 else
6518 return value;
6521 /* Perform appropriate conversions on the initial value of a variable,
6522 store it in the declaration DECL,
6523 and print any error messages that are appropriate.
6524 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6525 If the init is invalid, store an ERROR_MARK.
6527 INIT_LOC is the location of the initial value. */
6529 void
6530 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6532 tree value, type;
6533 bool npc = false;
6535 /* If variable's type was invalidly declared, just ignore it. */
6537 type = TREE_TYPE (decl);
6538 if (TREE_CODE (type) == ERROR_MARK)
6539 return;
6541 /* Digest the specified initializer into an expression. */
6543 if (init)
6544 npc = null_pointer_constant_p (init);
6545 value = digest_init (init_loc, type, init, origtype, npc,
6546 true, TREE_STATIC (decl));
6548 /* Store the expression if valid; else report error. */
6550 if (!in_system_header_at (input_location)
6551 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6552 warning (OPT_Wtraditional, "traditional C rejects automatic "
6553 "aggregate initialization");
6555 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6556 DECL_INITIAL (decl) = value;
6558 /* ANSI wants warnings about out-of-range constant initializers. */
6559 STRIP_TYPE_NOPS (value);
6560 if (TREE_STATIC (decl))
6561 constant_expression_warning (value);
6563 /* Check if we need to set array size from compound literal size. */
6564 if (TREE_CODE (type) == ARRAY_TYPE
6565 && TYPE_DOMAIN (type) == 0
6566 && value != error_mark_node)
6568 tree inside_init = init;
6570 STRIP_TYPE_NOPS (inside_init);
6571 inside_init = fold (inside_init);
6573 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6575 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6577 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6579 /* For int foo[] = (int [3]){1}; we need to set array size
6580 now since later on array initializer will be just the
6581 brace enclosed list of the compound literal. */
6582 tree etype = strip_array_types (TREE_TYPE (decl));
6583 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6584 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6585 layout_type (type);
6586 layout_decl (cldecl, 0);
6587 TREE_TYPE (decl)
6588 = c_build_qualified_type (type, TYPE_QUALS (etype));
6594 /* Methods for storing and printing names for error messages. */
6596 /* Implement a spelling stack that allows components of a name to be pushed
6597 and popped. Each element on the stack is this structure. */
6599 struct spelling
6601 int kind;
6602 union
6604 unsigned HOST_WIDE_INT i;
6605 const char *s;
6606 } u;
6609 #define SPELLING_STRING 1
6610 #define SPELLING_MEMBER 2
6611 #define SPELLING_BOUNDS 3
6613 static struct spelling *spelling; /* Next stack element (unused). */
6614 static struct spelling *spelling_base; /* Spelling stack base. */
6615 static int spelling_size; /* Size of the spelling stack. */
6617 /* Macros to save and restore the spelling stack around push_... functions.
6618 Alternative to SAVE_SPELLING_STACK. */
6620 #define SPELLING_DEPTH() (spelling - spelling_base)
6621 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6623 /* Push an element on the spelling stack with type KIND and assign VALUE
6624 to MEMBER. */
6626 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6628 int depth = SPELLING_DEPTH (); \
6630 if (depth >= spelling_size) \
6632 spelling_size += 10; \
6633 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6634 spelling_size); \
6635 RESTORE_SPELLING_DEPTH (depth); \
6638 spelling->kind = (KIND); \
6639 spelling->MEMBER = (VALUE); \
6640 spelling++; \
6643 /* Push STRING on the stack. Printed literally. */
6645 static void
6646 push_string (const char *string)
6648 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6651 /* Push a member name on the stack. Printed as '.' STRING. */
6653 static void
6654 push_member_name (tree decl)
6656 const char *const string
6657 = (DECL_NAME (decl)
6658 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6659 : _("<anonymous>"));
6660 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6663 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6665 static void
6666 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6668 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6671 /* Compute the maximum size in bytes of the printed spelling. */
6673 static int
6674 spelling_length (void)
6676 int size = 0;
6677 struct spelling *p;
6679 for (p = spelling_base; p < spelling; p++)
6681 if (p->kind == SPELLING_BOUNDS)
6682 size += 25;
6683 else
6684 size += strlen (p->u.s) + 1;
6687 return size;
6690 /* Print the spelling to BUFFER and return it. */
6692 static char *
6693 print_spelling (char *buffer)
6695 char *d = buffer;
6696 struct spelling *p;
6698 for (p = spelling_base; p < spelling; p++)
6699 if (p->kind == SPELLING_BOUNDS)
6701 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6702 d += strlen (d);
6704 else
6706 const char *s;
6707 if (p->kind == SPELLING_MEMBER)
6708 *d++ = '.';
6709 for (s = p->u.s; (*d = *s++); d++)
6712 *d++ = '\0';
6713 return buffer;
6716 /* Digest the parser output INIT as an initializer for type TYPE.
6717 Return a C expression of type TYPE to represent the initial value.
6719 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6721 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6723 If INIT is a string constant, STRICT_STRING is true if it is
6724 unparenthesized or we should not warn here for it being parenthesized.
6725 For other types of INIT, STRICT_STRING is not used.
6727 INIT_LOC is the location of the INIT.
6729 REQUIRE_CONSTANT requests an error if non-constant initializers or
6730 elements are seen. */
6732 static tree
6733 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6734 bool null_pointer_constant, bool strict_string,
6735 int require_constant)
6737 enum tree_code code = TREE_CODE (type);
6738 tree inside_init = init;
6739 tree semantic_type = NULL_TREE;
6740 bool maybe_const = true;
6742 if (type == error_mark_node
6743 || !init
6744 || error_operand_p (init))
6745 return error_mark_node;
6747 STRIP_TYPE_NOPS (inside_init);
6749 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6751 semantic_type = TREE_TYPE (inside_init);
6752 inside_init = TREE_OPERAND (inside_init, 0);
6754 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6755 inside_init = decl_constant_value_for_optimization (inside_init);
6757 /* Initialization of an array of chars from a string constant
6758 optionally enclosed in braces. */
6760 if (code == ARRAY_TYPE && inside_init
6761 && TREE_CODE (inside_init) == STRING_CST)
6763 tree typ1
6764 = (TYPE_ATOMIC (TREE_TYPE (type))
6765 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6766 TYPE_QUAL_ATOMIC)
6767 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6768 /* Note that an array could be both an array of character type
6769 and an array of wchar_t if wchar_t is signed char or unsigned
6770 char. */
6771 bool char_array = (typ1 == char_type_node
6772 || typ1 == signed_char_type_node
6773 || typ1 == unsigned_char_type_node);
6774 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6775 bool char16_array = !!comptypes (typ1, char16_type_node);
6776 bool char32_array = !!comptypes (typ1, char32_type_node);
6778 if (char_array || wchar_array || char16_array || char32_array)
6780 struct c_expr expr;
6781 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6782 expr.value = inside_init;
6783 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6784 expr.original_type = NULL;
6785 maybe_warn_string_init (init_loc, type, expr);
6787 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6788 pedwarn_init (init_loc, OPT_Wpedantic,
6789 "initialization of a flexible array member");
6791 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6792 TYPE_MAIN_VARIANT (type)))
6793 return inside_init;
6795 if (char_array)
6797 if (typ2 != char_type_node)
6799 error_init (init_loc, "char-array initialized from wide "
6800 "string");
6801 return error_mark_node;
6804 else
6806 if (typ2 == char_type_node)
6808 error_init (init_loc, "wide character array initialized "
6809 "from non-wide string");
6810 return error_mark_node;
6812 else if (!comptypes(typ1, typ2))
6814 error_init (init_loc, "wide character array initialized "
6815 "from incompatible wide string");
6816 return error_mark_node;
6820 TREE_TYPE (inside_init) = type;
6821 if (TYPE_DOMAIN (type) != 0
6822 && TYPE_SIZE (type) != 0
6823 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6825 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6827 /* Subtract the size of a single (possibly wide) character
6828 because it's ok to ignore the terminating null char
6829 that is counted in the length of the constant. */
6830 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6831 (len
6832 - (TYPE_PRECISION (typ1)
6833 / BITS_PER_UNIT))))
6834 pedwarn_init (init_loc, 0,
6835 ("initializer-string for array of chars "
6836 "is too long"));
6837 else if (warn_cxx_compat
6838 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6839 warning_at (init_loc, OPT_Wc___compat,
6840 ("initializer-string for array chars "
6841 "is too long for C++"));
6844 return inside_init;
6846 else if (INTEGRAL_TYPE_P (typ1))
6848 error_init (init_loc, "array of inappropriate type initialized "
6849 "from string constant");
6850 return error_mark_node;
6854 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6855 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6856 below and handle as a constructor. */
6857 if (code == VECTOR_TYPE
6858 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
6859 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6860 && TREE_CONSTANT (inside_init))
6862 if (TREE_CODE (inside_init) == VECTOR_CST
6863 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6864 TYPE_MAIN_VARIANT (type)))
6865 return inside_init;
6867 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6869 unsigned HOST_WIDE_INT ix;
6870 tree value;
6871 bool constant_p = true;
6873 /* Iterate through elements and check if all constructor
6874 elements are *_CSTs. */
6875 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6876 if (!CONSTANT_CLASS_P (value))
6878 constant_p = false;
6879 break;
6882 if (constant_p)
6883 return build_vector_from_ctor (type,
6884 CONSTRUCTOR_ELTS (inside_init));
6888 if (warn_sequence_point)
6889 verify_sequence_points (inside_init);
6891 /* Any type can be initialized
6892 from an expression of the same type, optionally with braces. */
6894 if (inside_init && TREE_TYPE (inside_init) != 0
6895 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6896 TYPE_MAIN_VARIANT (type))
6897 || (code == ARRAY_TYPE
6898 && comptypes (TREE_TYPE (inside_init), type))
6899 || (code == VECTOR_TYPE
6900 && comptypes (TREE_TYPE (inside_init), type))
6901 || (code == POINTER_TYPE
6902 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6903 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6904 TREE_TYPE (type)))))
6906 if (code == POINTER_TYPE)
6908 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6910 if (TREE_CODE (inside_init) == STRING_CST
6911 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6912 inside_init = array_to_pointer_conversion
6913 (init_loc, inside_init);
6914 else
6916 error_init (init_loc, "invalid use of non-lvalue array");
6917 return error_mark_node;
6922 if (code == VECTOR_TYPE)
6923 /* Although the types are compatible, we may require a
6924 conversion. */
6925 inside_init = convert (type, inside_init);
6927 if (require_constant
6928 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6930 /* As an extension, allow initializing objects with static storage
6931 duration with compound literals (which are then treated just as
6932 the brace enclosed list they contain). Also allow this for
6933 vectors, as we can only assign them with compound literals. */
6934 if (flag_isoc99 && code != VECTOR_TYPE)
6935 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6936 "is not constant");
6937 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6938 inside_init = DECL_INITIAL (decl);
6941 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6942 && TREE_CODE (inside_init) != CONSTRUCTOR)
6944 error_init (init_loc, "array initialized from non-constant array "
6945 "expression");
6946 return error_mark_node;
6949 /* Compound expressions can only occur here if -Wpedantic or
6950 -pedantic-errors is specified. In the later case, we always want
6951 an error. In the former case, we simply want a warning. */
6952 if (require_constant && pedantic
6953 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6955 inside_init
6956 = valid_compound_expr_initializer (inside_init,
6957 TREE_TYPE (inside_init));
6958 if (inside_init == error_mark_node)
6959 error_init (init_loc, "initializer element is not constant");
6960 else
6961 pedwarn_init (init_loc, OPT_Wpedantic,
6962 "initializer element is not constant");
6963 if (flag_pedantic_errors)
6964 inside_init = error_mark_node;
6966 else if (require_constant
6967 && !initializer_constant_valid_p (inside_init,
6968 TREE_TYPE (inside_init)))
6970 error_init (init_loc, "initializer element is not constant");
6971 inside_init = error_mark_node;
6973 else if (require_constant && !maybe_const)
6974 pedwarn_init (init_loc, OPT_Wpedantic,
6975 "initializer element is not a constant expression");
6977 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6978 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6979 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6980 type, inside_init, origtype,
6981 ic_init, null_pointer_constant,
6982 NULL_TREE, NULL_TREE, 0);
6983 return inside_init;
6986 /* Handle scalar types, including conversions. */
6988 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6989 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6990 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6992 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6993 && (TREE_CODE (init) == STRING_CST
6994 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6995 inside_init = init = array_to_pointer_conversion (init_loc, init);
6996 if (semantic_type)
6997 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6998 inside_init);
6999 inside_init
7000 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7001 inside_init, origtype, ic_init,
7002 null_pointer_constant, NULL_TREE, NULL_TREE,
7005 /* Check to see if we have already given an error message. */
7006 if (inside_init == error_mark_node)
7008 else if (require_constant && !TREE_CONSTANT (inside_init))
7010 error_init (init_loc, "initializer element is not constant");
7011 inside_init = error_mark_node;
7013 else if (require_constant
7014 && !initializer_constant_valid_p (inside_init,
7015 TREE_TYPE (inside_init)))
7017 error_init (init_loc, "initializer element is not computable at "
7018 "load time");
7019 inside_init = error_mark_node;
7021 else if (require_constant && !maybe_const)
7022 pedwarn_init (init_loc, OPT_Wpedantic,
7023 "initializer element is not a constant expression");
7025 return inside_init;
7028 /* Come here only for records and arrays. */
7030 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7032 error_init (init_loc, "variable-sized object may not be initialized");
7033 return error_mark_node;
7036 error_init (init_loc, "invalid initializer");
7037 return error_mark_node;
7040 /* Handle initializers that use braces. */
7042 /* Type of object we are accumulating a constructor for.
7043 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7044 static tree constructor_type;
7046 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7047 left to fill. */
7048 static tree constructor_fields;
7050 /* For an ARRAY_TYPE, this is the specified index
7051 at which to store the next element we get. */
7052 static tree constructor_index;
7054 /* For an ARRAY_TYPE, this is the maximum index. */
7055 static tree constructor_max_index;
7057 /* For a RECORD_TYPE, this is the first field not yet written out. */
7058 static tree constructor_unfilled_fields;
7060 /* For an ARRAY_TYPE, this is the index of the first element
7061 not yet written out. */
7062 static tree constructor_unfilled_index;
7064 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7065 This is so we can generate gaps between fields, when appropriate. */
7066 static tree constructor_bit_index;
7068 /* If we are saving up the elements rather than allocating them,
7069 this is the list of elements so far (in reverse order,
7070 most recent first). */
7071 static vec<constructor_elt, va_gc> *constructor_elements;
7073 /* 1 if constructor should be incrementally stored into a constructor chain,
7074 0 if all the elements should be kept in AVL tree. */
7075 static int constructor_incremental;
7077 /* 1 if so far this constructor's elements are all compile-time constants. */
7078 static int constructor_constant;
7080 /* 1 if so far this constructor's elements are all valid address constants. */
7081 static int constructor_simple;
7083 /* 1 if this constructor has an element that cannot be part of a
7084 constant expression. */
7085 static int constructor_nonconst;
7087 /* 1 if this constructor is erroneous so far. */
7088 static int constructor_erroneous;
7090 /* 1 if this constructor is the universal zero initializer { 0 }. */
7091 static int constructor_zeroinit;
7093 /* Structure for managing pending initializer elements, organized as an
7094 AVL tree. */
7096 struct init_node
7098 struct init_node *left, *right;
7099 struct init_node *parent;
7100 int balance;
7101 tree purpose;
7102 tree value;
7103 tree origtype;
7106 /* Tree of pending elements at this constructor level.
7107 These are elements encountered out of order
7108 which belong at places we haven't reached yet in actually
7109 writing the output.
7110 Will never hold tree nodes across GC runs. */
7111 static struct init_node *constructor_pending_elts;
7113 /* The SPELLING_DEPTH of this constructor. */
7114 static int constructor_depth;
7116 /* DECL node for which an initializer is being read.
7117 0 means we are reading a constructor expression
7118 such as (struct foo) {...}. */
7119 static tree constructor_decl;
7121 /* Nonzero if this is an initializer for a top-level decl. */
7122 static int constructor_top_level;
7124 /* Nonzero if there were any member designators in this initializer. */
7125 static int constructor_designated;
7127 /* Nesting depth of designator list. */
7128 static int designator_depth;
7130 /* Nonzero if there were diagnosed errors in this designator list. */
7131 static int designator_erroneous;
7134 /* This stack has a level for each implicit or explicit level of
7135 structuring in the initializer, including the outermost one. It
7136 saves the values of most of the variables above. */
7138 struct constructor_range_stack;
7140 struct constructor_stack
7142 struct constructor_stack *next;
7143 tree type;
7144 tree fields;
7145 tree index;
7146 tree max_index;
7147 tree unfilled_index;
7148 tree unfilled_fields;
7149 tree bit_index;
7150 vec<constructor_elt, va_gc> *elements;
7151 struct init_node *pending_elts;
7152 int offset;
7153 int depth;
7154 /* If value nonzero, this value should replace the entire
7155 constructor at this level. */
7156 struct c_expr replacement_value;
7157 struct constructor_range_stack *range_stack;
7158 char constant;
7159 char simple;
7160 char nonconst;
7161 char implicit;
7162 char erroneous;
7163 char outer;
7164 char incremental;
7165 char designated;
7166 int designator_depth;
7169 static struct constructor_stack *constructor_stack;
7171 /* This stack represents designators from some range designator up to
7172 the last designator in the list. */
7174 struct constructor_range_stack
7176 struct constructor_range_stack *next, *prev;
7177 struct constructor_stack *stack;
7178 tree range_start;
7179 tree index;
7180 tree range_end;
7181 tree fields;
7184 static struct constructor_range_stack *constructor_range_stack;
7186 /* This stack records separate initializers that are nested.
7187 Nested initializers can't happen in ANSI C, but GNU C allows them
7188 in cases like { ... (struct foo) { ... } ... }. */
7190 struct initializer_stack
7192 struct initializer_stack *next;
7193 tree decl;
7194 struct constructor_stack *constructor_stack;
7195 struct constructor_range_stack *constructor_range_stack;
7196 vec<constructor_elt, va_gc> *elements;
7197 struct spelling *spelling;
7198 struct spelling *spelling_base;
7199 int spelling_size;
7200 char top_level;
7201 char require_constant_value;
7202 char require_constant_elements;
7205 static struct initializer_stack *initializer_stack;
7207 /* Prepare to parse and output the initializer for variable DECL. */
7209 void
7210 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7212 const char *locus;
7213 struct initializer_stack *p = XNEW (struct initializer_stack);
7215 p->decl = constructor_decl;
7216 p->require_constant_value = require_constant_value;
7217 p->require_constant_elements = require_constant_elements;
7218 p->constructor_stack = constructor_stack;
7219 p->constructor_range_stack = constructor_range_stack;
7220 p->elements = constructor_elements;
7221 p->spelling = spelling;
7222 p->spelling_base = spelling_base;
7223 p->spelling_size = spelling_size;
7224 p->top_level = constructor_top_level;
7225 p->next = initializer_stack;
7226 initializer_stack = p;
7228 constructor_decl = decl;
7229 constructor_designated = 0;
7230 constructor_top_level = top_level;
7232 if (decl != 0 && decl != error_mark_node)
7234 require_constant_value = TREE_STATIC (decl);
7235 require_constant_elements
7236 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7237 /* For a scalar, you can always use any value to initialize,
7238 even within braces. */
7239 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7240 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7242 else
7244 require_constant_value = 0;
7245 require_constant_elements = 0;
7246 locus = _("(anonymous)");
7249 constructor_stack = 0;
7250 constructor_range_stack = 0;
7252 found_missing_braces = 0;
7254 spelling_base = 0;
7255 spelling_size = 0;
7256 RESTORE_SPELLING_DEPTH (0);
7258 if (locus)
7259 push_string (locus);
7262 void
7263 finish_init (void)
7265 struct initializer_stack *p = initializer_stack;
7267 /* Free the whole constructor stack of this initializer. */
7268 while (constructor_stack)
7270 struct constructor_stack *q = constructor_stack;
7271 constructor_stack = q->next;
7272 free (q);
7275 gcc_assert (!constructor_range_stack);
7277 /* Pop back to the data of the outer initializer (if any). */
7278 free (spelling_base);
7280 constructor_decl = p->decl;
7281 require_constant_value = p->require_constant_value;
7282 require_constant_elements = p->require_constant_elements;
7283 constructor_stack = p->constructor_stack;
7284 constructor_range_stack = p->constructor_range_stack;
7285 constructor_elements = p->elements;
7286 spelling = p->spelling;
7287 spelling_base = p->spelling_base;
7288 spelling_size = p->spelling_size;
7289 constructor_top_level = p->top_level;
7290 initializer_stack = p->next;
7291 free (p);
7294 /* Call here when we see the initializer is surrounded by braces.
7295 This is instead of a call to push_init_level;
7296 it is matched by a call to pop_init_level.
7298 TYPE is the type to initialize, for a constructor expression.
7299 For an initializer for a decl, TYPE is zero. */
7301 void
7302 really_start_incremental_init (tree type)
7304 struct constructor_stack *p = XNEW (struct constructor_stack);
7306 if (type == 0)
7307 type = TREE_TYPE (constructor_decl);
7309 if (VECTOR_TYPE_P (type)
7310 && TYPE_VECTOR_OPAQUE (type))
7311 error ("opaque vector types cannot be initialized");
7313 p->type = constructor_type;
7314 p->fields = constructor_fields;
7315 p->index = constructor_index;
7316 p->max_index = constructor_max_index;
7317 p->unfilled_index = constructor_unfilled_index;
7318 p->unfilled_fields = constructor_unfilled_fields;
7319 p->bit_index = constructor_bit_index;
7320 p->elements = constructor_elements;
7321 p->constant = constructor_constant;
7322 p->simple = constructor_simple;
7323 p->nonconst = constructor_nonconst;
7324 p->erroneous = constructor_erroneous;
7325 p->pending_elts = constructor_pending_elts;
7326 p->depth = constructor_depth;
7327 p->replacement_value.value = 0;
7328 p->replacement_value.original_code = ERROR_MARK;
7329 p->replacement_value.original_type = NULL;
7330 p->implicit = 0;
7331 p->range_stack = 0;
7332 p->outer = 0;
7333 p->incremental = constructor_incremental;
7334 p->designated = constructor_designated;
7335 p->designator_depth = designator_depth;
7336 p->next = 0;
7337 constructor_stack = p;
7339 constructor_constant = 1;
7340 constructor_simple = 1;
7341 constructor_nonconst = 0;
7342 constructor_depth = SPELLING_DEPTH ();
7343 constructor_elements = NULL;
7344 constructor_pending_elts = 0;
7345 constructor_type = type;
7346 constructor_incremental = 1;
7347 constructor_designated = 0;
7348 constructor_zeroinit = 1;
7349 designator_depth = 0;
7350 designator_erroneous = 0;
7352 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7354 constructor_fields = TYPE_FIELDS (constructor_type);
7355 /* Skip any nameless bit fields at the beginning. */
7356 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7357 && DECL_NAME (constructor_fields) == 0)
7358 constructor_fields = DECL_CHAIN (constructor_fields);
7360 constructor_unfilled_fields = constructor_fields;
7361 constructor_bit_index = bitsize_zero_node;
7363 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7365 if (TYPE_DOMAIN (constructor_type))
7367 constructor_max_index
7368 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7370 /* Detect non-empty initializations of zero-length arrays. */
7371 if (constructor_max_index == NULL_TREE
7372 && TYPE_SIZE (constructor_type))
7373 constructor_max_index = integer_minus_one_node;
7375 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7376 to initialize VLAs will cause a proper error; avoid tree
7377 checking errors as well by setting a safe value. */
7378 if (constructor_max_index
7379 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7380 constructor_max_index = integer_minus_one_node;
7382 constructor_index
7383 = convert (bitsizetype,
7384 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7386 else
7388 constructor_index = bitsize_zero_node;
7389 constructor_max_index = NULL_TREE;
7392 constructor_unfilled_index = constructor_index;
7394 else if (VECTOR_TYPE_P (constructor_type))
7396 /* Vectors are like simple fixed-size arrays. */
7397 constructor_max_index =
7398 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7399 constructor_index = bitsize_zero_node;
7400 constructor_unfilled_index = constructor_index;
7402 else
7404 /* Handle the case of int x = {5}; */
7405 constructor_fields = constructor_type;
7406 constructor_unfilled_fields = constructor_type;
7410 /* Push down into a subobject, for initialization.
7411 If this is for an explicit set of braces, IMPLICIT is 0.
7412 If it is because the next element belongs at a lower level,
7413 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7415 void
7416 push_init_level (location_t loc, int implicit,
7417 struct obstack *braced_init_obstack)
7419 struct constructor_stack *p;
7420 tree value = NULL_TREE;
7422 /* If we've exhausted any levels that didn't have braces,
7423 pop them now. If implicit == 1, this will have been done in
7424 process_init_element; do not repeat it here because in the case
7425 of excess initializers for an empty aggregate this leads to an
7426 infinite cycle of popping a level and immediately recreating
7427 it. */
7428 if (implicit != 1)
7430 while (constructor_stack->implicit)
7432 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7433 && constructor_fields == 0)
7434 process_init_element (input_location,
7435 pop_init_level (loc, 1, braced_init_obstack),
7436 true, braced_init_obstack);
7437 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7438 && constructor_max_index
7439 && tree_int_cst_lt (constructor_max_index,
7440 constructor_index))
7441 process_init_element (input_location,
7442 pop_init_level (loc, 1, braced_init_obstack),
7443 true, braced_init_obstack);
7444 else
7445 break;
7449 /* Unless this is an explicit brace, we need to preserve previous
7450 content if any. */
7451 if (implicit)
7453 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7454 value = find_init_member (constructor_fields, braced_init_obstack);
7455 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7456 value = find_init_member (constructor_index, braced_init_obstack);
7459 p = XNEW (struct constructor_stack);
7460 p->type = constructor_type;
7461 p->fields = constructor_fields;
7462 p->index = constructor_index;
7463 p->max_index = constructor_max_index;
7464 p->unfilled_index = constructor_unfilled_index;
7465 p->unfilled_fields = constructor_unfilled_fields;
7466 p->bit_index = constructor_bit_index;
7467 p->elements = constructor_elements;
7468 p->constant = constructor_constant;
7469 p->simple = constructor_simple;
7470 p->nonconst = constructor_nonconst;
7471 p->erroneous = constructor_erroneous;
7472 p->pending_elts = constructor_pending_elts;
7473 p->depth = constructor_depth;
7474 p->replacement_value.value = 0;
7475 p->replacement_value.original_code = ERROR_MARK;
7476 p->replacement_value.original_type = NULL;
7477 p->implicit = implicit;
7478 p->outer = 0;
7479 p->incremental = constructor_incremental;
7480 p->designated = constructor_designated;
7481 p->designator_depth = designator_depth;
7482 p->next = constructor_stack;
7483 p->range_stack = 0;
7484 constructor_stack = p;
7486 constructor_constant = 1;
7487 constructor_simple = 1;
7488 constructor_nonconst = 0;
7489 constructor_depth = SPELLING_DEPTH ();
7490 constructor_elements = NULL;
7491 constructor_incremental = 1;
7492 constructor_designated = 0;
7493 constructor_pending_elts = 0;
7494 if (!implicit)
7496 p->range_stack = constructor_range_stack;
7497 constructor_range_stack = 0;
7498 designator_depth = 0;
7499 designator_erroneous = 0;
7502 /* Don't die if an entire brace-pair level is superfluous
7503 in the containing level. */
7504 if (constructor_type == 0)
7506 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7508 /* Don't die if there are extra init elts at the end. */
7509 if (constructor_fields == 0)
7510 constructor_type = 0;
7511 else
7513 constructor_type = TREE_TYPE (constructor_fields);
7514 push_member_name (constructor_fields);
7515 constructor_depth++;
7517 /* If upper initializer is designated, then mark this as
7518 designated too to prevent bogus warnings. */
7519 constructor_designated = p->designated;
7521 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7523 constructor_type = TREE_TYPE (constructor_type);
7524 push_array_bounds (tree_to_uhwi (constructor_index));
7525 constructor_depth++;
7528 if (constructor_type == 0)
7530 error_init (loc, "extra brace group at end of initializer");
7531 constructor_fields = 0;
7532 constructor_unfilled_fields = 0;
7533 return;
7536 if (value && TREE_CODE (value) == CONSTRUCTOR)
7538 constructor_constant = TREE_CONSTANT (value);
7539 constructor_simple = TREE_STATIC (value);
7540 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7541 constructor_elements = CONSTRUCTOR_ELTS (value);
7542 if (!vec_safe_is_empty (constructor_elements)
7543 && (TREE_CODE (constructor_type) == RECORD_TYPE
7544 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7545 set_nonincremental_init (braced_init_obstack);
7548 if (implicit == 1)
7549 found_missing_braces = 1;
7551 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7553 constructor_fields = TYPE_FIELDS (constructor_type);
7554 /* Skip any nameless bit fields at the beginning. */
7555 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7556 && DECL_NAME (constructor_fields) == 0)
7557 constructor_fields = DECL_CHAIN (constructor_fields);
7559 constructor_unfilled_fields = constructor_fields;
7560 constructor_bit_index = bitsize_zero_node;
7562 else if (VECTOR_TYPE_P (constructor_type))
7564 /* Vectors are like simple fixed-size arrays. */
7565 constructor_max_index =
7566 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7567 constructor_index = bitsize_int (0);
7568 constructor_unfilled_index = constructor_index;
7570 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7572 if (TYPE_DOMAIN (constructor_type))
7574 constructor_max_index
7575 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7577 /* Detect non-empty initializations of zero-length arrays. */
7578 if (constructor_max_index == NULL_TREE
7579 && TYPE_SIZE (constructor_type))
7580 constructor_max_index = integer_minus_one_node;
7582 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7583 to initialize VLAs will cause a proper error; avoid tree
7584 checking errors as well by setting a safe value. */
7585 if (constructor_max_index
7586 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7587 constructor_max_index = integer_minus_one_node;
7589 constructor_index
7590 = convert (bitsizetype,
7591 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7593 else
7594 constructor_index = bitsize_zero_node;
7596 constructor_unfilled_index = constructor_index;
7597 if (value && TREE_CODE (value) == STRING_CST)
7599 /* We need to split the char/wchar array into individual
7600 characters, so that we don't have to special case it
7601 everywhere. */
7602 set_nonincremental_init_from_string (value, braced_init_obstack);
7605 else
7607 if (constructor_type != error_mark_node)
7608 warning_init (input_location, 0, "braces around scalar initializer");
7609 constructor_fields = constructor_type;
7610 constructor_unfilled_fields = constructor_type;
7614 /* At the end of an implicit or explicit brace level,
7615 finish up that level of constructor. If a single expression
7616 with redundant braces initialized that level, return the
7617 c_expr structure for that expression. Otherwise, the original_code
7618 element is set to ERROR_MARK.
7619 If we were outputting the elements as they are read, return 0 as the value
7620 from inner levels (process_init_element ignores that),
7621 but return error_mark_node as the value from the outermost level
7622 (that's what we want to put in DECL_INITIAL).
7623 Otherwise, return a CONSTRUCTOR expression as the value. */
7625 struct c_expr
7626 pop_init_level (location_t loc, int implicit,
7627 struct obstack *braced_init_obstack)
7629 struct constructor_stack *p;
7630 struct c_expr ret;
7631 ret.value = 0;
7632 ret.original_code = ERROR_MARK;
7633 ret.original_type = NULL;
7635 if (implicit == 0)
7637 /* When we come to an explicit close brace,
7638 pop any inner levels that didn't have explicit braces. */
7639 while (constructor_stack->implicit)
7640 process_init_element (input_location,
7641 pop_init_level (loc, 1, braced_init_obstack),
7642 true, braced_init_obstack);
7643 gcc_assert (!constructor_range_stack);
7646 /* Now output all pending elements. */
7647 constructor_incremental = 1;
7648 output_pending_init_elements (1, braced_init_obstack);
7650 p = constructor_stack;
7652 /* Error for initializing a flexible array member, or a zero-length
7653 array member in an inappropriate context. */
7654 if (constructor_type && constructor_fields
7655 && TREE_CODE (constructor_type) == ARRAY_TYPE
7656 && TYPE_DOMAIN (constructor_type)
7657 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7659 /* Silently discard empty initializations. The parser will
7660 already have pedwarned for empty brackets. */
7661 if (integer_zerop (constructor_unfilled_index))
7662 constructor_type = NULL_TREE;
7663 else
7665 gcc_assert (!TYPE_SIZE (constructor_type));
7667 if (constructor_depth > 2)
7668 error_init (loc, "initialization of flexible array member in a nested context");
7669 else
7670 pedwarn_init (loc, OPT_Wpedantic,
7671 "initialization of a flexible array member");
7673 /* We have already issued an error message for the existence
7674 of a flexible array member not at the end of the structure.
7675 Discard the initializer so that we do not die later. */
7676 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7677 constructor_type = NULL_TREE;
7681 switch (vec_safe_length (constructor_elements))
7683 case 0:
7684 /* Initialization with { } counts as zeroinit. */
7685 constructor_zeroinit = 1;
7686 break;
7687 case 1:
7688 /* This might be zeroinit as well. */
7689 if (integer_zerop ((*constructor_elements)[0].value))
7690 constructor_zeroinit = 1;
7691 break;
7692 default:
7693 /* If the constructor has more than one element, it can't be { 0 }. */
7694 constructor_zeroinit = 0;
7695 break;
7698 /* Warn when some structs are initialized with direct aggregation. */
7699 if (!implicit && found_missing_braces && warn_missing_braces
7700 && !constructor_zeroinit)
7701 warning_init (loc, OPT_Wmissing_braces,
7702 "missing braces around initializer");
7704 /* Warn when some struct elements are implicitly initialized to zero. */
7705 if (warn_missing_field_initializers
7706 && constructor_type
7707 && TREE_CODE (constructor_type) == RECORD_TYPE
7708 && constructor_unfilled_fields)
7710 /* Do not warn for flexible array members or zero-length arrays. */
7711 while (constructor_unfilled_fields
7712 && (!DECL_SIZE (constructor_unfilled_fields)
7713 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7714 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7716 if (constructor_unfilled_fields
7717 /* Do not warn if this level of the initializer uses member
7718 designators; it is likely to be deliberate. */
7719 && !constructor_designated
7720 /* Do not warn about initializing with { 0 } or with { }. */
7721 && !constructor_zeroinit)
7723 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7724 "missing initializer for field %qD of %qT",
7725 constructor_unfilled_fields,
7726 constructor_type))
7727 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7728 "%qD declared here", constructor_unfilled_fields);
7732 /* Pad out the end of the structure. */
7733 if (p->replacement_value.value)
7734 /* If this closes a superfluous brace pair,
7735 just pass out the element between them. */
7736 ret = p->replacement_value;
7737 else if (constructor_type == 0)
7739 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
7740 && TREE_CODE (constructor_type) != ARRAY_TYPE
7741 && !VECTOR_TYPE_P (constructor_type))
7743 /* A nonincremental scalar initializer--just return
7744 the element, after verifying there is just one. */
7745 if (vec_safe_is_empty (constructor_elements))
7747 if (!constructor_erroneous)
7748 error_init (loc, "empty scalar initializer");
7749 ret.value = error_mark_node;
7751 else if (vec_safe_length (constructor_elements) != 1)
7753 error_init (loc, "extra elements in scalar initializer");
7754 ret.value = (*constructor_elements)[0].value;
7756 else
7757 ret.value = (*constructor_elements)[0].value;
7759 else
7761 if (constructor_erroneous)
7762 ret.value = error_mark_node;
7763 else
7765 ret.value = build_constructor (constructor_type,
7766 constructor_elements);
7767 if (constructor_constant)
7768 TREE_CONSTANT (ret.value) = 1;
7769 if (constructor_constant && constructor_simple)
7770 TREE_STATIC (ret.value) = 1;
7771 if (constructor_nonconst)
7772 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7776 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7778 if (constructor_nonconst)
7779 ret.original_code = C_MAYBE_CONST_EXPR;
7780 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7781 ret.original_code = ERROR_MARK;
7784 constructor_type = p->type;
7785 constructor_fields = p->fields;
7786 constructor_index = p->index;
7787 constructor_max_index = p->max_index;
7788 constructor_unfilled_index = p->unfilled_index;
7789 constructor_unfilled_fields = p->unfilled_fields;
7790 constructor_bit_index = p->bit_index;
7791 constructor_elements = p->elements;
7792 constructor_constant = p->constant;
7793 constructor_simple = p->simple;
7794 constructor_nonconst = p->nonconst;
7795 constructor_erroneous = p->erroneous;
7796 constructor_incremental = p->incremental;
7797 constructor_designated = p->designated;
7798 designator_depth = p->designator_depth;
7799 constructor_pending_elts = p->pending_elts;
7800 constructor_depth = p->depth;
7801 if (!p->implicit)
7802 constructor_range_stack = p->range_stack;
7803 RESTORE_SPELLING_DEPTH (constructor_depth);
7805 constructor_stack = p->next;
7806 free (p);
7808 if (ret.value == 0 && constructor_stack == 0)
7809 ret.value = error_mark_node;
7810 return ret;
7813 /* Common handling for both array range and field name designators.
7814 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7816 static int
7817 set_designator (location_t loc, int array,
7818 struct obstack *braced_init_obstack)
7820 tree subtype;
7821 enum tree_code subcode;
7823 /* Don't die if an entire brace-pair level is superfluous
7824 in the containing level. */
7825 if (constructor_type == 0)
7826 return 1;
7828 /* If there were errors in this designator list already, bail out
7829 silently. */
7830 if (designator_erroneous)
7831 return 1;
7833 if (!designator_depth)
7835 gcc_assert (!constructor_range_stack);
7837 /* Designator list starts at the level of closest explicit
7838 braces. */
7839 while (constructor_stack->implicit)
7840 process_init_element (input_location,
7841 pop_init_level (loc, 1, braced_init_obstack),
7842 true, braced_init_obstack);
7843 constructor_designated = 1;
7844 return 0;
7847 switch (TREE_CODE (constructor_type))
7849 case RECORD_TYPE:
7850 case UNION_TYPE:
7851 subtype = TREE_TYPE (constructor_fields);
7852 if (subtype != error_mark_node)
7853 subtype = TYPE_MAIN_VARIANT (subtype);
7854 break;
7855 case ARRAY_TYPE:
7856 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7857 break;
7858 default:
7859 gcc_unreachable ();
7862 subcode = TREE_CODE (subtype);
7863 if (array && subcode != ARRAY_TYPE)
7865 error_init (loc, "array index in non-array initializer");
7866 return 1;
7868 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7870 error_init (loc, "field name not in record or union initializer");
7871 return 1;
7874 constructor_designated = 1;
7875 push_init_level (loc, 2, braced_init_obstack);
7876 return 0;
7879 /* If there are range designators in designator list, push a new designator
7880 to constructor_range_stack. RANGE_END is end of such stack range or
7881 NULL_TREE if there is no range designator at this level. */
7883 static void
7884 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7886 struct constructor_range_stack *p;
7888 p = (struct constructor_range_stack *)
7889 obstack_alloc (braced_init_obstack,
7890 sizeof (struct constructor_range_stack));
7891 p->prev = constructor_range_stack;
7892 p->next = 0;
7893 p->fields = constructor_fields;
7894 p->range_start = constructor_index;
7895 p->index = constructor_index;
7896 p->stack = constructor_stack;
7897 p->range_end = range_end;
7898 if (constructor_range_stack)
7899 constructor_range_stack->next = p;
7900 constructor_range_stack = p;
7903 /* Within an array initializer, specify the next index to be initialized.
7904 FIRST is that index. If LAST is nonzero, then initialize a range
7905 of indices, running from FIRST through LAST. */
7907 void
7908 set_init_index (location_t loc, tree first, tree last,
7909 struct obstack *braced_init_obstack)
7911 if (set_designator (loc, 1, braced_init_obstack))
7912 return;
7914 designator_erroneous = 1;
7916 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7917 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7919 error_init (loc, "array index in initializer not of integer type");
7920 return;
7923 if (TREE_CODE (first) != INTEGER_CST)
7925 first = c_fully_fold (first, false, NULL);
7926 if (TREE_CODE (first) == INTEGER_CST)
7927 pedwarn_init (loc, OPT_Wpedantic,
7928 "array index in initializer is not "
7929 "an integer constant expression");
7932 if (last && TREE_CODE (last) != INTEGER_CST)
7934 last = c_fully_fold (last, false, NULL);
7935 if (TREE_CODE (last) == INTEGER_CST)
7936 pedwarn_init (loc, OPT_Wpedantic,
7937 "array index in initializer is not "
7938 "an integer constant expression");
7941 if (TREE_CODE (first) != INTEGER_CST)
7942 error_init (loc, "nonconstant array index in initializer");
7943 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7944 error_init (loc, "nonconstant array index in initializer");
7945 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7946 error_init (loc, "array index in non-array initializer");
7947 else if (tree_int_cst_sgn (first) == -1)
7948 error_init (loc, "array index in initializer exceeds array bounds");
7949 else if (constructor_max_index
7950 && tree_int_cst_lt (constructor_max_index, first))
7951 error_init (loc, "array index in initializer exceeds array bounds");
7952 else
7954 constant_expression_warning (first);
7955 if (last)
7956 constant_expression_warning (last);
7957 constructor_index = convert (bitsizetype, first);
7958 if (tree_int_cst_lt (constructor_index, first))
7960 constructor_index = copy_node (constructor_index);
7961 TREE_OVERFLOW (constructor_index) = 1;
7964 if (last)
7966 if (tree_int_cst_equal (first, last))
7967 last = 0;
7968 else if (tree_int_cst_lt (last, first))
7970 error_init (loc, "empty index range in initializer");
7971 last = 0;
7973 else
7975 last = convert (bitsizetype, last);
7976 if (constructor_max_index != 0
7977 && tree_int_cst_lt (constructor_max_index, last))
7979 error_init (loc, "array index range in initializer exceeds "
7980 "array bounds");
7981 last = 0;
7986 designator_depth++;
7987 designator_erroneous = 0;
7988 if (constructor_range_stack || last)
7989 push_range_stack (last, braced_init_obstack);
7993 /* Within a struct initializer, specify the next field to be initialized. */
7995 void
7996 set_init_label (location_t loc, tree fieldname,
7997 struct obstack *braced_init_obstack)
7999 tree field;
8001 if (set_designator (loc, 0, braced_init_obstack))
8002 return;
8004 designator_erroneous = 1;
8006 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8008 error_init (loc, "field name not in record or union initializer");
8009 return;
8012 field = lookup_field (constructor_type, fieldname);
8014 if (field == 0)
8015 error_at (loc, "unknown field %qE specified in initializer", fieldname);
8016 else
8019 constructor_fields = TREE_VALUE (field);
8020 designator_depth++;
8021 designator_erroneous = 0;
8022 if (constructor_range_stack)
8023 push_range_stack (NULL_TREE, braced_init_obstack);
8024 field = TREE_CHAIN (field);
8025 if (field)
8027 if (set_designator (loc, 0, braced_init_obstack))
8028 return;
8031 while (field != NULL_TREE);
8034 /* Add a new initializer to the tree of pending initializers. PURPOSE
8035 identifies the initializer, either array index or field in a structure.
8036 VALUE is the value of that index or field. If ORIGTYPE is not
8037 NULL_TREE, it is the original type of VALUE.
8039 IMPLICIT is true if value comes from pop_init_level (1),
8040 the new initializer has been merged with the existing one
8041 and thus no warnings should be emitted about overriding an
8042 existing initializer. */
8044 static void
8045 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8046 bool implicit, struct obstack *braced_init_obstack)
8048 struct init_node *p, **q, *r;
8050 q = &constructor_pending_elts;
8051 p = 0;
8053 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8055 while (*q != 0)
8057 p = *q;
8058 if (tree_int_cst_lt (purpose, p->purpose))
8059 q = &p->left;
8060 else if (tree_int_cst_lt (p->purpose, purpose))
8061 q = &p->right;
8062 else
8064 if (!implicit)
8066 if (TREE_SIDE_EFFECTS (p->value))
8067 warning_init (loc, OPT_Woverride_init_side_effects,
8068 "initialized field with side-effects "
8069 "overwritten");
8070 else if (warn_override_init)
8071 warning_init (loc, OPT_Woverride_init,
8072 "initialized field overwritten");
8074 p->value = value;
8075 p->origtype = origtype;
8076 return;
8080 else
8082 tree bitpos;
8084 bitpos = bit_position (purpose);
8085 while (*q != NULL)
8087 p = *q;
8088 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8089 q = &p->left;
8090 else if (p->purpose != purpose)
8091 q = &p->right;
8092 else
8094 if (!implicit)
8096 if (TREE_SIDE_EFFECTS (p->value))
8097 warning_init (loc, OPT_Woverride_init_side_effects,
8098 "initialized field with side-effects "
8099 "overwritten");
8100 else if (warn_override_init)
8101 warning_init (loc, OPT_Woverride_init,
8102 "initialized field overwritten");
8104 p->value = value;
8105 p->origtype = origtype;
8106 return;
8111 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8112 sizeof (struct init_node));
8113 r->purpose = purpose;
8114 r->value = value;
8115 r->origtype = origtype;
8117 *q = r;
8118 r->parent = p;
8119 r->left = 0;
8120 r->right = 0;
8121 r->balance = 0;
8123 while (p)
8125 struct init_node *s;
8127 if (r == p->left)
8129 if (p->balance == 0)
8130 p->balance = -1;
8131 else if (p->balance < 0)
8133 if (r->balance < 0)
8135 /* L rotation. */
8136 p->left = r->right;
8137 if (p->left)
8138 p->left->parent = p;
8139 r->right = p;
8141 p->balance = 0;
8142 r->balance = 0;
8144 s = p->parent;
8145 p->parent = r;
8146 r->parent = s;
8147 if (s)
8149 if (s->left == p)
8150 s->left = r;
8151 else
8152 s->right = r;
8154 else
8155 constructor_pending_elts = r;
8157 else
8159 /* LR rotation. */
8160 struct init_node *t = r->right;
8162 r->right = t->left;
8163 if (r->right)
8164 r->right->parent = r;
8165 t->left = r;
8167 p->left = t->right;
8168 if (p->left)
8169 p->left->parent = p;
8170 t->right = p;
8172 p->balance = t->balance < 0;
8173 r->balance = -(t->balance > 0);
8174 t->balance = 0;
8176 s = p->parent;
8177 p->parent = t;
8178 r->parent = t;
8179 t->parent = s;
8180 if (s)
8182 if (s->left == p)
8183 s->left = t;
8184 else
8185 s->right = t;
8187 else
8188 constructor_pending_elts = t;
8190 break;
8192 else
8194 /* p->balance == +1; growth of left side balances the node. */
8195 p->balance = 0;
8196 break;
8199 else /* r == p->right */
8201 if (p->balance == 0)
8202 /* Growth propagation from right side. */
8203 p->balance++;
8204 else if (p->balance > 0)
8206 if (r->balance > 0)
8208 /* R rotation. */
8209 p->right = r->left;
8210 if (p->right)
8211 p->right->parent = p;
8212 r->left = p;
8214 p->balance = 0;
8215 r->balance = 0;
8217 s = p->parent;
8218 p->parent = r;
8219 r->parent = s;
8220 if (s)
8222 if (s->left == p)
8223 s->left = r;
8224 else
8225 s->right = r;
8227 else
8228 constructor_pending_elts = r;
8230 else /* r->balance == -1 */
8232 /* RL rotation */
8233 struct init_node *t = r->left;
8235 r->left = t->right;
8236 if (r->left)
8237 r->left->parent = r;
8238 t->right = r;
8240 p->right = t->left;
8241 if (p->right)
8242 p->right->parent = p;
8243 t->left = p;
8245 r->balance = (t->balance < 0);
8246 p->balance = -(t->balance > 0);
8247 t->balance = 0;
8249 s = p->parent;
8250 p->parent = t;
8251 r->parent = t;
8252 t->parent = s;
8253 if (s)
8255 if (s->left == p)
8256 s->left = t;
8257 else
8258 s->right = t;
8260 else
8261 constructor_pending_elts = t;
8263 break;
8265 else
8267 /* p->balance == -1; growth of right side balances the node. */
8268 p->balance = 0;
8269 break;
8273 r = p;
8274 p = p->parent;
8278 /* Build AVL tree from a sorted chain. */
8280 static void
8281 set_nonincremental_init (struct obstack * braced_init_obstack)
8283 unsigned HOST_WIDE_INT ix;
8284 tree index, value;
8286 if (TREE_CODE (constructor_type) != RECORD_TYPE
8287 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8288 return;
8290 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8291 add_pending_init (input_location, index, value, NULL_TREE, true,
8292 braced_init_obstack);
8293 constructor_elements = NULL;
8294 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8296 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8297 /* Skip any nameless bit fields at the beginning. */
8298 while (constructor_unfilled_fields != 0
8299 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8300 && DECL_NAME (constructor_unfilled_fields) == 0)
8301 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8304 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8306 if (TYPE_DOMAIN (constructor_type))
8307 constructor_unfilled_index
8308 = convert (bitsizetype,
8309 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8310 else
8311 constructor_unfilled_index = bitsize_zero_node;
8313 constructor_incremental = 0;
8316 /* Build AVL tree from a string constant. */
8318 static void
8319 set_nonincremental_init_from_string (tree str,
8320 struct obstack * braced_init_obstack)
8322 tree value, purpose, type;
8323 HOST_WIDE_INT val[2];
8324 const char *p, *end;
8325 int byte, wchar_bytes, charwidth, bitpos;
8327 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8329 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8330 charwidth = TYPE_PRECISION (char_type_node);
8331 type = TREE_TYPE (constructor_type);
8332 p = TREE_STRING_POINTER (str);
8333 end = p + TREE_STRING_LENGTH (str);
8335 for (purpose = bitsize_zero_node;
8336 p < end
8337 && !(constructor_max_index
8338 && tree_int_cst_lt (constructor_max_index, purpose));
8339 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8341 if (wchar_bytes == 1)
8343 val[0] = (unsigned char) *p++;
8344 val[1] = 0;
8346 else
8348 val[1] = 0;
8349 val[0] = 0;
8350 for (byte = 0; byte < wchar_bytes; byte++)
8352 if (BYTES_BIG_ENDIAN)
8353 bitpos = (wchar_bytes - byte - 1) * charwidth;
8354 else
8355 bitpos = byte * charwidth;
8356 val[bitpos % HOST_BITS_PER_WIDE_INT]
8357 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8358 << (bitpos % HOST_BITS_PER_WIDE_INT);
8362 if (!TYPE_UNSIGNED (type))
8364 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8365 if (bitpos < HOST_BITS_PER_WIDE_INT)
8367 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8369 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8370 val[1] = -1;
8373 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8375 if (val[0] < 0)
8376 val[1] = -1;
8378 else if (val[1] & (((HOST_WIDE_INT) 1)
8379 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8380 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8383 value = wide_int_to_tree (type,
8384 wide_int::from_array (val, 2,
8385 HOST_BITS_PER_WIDE_INT * 2));
8386 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8387 braced_init_obstack);
8390 constructor_incremental = 0;
8393 /* Return value of FIELD in pending initializer or zero if the field was
8394 not initialized yet. */
8396 static tree
8397 find_init_member (tree field, struct obstack * braced_init_obstack)
8399 struct init_node *p;
8401 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8403 if (constructor_incremental
8404 && tree_int_cst_lt (field, constructor_unfilled_index))
8405 set_nonincremental_init (braced_init_obstack);
8407 p = constructor_pending_elts;
8408 while (p)
8410 if (tree_int_cst_lt (field, p->purpose))
8411 p = p->left;
8412 else if (tree_int_cst_lt (p->purpose, field))
8413 p = p->right;
8414 else
8415 return p->value;
8418 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8420 tree bitpos = bit_position (field);
8422 if (constructor_incremental
8423 && (!constructor_unfilled_fields
8424 || tree_int_cst_lt (bitpos,
8425 bit_position (constructor_unfilled_fields))))
8426 set_nonincremental_init (braced_init_obstack);
8428 p = constructor_pending_elts;
8429 while (p)
8431 if (field == p->purpose)
8432 return p->value;
8433 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8434 p = p->left;
8435 else
8436 p = p->right;
8439 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8441 if (!vec_safe_is_empty (constructor_elements)
8442 && (constructor_elements->last ().index == field))
8443 return constructor_elements->last ().value;
8445 return 0;
8448 /* "Output" the next constructor element.
8449 At top level, really output it to assembler code now.
8450 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8451 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8452 TYPE is the data type that the containing data type wants here.
8453 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8454 If VALUE is a string constant, STRICT_STRING is true if it is
8455 unparenthesized or we should not warn here for it being parenthesized.
8456 For other types of VALUE, STRICT_STRING is not used.
8458 PENDING if non-nil means output pending elements that belong
8459 right after this element. (PENDING is normally 1;
8460 it is 0 while outputting pending elements, to avoid recursion.)
8462 IMPLICIT is true if value comes from pop_init_level (1),
8463 the new initializer has been merged with the existing one
8464 and thus no warnings should be emitted about overriding an
8465 existing initializer. */
8467 static void
8468 output_init_element (location_t loc, tree value, tree origtype,
8469 bool strict_string, tree type, tree field, int pending,
8470 bool implicit, struct obstack * braced_init_obstack)
8472 tree semantic_type = NULL_TREE;
8473 bool maybe_const = true;
8474 bool npc;
8476 if (type == error_mark_node || value == error_mark_node)
8478 constructor_erroneous = 1;
8479 return;
8481 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8482 && (TREE_CODE (value) == STRING_CST
8483 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8484 && !(TREE_CODE (value) == STRING_CST
8485 && TREE_CODE (type) == ARRAY_TYPE
8486 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8487 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8488 TYPE_MAIN_VARIANT (type)))
8489 value = array_to_pointer_conversion (input_location, value);
8491 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8492 && require_constant_value && pending)
8494 /* As an extension, allow initializing objects with static storage
8495 duration with compound literals (which are then treated just as
8496 the brace enclosed list they contain). */
8497 if (flag_isoc99)
8498 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8499 "constant");
8500 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8501 value = DECL_INITIAL (decl);
8504 npc = null_pointer_constant_p (value);
8505 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8507 semantic_type = TREE_TYPE (value);
8508 value = TREE_OPERAND (value, 0);
8510 value = c_fully_fold (value, require_constant_value, &maybe_const);
8512 if (value == error_mark_node)
8513 constructor_erroneous = 1;
8514 else if (!TREE_CONSTANT (value))
8515 constructor_constant = 0;
8516 else if (!initializer_constant_valid_p (value,
8517 TREE_TYPE (value),
8518 AGGREGATE_TYPE_P (constructor_type)
8519 && TYPE_REVERSE_STORAGE_ORDER
8520 (constructor_type))
8521 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8522 && DECL_C_BIT_FIELD (field)
8523 && TREE_CODE (value) != INTEGER_CST))
8524 constructor_simple = 0;
8525 if (!maybe_const)
8526 constructor_nonconst = 1;
8528 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8530 if (require_constant_value)
8532 error_init (loc, "initializer element is not constant");
8533 value = error_mark_node;
8535 else if (require_constant_elements)
8536 pedwarn (loc, OPT_Wpedantic,
8537 "initializer element is not computable at load time");
8539 else if (!maybe_const
8540 && (require_constant_value || require_constant_elements))
8541 pedwarn_init (loc, OPT_Wpedantic,
8542 "initializer element is not a constant expression");
8544 /* Issue -Wc++-compat warnings about initializing a bitfield with
8545 enum type. */
8546 if (warn_cxx_compat
8547 && field != NULL_TREE
8548 && TREE_CODE (field) == FIELD_DECL
8549 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8550 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8551 != TYPE_MAIN_VARIANT (type))
8552 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8554 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8555 if (checktype != error_mark_node
8556 && (TYPE_MAIN_VARIANT (checktype)
8557 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8558 warning_init (loc, OPT_Wc___compat,
8559 "enum conversion in initialization is invalid in C++");
8562 /* If this field is empty (and not at the end of structure),
8563 don't do anything other than checking the initializer. */
8564 if (field
8565 && (TREE_TYPE (field) == error_mark_node
8566 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8567 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8568 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8569 || DECL_CHAIN (field)))))
8570 return;
8572 if (semantic_type)
8573 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8574 value = digest_init (loc, type, value, origtype, npc, strict_string,
8575 require_constant_value);
8576 if (value == error_mark_node)
8578 constructor_erroneous = 1;
8579 return;
8581 if (require_constant_value || require_constant_elements)
8582 constant_expression_warning (value);
8584 /* If this element doesn't come next in sequence,
8585 put it on constructor_pending_elts. */
8586 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8587 && (!constructor_incremental
8588 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8590 if (constructor_incremental
8591 && tree_int_cst_lt (field, constructor_unfilled_index))
8592 set_nonincremental_init (braced_init_obstack);
8594 add_pending_init (loc, field, value, origtype, implicit,
8595 braced_init_obstack);
8596 return;
8598 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8599 && (!constructor_incremental
8600 || field != constructor_unfilled_fields))
8602 /* We do this for records but not for unions. In a union,
8603 no matter which field is specified, it can be initialized
8604 right away since it starts at the beginning of the union. */
8605 if (constructor_incremental)
8607 if (!constructor_unfilled_fields)
8608 set_nonincremental_init (braced_init_obstack);
8609 else
8611 tree bitpos, unfillpos;
8613 bitpos = bit_position (field);
8614 unfillpos = bit_position (constructor_unfilled_fields);
8616 if (tree_int_cst_lt (bitpos, unfillpos))
8617 set_nonincremental_init (braced_init_obstack);
8621 add_pending_init (loc, field, value, origtype, implicit,
8622 braced_init_obstack);
8623 return;
8625 else if (TREE_CODE (constructor_type) == UNION_TYPE
8626 && !vec_safe_is_empty (constructor_elements))
8628 if (!implicit)
8630 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8631 warning_init (loc, OPT_Woverride_init_side_effects,
8632 "initialized field with side-effects overwritten");
8633 else if (warn_override_init)
8634 warning_init (loc, OPT_Woverride_init,
8635 "initialized field overwritten");
8638 /* We can have just one union field set. */
8639 constructor_elements = NULL;
8642 /* Otherwise, output this element either to
8643 constructor_elements or to the assembler file. */
8645 constructor_elt celt = {field, value};
8646 vec_safe_push (constructor_elements, celt);
8648 /* Advance the variable that indicates sequential elements output. */
8649 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8650 constructor_unfilled_index
8651 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8652 bitsize_one_node);
8653 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8655 constructor_unfilled_fields
8656 = DECL_CHAIN (constructor_unfilled_fields);
8658 /* Skip any nameless bit fields. */
8659 while (constructor_unfilled_fields != 0
8660 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8661 && DECL_NAME (constructor_unfilled_fields) == 0)
8662 constructor_unfilled_fields =
8663 DECL_CHAIN (constructor_unfilled_fields);
8665 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8666 constructor_unfilled_fields = 0;
8668 /* Now output any pending elements which have become next. */
8669 if (pending)
8670 output_pending_init_elements (0, braced_init_obstack);
8673 /* Output any pending elements which have become next.
8674 As we output elements, constructor_unfilled_{fields,index}
8675 advances, which may cause other elements to become next;
8676 if so, they too are output.
8678 If ALL is 0, we return when there are
8679 no more pending elements to output now.
8681 If ALL is 1, we output space as necessary so that
8682 we can output all the pending elements. */
8683 static void
8684 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8686 struct init_node *elt = constructor_pending_elts;
8687 tree next;
8689 retry:
8691 /* Look through the whole pending tree.
8692 If we find an element that should be output now,
8693 output it. Otherwise, set NEXT to the element
8694 that comes first among those still pending. */
8696 next = 0;
8697 while (elt)
8699 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8701 if (tree_int_cst_equal (elt->purpose,
8702 constructor_unfilled_index))
8703 output_init_element (input_location, elt->value, elt->origtype,
8704 true, TREE_TYPE (constructor_type),
8705 constructor_unfilled_index, 0, false,
8706 braced_init_obstack);
8707 else if (tree_int_cst_lt (constructor_unfilled_index,
8708 elt->purpose))
8710 /* Advance to the next smaller node. */
8711 if (elt->left)
8712 elt = elt->left;
8713 else
8715 /* We have reached the smallest node bigger than the
8716 current unfilled index. Fill the space first. */
8717 next = elt->purpose;
8718 break;
8721 else
8723 /* Advance to the next bigger node. */
8724 if (elt->right)
8725 elt = elt->right;
8726 else
8728 /* We have reached the biggest node in a subtree. Find
8729 the parent of it, which is the next bigger node. */
8730 while (elt->parent && elt->parent->right == elt)
8731 elt = elt->parent;
8732 elt = elt->parent;
8733 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8734 elt->purpose))
8736 next = elt->purpose;
8737 break;
8742 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8744 tree ctor_unfilled_bitpos, elt_bitpos;
8746 /* If the current record is complete we are done. */
8747 if (constructor_unfilled_fields == 0)
8748 break;
8750 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8751 elt_bitpos = bit_position (elt->purpose);
8752 /* We can't compare fields here because there might be empty
8753 fields in between. */
8754 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8756 constructor_unfilled_fields = elt->purpose;
8757 output_init_element (input_location, elt->value, elt->origtype,
8758 true, TREE_TYPE (elt->purpose),
8759 elt->purpose, 0, false,
8760 braced_init_obstack);
8762 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8764 /* Advance to the next smaller node. */
8765 if (elt->left)
8766 elt = elt->left;
8767 else
8769 /* We have reached the smallest node bigger than the
8770 current unfilled field. Fill the space first. */
8771 next = elt->purpose;
8772 break;
8775 else
8777 /* Advance to the next bigger node. */
8778 if (elt->right)
8779 elt = elt->right;
8780 else
8782 /* We have reached the biggest node in a subtree. Find
8783 the parent of it, which is the next bigger node. */
8784 while (elt->parent && elt->parent->right == elt)
8785 elt = elt->parent;
8786 elt = elt->parent;
8787 if (elt
8788 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8789 bit_position (elt->purpose))))
8791 next = elt->purpose;
8792 break;
8799 /* Ordinarily return, but not if we want to output all
8800 and there are elements left. */
8801 if (!(all && next != 0))
8802 return;
8804 /* If it's not incremental, just skip over the gap, so that after
8805 jumping to retry we will output the next successive element. */
8806 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8807 constructor_unfilled_fields = next;
8808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8809 constructor_unfilled_index = next;
8811 /* ELT now points to the node in the pending tree with the next
8812 initializer to output. */
8813 goto retry;
8816 /* Add one non-braced element to the current constructor level.
8817 This adjusts the current position within the constructor's type.
8818 This may also start or terminate implicit levels
8819 to handle a partly-braced initializer.
8821 Once this has found the correct level for the new element,
8822 it calls output_init_element.
8824 IMPLICIT is true if value comes from pop_init_level (1),
8825 the new initializer has been merged with the existing one
8826 and thus no warnings should be emitted about overriding an
8827 existing initializer. */
8829 void
8830 process_init_element (location_t loc, struct c_expr value, bool implicit,
8831 struct obstack * braced_init_obstack)
8833 tree orig_value = value.value;
8834 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8835 bool strict_string = value.original_code == STRING_CST;
8836 bool was_designated = designator_depth != 0;
8838 designator_depth = 0;
8839 designator_erroneous = 0;
8841 if (!implicit && value.value && !integer_zerop (value.value))
8842 constructor_zeroinit = 0;
8844 /* Handle superfluous braces around string cst as in
8845 char x[] = {"foo"}; */
8846 if (string_flag
8847 && constructor_type
8848 && !was_designated
8849 && TREE_CODE (constructor_type) == ARRAY_TYPE
8850 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8851 && integer_zerop (constructor_unfilled_index))
8853 if (constructor_stack->replacement_value.value)
8854 error_init (loc, "excess elements in char array initializer");
8855 constructor_stack->replacement_value = value;
8856 return;
8859 if (constructor_stack->replacement_value.value != 0)
8861 error_init (loc, "excess elements in struct initializer");
8862 return;
8865 /* Ignore elements of a brace group if it is entirely superfluous
8866 and has already been diagnosed. */
8867 if (constructor_type == 0)
8868 return;
8870 if (!implicit && warn_designated_init && !was_designated
8871 && TREE_CODE (constructor_type) == RECORD_TYPE
8872 && lookup_attribute ("designated_init",
8873 TYPE_ATTRIBUTES (constructor_type)))
8874 warning_init (loc,
8875 OPT_Wdesignated_init,
8876 "positional initialization of field "
8877 "in %<struct%> declared with %<designated_init%> attribute");
8879 /* If we've exhausted any levels that didn't have braces,
8880 pop them now. */
8881 while (constructor_stack->implicit)
8883 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8884 && constructor_fields == 0)
8885 process_init_element (loc,
8886 pop_init_level (loc, 1, braced_init_obstack),
8887 true, braced_init_obstack);
8888 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8889 || VECTOR_TYPE_P (constructor_type))
8890 && constructor_max_index
8891 && tree_int_cst_lt (constructor_max_index,
8892 constructor_index))
8893 process_init_element (loc,
8894 pop_init_level (loc, 1, braced_init_obstack),
8895 true, braced_init_obstack);
8896 else
8897 break;
8900 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8901 if (constructor_range_stack)
8903 /* If value is a compound literal and we'll be just using its
8904 content, don't put it into a SAVE_EXPR. */
8905 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8906 || !require_constant_value)
8908 tree semantic_type = NULL_TREE;
8909 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8911 semantic_type = TREE_TYPE (value.value);
8912 value.value = TREE_OPERAND (value.value, 0);
8914 value.value = c_save_expr (value.value);
8915 if (semantic_type)
8916 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8917 value.value);
8921 while (1)
8923 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8925 tree fieldtype;
8926 enum tree_code fieldcode;
8928 if (constructor_fields == 0)
8930 pedwarn_init (loc, 0, "excess elements in struct initializer");
8931 break;
8934 fieldtype = TREE_TYPE (constructor_fields);
8935 if (fieldtype != error_mark_node)
8936 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8937 fieldcode = TREE_CODE (fieldtype);
8939 /* Error for non-static initialization of a flexible array member. */
8940 if (fieldcode == ARRAY_TYPE
8941 && !require_constant_value
8942 && TYPE_SIZE (fieldtype) == NULL_TREE
8943 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8945 error_init (loc, "non-static initialization of a flexible "
8946 "array member");
8947 break;
8950 /* Error for initialization of a flexible array member with
8951 a string constant if the structure is in an array. E.g.:
8952 struct S { int x; char y[]; };
8953 struct S s[] = { { 1, "foo" } };
8954 is invalid. */
8955 if (string_flag
8956 && fieldcode == ARRAY_TYPE
8957 && constructor_depth > 1
8958 && TYPE_SIZE (fieldtype) == NULL_TREE
8959 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8961 bool in_array_p = false;
8962 for (struct constructor_stack *p = constructor_stack;
8963 p && p->type; p = p->next)
8964 if (TREE_CODE (p->type) == ARRAY_TYPE)
8966 in_array_p = true;
8967 break;
8969 if (in_array_p)
8971 error_init (loc, "initialization of flexible array "
8972 "member in a nested context");
8973 break;
8977 /* Accept a string constant to initialize a subarray. */
8978 if (value.value != 0
8979 && fieldcode == ARRAY_TYPE
8980 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8981 && string_flag)
8982 value.value = orig_value;
8983 /* Otherwise, if we have come to a subaggregate,
8984 and we don't have an element of its type, push into it. */
8985 else if (value.value != 0
8986 && value.value != error_mark_node
8987 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8988 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8989 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8991 push_init_level (loc, 1, braced_init_obstack);
8992 continue;
8995 if (value.value)
8997 push_member_name (constructor_fields);
8998 output_init_element (loc, value.value, value.original_type,
8999 strict_string, fieldtype,
9000 constructor_fields, 1, implicit,
9001 braced_init_obstack);
9002 RESTORE_SPELLING_DEPTH (constructor_depth);
9004 else
9005 /* Do the bookkeeping for an element that was
9006 directly output as a constructor. */
9008 /* For a record, keep track of end position of last field. */
9009 if (DECL_SIZE (constructor_fields))
9010 constructor_bit_index
9011 = size_binop_loc (input_location, PLUS_EXPR,
9012 bit_position (constructor_fields),
9013 DECL_SIZE (constructor_fields));
9015 /* If the current field was the first one not yet written out,
9016 it isn't now, so update. */
9017 if (constructor_unfilled_fields == constructor_fields)
9019 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9020 /* Skip any nameless bit fields. */
9021 while (constructor_unfilled_fields != 0
9022 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9023 && DECL_NAME (constructor_unfilled_fields) == 0)
9024 constructor_unfilled_fields =
9025 DECL_CHAIN (constructor_unfilled_fields);
9029 constructor_fields = DECL_CHAIN (constructor_fields);
9030 /* Skip any nameless bit fields at the beginning. */
9031 while (constructor_fields != 0
9032 && DECL_C_BIT_FIELD (constructor_fields)
9033 && DECL_NAME (constructor_fields) == 0)
9034 constructor_fields = DECL_CHAIN (constructor_fields);
9036 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9038 tree fieldtype;
9039 enum tree_code fieldcode;
9041 if (constructor_fields == 0)
9043 pedwarn_init (loc, 0,
9044 "excess elements in union initializer");
9045 break;
9048 fieldtype = TREE_TYPE (constructor_fields);
9049 if (fieldtype != error_mark_node)
9050 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9051 fieldcode = TREE_CODE (fieldtype);
9053 /* Warn that traditional C rejects initialization of unions.
9054 We skip the warning if the value is zero. This is done
9055 under the assumption that the zero initializer in user
9056 code appears conditioned on e.g. __STDC__ to avoid
9057 "missing initializer" warnings and relies on default
9058 initialization to zero in the traditional C case.
9059 We also skip the warning if the initializer is designated,
9060 again on the assumption that this must be conditional on
9061 __STDC__ anyway (and we've already complained about the
9062 member-designator already). */
9063 if (!in_system_header_at (input_location) && !constructor_designated
9064 && !(value.value && (integer_zerop (value.value)
9065 || real_zerop (value.value))))
9066 warning (OPT_Wtraditional, "traditional C rejects initialization "
9067 "of unions");
9069 /* Accept a string constant to initialize a subarray. */
9070 if (value.value != 0
9071 && fieldcode == ARRAY_TYPE
9072 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9073 && string_flag)
9074 value.value = orig_value;
9075 /* Otherwise, if we have come to a subaggregate,
9076 and we don't have an element of its type, push into it. */
9077 else if (value.value != 0
9078 && value.value != error_mark_node
9079 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9080 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9081 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9083 push_init_level (loc, 1, braced_init_obstack);
9084 continue;
9087 if (value.value)
9089 push_member_name (constructor_fields);
9090 output_init_element (loc, value.value, value.original_type,
9091 strict_string, fieldtype,
9092 constructor_fields, 1, implicit,
9093 braced_init_obstack);
9094 RESTORE_SPELLING_DEPTH (constructor_depth);
9096 else
9097 /* Do the bookkeeping for an element that was
9098 directly output as a constructor. */
9100 constructor_bit_index = DECL_SIZE (constructor_fields);
9101 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9104 constructor_fields = 0;
9106 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9108 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9109 enum tree_code eltcode = TREE_CODE (elttype);
9111 /* Accept a string constant to initialize a subarray. */
9112 if (value.value != 0
9113 && eltcode == ARRAY_TYPE
9114 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9115 && string_flag)
9116 value.value = orig_value;
9117 /* Otherwise, if we have come to a subaggregate,
9118 and we don't have an element of its type, push into it. */
9119 else if (value.value != 0
9120 && value.value != error_mark_node
9121 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9122 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9123 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9125 push_init_level (loc, 1, braced_init_obstack);
9126 continue;
9129 if (constructor_max_index != 0
9130 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9131 || integer_all_onesp (constructor_max_index)))
9133 pedwarn_init (loc, 0,
9134 "excess elements in array initializer");
9135 break;
9138 /* Now output the actual element. */
9139 if (value.value)
9141 push_array_bounds (tree_to_uhwi (constructor_index));
9142 output_init_element (loc, value.value, value.original_type,
9143 strict_string, elttype,
9144 constructor_index, 1, implicit,
9145 braced_init_obstack);
9146 RESTORE_SPELLING_DEPTH (constructor_depth);
9149 constructor_index
9150 = size_binop_loc (input_location, PLUS_EXPR,
9151 constructor_index, bitsize_one_node);
9153 if (!value.value)
9154 /* If we are doing the bookkeeping for an element that was
9155 directly output as a constructor, we must update
9156 constructor_unfilled_index. */
9157 constructor_unfilled_index = constructor_index;
9159 else if (VECTOR_TYPE_P (constructor_type))
9161 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9163 /* Do a basic check of initializer size. Note that vectors
9164 always have a fixed size derived from their type. */
9165 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9167 pedwarn_init (loc, 0,
9168 "excess elements in vector initializer");
9169 break;
9172 /* Now output the actual element. */
9173 if (value.value)
9175 if (TREE_CODE (value.value) == VECTOR_CST)
9176 elttype = TYPE_MAIN_VARIANT (constructor_type);
9177 output_init_element (loc, value.value, value.original_type,
9178 strict_string, elttype,
9179 constructor_index, 1, implicit,
9180 braced_init_obstack);
9183 constructor_index
9184 = size_binop_loc (input_location,
9185 PLUS_EXPR, constructor_index, bitsize_one_node);
9187 if (!value.value)
9188 /* If we are doing the bookkeeping for an element that was
9189 directly output as a constructor, we must update
9190 constructor_unfilled_index. */
9191 constructor_unfilled_index = constructor_index;
9194 /* Handle the sole element allowed in a braced initializer
9195 for a scalar variable. */
9196 else if (constructor_type != error_mark_node
9197 && constructor_fields == 0)
9199 pedwarn_init (loc, 0,
9200 "excess elements in scalar initializer");
9201 break;
9203 else
9205 if (value.value)
9206 output_init_element (loc, value.value, value.original_type,
9207 strict_string, constructor_type,
9208 NULL_TREE, 1, implicit,
9209 braced_init_obstack);
9210 constructor_fields = 0;
9213 /* Handle range initializers either at this level or anywhere higher
9214 in the designator stack. */
9215 if (constructor_range_stack)
9217 struct constructor_range_stack *p, *range_stack;
9218 int finish = 0;
9220 range_stack = constructor_range_stack;
9221 constructor_range_stack = 0;
9222 while (constructor_stack != range_stack->stack)
9224 gcc_assert (constructor_stack->implicit);
9225 process_init_element (loc,
9226 pop_init_level (loc, 1,
9227 braced_init_obstack),
9228 true, braced_init_obstack);
9230 for (p = range_stack;
9231 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9232 p = p->prev)
9234 gcc_assert (constructor_stack->implicit);
9235 process_init_element (loc,
9236 pop_init_level (loc, 1,
9237 braced_init_obstack),
9238 true, braced_init_obstack);
9241 p->index = size_binop_loc (input_location,
9242 PLUS_EXPR, p->index, bitsize_one_node);
9243 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9244 finish = 1;
9246 while (1)
9248 constructor_index = p->index;
9249 constructor_fields = p->fields;
9250 if (finish && p->range_end && p->index == p->range_start)
9252 finish = 0;
9253 p->prev = 0;
9255 p = p->next;
9256 if (!p)
9257 break;
9258 push_init_level (loc, 2, braced_init_obstack);
9259 p->stack = constructor_stack;
9260 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9261 p->index = p->range_start;
9264 if (!finish)
9265 constructor_range_stack = range_stack;
9266 continue;
9269 break;
9272 constructor_range_stack = 0;
9275 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9276 (guaranteed to be 'volatile' or null) and ARGS (represented using
9277 an ASM_EXPR node). */
9278 tree
9279 build_asm_stmt (tree cv_qualifier, tree args)
9281 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9282 ASM_VOLATILE_P (args) = 1;
9283 return add_stmt (args);
9286 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9287 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9288 SIMPLE indicates whether there was anything at all after the
9289 string in the asm expression -- asm("blah") and asm("blah" : )
9290 are subtly different. We use a ASM_EXPR node to represent this. */
9291 tree
9292 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9293 tree clobbers, tree labels, bool simple)
9295 tree tail;
9296 tree args;
9297 int i;
9298 const char *constraint;
9299 const char **oconstraints;
9300 bool allows_mem, allows_reg, is_inout;
9301 int ninputs, noutputs;
9303 ninputs = list_length (inputs);
9304 noutputs = list_length (outputs);
9305 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9307 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9309 /* Remove output conversions that change the type but not the mode. */
9310 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9312 tree output = TREE_VALUE (tail);
9314 output = c_fully_fold (output, false, NULL);
9316 /* ??? Really, this should not be here. Users should be using a
9317 proper lvalue, dammit. But there's a long history of using casts
9318 in the output operands. In cases like longlong.h, this becomes a
9319 primitive form of typechecking -- if the cast can be removed, then
9320 the output operand had a type of the proper width; otherwise we'll
9321 get an error. Gross, but ... */
9322 STRIP_NOPS (output);
9324 if (!lvalue_or_else (loc, output, lv_asm))
9325 output = error_mark_node;
9327 if (output != error_mark_node
9328 && (TREE_READONLY (output)
9329 || TYPE_READONLY (TREE_TYPE (output))
9330 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9331 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9332 readonly_error (loc, output, lv_asm);
9334 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9335 oconstraints[i] = constraint;
9337 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9338 &allows_mem, &allows_reg, &is_inout))
9340 /* If the operand is going to end up in memory,
9341 mark it addressable. */
9342 if (!allows_reg && !c_mark_addressable (output))
9343 output = error_mark_node;
9344 if (!(!allows_reg && allows_mem)
9345 && output != error_mark_node
9346 && VOID_TYPE_P (TREE_TYPE (output)))
9348 error_at (loc, "invalid use of void expression");
9349 output = error_mark_node;
9352 else
9353 output = error_mark_node;
9355 TREE_VALUE (tail) = output;
9358 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9360 tree input;
9362 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9363 input = TREE_VALUE (tail);
9365 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9366 oconstraints, &allows_mem, &allows_reg))
9368 /* If the operand is going to end up in memory,
9369 mark it addressable. */
9370 if (!allows_reg && allows_mem)
9372 input = c_fully_fold (input, false, NULL);
9374 /* Strip the nops as we allow this case. FIXME, this really
9375 should be rejected or made deprecated. */
9376 STRIP_NOPS (input);
9377 if (!c_mark_addressable (input))
9378 input = error_mark_node;
9380 else
9382 struct c_expr expr;
9383 memset (&expr, 0, sizeof (expr));
9384 expr.value = input;
9385 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9386 input = c_fully_fold (expr.value, false, NULL);
9388 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9390 error_at (loc, "invalid use of void expression");
9391 input = error_mark_node;
9395 else
9396 input = error_mark_node;
9398 TREE_VALUE (tail) = input;
9401 /* ASMs with labels cannot have outputs. This should have been
9402 enforced by the parser. */
9403 gcc_assert (outputs == NULL || labels == NULL);
9405 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9407 /* asm statements without outputs, including simple ones, are treated
9408 as volatile. */
9409 ASM_INPUT_P (args) = simple;
9410 ASM_VOLATILE_P (args) = (noutputs == 0);
9412 return args;
9415 /* Generate a goto statement to LABEL. LOC is the location of the
9416 GOTO. */
9418 tree
9419 c_finish_goto_label (location_t loc, tree label)
9421 tree decl = lookup_label_for_goto (loc, label);
9422 if (!decl)
9423 return NULL_TREE;
9424 TREE_USED (decl) = 1;
9426 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9427 SET_EXPR_LOCATION (t, loc);
9428 return add_stmt (t);
9432 /* Generate a computed goto statement to EXPR. LOC is the location of
9433 the GOTO. */
9435 tree
9436 c_finish_goto_ptr (location_t loc, tree expr)
9438 tree t;
9439 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9440 expr = c_fully_fold (expr, false, NULL);
9441 expr = convert (ptr_type_node, expr);
9442 t = build1 (GOTO_EXPR, void_type_node, expr);
9443 SET_EXPR_LOCATION (t, loc);
9444 return add_stmt (t);
9447 /* Generate a C `return' statement. RETVAL is the expression for what
9448 to return, or a null pointer for `return;' with no value. LOC is
9449 the location of the return statement, or the location of the expression,
9450 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9451 is the original type of RETVAL. */
9453 tree
9454 c_finish_return (location_t loc, tree retval, tree origtype)
9456 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9457 bool no_warning = false;
9458 bool npc = false;
9459 size_t rank = 0;
9461 /* Use the expansion point to handle cases such as returning NULL
9462 in a function returning void. */
9463 source_location xloc = expansion_point_location_if_in_system_header (loc);
9465 if (TREE_THIS_VOLATILE (current_function_decl))
9466 warning_at (xloc, 0,
9467 "function declared %<noreturn%> has a %<return%> statement");
9469 if (flag_cilkplus && contains_array_notation_expr (retval))
9471 /* Array notations are allowed in a return statement if it is inside a
9472 built-in array notation reduction function. */
9473 if (!find_rank (loc, retval, retval, false, &rank))
9474 return error_mark_node;
9475 if (rank >= 1)
9477 error_at (loc, "array notation expression cannot be used as a "
9478 "return value");
9479 return error_mark_node;
9482 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9484 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9485 "allowed");
9486 return error_mark_node;
9488 if (retval)
9490 tree semantic_type = NULL_TREE;
9491 npc = null_pointer_constant_p (retval);
9492 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9494 semantic_type = TREE_TYPE (retval);
9495 retval = TREE_OPERAND (retval, 0);
9497 retval = c_fully_fold (retval, false, NULL);
9498 if (semantic_type)
9499 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9502 if (!retval)
9504 current_function_returns_null = 1;
9505 if ((warn_return_type || flag_isoc99)
9506 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9508 if (flag_isoc99)
9509 pedwarn (loc, 0, "%<return%> with no value, in "
9510 "function returning non-void");
9511 else
9512 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9513 "in function returning non-void");
9514 no_warning = true;
9517 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9519 current_function_returns_null = 1;
9520 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9521 pedwarn (xloc, 0,
9522 "%<return%> with a value, in function returning void");
9523 else
9524 pedwarn (xloc, OPT_Wpedantic, "ISO C forbids "
9525 "%<return%> with expression, in function returning void");
9527 else
9529 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9530 retval, origtype, ic_return,
9531 npc, NULL_TREE, NULL_TREE, 0);
9532 tree res = DECL_RESULT (current_function_decl);
9533 tree inner;
9534 bool save;
9536 current_function_returns_value = 1;
9537 if (t == error_mark_node)
9538 return NULL_TREE;
9540 save = in_late_binary_op;
9541 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9542 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9543 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9544 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9545 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9546 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9547 in_late_binary_op = true;
9548 inner = t = convert (TREE_TYPE (res), t);
9549 in_late_binary_op = save;
9551 /* Strip any conversions, additions, and subtractions, and see if
9552 we are returning the address of a local variable. Warn if so. */
9553 while (1)
9555 switch (TREE_CODE (inner))
9557 CASE_CONVERT:
9558 case NON_LVALUE_EXPR:
9559 case PLUS_EXPR:
9560 case POINTER_PLUS_EXPR:
9561 inner = TREE_OPERAND (inner, 0);
9562 continue;
9564 case MINUS_EXPR:
9565 /* If the second operand of the MINUS_EXPR has a pointer
9566 type (or is converted from it), this may be valid, so
9567 don't give a warning. */
9569 tree op1 = TREE_OPERAND (inner, 1);
9571 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9572 && (CONVERT_EXPR_P (op1)
9573 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9574 op1 = TREE_OPERAND (op1, 0);
9576 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9577 break;
9579 inner = TREE_OPERAND (inner, 0);
9580 continue;
9583 case ADDR_EXPR:
9584 inner = TREE_OPERAND (inner, 0);
9586 while (REFERENCE_CLASS_P (inner)
9587 && !INDIRECT_REF_P (inner))
9588 inner = TREE_OPERAND (inner, 0);
9590 if (DECL_P (inner)
9591 && !DECL_EXTERNAL (inner)
9592 && !TREE_STATIC (inner)
9593 && DECL_CONTEXT (inner) == current_function_decl)
9595 if (TREE_CODE (inner) == LABEL_DECL)
9596 warning_at (loc, OPT_Wreturn_local_addr,
9597 "function returns address of label");
9598 else
9600 warning_at (loc, OPT_Wreturn_local_addr,
9601 "function returns address of local variable");
9602 tree zero = build_zero_cst (TREE_TYPE (res));
9603 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9606 break;
9608 default:
9609 break;
9612 break;
9615 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9616 SET_EXPR_LOCATION (retval, loc);
9618 if (warn_sequence_point)
9619 verify_sequence_points (retval);
9622 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9623 TREE_NO_WARNING (ret_stmt) |= no_warning;
9624 return add_stmt (ret_stmt);
9627 struct c_switch {
9628 /* The SWITCH_EXPR being built. */
9629 tree switch_expr;
9631 /* The original type of the testing expression, i.e. before the
9632 default conversion is applied. */
9633 tree orig_type;
9635 /* A splay-tree mapping the low element of a case range to the high
9636 element, or NULL_TREE if there is no high element. Used to
9637 determine whether or not a new case label duplicates an old case
9638 label. We need a tree, rather than simply a hash table, because
9639 of the GNU case range extension. */
9640 splay_tree cases;
9642 /* The bindings at the point of the switch. This is used for
9643 warnings crossing decls when branching to a case label. */
9644 struct c_spot_bindings *bindings;
9646 /* The next node on the stack. */
9647 struct c_switch *next;
9649 /* Remember whether the controlling expression had boolean type
9650 before integer promotions for the sake of -Wswitch-bool. */
9651 bool bool_cond_p;
9653 /* Remember whether there was a case value that is outside the
9654 range of the ORIG_TYPE. */
9655 bool outside_range_p;
9658 /* A stack of the currently active switch statements. The innermost
9659 switch statement is on the top of the stack. There is no need to
9660 mark the stack for garbage collection because it is only active
9661 during the processing of the body of a function, and we never
9662 collect at that point. */
9664 struct c_switch *c_switch_stack;
9666 /* Start a C switch statement, testing expression EXP. Return the new
9667 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9668 SWITCH_COND_LOC is the location of the switch's condition.
9669 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9671 tree
9672 c_start_case (location_t switch_loc,
9673 location_t switch_cond_loc,
9674 tree exp, bool explicit_cast_p)
9676 tree orig_type = error_mark_node;
9677 bool bool_cond_p = false;
9678 struct c_switch *cs;
9680 if (exp != error_mark_node)
9682 orig_type = TREE_TYPE (exp);
9684 if (!INTEGRAL_TYPE_P (orig_type))
9686 if (orig_type != error_mark_node)
9688 error_at (switch_cond_loc, "switch quantity not an integer");
9689 orig_type = error_mark_node;
9691 exp = integer_zero_node;
9693 else
9695 tree type = TYPE_MAIN_VARIANT (orig_type);
9696 tree e = exp;
9698 /* Warn if the condition has boolean value. */
9699 while (TREE_CODE (e) == COMPOUND_EXPR)
9700 e = TREE_OPERAND (e, 1);
9702 if ((TREE_CODE (type) == BOOLEAN_TYPE
9703 || truth_value_p (TREE_CODE (e)))
9704 /* Explicit cast to int suppresses this warning. */
9705 && !(TREE_CODE (type) == INTEGER_TYPE
9706 && explicit_cast_p))
9707 bool_cond_p = true;
9709 if (!in_system_header_at (input_location)
9710 && (type == long_integer_type_node
9711 || type == long_unsigned_type_node))
9712 warning_at (switch_cond_loc,
9713 OPT_Wtraditional, "%<long%> switch expression not "
9714 "converted to %<int%> in ISO C");
9716 exp = c_fully_fold (exp, false, NULL);
9717 exp = default_conversion (exp);
9719 if (warn_sequence_point)
9720 verify_sequence_points (exp);
9724 /* Add this new SWITCH_EXPR to the stack. */
9725 cs = XNEW (struct c_switch);
9726 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9727 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9728 cs->orig_type = orig_type;
9729 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9730 cs->bindings = c_get_switch_bindings ();
9731 cs->bool_cond_p = bool_cond_p;
9732 cs->outside_range_p = false;
9733 cs->next = c_switch_stack;
9734 c_switch_stack = cs;
9736 return add_stmt (cs->switch_expr);
9739 /* Process a case label at location LOC. */
9741 tree
9742 do_case (location_t loc, tree low_value, tree high_value)
9744 tree label = NULL_TREE;
9746 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9748 low_value = c_fully_fold (low_value, false, NULL);
9749 if (TREE_CODE (low_value) == INTEGER_CST)
9750 pedwarn (loc, OPT_Wpedantic,
9751 "case label is not an integer constant expression");
9754 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9756 high_value = c_fully_fold (high_value, false, NULL);
9757 if (TREE_CODE (high_value) == INTEGER_CST)
9758 pedwarn (input_location, OPT_Wpedantic,
9759 "case label is not an integer constant expression");
9762 if (c_switch_stack == NULL)
9764 if (low_value)
9765 error_at (loc, "case label not within a switch statement");
9766 else
9767 error_at (loc, "%<default%> label not within a switch statement");
9768 return NULL_TREE;
9771 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9772 EXPR_LOCATION (c_switch_stack->switch_expr),
9773 loc))
9774 return NULL_TREE;
9776 label = c_add_case_label (loc, c_switch_stack->cases,
9777 SWITCH_COND (c_switch_stack->switch_expr),
9778 c_switch_stack->orig_type,
9779 low_value, high_value,
9780 &c_switch_stack->outside_range_p);
9781 if (label == error_mark_node)
9782 label = NULL_TREE;
9783 return label;
9786 /* Finish the switch statement. TYPE is the original type of the
9787 controlling expression of the switch, or NULL_TREE. */
9789 void
9790 c_finish_case (tree body, tree type)
9792 struct c_switch *cs = c_switch_stack;
9793 location_t switch_location;
9795 SWITCH_BODY (cs->switch_expr) = body;
9797 /* Emit warnings as needed. */
9798 switch_location = EXPR_LOCATION (cs->switch_expr);
9799 c_do_switch_warnings (cs->cases, switch_location,
9800 type ? type : TREE_TYPE (cs->switch_expr),
9801 SWITCH_COND (cs->switch_expr),
9802 cs->bool_cond_p, cs->outside_range_p);
9804 /* Pop the stack. */
9805 c_switch_stack = cs->next;
9806 splay_tree_delete (cs->cases);
9807 c_release_switch_bindings (cs->bindings);
9808 XDELETE (cs);
9811 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9812 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9813 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9814 statement, and was not surrounded with parenthesis. */
9816 void
9817 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9818 tree else_block, bool nested_if)
9820 tree stmt;
9822 /* If the condition has array notations, then the rank of the then_block and
9823 else_block must be either 0 or be equal to the rank of the condition. If
9824 the condition does not have array notations then break them up as it is
9825 broken up in a normal expression. */
9826 if (flag_cilkplus && contains_array_notation_expr (cond))
9828 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9829 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9830 return;
9831 if (then_block
9832 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9833 return;
9834 if (else_block
9835 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9836 return;
9837 if (cond_rank != then_rank && then_rank != 0)
9839 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9840 " and the then-block");
9841 return;
9843 else if (cond_rank != else_rank && else_rank != 0)
9845 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9846 " and the else-block");
9847 return;
9850 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9851 if (warn_parentheses && nested_if && else_block == NULL)
9853 tree inner_if = then_block;
9855 /* We know from the grammar productions that there is an IF nested
9856 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9857 it might not be exactly THEN_BLOCK, but should be the last
9858 non-container statement within. */
9859 while (1)
9860 switch (TREE_CODE (inner_if))
9862 case COND_EXPR:
9863 goto found;
9864 case BIND_EXPR:
9865 inner_if = BIND_EXPR_BODY (inner_if);
9866 break;
9867 case STATEMENT_LIST:
9868 inner_if = expr_last (then_block);
9869 break;
9870 case TRY_FINALLY_EXPR:
9871 case TRY_CATCH_EXPR:
9872 inner_if = TREE_OPERAND (inner_if, 0);
9873 break;
9874 default:
9875 gcc_unreachable ();
9877 found:
9879 if (COND_EXPR_ELSE (inner_if))
9880 warning_at (if_locus, OPT_Wparentheses,
9881 "suggest explicit braces to avoid ambiguous %<else%>");
9884 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9885 SET_EXPR_LOCATION (stmt, if_locus);
9886 add_stmt (stmt);
9889 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9890 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9891 is false for DO loops. INCR is the FOR increment expression. BODY is
9892 the statement controlled by the loop. BLAB is the break label. CLAB is
9893 the continue label. Everything is allowed to be NULL. */
9895 void
9896 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9897 tree blab, tree clab, bool cond_is_first)
9899 tree entry = NULL, exit = NULL, t;
9901 /* In theory could forbid cilk spawn for loop increment expression,
9902 but it should work just fine. */
9904 /* If the condition is zero don't generate a loop construct. */
9905 if (cond && integer_zerop (cond))
9907 if (cond_is_first)
9909 t = build_and_jump (&blab);
9910 SET_EXPR_LOCATION (t, start_locus);
9911 add_stmt (t);
9914 else
9916 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9918 /* If we have an exit condition, then we build an IF with gotos either
9919 out of the loop, or to the top of it. If there's no exit condition,
9920 then we just build a jump back to the top. */
9921 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9923 if (cond && !integer_nonzerop (cond))
9925 /* Canonicalize the loop condition to the end. This means
9926 generating a branch to the loop condition. Reuse the
9927 continue label, if possible. */
9928 if (cond_is_first)
9930 if (incr || !clab)
9932 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9933 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9935 else
9936 t = build1 (GOTO_EXPR, void_type_node, clab);
9937 SET_EXPR_LOCATION (t, start_locus);
9938 add_stmt (t);
9941 t = build_and_jump (&blab);
9942 if (cond_is_first)
9943 exit = fold_build3_loc (start_locus,
9944 COND_EXPR, void_type_node, cond, exit, t);
9945 else
9946 exit = fold_build3_loc (input_location,
9947 COND_EXPR, void_type_node, cond, exit, t);
9949 else
9951 /* For the backward-goto's location of an unconditional loop
9952 use the beginning of the body, or, if there is none, the
9953 top of the loop. */
9954 location_t loc = EXPR_LOCATION (expr_first (body));
9955 if (loc == UNKNOWN_LOCATION)
9956 loc = start_locus;
9957 SET_EXPR_LOCATION (exit, loc);
9960 add_stmt (top);
9963 if (body)
9964 add_stmt (body);
9965 if (clab)
9966 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9967 if (incr)
9968 add_stmt (incr);
9969 if (entry)
9970 add_stmt (entry);
9971 if (exit)
9972 add_stmt (exit);
9973 if (blab)
9974 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9977 tree
9978 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9980 bool skip;
9981 tree label = *label_p;
9983 /* In switch statements break is sometimes stylistically used after
9984 a return statement. This can lead to spurious warnings about
9985 control reaching the end of a non-void function when it is
9986 inlined. Note that we are calling block_may_fallthru with
9987 language specific tree nodes; this works because
9988 block_may_fallthru returns true when given something it does not
9989 understand. */
9990 skip = !block_may_fallthru (cur_stmt_list);
9992 if (!label)
9994 if (!skip)
9995 *label_p = label = create_artificial_label (loc);
9997 else if (TREE_CODE (label) == LABEL_DECL)
9999 else switch (TREE_INT_CST_LOW (label))
10001 case 0:
10002 if (is_break)
10003 error_at (loc, "break statement not within loop or switch");
10004 else
10005 error_at (loc, "continue statement not within a loop");
10006 return NULL_TREE;
10008 case 1:
10009 gcc_assert (is_break);
10010 error_at (loc, "break statement used with OpenMP for loop");
10011 return NULL_TREE;
10013 case 2:
10014 if (is_break)
10015 error ("break statement within %<#pragma simd%> loop body");
10016 else
10017 error ("continue statement within %<#pragma simd%> loop body");
10018 return NULL_TREE;
10020 default:
10021 gcc_unreachable ();
10024 if (skip)
10025 return NULL_TREE;
10027 if (!is_break)
10028 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10030 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10033 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10035 static void
10036 emit_side_effect_warnings (location_t loc, tree expr)
10038 if (expr == error_mark_node)
10040 else if (!TREE_SIDE_EFFECTS (expr))
10042 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10043 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10045 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10047 tree r = expr;
10048 location_t cloc = loc;
10049 while (TREE_CODE (r) == COMPOUND_EXPR)
10051 if (EXPR_HAS_LOCATION (r))
10052 cloc = EXPR_LOCATION (r);
10053 r = TREE_OPERAND (r, 1);
10055 if (!TREE_SIDE_EFFECTS (r)
10056 && !VOID_TYPE_P (TREE_TYPE (r))
10057 && !CONVERT_EXPR_P (r)
10058 && !TREE_NO_WARNING (r)
10059 && !TREE_NO_WARNING (expr))
10060 warning_at (cloc, OPT_Wunused_value,
10061 "right-hand operand of comma expression has no effect");
10063 else
10064 warn_if_unused_value (expr, loc);
10067 /* Process an expression as if it were a complete statement. Emit
10068 diagnostics, but do not call ADD_STMT. LOC is the location of the
10069 statement. */
10071 tree
10072 c_process_expr_stmt (location_t loc, tree expr)
10074 tree exprv;
10076 if (!expr)
10077 return NULL_TREE;
10079 expr = c_fully_fold (expr, false, NULL);
10081 if (warn_sequence_point)
10082 verify_sequence_points (expr);
10084 if (TREE_TYPE (expr) != error_mark_node
10085 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10086 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10087 error_at (loc, "expression statement has incomplete type");
10089 /* If we're not processing a statement expression, warn about unused values.
10090 Warnings for statement expressions will be emitted later, once we figure
10091 out which is the result. */
10092 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10093 && warn_unused_value)
10094 emit_side_effect_warnings (loc, expr);
10096 exprv = expr;
10097 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10098 exprv = TREE_OPERAND (exprv, 1);
10099 while (CONVERT_EXPR_P (exprv))
10100 exprv = TREE_OPERAND (exprv, 0);
10101 if (DECL_P (exprv)
10102 || handled_component_p (exprv)
10103 || TREE_CODE (exprv) == ADDR_EXPR)
10104 mark_exp_read (exprv);
10106 /* If the expression is not of a type to which we cannot assign a line
10107 number, wrap the thing in a no-op NOP_EXPR. */
10108 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10110 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10111 SET_EXPR_LOCATION (expr, loc);
10114 return expr;
10117 /* Emit an expression as a statement. LOC is the location of the
10118 expression. */
10120 tree
10121 c_finish_expr_stmt (location_t loc, tree expr)
10123 if (expr)
10124 return add_stmt (c_process_expr_stmt (loc, expr));
10125 else
10126 return NULL;
10129 /* Do the opposite and emit a statement as an expression. To begin,
10130 create a new binding level and return it. */
10132 tree
10133 c_begin_stmt_expr (void)
10135 tree ret;
10137 /* We must force a BLOCK for this level so that, if it is not expanded
10138 later, there is a way to turn off the entire subtree of blocks that
10139 are contained in it. */
10140 keep_next_level ();
10141 ret = c_begin_compound_stmt (true);
10143 c_bindings_start_stmt_expr (c_switch_stack == NULL
10144 ? NULL
10145 : c_switch_stack->bindings);
10147 /* Mark the current statement list as belonging to a statement list. */
10148 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10150 return ret;
10153 /* LOC is the location of the compound statement to which this body
10154 belongs. */
10156 tree
10157 c_finish_stmt_expr (location_t loc, tree body)
10159 tree last, type, tmp, val;
10160 tree *last_p;
10162 body = c_end_compound_stmt (loc, body, true);
10164 c_bindings_end_stmt_expr (c_switch_stack == NULL
10165 ? NULL
10166 : c_switch_stack->bindings);
10168 /* Locate the last statement in BODY. See c_end_compound_stmt
10169 about always returning a BIND_EXPR. */
10170 last_p = &BIND_EXPR_BODY (body);
10171 last = BIND_EXPR_BODY (body);
10173 continue_searching:
10174 if (TREE_CODE (last) == STATEMENT_LIST)
10176 tree_stmt_iterator i;
10178 /* This can happen with degenerate cases like ({ }). No value. */
10179 if (!TREE_SIDE_EFFECTS (last))
10180 return body;
10182 /* If we're supposed to generate side effects warnings, process
10183 all of the statements except the last. */
10184 if (warn_unused_value)
10186 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10188 location_t tloc;
10189 tree t = tsi_stmt (i);
10191 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10192 emit_side_effect_warnings (tloc, t);
10195 else
10196 i = tsi_last (last);
10197 last_p = tsi_stmt_ptr (i);
10198 last = *last_p;
10201 /* If the end of the list is exception related, then the list was split
10202 by a call to push_cleanup. Continue searching. */
10203 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10204 || TREE_CODE (last) == TRY_CATCH_EXPR)
10206 last_p = &TREE_OPERAND (last, 0);
10207 last = *last_p;
10208 goto continue_searching;
10211 if (last == error_mark_node)
10212 return last;
10214 /* In the case that the BIND_EXPR is not necessary, return the
10215 expression out from inside it. */
10216 if (last == BIND_EXPR_BODY (body)
10217 && BIND_EXPR_VARS (body) == NULL)
10219 /* Even if this looks constant, do not allow it in a constant
10220 expression. */
10221 last = c_wrap_maybe_const (last, true);
10222 /* Do not warn if the return value of a statement expression is
10223 unused. */
10224 TREE_NO_WARNING (last) = 1;
10225 return last;
10228 /* Extract the type of said expression. */
10229 type = TREE_TYPE (last);
10231 /* If we're not returning a value at all, then the BIND_EXPR that
10232 we already have is a fine expression to return. */
10233 if (!type || VOID_TYPE_P (type))
10234 return body;
10236 /* Now that we've located the expression containing the value, it seems
10237 silly to make voidify_wrapper_expr repeat the process. Create a
10238 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10239 tmp = create_tmp_var_raw (type);
10241 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10242 tree_expr_nonnegative_p giving up immediately. */
10243 val = last;
10244 if (TREE_CODE (val) == NOP_EXPR
10245 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10246 val = TREE_OPERAND (val, 0);
10248 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10249 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10252 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10253 SET_EXPR_LOCATION (t, loc);
10254 return t;
10258 /* Begin and end compound statements. This is as simple as pushing
10259 and popping new statement lists from the tree. */
10261 tree
10262 c_begin_compound_stmt (bool do_scope)
10264 tree stmt = push_stmt_list ();
10265 if (do_scope)
10266 push_scope ();
10267 return stmt;
10270 /* End a compound statement. STMT is the statement. LOC is the
10271 location of the compound statement-- this is usually the location
10272 of the opening brace. */
10274 tree
10275 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10277 tree block = NULL;
10279 if (do_scope)
10281 if (c_dialect_objc ())
10282 objc_clear_super_receiver ();
10283 block = pop_scope ();
10286 stmt = pop_stmt_list (stmt);
10287 stmt = c_build_bind_expr (loc, block, stmt);
10289 /* If this compound statement is nested immediately inside a statement
10290 expression, then force a BIND_EXPR to be created. Otherwise we'll
10291 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10292 STATEMENT_LISTs merge, and thus we can lose track of what statement
10293 was really last. */
10294 if (building_stmt_list_p ()
10295 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10296 && TREE_CODE (stmt) != BIND_EXPR)
10298 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10299 TREE_SIDE_EFFECTS (stmt) = 1;
10300 SET_EXPR_LOCATION (stmt, loc);
10303 return stmt;
10306 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10307 when the current scope is exited. EH_ONLY is true when this is not
10308 meant to apply to normal control flow transfer. */
10310 void
10311 push_cleanup (tree decl, tree cleanup, bool eh_only)
10313 enum tree_code code;
10314 tree stmt, list;
10315 bool stmt_expr;
10317 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10318 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10319 add_stmt (stmt);
10320 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10321 list = push_stmt_list ();
10322 TREE_OPERAND (stmt, 0) = list;
10323 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10326 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10327 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10329 static tree
10330 build_vec_cmp (tree_code code, tree type,
10331 tree arg0, tree arg1)
10333 tree zero_vec = build_zero_cst (type);
10334 tree minus_one_vec = build_minus_one_cst (type);
10335 tree cmp_type = build_same_sized_truth_vector_type (type);
10336 tree cmp = build2 (code, cmp_type, arg0, arg1);
10337 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10340 /* Build a binary-operation expression without default conversions.
10341 CODE is the kind of expression to build.
10342 LOCATION is the operator's location.
10343 This function differs from `build' in several ways:
10344 the data type of the result is computed and recorded in it,
10345 warnings are generated if arg data types are invalid,
10346 special handling for addition and subtraction of pointers is known,
10347 and some optimization is done (operations on narrow ints
10348 are done in the narrower type when that gives the same result).
10349 Constant folding is also done before the result is returned.
10351 Note that the operands will never have enumeral types, or function
10352 or array types, because either they will have the default conversions
10353 performed or they have both just been converted to some other type in which
10354 the arithmetic is to be done. */
10356 tree
10357 build_binary_op (location_t location, enum tree_code code,
10358 tree orig_op0, tree orig_op1, int convert_p)
10360 tree type0, type1, orig_type0, orig_type1;
10361 tree eptype;
10362 enum tree_code code0, code1;
10363 tree op0, op1;
10364 tree ret = error_mark_node;
10365 const char *invalid_op_diag;
10366 bool op0_int_operands, op1_int_operands;
10367 bool int_const, int_const_or_overflow, int_operands;
10369 /* Expression code to give to the expression when it is built.
10370 Normally this is CODE, which is what the caller asked for,
10371 but in some special cases we change it. */
10372 enum tree_code resultcode = code;
10374 /* Data type in which the computation is to be performed.
10375 In the simplest cases this is the common type of the arguments. */
10376 tree result_type = NULL;
10378 /* When the computation is in excess precision, the type of the
10379 final EXCESS_PRECISION_EXPR. */
10380 tree semantic_result_type = NULL;
10382 /* Nonzero means operands have already been type-converted
10383 in whatever way is necessary.
10384 Zero means they need to be converted to RESULT_TYPE. */
10385 int converted = 0;
10387 /* Nonzero means create the expression with this type, rather than
10388 RESULT_TYPE. */
10389 tree build_type = 0;
10391 /* Nonzero means after finally constructing the expression
10392 convert it to this type. */
10393 tree final_type = 0;
10395 /* Nonzero if this is an operation like MIN or MAX which can
10396 safely be computed in short if both args are promoted shorts.
10397 Also implies COMMON.
10398 -1 indicates a bitwise operation; this makes a difference
10399 in the exact conditions for when it is safe to do the operation
10400 in a narrower mode. */
10401 int shorten = 0;
10403 /* Nonzero if this is a comparison operation;
10404 if both args are promoted shorts, compare the original shorts.
10405 Also implies COMMON. */
10406 int short_compare = 0;
10408 /* Nonzero if this is a right-shift operation, which can be computed on the
10409 original short and then promoted if the operand is a promoted short. */
10410 int short_shift = 0;
10412 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10413 int common = 0;
10415 /* True means types are compatible as far as ObjC is concerned. */
10416 bool objc_ok;
10418 /* True means this is an arithmetic operation that may need excess
10419 precision. */
10420 bool may_need_excess_precision;
10422 /* True means this is a boolean operation that converts both its
10423 operands to truth-values. */
10424 bool boolean_op = false;
10426 /* Remember whether we're doing / or %. */
10427 bool doing_div_or_mod = false;
10429 /* Remember whether we're doing << or >>. */
10430 bool doing_shift = false;
10432 /* Tree holding instrumentation expression. */
10433 tree instrument_expr = NULL;
10435 if (location == UNKNOWN_LOCATION)
10436 location = input_location;
10438 op0 = orig_op0;
10439 op1 = orig_op1;
10441 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10442 if (op0_int_operands)
10443 op0 = remove_c_maybe_const_expr (op0);
10444 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10445 if (op1_int_operands)
10446 op1 = remove_c_maybe_const_expr (op1);
10447 int_operands = (op0_int_operands && op1_int_operands);
10448 if (int_operands)
10450 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10451 && TREE_CODE (orig_op1) == INTEGER_CST);
10452 int_const = (int_const_or_overflow
10453 && !TREE_OVERFLOW (orig_op0)
10454 && !TREE_OVERFLOW (orig_op1));
10456 else
10457 int_const = int_const_or_overflow = false;
10459 /* Do not apply default conversion in mixed vector/scalar expression. */
10460 if (convert_p
10461 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10463 op0 = default_conversion (op0);
10464 op1 = default_conversion (op1);
10467 /* When Cilk Plus is enabled and there are array notations inside op0, then
10468 we check to see if there are builtin array notation functions. If
10469 so, then we take on the type of the array notation inside it. */
10470 if (flag_cilkplus && contains_array_notation_expr (op0))
10471 orig_type0 = type0 = find_correct_array_notation_type (op0);
10472 else
10473 orig_type0 = type0 = TREE_TYPE (op0);
10475 if (flag_cilkplus && contains_array_notation_expr (op1))
10476 orig_type1 = type1 = find_correct_array_notation_type (op1);
10477 else
10478 orig_type1 = type1 = TREE_TYPE (op1);
10480 /* The expression codes of the data types of the arguments tell us
10481 whether the arguments are integers, floating, pointers, etc. */
10482 code0 = TREE_CODE (type0);
10483 code1 = TREE_CODE (type1);
10485 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10486 STRIP_TYPE_NOPS (op0);
10487 STRIP_TYPE_NOPS (op1);
10489 /* If an error was already reported for one of the arguments,
10490 avoid reporting another error. */
10492 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10493 return error_mark_node;
10495 if (code0 == POINTER_TYPE
10496 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10497 return error_mark_node;
10499 if (code1 == POINTER_TYPE
10500 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10501 return error_mark_node;
10503 if ((invalid_op_diag
10504 = targetm.invalid_binary_op (code, type0, type1)))
10506 error_at (location, invalid_op_diag);
10507 return error_mark_node;
10510 switch (code)
10512 case PLUS_EXPR:
10513 case MINUS_EXPR:
10514 case MULT_EXPR:
10515 case TRUNC_DIV_EXPR:
10516 case CEIL_DIV_EXPR:
10517 case FLOOR_DIV_EXPR:
10518 case ROUND_DIV_EXPR:
10519 case EXACT_DIV_EXPR:
10520 may_need_excess_precision = true;
10521 break;
10522 default:
10523 may_need_excess_precision = false;
10524 break;
10526 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10528 op0 = TREE_OPERAND (op0, 0);
10529 type0 = TREE_TYPE (op0);
10531 else if (may_need_excess_precision
10532 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10534 type0 = eptype;
10535 op0 = convert (eptype, op0);
10537 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10539 op1 = TREE_OPERAND (op1, 0);
10540 type1 = TREE_TYPE (op1);
10542 else if (may_need_excess_precision
10543 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10545 type1 = eptype;
10546 op1 = convert (eptype, op1);
10549 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10551 /* In case when one of the operands of the binary operation is
10552 a vector and another is a scalar -- convert scalar to vector. */
10553 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10555 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10556 true);
10558 switch (convert_flag)
10560 case stv_error:
10561 return error_mark_node;
10562 case stv_firstarg:
10564 bool maybe_const = true;
10565 tree sc;
10566 sc = c_fully_fold (op0, false, &maybe_const);
10567 sc = save_expr (sc);
10568 sc = convert (TREE_TYPE (type1), sc);
10569 op0 = build_vector_from_val (type1, sc);
10570 if (!maybe_const)
10571 op0 = c_wrap_maybe_const (op0, true);
10572 orig_type0 = type0 = TREE_TYPE (op0);
10573 code0 = TREE_CODE (type0);
10574 converted = 1;
10575 break;
10577 case stv_secondarg:
10579 bool maybe_const = true;
10580 tree sc;
10581 sc = c_fully_fold (op1, false, &maybe_const);
10582 sc = save_expr (sc);
10583 sc = convert (TREE_TYPE (type0), sc);
10584 op1 = build_vector_from_val (type0, sc);
10585 if (!maybe_const)
10586 op1 = c_wrap_maybe_const (op1, true);
10587 orig_type1 = type1 = TREE_TYPE (op1);
10588 code1 = TREE_CODE (type1);
10589 converted = 1;
10590 break;
10592 default:
10593 break;
10597 switch (code)
10599 case PLUS_EXPR:
10600 /* Handle the pointer + int case. */
10601 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10603 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10604 goto return_build_binary_op;
10606 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10608 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10609 goto return_build_binary_op;
10611 else
10612 common = 1;
10613 break;
10615 case MINUS_EXPR:
10616 /* Subtraction of two similar pointers.
10617 We must subtract them as integers, then divide by object size. */
10618 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10619 && comp_target_types (location, type0, type1))
10621 ret = pointer_diff (location, op0, op1);
10622 goto return_build_binary_op;
10624 /* Handle pointer minus int. Just like pointer plus int. */
10625 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10627 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10628 goto return_build_binary_op;
10630 else
10631 common = 1;
10632 break;
10634 case MULT_EXPR:
10635 common = 1;
10636 break;
10638 case TRUNC_DIV_EXPR:
10639 case CEIL_DIV_EXPR:
10640 case FLOOR_DIV_EXPR:
10641 case ROUND_DIV_EXPR:
10642 case EXACT_DIV_EXPR:
10643 doing_div_or_mod = true;
10644 warn_for_div_by_zero (location, op1);
10646 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10647 || code0 == FIXED_POINT_TYPE
10648 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10649 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10650 || code1 == FIXED_POINT_TYPE
10651 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10653 enum tree_code tcode0 = code0, tcode1 = code1;
10655 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10656 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10657 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10658 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10660 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10661 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10662 resultcode = RDIV_EXPR;
10663 else
10664 /* Although it would be tempting to shorten always here, that
10665 loses on some targets, since the modulo instruction is
10666 undefined if the quotient can't be represented in the
10667 computation mode. We shorten only if unsigned or if
10668 dividing by something we know != -1. */
10669 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10670 || (TREE_CODE (op1) == INTEGER_CST
10671 && !integer_all_onesp (op1)));
10672 common = 1;
10674 break;
10676 case BIT_AND_EXPR:
10677 case BIT_IOR_EXPR:
10678 case BIT_XOR_EXPR:
10679 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10680 shorten = -1;
10681 /* Allow vector types which are not floating point types. */
10682 else if (code0 == VECTOR_TYPE
10683 && code1 == VECTOR_TYPE
10684 && !VECTOR_FLOAT_TYPE_P (type0)
10685 && !VECTOR_FLOAT_TYPE_P (type1))
10686 common = 1;
10687 break;
10689 case TRUNC_MOD_EXPR:
10690 case FLOOR_MOD_EXPR:
10691 doing_div_or_mod = true;
10692 warn_for_div_by_zero (location, op1);
10694 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10695 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10696 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10697 common = 1;
10698 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10700 /* Although it would be tempting to shorten always here, that loses
10701 on some targets, since the modulo instruction is undefined if the
10702 quotient can't be represented in the computation mode. We shorten
10703 only if unsigned or if dividing by something we know != -1. */
10704 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10705 || (TREE_CODE (op1) == INTEGER_CST
10706 && !integer_all_onesp (op1)));
10707 common = 1;
10709 break;
10711 case TRUTH_ANDIF_EXPR:
10712 case TRUTH_ORIF_EXPR:
10713 case TRUTH_AND_EXPR:
10714 case TRUTH_OR_EXPR:
10715 case TRUTH_XOR_EXPR:
10716 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10717 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10718 || code0 == FIXED_POINT_TYPE)
10719 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10720 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10721 || code1 == FIXED_POINT_TYPE))
10723 /* Result of these operations is always an int,
10724 but that does not mean the operands should be
10725 converted to ints! */
10726 result_type = integer_type_node;
10727 if (op0_int_operands)
10729 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10730 op0 = remove_c_maybe_const_expr (op0);
10732 else
10733 op0 = c_objc_common_truthvalue_conversion (location, op0);
10734 if (op1_int_operands)
10736 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10737 op1 = remove_c_maybe_const_expr (op1);
10739 else
10740 op1 = c_objc_common_truthvalue_conversion (location, op1);
10741 converted = 1;
10742 boolean_op = true;
10744 if (code == TRUTH_ANDIF_EXPR)
10746 int_const_or_overflow = (int_operands
10747 && TREE_CODE (orig_op0) == INTEGER_CST
10748 && (op0 == truthvalue_false_node
10749 || TREE_CODE (orig_op1) == INTEGER_CST));
10750 int_const = (int_const_or_overflow
10751 && !TREE_OVERFLOW (orig_op0)
10752 && (op0 == truthvalue_false_node
10753 || !TREE_OVERFLOW (orig_op1)));
10755 else if (code == TRUTH_ORIF_EXPR)
10757 int_const_or_overflow = (int_operands
10758 && TREE_CODE (orig_op0) == INTEGER_CST
10759 && (op0 == truthvalue_true_node
10760 || TREE_CODE (orig_op1) == INTEGER_CST));
10761 int_const = (int_const_or_overflow
10762 && !TREE_OVERFLOW (orig_op0)
10763 && (op0 == truthvalue_true_node
10764 || !TREE_OVERFLOW (orig_op1)));
10766 break;
10768 /* Shift operations: result has same type as first operand;
10769 always convert second operand to int.
10770 Also set SHORT_SHIFT if shifting rightward. */
10772 case RSHIFT_EXPR:
10773 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10774 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10776 result_type = type0;
10777 converted = 1;
10779 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10780 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10781 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10782 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10784 result_type = type0;
10785 converted = 1;
10787 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10788 && code1 == INTEGER_TYPE)
10790 doing_shift = true;
10791 if (TREE_CODE (op1) == INTEGER_CST)
10793 if (tree_int_cst_sgn (op1) < 0)
10795 int_const = false;
10796 if (c_inhibit_evaluation_warnings == 0)
10797 warning_at (location, OPT_Wshift_count_negative,
10798 "right shift count is negative");
10800 else
10802 if (!integer_zerop (op1))
10803 short_shift = 1;
10805 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10807 int_const = false;
10808 if (c_inhibit_evaluation_warnings == 0)
10809 warning_at (location, OPT_Wshift_count_overflow,
10810 "right shift count >= width of type");
10815 /* Use the type of the value to be shifted. */
10816 result_type = type0;
10817 /* Avoid converting op1 to result_type later. */
10818 converted = 1;
10820 break;
10822 case LSHIFT_EXPR:
10823 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10824 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10826 result_type = type0;
10827 converted = 1;
10829 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10830 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10831 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10832 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10834 result_type = type0;
10835 converted = 1;
10837 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10838 && code1 == INTEGER_TYPE)
10840 doing_shift = true;
10841 if (TREE_CODE (op0) == INTEGER_CST
10842 && tree_int_cst_sgn (op0) < 0)
10844 /* Don't reject a left shift of a negative value in a context
10845 where a constant expression is needed in C90. */
10846 if (flag_isoc99)
10847 int_const = false;
10848 if (c_inhibit_evaluation_warnings == 0)
10849 warning_at (location, OPT_Wshift_negative_value,
10850 "left shift of negative value");
10852 if (TREE_CODE (op1) == INTEGER_CST)
10854 if (tree_int_cst_sgn (op1) < 0)
10856 int_const = false;
10857 if (c_inhibit_evaluation_warnings == 0)
10858 warning_at (location, OPT_Wshift_count_negative,
10859 "left shift count is negative");
10861 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10863 int_const = false;
10864 if (c_inhibit_evaluation_warnings == 0)
10865 warning_at (location, OPT_Wshift_count_overflow,
10866 "left shift count >= width of type");
10868 else if (TREE_CODE (op0) == INTEGER_CST
10869 && maybe_warn_shift_overflow (location, op0, op1)
10870 && flag_isoc99)
10871 int_const = false;
10874 /* Use the type of the value to be shifted. */
10875 result_type = type0;
10876 /* Avoid converting op1 to result_type later. */
10877 converted = 1;
10879 break;
10881 case EQ_EXPR:
10882 case NE_EXPR:
10883 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10885 tree intt;
10886 if (!vector_types_compatible_elements_p (type0, type1))
10888 error_at (location, "comparing vectors with different "
10889 "element types");
10890 return error_mark_node;
10893 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10895 error_at (location, "comparing vectors with different "
10896 "number of elements");
10897 return error_mark_node;
10900 /* Always construct signed integer vector type. */
10901 intt = c_common_type_for_size (GET_MODE_BITSIZE
10902 (TYPE_MODE (TREE_TYPE (type0))), 0);
10903 result_type = build_opaque_vector_type (intt,
10904 TYPE_VECTOR_SUBPARTS (type0));
10905 converted = 1;
10906 ret = build_vec_cmp (resultcode, result_type, op0, op1);
10907 goto return_build_binary_op;
10909 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10910 warning_at (location,
10911 OPT_Wfloat_equal,
10912 "comparing floating point with == or != is unsafe");
10913 /* Result of comparison is always int,
10914 but don't convert the args to int! */
10915 build_type = integer_type_node;
10916 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10917 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10918 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10919 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10920 short_compare = 1;
10921 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10923 if (warn_nonnull
10924 && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0))
10925 warning_at (location, OPT_Wnonnull,
10926 "nonnull argument %qD compared to NULL", op0);
10928 if (TREE_CODE (op0) == ADDR_EXPR
10929 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10931 if (code == EQ_EXPR)
10932 warning_at (location,
10933 OPT_Waddress,
10934 "the comparison will always evaluate as %<false%> "
10935 "for the address of %qD will never be NULL",
10936 TREE_OPERAND (op0, 0));
10937 else
10938 warning_at (location,
10939 OPT_Waddress,
10940 "the comparison will always evaluate as %<true%> "
10941 "for the address of %qD will never be NULL",
10942 TREE_OPERAND (op0, 0));
10944 result_type = type0;
10946 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10948 if (warn_nonnull
10949 && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1))
10950 warning_at (location, OPT_Wnonnull,
10951 "nonnull argument %qD compared to NULL", op1);
10953 if (TREE_CODE (op1) == ADDR_EXPR
10954 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10956 if (code == EQ_EXPR)
10957 warning_at (location,
10958 OPT_Waddress,
10959 "the comparison will always evaluate as %<false%> "
10960 "for the address of %qD will never be NULL",
10961 TREE_OPERAND (op1, 0));
10962 else
10963 warning_at (location,
10964 OPT_Waddress,
10965 "the comparison will always evaluate as %<true%> "
10966 "for the address of %qD will never be NULL",
10967 TREE_OPERAND (op1, 0));
10969 result_type = type1;
10971 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10973 tree tt0 = TREE_TYPE (type0);
10974 tree tt1 = TREE_TYPE (type1);
10975 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10976 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10977 addr_space_t as_common = ADDR_SPACE_GENERIC;
10979 /* Anything compares with void *. void * compares with anything.
10980 Otherwise, the targets must be compatible
10981 and both must be object or both incomplete. */
10982 if (comp_target_types (location, type0, type1))
10983 result_type = common_pointer_type (type0, type1);
10984 else if (!addr_space_superset (as0, as1, &as_common))
10986 error_at (location, "comparison of pointers to "
10987 "disjoint address spaces");
10988 return error_mark_node;
10990 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10992 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10993 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10994 "comparison of %<void *%> with function pointer");
10996 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10998 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10999 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11000 "comparison of %<void *%> with function pointer");
11002 else
11003 /* Avoid warning about the volatile ObjC EH puts on decls. */
11004 if (!objc_ok)
11005 pedwarn (location, 0,
11006 "comparison of distinct pointer types lacks a cast");
11008 if (result_type == NULL_TREE)
11010 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11011 result_type = build_pointer_type
11012 (build_qualified_type (void_type_node, qual));
11015 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11017 result_type = type0;
11018 pedwarn (location, 0, "comparison between pointer and integer");
11020 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11022 result_type = type1;
11023 pedwarn (location, 0, "comparison between pointer and integer");
11025 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11026 || truth_value_p (TREE_CODE (orig_op0)))
11027 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11028 || truth_value_p (TREE_CODE (orig_op1))))
11029 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11030 break;
11032 case LE_EXPR:
11033 case GE_EXPR:
11034 case LT_EXPR:
11035 case GT_EXPR:
11036 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11038 tree intt;
11039 if (!vector_types_compatible_elements_p (type0, type1))
11041 error_at (location, "comparing vectors with different "
11042 "element types");
11043 return error_mark_node;
11046 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11048 error_at (location, "comparing vectors with different "
11049 "number of elements");
11050 return error_mark_node;
11053 /* Always construct signed integer vector type. */
11054 intt = c_common_type_for_size (GET_MODE_BITSIZE
11055 (TYPE_MODE (TREE_TYPE (type0))), 0);
11056 result_type = build_opaque_vector_type (intt,
11057 TYPE_VECTOR_SUBPARTS (type0));
11058 converted = 1;
11059 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11060 goto return_build_binary_op;
11062 build_type = integer_type_node;
11063 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11064 || code0 == FIXED_POINT_TYPE)
11065 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11066 || code1 == FIXED_POINT_TYPE))
11067 short_compare = 1;
11068 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11070 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11071 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11072 addr_space_t as_common;
11074 if (comp_target_types (location, type0, type1))
11076 result_type = common_pointer_type (type0, type1);
11077 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11078 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11079 pedwarn (location, 0,
11080 "comparison of complete and incomplete pointers");
11081 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11082 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11083 "ordered comparisons of pointers to functions");
11084 else if (null_pointer_constant_p (orig_op0)
11085 || null_pointer_constant_p (orig_op1))
11086 warning_at (location, OPT_Wextra,
11087 "ordered comparison of pointer with null pointer");
11090 else if (!addr_space_superset (as0, as1, &as_common))
11092 error_at (location, "comparison of pointers to "
11093 "disjoint address spaces");
11094 return error_mark_node;
11096 else
11098 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11099 result_type = build_pointer_type
11100 (build_qualified_type (void_type_node, qual));
11101 pedwarn (location, 0,
11102 "comparison of distinct pointer types lacks a cast");
11105 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11107 result_type = type0;
11108 if (pedantic)
11109 pedwarn (location, OPT_Wpedantic,
11110 "ordered comparison of pointer with integer zero");
11111 else if (extra_warnings)
11112 warning_at (location, OPT_Wextra,
11113 "ordered comparison of pointer with integer zero");
11115 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11117 result_type = type1;
11118 if (pedantic)
11119 pedwarn (location, OPT_Wpedantic,
11120 "ordered comparison of pointer with integer zero");
11121 else if (extra_warnings)
11122 warning_at (location, OPT_Wextra,
11123 "ordered comparison of pointer with integer zero");
11125 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11127 result_type = type0;
11128 pedwarn (location, 0, "comparison between pointer and integer");
11130 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11132 result_type = type1;
11133 pedwarn (location, 0, "comparison between pointer and integer");
11135 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11136 || truth_value_p (TREE_CODE (orig_op0)))
11137 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11138 || truth_value_p (TREE_CODE (orig_op1))))
11139 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11140 break;
11142 default:
11143 gcc_unreachable ();
11146 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11147 return error_mark_node;
11149 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11150 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11151 || !vector_types_compatible_elements_p (type0, type1)))
11153 binary_op_error (location, code, type0, type1);
11154 return error_mark_node;
11157 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11158 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11160 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11161 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11163 bool first_complex = (code0 == COMPLEX_TYPE);
11164 bool second_complex = (code1 == COMPLEX_TYPE);
11165 int none_complex = (!first_complex && !second_complex);
11167 if (shorten || common || short_compare)
11169 result_type = c_common_type (type0, type1);
11170 do_warn_double_promotion (result_type, type0, type1,
11171 "implicit conversion from %qT to %qT "
11172 "to match other operand of binary "
11173 "expression",
11174 location);
11175 if (result_type == error_mark_node)
11176 return error_mark_node;
11179 if (first_complex != second_complex
11180 && (code == PLUS_EXPR
11181 || code == MINUS_EXPR
11182 || code == MULT_EXPR
11183 || (code == TRUNC_DIV_EXPR && first_complex))
11184 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11185 && flag_signed_zeros)
11187 /* An operation on mixed real/complex operands must be
11188 handled specially, but the language-independent code can
11189 more easily optimize the plain complex arithmetic if
11190 -fno-signed-zeros. */
11191 tree real_type = TREE_TYPE (result_type);
11192 tree real, imag;
11193 if (type0 != orig_type0 || type1 != orig_type1)
11195 gcc_assert (may_need_excess_precision && common);
11196 semantic_result_type = c_common_type (orig_type0, orig_type1);
11198 if (first_complex)
11200 if (TREE_TYPE (op0) != result_type)
11201 op0 = convert_and_check (location, result_type, op0);
11202 if (TREE_TYPE (op1) != real_type)
11203 op1 = convert_and_check (location, real_type, op1);
11205 else
11207 if (TREE_TYPE (op0) != real_type)
11208 op0 = convert_and_check (location, real_type, op0);
11209 if (TREE_TYPE (op1) != result_type)
11210 op1 = convert_and_check (location, result_type, op1);
11212 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11213 return error_mark_node;
11214 if (first_complex)
11216 op0 = c_save_expr (op0);
11217 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11218 op0, 1);
11219 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11220 op0, 1);
11221 switch (code)
11223 case MULT_EXPR:
11224 case TRUNC_DIV_EXPR:
11225 op1 = c_save_expr (op1);
11226 imag = build2 (resultcode, real_type, imag, op1);
11227 /* Fall through. */
11228 case PLUS_EXPR:
11229 case MINUS_EXPR:
11230 real = build2 (resultcode, real_type, real, op1);
11231 break;
11232 default:
11233 gcc_unreachable();
11236 else
11238 op1 = c_save_expr (op1);
11239 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11240 op1, 1);
11241 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11242 op1, 1);
11243 switch (code)
11245 case MULT_EXPR:
11246 op0 = c_save_expr (op0);
11247 imag = build2 (resultcode, real_type, op0, imag);
11248 /* Fall through. */
11249 case PLUS_EXPR:
11250 real = build2 (resultcode, real_type, op0, real);
11251 break;
11252 case MINUS_EXPR:
11253 real = build2 (resultcode, real_type, op0, real);
11254 imag = build1 (NEGATE_EXPR, real_type, imag);
11255 break;
11256 default:
11257 gcc_unreachable();
11260 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11261 goto return_build_binary_op;
11264 /* For certain operations (which identify themselves by shorten != 0)
11265 if both args were extended from the same smaller type,
11266 do the arithmetic in that type and then extend.
11268 shorten !=0 and !=1 indicates a bitwise operation.
11269 For them, this optimization is safe only if
11270 both args are zero-extended or both are sign-extended.
11271 Otherwise, we might change the result.
11272 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11273 but calculated in (unsigned short) it would be (unsigned short)-1. */
11275 if (shorten && none_complex)
11277 final_type = result_type;
11278 result_type = shorten_binary_op (result_type, op0, op1,
11279 shorten == -1);
11282 /* Shifts can be shortened if shifting right. */
11284 if (short_shift)
11286 int unsigned_arg;
11287 tree arg0 = get_narrower (op0, &unsigned_arg);
11289 final_type = result_type;
11291 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11292 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11294 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11295 && tree_int_cst_sgn (op1) > 0
11296 /* We can shorten only if the shift count is less than the
11297 number of bits in the smaller type size. */
11298 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11299 /* We cannot drop an unsigned shift after sign-extension. */
11300 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11302 /* Do an unsigned shift if the operand was zero-extended. */
11303 result_type
11304 = c_common_signed_or_unsigned_type (unsigned_arg,
11305 TREE_TYPE (arg0));
11306 /* Convert value-to-be-shifted to that type. */
11307 if (TREE_TYPE (op0) != result_type)
11308 op0 = convert (result_type, op0);
11309 converted = 1;
11313 /* Comparison operations are shortened too but differently.
11314 They identify themselves by setting short_compare = 1. */
11316 if (short_compare)
11318 /* Don't write &op0, etc., because that would prevent op0
11319 from being kept in a register.
11320 Instead, make copies of the our local variables and
11321 pass the copies by reference, then copy them back afterward. */
11322 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11323 enum tree_code xresultcode = resultcode;
11324 tree val
11325 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11326 &xresultcode);
11328 if (val != 0)
11330 ret = val;
11331 goto return_build_binary_op;
11334 op0 = xop0, op1 = xop1;
11335 converted = 1;
11336 resultcode = xresultcode;
11338 if (c_inhibit_evaluation_warnings == 0)
11340 bool op0_maybe_const = true;
11341 bool op1_maybe_const = true;
11342 tree orig_op0_folded, orig_op1_folded;
11344 if (in_late_binary_op)
11346 orig_op0_folded = orig_op0;
11347 orig_op1_folded = orig_op1;
11349 else
11351 /* Fold for the sake of possible warnings, as in
11352 build_conditional_expr. This requires the
11353 "original" values to be folded, not just op0 and
11354 op1. */
11355 c_inhibit_evaluation_warnings++;
11356 op0 = c_fully_fold (op0, require_constant_value,
11357 &op0_maybe_const);
11358 op1 = c_fully_fold (op1, require_constant_value,
11359 &op1_maybe_const);
11360 c_inhibit_evaluation_warnings--;
11361 orig_op0_folded = c_fully_fold (orig_op0,
11362 require_constant_value,
11363 NULL);
11364 orig_op1_folded = c_fully_fold (orig_op1,
11365 require_constant_value,
11366 NULL);
11369 if (warn_sign_compare)
11370 warn_for_sign_compare (location, orig_op0_folded,
11371 orig_op1_folded, op0, op1,
11372 result_type, resultcode);
11373 if (!in_late_binary_op && !int_operands)
11375 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11376 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11377 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11378 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11384 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11385 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11386 Then the expression will be built.
11387 It will be given type FINAL_TYPE if that is nonzero;
11388 otherwise, it will be given type RESULT_TYPE. */
11390 if (!result_type)
11392 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11393 return error_mark_node;
11396 if (build_type == NULL_TREE)
11398 build_type = result_type;
11399 if ((type0 != orig_type0 || type1 != orig_type1)
11400 && !boolean_op)
11402 gcc_assert (may_need_excess_precision && common);
11403 semantic_result_type = c_common_type (orig_type0, orig_type1);
11407 if (!converted)
11409 op0 = ep_convert_and_check (location, result_type, op0,
11410 semantic_result_type);
11411 op1 = ep_convert_and_check (location, result_type, op1,
11412 semantic_result_type);
11414 /* This can happen if one operand has a vector type, and the other
11415 has a different type. */
11416 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11417 return error_mark_node;
11420 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11421 | SANITIZE_FLOAT_DIVIDE))
11422 && do_ubsan_in_current_function ()
11423 && (doing_div_or_mod || doing_shift)
11424 && !require_constant_value)
11426 /* OP0 and/or OP1 might have side-effects. */
11427 op0 = c_save_expr (op0);
11428 op1 = c_save_expr (op1);
11429 op0 = c_fully_fold (op0, false, NULL);
11430 op1 = c_fully_fold (op1, false, NULL);
11431 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11432 | SANITIZE_FLOAT_DIVIDE)))
11433 instrument_expr = ubsan_instrument_division (location, op0, op1);
11434 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11435 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11438 /* Treat expressions in initializers specially as they can't trap. */
11439 if (int_const_or_overflow)
11440 ret = (require_constant_value
11441 ? fold_build2_initializer_loc (location, resultcode, build_type,
11442 op0, op1)
11443 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11444 else
11445 ret = build2 (resultcode, build_type, op0, op1);
11446 if (final_type != 0)
11447 ret = convert (final_type, ret);
11449 return_build_binary_op:
11450 gcc_assert (ret != error_mark_node);
11451 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11452 ret = (int_operands
11453 ? note_integer_operands (ret)
11454 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11455 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11456 && !in_late_binary_op)
11457 ret = note_integer_operands (ret);
11458 if (semantic_result_type)
11459 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11460 protected_set_expr_location (ret, location);
11462 if (instrument_expr != NULL)
11463 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11464 instrument_expr, ret);
11466 return ret;
11470 /* Convert EXPR to be a truth-value, validating its type for this
11471 purpose. LOCATION is the source location for the expression. */
11473 tree
11474 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11476 bool int_const, int_operands;
11478 switch (TREE_CODE (TREE_TYPE (expr)))
11480 case ARRAY_TYPE:
11481 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11482 return error_mark_node;
11484 case RECORD_TYPE:
11485 error_at (location, "used struct type value where scalar is required");
11486 return error_mark_node;
11488 case UNION_TYPE:
11489 error_at (location, "used union type value where scalar is required");
11490 return error_mark_node;
11492 case VOID_TYPE:
11493 error_at (location, "void value not ignored as it ought to be");
11494 return error_mark_node;
11496 case POINTER_TYPE:
11497 if (reject_gcc_builtin (expr))
11498 return error_mark_node;
11499 break;
11501 case FUNCTION_TYPE:
11502 gcc_unreachable ();
11504 case VECTOR_TYPE:
11505 error_at (location, "used vector type where scalar is required");
11506 return error_mark_node;
11508 default:
11509 break;
11512 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11513 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11514 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11516 expr = remove_c_maybe_const_expr (expr);
11517 expr = build2 (NE_EXPR, integer_type_node, expr,
11518 convert (TREE_TYPE (expr), integer_zero_node));
11519 expr = note_integer_operands (expr);
11521 else
11522 /* ??? Should we also give an error for vectors rather than leaving
11523 those to give errors later? */
11524 expr = c_common_truthvalue_conversion (location, expr);
11526 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11528 if (TREE_OVERFLOW (expr))
11529 return expr;
11530 else
11531 return note_integer_operands (expr);
11533 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11534 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11535 return expr;
11539 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11540 required. */
11542 tree
11543 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11545 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11547 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11548 /* Executing a compound literal inside a function reinitializes
11549 it. */
11550 if (!TREE_STATIC (decl))
11551 *se = true;
11552 return decl;
11554 else
11555 return expr;
11558 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11559 statement. LOC is the location of the construct. */
11561 tree
11562 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11563 tree clauses)
11565 body = c_end_compound_stmt (loc, body, true);
11567 tree stmt = make_node (code);
11568 TREE_TYPE (stmt) = void_type_node;
11569 OMP_BODY (stmt) = body;
11570 OMP_CLAUSES (stmt) = clauses;
11571 SET_EXPR_LOCATION (stmt, loc);
11573 return add_stmt (stmt);
11576 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11577 statement. LOC is the location of the OACC_DATA. */
11579 tree
11580 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11582 tree stmt;
11584 block = c_end_compound_stmt (loc, block, true);
11586 stmt = make_node (OACC_DATA);
11587 TREE_TYPE (stmt) = void_type_node;
11588 OACC_DATA_CLAUSES (stmt) = clauses;
11589 OACC_DATA_BODY (stmt) = block;
11590 SET_EXPR_LOCATION (stmt, loc);
11592 return add_stmt (stmt);
11595 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11597 tree
11598 c_begin_omp_parallel (void)
11600 tree block;
11602 keep_next_level ();
11603 block = c_begin_compound_stmt (true);
11605 return block;
11608 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11609 statement. LOC is the location of the OMP_PARALLEL. */
11611 tree
11612 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11614 tree stmt;
11616 block = c_end_compound_stmt (loc, block, true);
11618 stmt = make_node (OMP_PARALLEL);
11619 TREE_TYPE (stmt) = void_type_node;
11620 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11621 OMP_PARALLEL_BODY (stmt) = block;
11622 SET_EXPR_LOCATION (stmt, loc);
11624 return add_stmt (stmt);
11627 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11629 tree
11630 c_begin_omp_task (void)
11632 tree block;
11634 keep_next_level ();
11635 block = c_begin_compound_stmt (true);
11637 return block;
11640 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11641 statement. LOC is the location of the #pragma. */
11643 tree
11644 c_finish_omp_task (location_t loc, tree clauses, tree block)
11646 tree stmt;
11648 block = c_end_compound_stmt (loc, block, true);
11650 stmt = make_node (OMP_TASK);
11651 TREE_TYPE (stmt) = void_type_node;
11652 OMP_TASK_CLAUSES (stmt) = clauses;
11653 OMP_TASK_BODY (stmt) = block;
11654 SET_EXPR_LOCATION (stmt, loc);
11656 return add_stmt (stmt);
11659 /* Generate GOMP_cancel call for #pragma omp cancel. */
11661 void
11662 c_finish_omp_cancel (location_t loc, tree clauses)
11664 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11665 int mask = 0;
11666 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11667 mask = 1;
11668 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11669 mask = 2;
11670 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11671 mask = 4;
11672 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11673 mask = 8;
11674 else
11676 error_at (loc, "%<#pragma omp cancel must specify one of "
11677 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11678 "clauses");
11679 return;
11681 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11682 if (ifc != NULL_TREE)
11684 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11685 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11686 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11687 build_zero_cst (type));
11689 else
11690 ifc = boolean_true_node;
11691 tree stmt = build_call_expr_loc (loc, fn, 2,
11692 build_int_cst (integer_type_node, mask),
11693 ifc);
11694 add_stmt (stmt);
11697 /* Generate GOMP_cancellation_point call for
11698 #pragma omp cancellation point. */
11700 void
11701 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11703 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11704 int mask = 0;
11705 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11706 mask = 1;
11707 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11708 mask = 2;
11709 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11710 mask = 4;
11711 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11712 mask = 8;
11713 else
11715 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11716 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11717 "clauses");
11718 return;
11720 tree stmt = build_call_expr_loc (loc, fn, 1,
11721 build_int_cst (integer_type_node, mask));
11722 add_stmt (stmt);
11725 /* Helper function for handle_omp_array_sections. Called recursively
11726 to handle multiple array-section-subscripts. C is the clause,
11727 T current expression (initially OMP_CLAUSE_DECL), which is either
11728 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11729 expression if specified, TREE_VALUE length expression if specified,
11730 TREE_CHAIN is what it has been specified after, or some decl.
11731 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11732 set to true if any of the array-section-subscript could have length
11733 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11734 first array-section-subscript which is known not to have length
11735 of one. Given say:
11736 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11737 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11738 all are or may have length of 1, array-section-subscript [:2] is the
11739 first one known not to have length 1. For array-section-subscript
11740 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11741 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11742 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11743 case though, as some lengths could be zero. */
11745 static tree
11746 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11747 bool &maybe_zero_len, unsigned int &first_non_one,
11748 bool is_omp)
11750 tree ret, low_bound, length, type;
11751 if (TREE_CODE (t) != TREE_LIST)
11753 if (error_operand_p (t))
11754 return error_mark_node;
11755 ret = t;
11756 if (TREE_CODE (t) == COMPONENT_REF
11757 && is_omp
11758 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11759 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11760 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11762 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11764 error_at (OMP_CLAUSE_LOCATION (c),
11765 "bit-field %qE in %qs clause",
11766 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11767 return error_mark_node;
11769 while (TREE_CODE (t) == COMPONENT_REF)
11771 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11773 error_at (OMP_CLAUSE_LOCATION (c),
11774 "%qE is a member of a union", t);
11775 return error_mark_node;
11777 t = TREE_OPERAND (t, 0);
11780 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
11782 if (DECL_P (t))
11783 error_at (OMP_CLAUSE_LOCATION (c),
11784 "%qD is not a variable in %qs clause", t,
11785 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11786 else
11787 error_at (OMP_CLAUSE_LOCATION (c),
11788 "%qE is not a variable in %qs clause", t,
11789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11790 return error_mark_node;
11792 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11793 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
11795 error_at (OMP_CLAUSE_LOCATION (c),
11796 "%qD is threadprivate variable in %qs clause", t,
11797 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11798 return error_mark_node;
11800 return ret;
11803 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11804 maybe_zero_len, first_non_one, is_omp);
11805 if (ret == error_mark_node || ret == NULL_TREE)
11806 return ret;
11808 type = TREE_TYPE (ret);
11809 low_bound = TREE_PURPOSE (t);
11810 length = TREE_VALUE (t);
11812 if (low_bound == error_mark_node || length == error_mark_node)
11813 return error_mark_node;
11815 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11817 error_at (OMP_CLAUSE_LOCATION (c),
11818 "low bound %qE of array section does not have integral type",
11819 low_bound);
11820 return error_mark_node;
11822 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11824 error_at (OMP_CLAUSE_LOCATION (c),
11825 "length %qE of array section does not have integral type",
11826 length);
11827 return error_mark_node;
11829 if (low_bound
11830 && TREE_CODE (low_bound) == INTEGER_CST
11831 && TYPE_PRECISION (TREE_TYPE (low_bound))
11832 > TYPE_PRECISION (sizetype))
11833 low_bound = fold_convert (sizetype, low_bound);
11834 if (length
11835 && TREE_CODE (length) == INTEGER_CST
11836 && TYPE_PRECISION (TREE_TYPE (length))
11837 > TYPE_PRECISION (sizetype))
11838 length = fold_convert (sizetype, length);
11839 if (low_bound == NULL_TREE)
11840 low_bound = integer_zero_node;
11842 if (length != NULL_TREE)
11844 if (!integer_nonzerop (length))
11846 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11847 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11849 if (integer_zerop (length))
11851 error_at (OMP_CLAUSE_LOCATION (c),
11852 "zero length array section in %qs clause",
11853 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11854 return error_mark_node;
11857 else
11858 maybe_zero_len = true;
11860 if (first_non_one == types.length ()
11861 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11862 first_non_one++;
11864 if (TREE_CODE (type) == ARRAY_TYPE)
11866 if (length == NULL_TREE
11867 && (TYPE_DOMAIN (type) == NULL_TREE
11868 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11870 error_at (OMP_CLAUSE_LOCATION (c),
11871 "for unknown bound array type length expression must "
11872 "be specified");
11873 return error_mark_node;
11875 if (TREE_CODE (low_bound) == INTEGER_CST
11876 && tree_int_cst_sgn (low_bound) == -1)
11878 error_at (OMP_CLAUSE_LOCATION (c),
11879 "negative low bound in array section in %qs clause",
11880 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11881 return error_mark_node;
11883 if (length != NULL_TREE
11884 && TREE_CODE (length) == INTEGER_CST
11885 && tree_int_cst_sgn (length) == -1)
11887 error_at (OMP_CLAUSE_LOCATION (c),
11888 "negative length in array section in %qs clause",
11889 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11890 return error_mark_node;
11892 if (TYPE_DOMAIN (type)
11893 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11894 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11895 == INTEGER_CST)
11897 tree size = size_binop (PLUS_EXPR,
11898 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11899 size_one_node);
11900 if (TREE_CODE (low_bound) == INTEGER_CST)
11902 if (tree_int_cst_lt (size, low_bound))
11904 error_at (OMP_CLAUSE_LOCATION (c),
11905 "low bound %qE above array section size "
11906 "in %qs clause", low_bound,
11907 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11908 return error_mark_node;
11910 if (tree_int_cst_equal (size, low_bound))
11912 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11913 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11915 error_at (OMP_CLAUSE_LOCATION (c),
11916 "zero length array section in %qs clause",
11917 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11918 return error_mark_node;
11920 maybe_zero_len = true;
11922 else if (length == NULL_TREE
11923 && first_non_one == types.length ()
11924 && tree_int_cst_equal
11925 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11926 low_bound))
11927 first_non_one++;
11929 else if (length == NULL_TREE)
11931 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11932 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11933 maybe_zero_len = true;
11934 if (first_non_one == types.length ())
11935 first_non_one++;
11937 if (length && TREE_CODE (length) == INTEGER_CST)
11939 if (tree_int_cst_lt (size, length))
11941 error_at (OMP_CLAUSE_LOCATION (c),
11942 "length %qE above array section size "
11943 "in %qs clause", length,
11944 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11945 return error_mark_node;
11947 if (TREE_CODE (low_bound) == INTEGER_CST)
11949 tree lbpluslen
11950 = size_binop (PLUS_EXPR,
11951 fold_convert (sizetype, low_bound),
11952 fold_convert (sizetype, length));
11953 if (TREE_CODE (lbpluslen) == INTEGER_CST
11954 && tree_int_cst_lt (size, lbpluslen))
11956 error_at (OMP_CLAUSE_LOCATION (c),
11957 "high bound %qE above array section size "
11958 "in %qs clause", lbpluslen,
11959 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11960 return error_mark_node;
11965 else if (length == NULL_TREE)
11967 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11968 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11969 maybe_zero_len = true;
11970 if (first_non_one == types.length ())
11971 first_non_one++;
11974 /* For [lb:] we will need to evaluate lb more than once. */
11975 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11977 tree lb = c_save_expr (low_bound);
11978 if (lb != low_bound)
11980 TREE_PURPOSE (t) = lb;
11981 low_bound = lb;
11985 else if (TREE_CODE (type) == POINTER_TYPE)
11987 if (length == NULL_TREE)
11989 error_at (OMP_CLAUSE_LOCATION (c),
11990 "for pointer type length expression must be specified");
11991 return error_mark_node;
11993 if (length != NULL_TREE
11994 && TREE_CODE (length) == INTEGER_CST
11995 && tree_int_cst_sgn (length) == -1)
11997 error_at (OMP_CLAUSE_LOCATION (c),
11998 "negative length in array section in %qs clause",
11999 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12000 return error_mark_node;
12002 /* If there is a pointer type anywhere but in the very first
12003 array-section-subscript, the array section can't be contiguous. */
12004 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12005 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12007 error_at (OMP_CLAUSE_LOCATION (c),
12008 "array section is not contiguous in %qs clause",
12009 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12010 return error_mark_node;
12013 else
12015 error_at (OMP_CLAUSE_LOCATION (c),
12016 "%qE does not have pointer or array type", ret);
12017 return error_mark_node;
12019 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12020 types.safe_push (TREE_TYPE (ret));
12021 /* We will need to evaluate lb more than once. */
12022 tree lb = c_save_expr (low_bound);
12023 if (lb != low_bound)
12025 TREE_PURPOSE (t) = lb;
12026 low_bound = lb;
12028 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12029 return ret;
12032 /* Handle array sections for clause C. */
12034 static bool
12035 handle_omp_array_sections (tree c, bool is_omp)
12037 bool maybe_zero_len = false;
12038 unsigned int first_non_one = 0;
12039 auto_vec<tree, 10> types;
12040 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12041 maybe_zero_len, first_non_one,
12042 is_omp);
12043 if (first == error_mark_node)
12044 return true;
12045 if (first == NULL_TREE)
12046 return false;
12047 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12049 tree t = OMP_CLAUSE_DECL (c);
12050 tree tem = NULL_TREE;
12051 /* Need to evaluate side effects in the length expressions
12052 if any. */
12053 while (TREE_CODE (t) == TREE_LIST)
12055 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12057 if (tem == NULL_TREE)
12058 tem = TREE_VALUE (t);
12059 else
12060 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12061 TREE_VALUE (t), tem);
12063 t = TREE_CHAIN (t);
12065 if (tem)
12066 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12067 first = c_fully_fold (first, false, NULL);
12068 OMP_CLAUSE_DECL (c) = first;
12070 else
12072 unsigned int num = types.length (), i;
12073 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12074 tree condition = NULL_TREE;
12076 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12077 maybe_zero_len = true;
12079 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12080 t = TREE_CHAIN (t))
12082 tree low_bound = TREE_PURPOSE (t);
12083 tree length = TREE_VALUE (t);
12085 i--;
12086 if (low_bound
12087 && TREE_CODE (low_bound) == INTEGER_CST
12088 && TYPE_PRECISION (TREE_TYPE (low_bound))
12089 > TYPE_PRECISION (sizetype))
12090 low_bound = fold_convert (sizetype, low_bound);
12091 if (length
12092 && TREE_CODE (length) == INTEGER_CST
12093 && TYPE_PRECISION (TREE_TYPE (length))
12094 > TYPE_PRECISION (sizetype))
12095 length = fold_convert (sizetype, length);
12096 if (low_bound == NULL_TREE)
12097 low_bound = integer_zero_node;
12098 if (!maybe_zero_len && i > first_non_one)
12100 if (integer_nonzerop (low_bound))
12101 goto do_warn_noncontiguous;
12102 if (length != NULL_TREE
12103 && TREE_CODE (length) == INTEGER_CST
12104 && TYPE_DOMAIN (types[i])
12105 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12106 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12107 == INTEGER_CST)
12109 tree size;
12110 size = size_binop (PLUS_EXPR,
12111 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12112 size_one_node);
12113 if (!tree_int_cst_equal (length, size))
12115 do_warn_noncontiguous:
12116 error_at (OMP_CLAUSE_LOCATION (c),
12117 "array section is not contiguous in %qs "
12118 "clause",
12119 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12120 return true;
12123 if (length != NULL_TREE
12124 && TREE_SIDE_EFFECTS (length))
12126 if (side_effects == NULL_TREE)
12127 side_effects = length;
12128 else
12129 side_effects = build2 (COMPOUND_EXPR,
12130 TREE_TYPE (side_effects),
12131 length, side_effects);
12134 else
12136 tree l;
12138 if (i > first_non_one
12139 && ((length && integer_nonzerop (length))
12140 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12141 continue;
12142 if (length)
12143 l = fold_convert (sizetype, length);
12144 else
12146 l = size_binop (PLUS_EXPR,
12147 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12148 size_one_node);
12149 l = size_binop (MINUS_EXPR, l,
12150 fold_convert (sizetype, low_bound));
12152 if (i > first_non_one)
12154 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12155 size_zero_node);
12156 if (condition == NULL_TREE)
12157 condition = l;
12158 else
12159 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12160 l, condition);
12162 else if (size == NULL_TREE)
12164 size = size_in_bytes (TREE_TYPE (types[i]));
12165 tree eltype = TREE_TYPE (types[num - 1]);
12166 while (TREE_CODE (eltype) == ARRAY_TYPE)
12167 eltype = TREE_TYPE (eltype);
12168 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12170 if (integer_zerop (size)
12171 || integer_zerop (size_in_bytes (eltype)))
12173 error_at (OMP_CLAUSE_LOCATION (c),
12174 "zero length array section in %qs clause",
12175 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12176 return error_mark_node;
12178 size = size_binop (EXACT_DIV_EXPR, size,
12179 size_in_bytes (eltype));
12181 size = size_binop (MULT_EXPR, size, l);
12182 if (condition)
12183 size = fold_build3 (COND_EXPR, sizetype, condition,
12184 size, size_zero_node);
12186 else
12187 size = size_binop (MULT_EXPR, size, l);
12190 if (side_effects)
12191 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12192 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12194 size = size_binop (MINUS_EXPR, size, size_one_node);
12195 size = c_fully_fold (size, false, NULL);
12196 tree index_type = build_index_type (size);
12197 tree eltype = TREE_TYPE (first);
12198 while (TREE_CODE (eltype) == ARRAY_TYPE)
12199 eltype = TREE_TYPE (eltype);
12200 tree type = build_array_type (eltype, index_type);
12201 tree ptype = build_pointer_type (eltype);
12202 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12203 t = build_fold_addr_expr (t);
12204 tree t2 = build_fold_addr_expr (first);
12205 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12206 ptrdiff_type_node, t2);
12207 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12208 ptrdiff_type_node, t2,
12209 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12210 ptrdiff_type_node, t));
12211 t2 = c_fully_fold (t2, false, NULL);
12212 if (tree_fits_shwi_p (t2))
12213 t = build2 (MEM_REF, type, t,
12214 build_int_cst (ptype, tree_to_shwi (t2)));
12215 else
12217 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12218 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12219 TREE_TYPE (t), t, t2);
12220 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12222 OMP_CLAUSE_DECL (c) = t;
12223 return false;
12225 first = c_fully_fold (first, false, NULL);
12226 OMP_CLAUSE_DECL (c) = first;
12227 if (size)
12228 size = c_fully_fold (size, false, NULL);
12229 OMP_CLAUSE_SIZE (c) = size;
12230 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12231 || (TREE_CODE (t) == COMPONENT_REF
12232 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12233 return false;
12234 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12235 if (is_omp)
12236 switch (OMP_CLAUSE_MAP_KIND (c))
12238 case GOMP_MAP_ALLOC:
12239 case GOMP_MAP_TO:
12240 case GOMP_MAP_FROM:
12241 case GOMP_MAP_TOFROM:
12242 case GOMP_MAP_ALWAYS_TO:
12243 case GOMP_MAP_ALWAYS_FROM:
12244 case GOMP_MAP_ALWAYS_TOFROM:
12245 case GOMP_MAP_RELEASE:
12246 case GOMP_MAP_DELETE:
12247 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12248 break;
12249 default:
12250 break;
12252 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12253 if (!is_omp)
12254 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12255 else if (TREE_CODE (t) == COMPONENT_REF)
12256 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12257 else
12258 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12259 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12260 && !c_mark_addressable (t))
12261 return false;
12262 OMP_CLAUSE_DECL (c2) = t;
12263 t = build_fold_addr_expr (first);
12264 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12265 tree ptr = OMP_CLAUSE_DECL (c2);
12266 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12267 ptr = build_fold_addr_expr (ptr);
12268 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12269 ptrdiff_type_node, t,
12270 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12271 ptrdiff_type_node, ptr));
12272 t = c_fully_fold (t, false, NULL);
12273 OMP_CLAUSE_SIZE (c2) = t;
12274 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12275 OMP_CLAUSE_CHAIN (c) = c2;
12277 return false;
12280 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12281 an inline call. But, remap
12282 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12283 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12285 static tree
12286 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12287 tree decl, tree placeholder)
12289 copy_body_data id;
12290 hash_map<tree, tree> decl_map;
12292 decl_map.put (omp_decl1, placeholder);
12293 decl_map.put (omp_decl2, decl);
12294 memset (&id, 0, sizeof (id));
12295 id.src_fn = DECL_CONTEXT (omp_decl1);
12296 id.dst_fn = current_function_decl;
12297 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12298 id.decl_map = &decl_map;
12300 id.copy_decl = copy_decl_no_change;
12301 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12302 id.transform_new_cfg = true;
12303 id.transform_return_to_modify = false;
12304 id.transform_lang_insert_block = NULL;
12305 id.eh_lp_nr = 0;
12306 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12307 return stmt;
12310 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12311 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12313 static tree
12314 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12316 if (*tp == (tree) data)
12317 return *tp;
12318 return NULL_TREE;
12321 /* For all elements of CLAUSES, validate them against their constraints.
12322 Remove any elements from the list that are invalid. */
12324 tree
12325 c_finish_omp_clauses (tree clauses, bool is_omp, bool declare_simd)
12327 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12328 bitmap_head aligned_head, map_head, map_field_head;
12329 tree c, t, type, *pc;
12330 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12331 bool branch_seen = false;
12332 bool copyprivate_seen = false;
12333 bool linear_variable_step_check = false;
12334 tree *nowait_clause = NULL;
12335 bool ordered_seen = false;
12336 tree schedule_clause = NULL_TREE;
12338 bitmap_obstack_initialize (NULL);
12339 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12340 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12341 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12342 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12343 bitmap_initialize (&map_head, &bitmap_default_obstack);
12344 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12346 for (pc = &clauses, c = clauses; c ; c = *pc)
12348 bool remove = false;
12349 bool need_complete = false;
12350 bool need_implicitly_determined = false;
12352 switch (OMP_CLAUSE_CODE (c))
12354 case OMP_CLAUSE_SHARED:
12355 need_implicitly_determined = true;
12356 goto check_dup_generic;
12358 case OMP_CLAUSE_PRIVATE:
12359 need_complete = true;
12360 need_implicitly_determined = true;
12361 goto check_dup_generic;
12363 case OMP_CLAUSE_REDUCTION:
12364 need_implicitly_determined = true;
12365 t = OMP_CLAUSE_DECL (c);
12366 if (TREE_CODE (t) == TREE_LIST)
12368 if (handle_omp_array_sections (c, is_omp))
12370 remove = true;
12371 break;
12374 t = OMP_CLAUSE_DECL (c);
12376 t = require_complete_type (t);
12377 if (t == error_mark_node)
12379 remove = true;
12380 break;
12382 type = TREE_TYPE (t);
12383 if (TREE_CODE (t) == MEM_REF)
12384 type = TREE_TYPE (type);
12385 if (TREE_CODE (type) == ARRAY_TYPE)
12387 tree oatype = type;
12388 gcc_assert (TREE_CODE (t) != MEM_REF);
12389 while (TREE_CODE (type) == ARRAY_TYPE)
12390 type = TREE_TYPE (type);
12391 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12393 error_at (OMP_CLAUSE_LOCATION (c),
12394 "%qD in %<reduction%> clause is a zero size array",
12396 remove = true;
12397 break;
12399 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12400 TYPE_SIZE_UNIT (type));
12401 if (integer_zerop (size))
12403 error_at (OMP_CLAUSE_LOCATION (c),
12404 "%qD in %<reduction%> clause is a zero size array",
12406 remove = true;
12407 break;
12409 size = size_binop (MINUS_EXPR, size, size_one_node);
12410 tree index_type = build_index_type (size);
12411 tree atype = build_array_type (type, index_type);
12412 tree ptype = build_pointer_type (type);
12413 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12414 t = build_fold_addr_expr (t);
12415 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12416 OMP_CLAUSE_DECL (c) = t;
12418 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12419 && (FLOAT_TYPE_P (type)
12420 || TREE_CODE (type) == COMPLEX_TYPE))
12422 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12423 const char *r_name = NULL;
12425 switch (r_code)
12427 case PLUS_EXPR:
12428 case MULT_EXPR:
12429 case MINUS_EXPR:
12430 break;
12431 case MIN_EXPR:
12432 if (TREE_CODE (type) == COMPLEX_TYPE)
12433 r_name = "min";
12434 break;
12435 case MAX_EXPR:
12436 if (TREE_CODE (type) == COMPLEX_TYPE)
12437 r_name = "max";
12438 break;
12439 case BIT_AND_EXPR:
12440 r_name = "&";
12441 break;
12442 case BIT_XOR_EXPR:
12443 r_name = "^";
12444 break;
12445 case BIT_IOR_EXPR:
12446 r_name = "|";
12447 break;
12448 case TRUTH_ANDIF_EXPR:
12449 if (FLOAT_TYPE_P (type))
12450 r_name = "&&";
12451 break;
12452 case TRUTH_ORIF_EXPR:
12453 if (FLOAT_TYPE_P (type))
12454 r_name = "||";
12455 break;
12456 default:
12457 gcc_unreachable ();
12459 if (r_name)
12461 error_at (OMP_CLAUSE_LOCATION (c),
12462 "%qE has invalid type for %<reduction(%s)%>",
12463 t, r_name);
12464 remove = true;
12465 break;
12468 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12470 error_at (OMP_CLAUSE_LOCATION (c),
12471 "user defined reduction not found for %qE", t);
12472 remove = true;
12473 break;
12475 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12477 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12478 type = TYPE_MAIN_VARIANT (type);
12479 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12480 VAR_DECL, NULL_TREE, type);
12481 tree decl_placeholder = NULL_TREE;
12482 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12483 DECL_ARTIFICIAL (placeholder) = 1;
12484 DECL_IGNORED_P (placeholder) = 1;
12485 if (TREE_CODE (t) == MEM_REF)
12487 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12488 VAR_DECL, NULL_TREE, type);
12489 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12490 DECL_ARTIFICIAL (decl_placeholder) = 1;
12491 DECL_IGNORED_P (decl_placeholder) = 1;
12493 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12494 c_mark_addressable (placeholder);
12495 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12496 c_mark_addressable (decl_placeholder ? decl_placeholder
12497 : OMP_CLAUSE_DECL (c));
12498 OMP_CLAUSE_REDUCTION_MERGE (c)
12499 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12500 TREE_VEC_ELT (list, 0),
12501 TREE_VEC_ELT (list, 1),
12502 decl_placeholder ? decl_placeholder
12503 : OMP_CLAUSE_DECL (c), placeholder);
12504 OMP_CLAUSE_REDUCTION_MERGE (c)
12505 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12506 void_type_node, NULL_TREE,
12507 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12508 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12509 if (TREE_VEC_LENGTH (list) == 6)
12511 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12512 c_mark_addressable (decl_placeholder ? decl_placeholder
12513 : OMP_CLAUSE_DECL (c));
12514 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12515 c_mark_addressable (placeholder);
12516 tree init = TREE_VEC_ELT (list, 5);
12517 if (init == error_mark_node)
12518 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12519 OMP_CLAUSE_REDUCTION_INIT (c)
12520 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12521 TREE_VEC_ELT (list, 3),
12522 decl_placeholder ? decl_placeholder
12523 : OMP_CLAUSE_DECL (c), placeholder);
12524 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12526 tree v = decl_placeholder ? decl_placeholder : t;
12527 OMP_CLAUSE_REDUCTION_INIT (c)
12528 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12529 OMP_CLAUSE_REDUCTION_INIT (c));
12531 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12532 c_find_omp_placeholder_r,
12533 placeholder, NULL))
12534 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12536 else
12538 tree init;
12539 tree v = decl_placeholder ? decl_placeholder : t;
12540 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12541 init = build_constructor (TREE_TYPE (v), NULL);
12542 else
12543 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12544 OMP_CLAUSE_REDUCTION_INIT (c)
12545 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12547 OMP_CLAUSE_REDUCTION_INIT (c)
12548 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12549 void_type_node, NULL_TREE,
12550 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12551 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12553 if (TREE_CODE (t) == MEM_REF)
12555 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12556 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12557 != INTEGER_CST)
12559 sorry ("variable length element type in array "
12560 "%<reduction%> clause");
12561 remove = true;
12562 break;
12564 t = TREE_OPERAND (t, 0);
12565 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12566 t = TREE_OPERAND (t, 0);
12567 if (TREE_CODE (t) == ADDR_EXPR)
12568 t = TREE_OPERAND (t, 0);
12570 goto check_dup_generic_t;
12572 case OMP_CLAUSE_COPYPRIVATE:
12573 copyprivate_seen = true;
12574 if (nowait_clause)
12576 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12577 "%<nowait%> clause must not be used together "
12578 "with %<copyprivate%>");
12579 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12580 nowait_clause = NULL;
12582 goto check_dup_generic;
12584 case OMP_CLAUSE_COPYIN:
12585 t = OMP_CLAUSE_DECL (c);
12586 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12588 error_at (OMP_CLAUSE_LOCATION (c),
12589 "%qE must be %<threadprivate%> for %<copyin%>", t);
12590 remove = true;
12591 break;
12593 goto check_dup_generic;
12595 case OMP_CLAUSE_LINEAR:
12596 if (!declare_simd)
12597 need_implicitly_determined = true;
12598 t = OMP_CLAUSE_DECL (c);
12599 if (!declare_simd
12600 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12602 error_at (OMP_CLAUSE_LOCATION (c),
12603 "modifier should not be specified in %<linear%> "
12604 "clause on %<simd%> or %<for%> constructs");
12605 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12607 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12608 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12610 error_at (OMP_CLAUSE_LOCATION (c),
12611 "linear clause applied to non-integral non-pointer "
12612 "variable with type %qT", TREE_TYPE (t));
12613 remove = true;
12614 break;
12616 if (declare_simd)
12618 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12619 if (TREE_CODE (s) == PARM_DECL)
12621 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12622 /* map_head bitmap is used as uniform_head if
12623 declare_simd. */
12624 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12625 linear_variable_step_check = true;
12626 goto check_dup_generic;
12628 if (TREE_CODE (s) != INTEGER_CST)
12630 error_at (OMP_CLAUSE_LOCATION (c),
12631 "%<linear%> clause step %qE is neither constant "
12632 "nor a parameter", s);
12633 remove = true;
12634 break;
12637 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12639 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12640 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12641 OMP_CLAUSE_DECL (c), s);
12642 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12643 sizetype, fold_convert (sizetype, s),
12644 fold_convert
12645 (sizetype, OMP_CLAUSE_DECL (c)));
12646 if (s == error_mark_node)
12647 s = size_one_node;
12648 OMP_CLAUSE_LINEAR_STEP (c) = s;
12650 else
12651 OMP_CLAUSE_LINEAR_STEP (c)
12652 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12653 goto check_dup_generic;
12655 check_dup_generic:
12656 t = OMP_CLAUSE_DECL (c);
12657 check_dup_generic_t:
12658 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12660 error_at (OMP_CLAUSE_LOCATION (c),
12661 "%qE is not a variable in clause %qs", t,
12662 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12663 remove = true;
12665 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12666 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12667 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12669 error_at (OMP_CLAUSE_LOCATION (c),
12670 "%qE appears more than once in data clauses", t);
12671 remove = true;
12673 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12674 && bitmap_bit_p (&map_head, DECL_UID (t)))
12676 error ("%qD appears both in data and map clauses", t);
12677 remove = true;
12679 else
12680 bitmap_set_bit (&generic_head, DECL_UID (t));
12681 break;
12683 case OMP_CLAUSE_FIRSTPRIVATE:
12684 t = OMP_CLAUSE_DECL (c);
12685 need_complete = true;
12686 need_implicitly_determined = true;
12687 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12689 error_at (OMP_CLAUSE_LOCATION (c),
12690 "%qE is not a variable in clause %<firstprivate%>", t);
12691 remove = true;
12693 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12694 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12696 error_at (OMP_CLAUSE_LOCATION (c),
12697 "%qE appears more than once in data clauses", t);
12698 remove = true;
12700 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12702 error ("%qD appears both in data and map clauses", t);
12703 remove = true;
12705 else
12706 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12707 break;
12709 case OMP_CLAUSE_LASTPRIVATE:
12710 t = OMP_CLAUSE_DECL (c);
12711 need_complete = true;
12712 need_implicitly_determined = true;
12713 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12715 error_at (OMP_CLAUSE_LOCATION (c),
12716 "%qE is not a variable in clause %<lastprivate%>", t);
12717 remove = true;
12719 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12720 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12722 error_at (OMP_CLAUSE_LOCATION (c),
12723 "%qE appears more than once in data clauses", t);
12724 remove = true;
12726 else
12727 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12728 break;
12730 case OMP_CLAUSE_ALIGNED:
12731 t = OMP_CLAUSE_DECL (c);
12732 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12734 error_at (OMP_CLAUSE_LOCATION (c),
12735 "%qE is not a variable in %<aligned%> clause", t);
12736 remove = true;
12738 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12739 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12741 error_at (OMP_CLAUSE_LOCATION (c),
12742 "%qE in %<aligned%> clause is neither a pointer nor "
12743 "an array", t);
12744 remove = true;
12746 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12748 error_at (OMP_CLAUSE_LOCATION (c),
12749 "%qE appears more than once in %<aligned%> clauses",
12751 remove = true;
12753 else
12754 bitmap_set_bit (&aligned_head, DECL_UID (t));
12755 break;
12757 case OMP_CLAUSE_DEPEND:
12758 t = OMP_CLAUSE_DECL (c);
12759 if (t == NULL_TREE)
12761 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
12762 == OMP_CLAUSE_DEPEND_SOURCE);
12763 break;
12765 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12767 gcc_assert (TREE_CODE (t) == TREE_LIST);
12768 for (; t; t = TREE_CHAIN (t))
12770 tree decl = TREE_VALUE (t);
12771 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
12773 tree offset = TREE_PURPOSE (t);
12774 bool neg = wi::neg_p ((wide_int) offset);
12775 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
12776 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
12777 neg ? MINUS_EXPR : PLUS_EXPR,
12778 decl, offset);
12779 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12780 sizetype,
12781 fold_convert (sizetype, t2),
12782 fold_convert (sizetype, decl));
12783 if (t2 == error_mark_node)
12785 remove = true;
12786 break;
12788 TREE_PURPOSE (t) = t2;
12791 break;
12793 if (TREE_CODE (t) == TREE_LIST)
12795 if (handle_omp_array_sections (c, is_omp))
12796 remove = true;
12797 break;
12799 if (t == error_mark_node)
12800 remove = true;
12801 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12803 error_at (OMP_CLAUSE_LOCATION (c),
12804 "%qE is not a variable in %<depend%> clause", t);
12805 remove = true;
12807 else if (!c_mark_addressable (t))
12808 remove = true;
12809 break;
12811 case OMP_CLAUSE_MAP:
12812 case OMP_CLAUSE_TO:
12813 case OMP_CLAUSE_FROM:
12814 case OMP_CLAUSE__CACHE_:
12815 t = OMP_CLAUSE_DECL (c);
12816 if (TREE_CODE (t) == TREE_LIST)
12818 if (handle_omp_array_sections (c, is_omp))
12819 remove = true;
12820 else
12822 t = OMP_CLAUSE_DECL (c);
12823 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12825 error_at (OMP_CLAUSE_LOCATION (c),
12826 "array section does not have mappable type "
12827 "in %qs clause",
12828 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12829 remove = true;
12831 while (TREE_CODE (t) == ARRAY_REF)
12832 t = TREE_OPERAND (t, 0);
12833 if (TREE_CODE (t) == COMPONENT_REF
12834 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12836 while (TREE_CODE (t) == COMPONENT_REF)
12837 t = TREE_OPERAND (t, 0);
12838 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
12839 break;
12840 if (bitmap_bit_p (&map_head, DECL_UID (t)))
12842 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12843 error ("%qD appears more than once in motion"
12844 " clauses", t);
12845 else
12846 error ("%qD appears more than once in map"
12847 " clauses", t);
12848 remove = true;
12850 else
12852 bitmap_set_bit (&map_head, DECL_UID (t));
12853 bitmap_set_bit (&map_field_head, DECL_UID (t));
12857 break;
12859 if (t == error_mark_node)
12861 remove = true;
12862 break;
12864 if (TREE_CODE (t) == COMPONENT_REF
12865 && is_omp
12866 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
12868 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12870 error_at (OMP_CLAUSE_LOCATION (c),
12871 "bit-field %qE in %qs clause",
12872 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12873 remove = true;
12875 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12877 error_at (OMP_CLAUSE_LOCATION (c),
12878 "%qE does not have a mappable type in %qs clause",
12879 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12880 remove = true;
12882 while (TREE_CODE (t) == COMPONENT_REF)
12884 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
12885 == UNION_TYPE)
12887 error_at (OMP_CLAUSE_LOCATION (c),
12888 "%qE is a member of a union", t);
12889 remove = true;
12890 break;
12892 t = TREE_OPERAND (t, 0);
12894 if (remove)
12895 break;
12896 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
12898 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
12899 break;
12902 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12904 error_at (OMP_CLAUSE_LOCATION (c),
12905 "%qE is not a variable in %qs clause", t,
12906 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12907 remove = true;
12909 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12911 error_at (OMP_CLAUSE_LOCATION (c),
12912 "%qD is threadprivate variable in %qs clause", t,
12913 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12914 remove = true;
12916 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12917 || (OMP_CLAUSE_MAP_KIND (c)
12918 != GOMP_MAP_FIRSTPRIVATE_POINTER))
12919 && !c_mark_addressable (t))
12920 remove = true;
12921 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12922 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12923 || (OMP_CLAUSE_MAP_KIND (c)
12924 == GOMP_MAP_FIRSTPRIVATE_POINTER)
12925 || (OMP_CLAUSE_MAP_KIND (c)
12926 == GOMP_MAP_FORCE_DEVICEPTR)))
12927 && t == OMP_CLAUSE_DECL (c)
12928 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12930 error_at (OMP_CLAUSE_LOCATION (c),
12931 "%qD does not have a mappable type in %qs clause", t,
12932 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12933 remove = true;
12935 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12936 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
12938 if (bitmap_bit_p (&generic_head, DECL_UID (t))
12939 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12941 error ("%qD appears more than once in data clauses", t);
12942 remove = true;
12944 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12946 error ("%qD appears both in data and map clauses", t);
12947 remove = true;
12949 else
12950 bitmap_set_bit (&generic_head, DECL_UID (t));
12952 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12954 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12955 error ("%qD appears more than once in motion clauses", t);
12956 else
12957 error ("%qD appears more than once in map clauses", t);
12958 remove = true;
12960 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12961 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12963 error ("%qD appears both in data and map clauses", t);
12964 remove = true;
12966 else
12968 bitmap_set_bit (&map_head, DECL_UID (t));
12969 if (t != OMP_CLAUSE_DECL (c)
12970 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
12971 bitmap_set_bit (&map_field_head, DECL_UID (t));
12973 break;
12975 case OMP_CLAUSE_TO_DECLARE:
12976 case OMP_CLAUSE_LINK:
12977 t = OMP_CLAUSE_DECL (c);
12978 if (TREE_CODE (t) == FUNCTION_DECL
12979 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
12981 else if (!VAR_P (t))
12983 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
12984 error_at (OMP_CLAUSE_LOCATION (c),
12985 "%qE is neither a variable nor a function name in "
12986 "clause %qs", t,
12987 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12988 else
12989 error_at (OMP_CLAUSE_LOCATION (c),
12990 "%qE is not a variable in clause %qs", t,
12991 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12992 remove = true;
12994 else if (DECL_THREAD_LOCAL_P (t))
12996 error_at (OMP_CLAUSE_LOCATION (c),
12997 "%qD is threadprivate variable in %qs clause", t,
12998 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12999 remove = true;
13001 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13003 error_at (OMP_CLAUSE_LOCATION (c),
13004 "%qD does not have a mappable type in %qs clause", t,
13005 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13006 remove = true;
13008 if (remove)
13009 break;
13010 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13012 error_at (OMP_CLAUSE_LOCATION (c),
13013 "%qE appears more than once on the same "
13014 "%<declare target%> directive", t);
13015 remove = true;
13017 else
13018 bitmap_set_bit (&generic_head, DECL_UID (t));
13019 break;
13021 case OMP_CLAUSE_UNIFORM:
13022 t = OMP_CLAUSE_DECL (c);
13023 if (TREE_CODE (t) != PARM_DECL)
13025 if (DECL_P (t))
13026 error_at (OMP_CLAUSE_LOCATION (c),
13027 "%qD is not an argument in %<uniform%> clause", t);
13028 else
13029 error_at (OMP_CLAUSE_LOCATION (c),
13030 "%qE is not an argument in %<uniform%> clause", t);
13031 remove = true;
13032 break;
13034 /* map_head bitmap is used as uniform_head if declare_simd. */
13035 bitmap_set_bit (&map_head, DECL_UID (t));
13036 goto check_dup_generic;
13038 case OMP_CLAUSE_IS_DEVICE_PTR:
13039 case OMP_CLAUSE_USE_DEVICE_PTR:
13040 t = OMP_CLAUSE_DECL (c);
13041 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13042 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13044 error_at (OMP_CLAUSE_LOCATION (c),
13045 "%qs variable is neither a pointer nor an array",
13046 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13047 remove = true;
13049 goto check_dup_generic;
13051 case OMP_CLAUSE_NOWAIT:
13052 if (copyprivate_seen)
13054 error_at (OMP_CLAUSE_LOCATION (c),
13055 "%<nowait%> clause must not be used together "
13056 "with %<copyprivate%>");
13057 remove = true;
13058 break;
13060 nowait_clause = pc;
13061 pc = &OMP_CLAUSE_CHAIN (c);
13062 continue;
13064 case OMP_CLAUSE_IF:
13065 case OMP_CLAUSE_NUM_THREADS:
13066 case OMP_CLAUSE_NUM_TEAMS:
13067 case OMP_CLAUSE_THREAD_LIMIT:
13068 case OMP_CLAUSE_DEFAULT:
13069 case OMP_CLAUSE_UNTIED:
13070 case OMP_CLAUSE_COLLAPSE:
13071 case OMP_CLAUSE_FINAL:
13072 case OMP_CLAUSE_MERGEABLE:
13073 case OMP_CLAUSE_DEVICE:
13074 case OMP_CLAUSE_DIST_SCHEDULE:
13075 case OMP_CLAUSE_PARALLEL:
13076 case OMP_CLAUSE_FOR:
13077 case OMP_CLAUSE_SECTIONS:
13078 case OMP_CLAUSE_TASKGROUP:
13079 case OMP_CLAUSE_PROC_BIND:
13080 case OMP_CLAUSE_PRIORITY:
13081 case OMP_CLAUSE_GRAINSIZE:
13082 case OMP_CLAUSE_NUM_TASKS:
13083 case OMP_CLAUSE_NOGROUP:
13084 case OMP_CLAUSE_THREADS:
13085 case OMP_CLAUSE_SIMD:
13086 case OMP_CLAUSE_HINT:
13087 case OMP_CLAUSE_DEFAULTMAP:
13088 case OMP_CLAUSE__CILK_FOR_COUNT_:
13089 case OMP_CLAUSE_NUM_GANGS:
13090 case OMP_CLAUSE_NUM_WORKERS:
13091 case OMP_CLAUSE_VECTOR_LENGTH:
13092 case OMP_CLAUSE_ASYNC:
13093 case OMP_CLAUSE_WAIT:
13094 case OMP_CLAUSE_AUTO:
13095 case OMP_CLAUSE_INDEPENDENT:
13096 case OMP_CLAUSE_SEQ:
13097 case OMP_CLAUSE_GANG:
13098 case OMP_CLAUSE_WORKER:
13099 case OMP_CLAUSE_VECTOR:
13100 case OMP_CLAUSE_TILE:
13101 pc = &OMP_CLAUSE_CHAIN (c);
13102 continue;
13104 case OMP_CLAUSE_SCHEDULE:
13105 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13107 const char *p = NULL;
13108 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13110 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13111 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13112 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13113 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13114 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13115 default: gcc_unreachable ();
13117 if (p)
13119 error_at (OMP_CLAUSE_LOCATION (c),
13120 "%<nonmonotonic%> modifier specified for %qs "
13121 "schedule kind", p);
13122 OMP_CLAUSE_SCHEDULE_KIND (c)
13123 = (enum omp_clause_schedule_kind)
13124 (OMP_CLAUSE_SCHEDULE_KIND (c)
13125 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13128 schedule_clause = c;
13129 pc = &OMP_CLAUSE_CHAIN (c);
13130 continue;
13132 case OMP_CLAUSE_ORDERED:
13133 ordered_seen = true;
13134 pc = &OMP_CLAUSE_CHAIN (c);
13135 continue;
13137 case OMP_CLAUSE_SAFELEN:
13138 safelen = c;
13139 pc = &OMP_CLAUSE_CHAIN (c);
13140 continue;
13141 case OMP_CLAUSE_SIMDLEN:
13142 simdlen = c;
13143 pc = &OMP_CLAUSE_CHAIN (c);
13144 continue;
13146 case OMP_CLAUSE_INBRANCH:
13147 case OMP_CLAUSE_NOTINBRANCH:
13148 if (branch_seen)
13150 error_at (OMP_CLAUSE_LOCATION (c),
13151 "%<inbranch%> clause is incompatible with "
13152 "%<notinbranch%>");
13153 remove = true;
13154 break;
13156 branch_seen = true;
13157 pc = &OMP_CLAUSE_CHAIN (c);
13158 continue;
13160 default:
13161 gcc_unreachable ();
13164 if (!remove)
13166 t = OMP_CLAUSE_DECL (c);
13168 if (need_complete)
13170 t = require_complete_type (t);
13171 if (t == error_mark_node)
13172 remove = true;
13175 if (need_implicitly_determined)
13177 const char *share_name = NULL;
13179 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13180 share_name = "threadprivate";
13181 else switch (c_omp_predetermined_sharing (t))
13183 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13184 break;
13185 case OMP_CLAUSE_DEFAULT_SHARED:
13186 /* const vars may be specified in firstprivate clause. */
13187 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13188 && TREE_READONLY (t))
13189 break;
13190 share_name = "shared";
13191 break;
13192 case OMP_CLAUSE_DEFAULT_PRIVATE:
13193 share_name = "private";
13194 break;
13195 default:
13196 gcc_unreachable ();
13198 if (share_name)
13200 error_at (OMP_CLAUSE_LOCATION (c),
13201 "%qE is predetermined %qs for %qs",
13202 t, share_name,
13203 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13204 remove = true;
13209 if (remove)
13210 *pc = OMP_CLAUSE_CHAIN (c);
13211 else
13212 pc = &OMP_CLAUSE_CHAIN (c);
13215 if (simdlen
13216 && safelen
13217 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13218 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13220 error_at (OMP_CLAUSE_LOCATION (simdlen),
13221 "%<simdlen%> clause value is bigger than "
13222 "%<safelen%> clause value");
13223 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13224 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13227 if (ordered_seen
13228 && schedule_clause
13229 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13230 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13232 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13233 "%<nonmonotonic%> schedule modifier specified together "
13234 "with %<ordered%> clause");
13235 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13236 = (enum omp_clause_schedule_kind)
13237 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13238 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13241 if (linear_variable_step_check)
13242 for (pc = &clauses, c = clauses; c ; c = *pc)
13244 bool remove = false;
13245 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13246 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13247 && !bitmap_bit_p (&map_head,
13248 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13250 error_at (OMP_CLAUSE_LOCATION (c),
13251 "%<linear%> clause step is a parameter %qD not "
13252 "specified in %<uniform%> clause",
13253 OMP_CLAUSE_LINEAR_STEP (c));
13254 remove = true;
13257 if (remove)
13258 *pc = OMP_CLAUSE_CHAIN (c);
13259 else
13260 pc = &OMP_CLAUSE_CHAIN (c);
13263 bitmap_obstack_release (NULL);
13264 return clauses;
13267 /* Create a transaction node. */
13269 tree
13270 c_finish_transaction (location_t loc, tree block, int flags)
13272 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13273 if (flags & TM_STMT_ATTR_OUTER)
13274 TRANSACTION_EXPR_OUTER (stmt) = 1;
13275 if (flags & TM_STMT_ATTR_RELAXED)
13276 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13277 return add_stmt (stmt);
13280 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13281 down to the element type of an array. */
13283 tree
13284 c_build_qualified_type (tree type, int type_quals)
13286 if (type == error_mark_node)
13287 return type;
13289 if (TREE_CODE (type) == ARRAY_TYPE)
13291 tree t;
13292 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13293 type_quals);
13295 /* See if we already have an identically qualified type. */
13296 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13298 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13299 && TYPE_NAME (t) == TYPE_NAME (type)
13300 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13301 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13302 TYPE_ATTRIBUTES (type)))
13303 break;
13305 if (!t)
13307 tree domain = TYPE_DOMAIN (type);
13309 t = build_variant_type_copy (type);
13310 TREE_TYPE (t) = element_type;
13312 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13313 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13314 SET_TYPE_STRUCTURAL_EQUALITY (t);
13315 else if (TYPE_CANONICAL (element_type) != element_type
13316 || (domain && TYPE_CANONICAL (domain) != domain))
13318 tree unqualified_canon
13319 = build_array_type (TYPE_CANONICAL (element_type),
13320 domain? TYPE_CANONICAL (domain)
13321 : NULL_TREE);
13322 if (TYPE_REVERSE_STORAGE_ORDER (type))
13324 unqualified_canon
13325 = build_distinct_type_copy (unqualified_canon);
13326 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13328 TYPE_CANONICAL (t)
13329 = c_build_qualified_type (unqualified_canon, type_quals);
13331 else
13332 TYPE_CANONICAL (t) = t;
13334 return t;
13337 /* A restrict-qualified pointer type must be a pointer to object or
13338 incomplete type. Note that the use of POINTER_TYPE_P also allows
13339 REFERENCE_TYPEs, which is appropriate for C++. */
13340 if ((type_quals & TYPE_QUAL_RESTRICT)
13341 && (!POINTER_TYPE_P (type)
13342 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13344 error ("invalid use of %<restrict%>");
13345 type_quals &= ~TYPE_QUAL_RESTRICT;
13348 tree var_type = build_qualified_type (type, type_quals);
13349 /* A variant type does not inherit the list of incomplete vars from the
13350 type main variant. */
13351 if (RECORD_OR_UNION_TYPE_P (var_type))
13352 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13353 return var_type;
13356 /* Build a VA_ARG_EXPR for the C parser. */
13358 tree
13359 c_build_va_arg (location_t loc, tree expr, tree type)
13361 if (error_operand_p (type))
13362 return error_mark_node;
13363 else if (!COMPLETE_TYPE_P (type))
13365 error_at (loc, "second argument to %<va_arg%> is of incomplete "
13366 "type %qT", type);
13367 return error_mark_node;
13369 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13370 warning_at (loc, OPT_Wc___compat,
13371 "C++ requires promoted type, not enum type, in %<va_arg%>");
13372 return build_va_arg (loc, expr, type);
13375 /* Return truthvalue of whether T1 is the same tree structure as T2.
13376 Return 1 if they are the same. Return 0 if they are different. */
13378 bool
13379 c_tree_equal (tree t1, tree t2)
13381 enum tree_code code1, code2;
13383 if (t1 == t2)
13384 return true;
13385 if (!t1 || !t2)
13386 return false;
13388 for (code1 = TREE_CODE (t1);
13389 CONVERT_EXPR_CODE_P (code1)
13390 || code1 == NON_LVALUE_EXPR;
13391 code1 = TREE_CODE (t1))
13392 t1 = TREE_OPERAND (t1, 0);
13393 for (code2 = TREE_CODE (t2);
13394 CONVERT_EXPR_CODE_P (code2)
13395 || code2 == NON_LVALUE_EXPR;
13396 code2 = TREE_CODE (t2))
13397 t2 = TREE_OPERAND (t2, 0);
13399 /* They might have become equal now. */
13400 if (t1 == t2)
13401 return true;
13403 if (code1 != code2)
13404 return false;
13406 switch (code1)
13408 case INTEGER_CST:
13409 return wi::eq_p (t1, t2);
13411 case REAL_CST:
13412 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13414 case STRING_CST:
13415 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13416 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13417 TREE_STRING_LENGTH (t1));
13419 case FIXED_CST:
13420 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13421 TREE_FIXED_CST (t2));
13423 case COMPLEX_CST:
13424 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13425 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13427 case VECTOR_CST:
13428 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13430 case CONSTRUCTOR:
13431 /* We need to do this when determining whether or not two
13432 non-type pointer to member function template arguments
13433 are the same. */
13434 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13435 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13436 return false;
13438 tree field, value;
13439 unsigned int i;
13440 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13442 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13443 if (!c_tree_equal (field, elt2->index)
13444 || !c_tree_equal (value, elt2->value))
13445 return false;
13448 return true;
13450 case TREE_LIST:
13451 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13452 return false;
13453 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13454 return false;
13455 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13457 case SAVE_EXPR:
13458 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13460 case CALL_EXPR:
13462 tree arg1, arg2;
13463 call_expr_arg_iterator iter1, iter2;
13464 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13465 return false;
13466 for (arg1 = first_call_expr_arg (t1, &iter1),
13467 arg2 = first_call_expr_arg (t2, &iter2);
13468 arg1 && arg2;
13469 arg1 = next_call_expr_arg (&iter1),
13470 arg2 = next_call_expr_arg (&iter2))
13471 if (!c_tree_equal (arg1, arg2))
13472 return false;
13473 if (arg1 || arg2)
13474 return false;
13475 return true;
13478 case TARGET_EXPR:
13480 tree o1 = TREE_OPERAND (t1, 0);
13481 tree o2 = TREE_OPERAND (t2, 0);
13483 /* Special case: if either target is an unallocated VAR_DECL,
13484 it means that it's going to be unified with whatever the
13485 TARGET_EXPR is really supposed to initialize, so treat it
13486 as being equivalent to anything. */
13487 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
13488 && !DECL_RTL_SET_P (o1))
13489 /*Nop*/;
13490 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
13491 && !DECL_RTL_SET_P (o2))
13492 /*Nop*/;
13493 else if (!c_tree_equal (o1, o2))
13494 return false;
13496 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13499 case COMPONENT_REF:
13500 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13501 return false;
13502 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13504 case PARM_DECL:
13505 case VAR_DECL:
13506 case CONST_DECL:
13507 case FIELD_DECL:
13508 case FUNCTION_DECL:
13509 case IDENTIFIER_NODE:
13510 case SSA_NAME:
13511 return false;
13513 case TREE_VEC:
13515 unsigned ix;
13516 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13517 return false;
13518 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13519 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13520 TREE_VEC_ELT (t2, ix)))
13521 return false;
13522 return true;
13525 default:
13526 break;
13529 switch (TREE_CODE_CLASS (code1))
13531 case tcc_unary:
13532 case tcc_binary:
13533 case tcc_comparison:
13534 case tcc_expression:
13535 case tcc_vl_exp:
13536 case tcc_reference:
13537 case tcc_statement:
13539 int i, n = TREE_OPERAND_LENGTH (t1);
13541 switch (code1)
13543 case PREINCREMENT_EXPR:
13544 case PREDECREMENT_EXPR:
13545 case POSTINCREMENT_EXPR:
13546 case POSTDECREMENT_EXPR:
13547 n = 1;
13548 break;
13549 case ARRAY_REF:
13550 n = 2;
13551 break;
13552 default:
13553 break;
13556 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13557 && n != TREE_OPERAND_LENGTH (t2))
13558 return false;
13560 for (i = 0; i < n; ++i)
13561 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13562 return false;
13564 return true;
13567 case tcc_type:
13568 return comptypes (t1, t2);
13569 default:
13570 gcc_unreachable ();
13572 /* We can get here with --disable-checking. */
13573 return false;
13576 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13577 spawn-helper and BODY is the newly created body for FNDECL. */
13579 void
13580 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13582 tree list = alloc_stmt_list ();
13583 tree frame = make_cilk_frame (fndecl);
13584 tree dtor = create_cilk_function_exit (frame, false, true);
13585 add_local_decl (cfun, frame);
13587 DECL_SAVED_TREE (fndecl) = list;
13588 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13589 frame);
13590 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13591 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13593 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13594 append_to_statement_list (detach_expr, &body_list);
13596 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13597 body = fold_build_cleanup_point_expr (void_type_node, body);
13599 append_to_statement_list (body, &body_list);
13600 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13601 body_list, dtor), &list);
13604 /* Returns true when the function declaration FNDECL is implicit,
13605 introduced as a result of a call to an otherwise undeclared
13606 function, and false otherwise. */
13608 bool
13609 c_decl_implicit (const_tree fndecl)
13611 return C_DECL_IMPLICIT (fndecl);