Implement C _FloatN, _FloatNx types.
[official-gcc.git] / gcc / c / c-typeck.c
blobbc8728a49b661b9f62e0f7f9340b83134d6a0ddb
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2016 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-tree.h"
51 #include "gcc-rich-location.h"
53 /* Possible cases of implicit bad conversions. Used to select
54 diagnostic messages in convert_for_assignment. */
55 enum impl_conv {
56 ic_argpass,
57 ic_assign,
58 ic_init,
59 ic_return
62 /* The level of nesting inside "__alignof__". */
63 int in_alignof;
65 /* The level of nesting inside "sizeof". */
66 int in_sizeof;
68 /* The level of nesting inside "typeof". */
69 int in_typeof;
71 /* The argument of last parsed sizeof expression, only to be tested
72 if expr.original_code == SIZEOF_EXPR. */
73 tree c_last_sizeof_arg;
75 /* Nonzero if we might need to print a "missing braces around
76 initializer" message within this initializer. */
77 static int found_missing_braces;
79 static int require_constant_value;
80 static int require_constant_elements;
82 static bool null_pointer_constant_p (const_tree);
83 static tree qualify_type (tree, tree);
84 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
85 bool *);
86 static int comp_target_types (location_t, tree, tree);
87 static int function_types_compatible_p (const_tree, const_tree, bool *,
88 bool *);
89 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
90 static tree lookup_field (tree, tree);
91 static int convert_arguments (location_t, vec<location_t>, tree,
92 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
93 tree);
94 static tree pointer_diff (location_t, tree, tree);
95 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
96 enum impl_conv, bool, tree, tree, int);
97 static tree valid_compound_expr_initializer (tree, tree);
98 static void push_string (const char *);
99 static void push_member_name (tree);
100 static int spelling_length (void);
101 static char *print_spelling (char *);
102 static void warning_init (location_t, int, const char *);
103 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
104 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
105 bool, struct obstack *);
106 static void output_pending_init_elements (int, struct obstack *);
107 static int set_designator (location_t, int, struct obstack *);
108 static void push_range_stack (tree, struct obstack *);
109 static void add_pending_init (location_t, tree, tree, tree, bool,
110 struct obstack *);
111 static void set_nonincremental_init (struct obstack *);
112 static void set_nonincremental_init_from_string (tree, struct obstack *);
113 static tree find_init_member (tree, struct obstack *);
114 static void readonly_warning (tree, enum lvalue_use);
115 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
116 static void record_maybe_used_decl (tree);
117 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
119 /* Return true if EXP is a null pointer constant, false otherwise. */
121 static bool
122 null_pointer_constant_p (const_tree expr)
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type = TREE_TYPE (expr);
127 return (TREE_CODE (expr) == INTEGER_CST
128 && !TREE_OVERFLOW (expr)
129 && integer_zerop (expr)
130 && (INTEGRAL_TYPE_P (type)
131 || (TREE_CODE (type) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type))
133 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
136 /* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141 static tree
142 note_integer_operands (tree expr)
144 tree ret;
145 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 ret = copy_node (expr);
148 TREE_OVERFLOW (ret) = 1;
150 else
152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 return ret;
158 /* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 expression. */
163 static inline tree
164 remove_c_maybe_const_expr (tree expr)
166 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
167 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 else
169 return expr;
172 \f/* This is a cache to hold if two types are compatible or not. */
174 struct tagged_tu_seen_cache {
175 const struct tagged_tu_seen_cache * next;
176 const_tree t1;
177 const_tree t2;
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
180 int val;
183 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
184 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.)
188 LOC is the location of the use. */
190 tree
191 require_complete_type (location_t loc, tree value)
193 tree type = TREE_TYPE (value);
195 if (error_operand_p (value))
196 return error_mark_node;
198 /* First, detect a valid value with a complete type. */
199 if (COMPLETE_TYPE_P (type))
200 return value;
202 c_incomplete_type_error (loc, value, type);
203 return error_mark_node;
206 /* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
208 and TYPE is the type that was invalid. LOC is the location for
209 the error. */
211 void
212 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type) == ERROR_MARK)
216 return;
218 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
219 error_at (loc, "%qD has an incomplete type %qT", value, type);
220 else
222 retry:
223 /* We must print an error message. Be clever about what it says. */
225 switch (TREE_CODE (type))
227 case RECORD_TYPE:
228 case UNION_TYPE:
229 case ENUMERAL_TYPE:
230 break;
232 case VOID_TYPE:
233 error_at (loc, "invalid use of void expression");
234 return;
236 case ARRAY_TYPE:
237 if (TYPE_DOMAIN (type))
239 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
241 error_at (loc, "invalid use of flexible array member");
242 return;
244 type = TREE_TYPE (type);
245 goto retry;
247 error_at (loc, "invalid use of array with unspecified bounds");
248 return;
250 default:
251 gcc_unreachable ();
254 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
255 error_at (loc, "invalid use of undefined type %qT", type);
256 else
257 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
258 error_at (loc, "invalid use of incomplete typedef %qT", type);
262 /* Given a type, apply default promotions wrt unnamed function
263 arguments and return the new type. */
265 tree
266 c_type_promotes_to (tree type)
268 tree ret = NULL_TREE;
270 if (TYPE_MAIN_VARIANT (type) == float_type_node)
271 ret = double_type_node;
272 else if (c_promoting_integer_type_p (type))
274 /* Preserve unsignedness if not really getting any wider. */
275 if (TYPE_UNSIGNED (type)
276 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
277 ret = unsigned_type_node;
278 else
279 ret = integer_type_node;
282 if (ret != NULL_TREE)
283 return (TYPE_ATOMIC (type)
284 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
285 : ret);
287 return type;
290 /* Return true if between two named address spaces, whether there is a superset
291 named address space that encompasses both address spaces. If there is a
292 superset, return which address space is the superset. */
294 static bool
295 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
297 if (as1 == as2)
299 *common = as1;
300 return true;
302 else if (targetm.addr_space.subset_p (as1, as2))
304 *common = as2;
305 return true;
307 else if (targetm.addr_space.subset_p (as2, as1))
309 *common = as1;
310 return true;
312 else
313 return false;
316 /* Return a variant of TYPE which has all the type qualifiers of LIKE
317 as well as those of TYPE. */
319 static tree
320 qualify_type (tree type, tree like)
322 addr_space_t as_type = TYPE_ADDR_SPACE (type);
323 addr_space_t as_like = TYPE_ADDR_SPACE (like);
324 addr_space_t as_common;
326 /* If the two named address spaces are different, determine the common
327 superset address space. If there isn't one, raise an error. */
328 if (!addr_space_superset (as_type, as_like, &as_common))
330 as_common = as_type;
331 error ("%qT and %qT are in disjoint named address spaces",
332 type, like);
335 return c_build_qualified_type (type,
336 TYPE_QUALS_NO_ADDR_SPACE (type)
337 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
338 | ENCODE_QUAL_ADDR_SPACE (as_common));
341 /* Return true iff the given tree T is a variable length array. */
343 bool
344 c_vla_type_p (const_tree t)
346 if (TREE_CODE (t) == ARRAY_TYPE
347 && C_TYPE_VARIABLE_SIZE (t))
348 return true;
349 return false;
352 /* Return the composite type of two compatible types.
354 We assume that comptypes has already been done and returned
355 nonzero; if that isn't so, this may crash. In particular, we
356 assume that qualifiers match. */
358 tree
359 composite_type (tree t1, tree t2)
361 enum tree_code code1;
362 enum tree_code code2;
363 tree attributes;
365 /* Save time if the two types are the same. */
367 if (t1 == t2) return t1;
369 /* If one type is nonsense, use the other. */
370 if (t1 == error_mark_node)
371 return t2;
372 if (t2 == error_mark_node)
373 return t1;
375 code1 = TREE_CODE (t1);
376 code2 = TREE_CODE (t2);
378 /* Merge the attributes. */
379 attributes = targetm.merge_type_attributes (t1, t2);
381 /* If one is an enumerated type and the other is the compatible
382 integer type, the composite type might be either of the two
383 (DR#013 question 3). For consistency, use the enumerated type as
384 the composite type. */
386 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
387 return t1;
388 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
389 return t2;
391 gcc_assert (code1 == code2);
393 switch (code1)
395 case POINTER_TYPE:
396 /* For two pointers, do this recursively on the target type. */
398 tree pointed_to_1 = TREE_TYPE (t1);
399 tree pointed_to_2 = TREE_TYPE (t2);
400 tree target = composite_type (pointed_to_1, pointed_to_2);
401 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
402 t1 = build_type_attribute_variant (t1, attributes);
403 return qualify_type (t1, t2);
406 case ARRAY_TYPE:
408 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
409 int quals;
410 tree unqual_elt;
411 tree d1 = TYPE_DOMAIN (t1);
412 tree d2 = TYPE_DOMAIN (t2);
413 bool d1_variable, d2_variable;
414 bool d1_zero, d2_zero;
415 bool t1_complete, t2_complete;
417 /* We should not have any type quals on arrays at all. */
418 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
419 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
421 t1_complete = COMPLETE_TYPE_P (t1);
422 t2_complete = COMPLETE_TYPE_P (t2);
424 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
425 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
427 d1_variable = (!d1_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
430 d2_variable = (!d2_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
433 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
434 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
436 /* Save space: see if the result is identical to one of the args. */
437 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
438 && (d2_variable || d2_zero || !d1_variable))
439 return build_type_attribute_variant (t1, attributes);
440 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
441 && (d1_variable || d1_zero || !d2_variable))
442 return build_type_attribute_variant (t2, attributes);
444 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t1, attributes);
446 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
447 return build_type_attribute_variant (t2, attributes);
449 /* Merge the element types, and have a size if either arg has
450 one. We may have qualifiers on the element types. To set
451 up TYPE_MAIN_VARIANT correctly, we need to form the
452 composite of the unqualified types and add the qualifiers
453 back at the end. */
454 quals = TYPE_QUALS (strip_array_types (elt));
455 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
456 t1 = build_array_type (unqual_elt,
457 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
458 && (d2_variable
459 || d2_zero
460 || !d1_variable))
461 ? t1
462 : t2));
463 /* Ensure a composite type involving a zero-length array type
464 is a zero-length type not an incomplete type. */
465 if (d1_zero && d2_zero
466 && (t1_complete || t2_complete)
467 && !COMPLETE_TYPE_P (t1))
469 TYPE_SIZE (t1) = bitsize_zero_node;
470 TYPE_SIZE_UNIT (t1) = size_zero_node;
472 t1 = c_build_qualified_type (t1, quals);
473 return build_type_attribute_variant (t1, attributes);
476 case ENUMERAL_TYPE:
477 case RECORD_TYPE:
478 case UNION_TYPE:
479 if (attributes != NULL)
481 /* Try harder not to create a new aggregate type. */
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
483 return t1;
484 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
485 return t2;
487 return build_type_attribute_variant (t1, attributes);
489 case FUNCTION_TYPE:
490 /* Function types: prefer the one that specified arg types.
491 If both do, merge the arg types. Also merge the return types. */
493 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
494 tree p1 = TYPE_ARG_TYPES (t1);
495 tree p2 = TYPE_ARG_TYPES (t2);
496 int len;
497 tree newargs, n;
498 int i;
500 /* Save space: see if the result is identical to one of the args. */
501 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
502 return build_type_attribute_variant (t1, attributes);
503 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
504 return build_type_attribute_variant (t2, attributes);
506 /* Simple way if one arg fails to specify argument types. */
507 if (TYPE_ARG_TYPES (t1) == 0)
509 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
510 t1 = build_type_attribute_variant (t1, attributes);
511 return qualify_type (t1, t2);
513 if (TYPE_ARG_TYPES (t2) == 0)
515 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
516 t1 = build_type_attribute_variant (t1, attributes);
517 return qualify_type (t1, t2);
520 /* If both args specify argument types, we must merge the two
521 lists, argument by argument. */
523 for (len = 0, newargs = p1;
524 newargs && newargs != void_list_node;
525 len++, newargs = TREE_CHAIN (newargs))
528 for (i = 0; i < len; i++)
529 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
531 n = newargs;
533 for (; p1 && p1 != void_list_node;
534 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
536 /* A null type means arg type is not specified.
537 Take whatever the other function type has. */
538 if (TREE_VALUE (p1) == 0)
540 TREE_VALUE (n) = TREE_VALUE (p2);
541 goto parm_done;
543 if (TREE_VALUE (p2) == 0)
545 TREE_VALUE (n) = TREE_VALUE (p1);
546 goto parm_done;
549 /* Given wait (union {union wait *u; int *i} *)
550 and wait (union wait *),
551 prefer union wait * as type of parm. */
552 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
553 && TREE_VALUE (p1) != TREE_VALUE (p2))
555 tree memb;
556 tree mv2 = TREE_VALUE (p2);
557 if (mv2 && mv2 != error_mark_node
558 && TREE_CODE (mv2) != ARRAY_TYPE)
559 mv2 = TYPE_MAIN_VARIANT (mv2);
560 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
561 memb; memb = DECL_CHAIN (memb))
563 tree mv3 = TREE_TYPE (memb);
564 if (mv3 && mv3 != error_mark_node
565 && TREE_CODE (mv3) != ARRAY_TYPE)
566 mv3 = TYPE_MAIN_VARIANT (mv3);
567 if (comptypes (mv3, mv2))
569 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
570 TREE_VALUE (p2));
571 pedwarn (input_location, OPT_Wpedantic,
572 "function types not truly compatible in ISO C");
573 goto parm_done;
577 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
578 && TREE_VALUE (p2) != TREE_VALUE (p1))
580 tree memb;
581 tree mv1 = TREE_VALUE (p1);
582 if (mv1 && mv1 != error_mark_node
583 && TREE_CODE (mv1) != ARRAY_TYPE)
584 mv1 = TYPE_MAIN_VARIANT (mv1);
585 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
586 memb; memb = DECL_CHAIN (memb))
588 tree mv3 = TREE_TYPE (memb);
589 if (mv3 && mv3 != error_mark_node
590 && TREE_CODE (mv3) != ARRAY_TYPE)
591 mv3 = TYPE_MAIN_VARIANT (mv3);
592 if (comptypes (mv3, mv1))
594 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
595 TREE_VALUE (p1));
596 pedwarn (input_location, OPT_Wpedantic,
597 "function types not truly compatible in ISO C");
598 goto parm_done;
602 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
603 parm_done: ;
606 t1 = build_function_type (valtype, newargs);
607 t1 = qualify_type (t1, t2);
608 /* ... falls through ... */
611 default:
612 return build_type_attribute_variant (t1, attributes);
617 /* Return the type of a conditional expression between pointers to
618 possibly differently qualified versions of compatible types.
620 We assume that comp_target_types has already been done and returned
621 nonzero; if that isn't so, this may crash. */
623 static tree
624 common_pointer_type (tree t1, tree t2)
626 tree attributes;
627 tree pointed_to_1, mv1;
628 tree pointed_to_2, mv2;
629 tree target;
630 unsigned target_quals;
631 addr_space_t as1, as2, as_common;
632 int quals1, quals2;
634 /* Save time if the two types are the same. */
636 if (t1 == t2) return t1;
638 /* If one type is nonsense, use the other. */
639 if (t1 == error_mark_node)
640 return t2;
641 if (t2 == error_mark_node)
642 return t1;
644 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
645 && TREE_CODE (t2) == POINTER_TYPE);
647 /* Merge the attributes. */
648 attributes = targetm.merge_type_attributes (t1, t2);
650 /* Find the composite type of the target types, and combine the
651 qualifiers of the two types' targets. Do not lose qualifiers on
652 array element types by taking the TYPE_MAIN_VARIANT. */
653 mv1 = pointed_to_1 = TREE_TYPE (t1);
654 mv2 = pointed_to_2 = TREE_TYPE (t2);
655 if (TREE_CODE (mv1) != ARRAY_TYPE)
656 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
657 if (TREE_CODE (mv2) != ARRAY_TYPE)
658 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
659 target = composite_type (mv1, mv2);
661 /* Strip array types to get correct qualifier for pointers to arrays */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
665 /* For function types do not merge const qualifiers, but drop them
666 if used inconsistently. The middle-end uses these to mark const
667 and noreturn functions. */
668 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
669 target_quals = (quals1 & quals2);
670 else
671 target_quals = (quals1 | quals2);
673 /* If the two named address spaces are different, determine the common
674 superset address space. This is guaranteed to exist due to the
675 assumption that comp_target_type returned non-zero. */
676 as1 = TYPE_ADDR_SPACE (pointed_to_1);
677 as2 = TYPE_ADDR_SPACE (pointed_to_2);
678 if (!addr_space_superset (as1, as2, &as_common))
679 gcc_unreachable ();
681 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
683 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
684 return build_type_attribute_variant (t1, attributes);
687 /* Return the common type for two arithmetic types under the usual
688 arithmetic conversions. The default conversions have already been
689 applied, and enumerated types converted to their compatible integer
690 types. The resulting type is unqualified and has no attributes.
692 This is the type for the result of most arithmetic operations
693 if the operands have the given two types. */
695 static tree
696 c_common_type (tree t1, tree t2)
698 enum tree_code code1;
699 enum tree_code code2;
701 /* If one type is nonsense, use the other. */
702 if (t1 == error_mark_node)
703 return t2;
704 if (t2 == error_mark_node)
705 return t1;
707 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
708 t1 = TYPE_MAIN_VARIANT (t1);
710 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
711 t2 = TYPE_MAIN_VARIANT (t2);
713 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
714 t1 = build_type_attribute_variant (t1, NULL_TREE);
716 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
717 t2 = build_type_attribute_variant (t2, NULL_TREE);
719 /* Save time if the two types are the same. */
721 if (t1 == t2) return t1;
723 code1 = TREE_CODE (t1);
724 code2 = TREE_CODE (t2);
726 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
727 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
728 || code1 == INTEGER_TYPE);
729 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
730 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
731 || code2 == INTEGER_TYPE);
733 /* When one operand is a decimal float type, the other operand cannot be
734 a generic float type or a complex type. We also disallow vector types
735 here. */
736 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
737 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
739 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
741 error ("can%'t mix operands of decimal float and vector types");
742 return error_mark_node;
744 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
746 error ("can%'t mix operands of decimal float and complex types");
747 return error_mark_node;
749 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
751 error ("can%'t mix operands of decimal float and other float types");
752 return error_mark_node;
756 /* If one type is a vector type, return that type. (How the usual
757 arithmetic conversions apply to the vector types extension is not
758 precisely specified.) */
759 if (code1 == VECTOR_TYPE)
760 return t1;
762 if (code2 == VECTOR_TYPE)
763 return t2;
765 /* If one type is complex, form the common type of the non-complex
766 components, then make that complex. Use T1 or T2 if it is the
767 required type. */
768 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
770 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
771 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
772 tree subtype = c_common_type (subtype1, subtype2);
774 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
775 return t1;
776 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
777 return t2;
778 else
779 return build_complex_type (subtype);
782 /* If only one is real, use it as the result. */
784 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
785 return t1;
787 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
788 return t2;
790 /* If both are real and either are decimal floating point types, use
791 the decimal floating point type with the greater precision. */
793 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
795 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
797 return dfloat128_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
800 return dfloat64_type_node;
801 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
803 return dfloat32_type_node;
806 /* Deal with fixed-point types. */
807 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
809 unsigned int unsignedp = 0, satp = 0;
810 machine_mode m1, m2;
811 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
813 m1 = TYPE_MODE (t1);
814 m2 = TYPE_MODE (t2);
816 /* If one input type is saturating, the result type is saturating. */
817 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
818 satp = 1;
820 /* If both fixed-point types are unsigned, the result type is unsigned.
821 When mixing fixed-point and integer types, follow the sign of the
822 fixed-point type.
823 Otherwise, the result type is signed. */
824 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
825 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
826 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
827 && TYPE_UNSIGNED (t1))
828 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
829 && TYPE_UNSIGNED (t2)))
830 unsignedp = 1;
832 /* The result type is signed. */
833 if (unsignedp == 0)
835 /* If the input type is unsigned, we need to convert to the
836 signed type. */
837 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
839 enum mode_class mclass = (enum mode_class) 0;
840 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
841 mclass = MODE_FRACT;
842 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
843 mclass = MODE_ACCUM;
844 else
845 gcc_unreachable ();
846 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
848 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
850 enum mode_class mclass = (enum mode_class) 0;
851 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
852 mclass = MODE_FRACT;
853 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
854 mclass = MODE_ACCUM;
855 else
856 gcc_unreachable ();
857 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
861 if (code1 == FIXED_POINT_TYPE)
863 fbit1 = GET_MODE_FBIT (m1);
864 ibit1 = GET_MODE_IBIT (m1);
866 else
868 fbit1 = 0;
869 /* Signed integers need to subtract one sign bit. */
870 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
873 if (code2 == FIXED_POINT_TYPE)
875 fbit2 = GET_MODE_FBIT (m2);
876 ibit2 = GET_MODE_IBIT (m2);
878 else
880 fbit2 = 0;
881 /* Signed integers need to subtract one sign bit. */
882 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
885 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
886 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
887 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
888 satp);
891 /* Both real or both integers; use the one with greater precision. */
893 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
894 return t1;
895 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
896 return t2;
898 /* Same precision. Prefer long longs to longs to ints when the
899 same precision, following the C99 rules on integer type rank
900 (which are equivalent to the C90 rules for C90 types). */
902 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
903 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
904 return long_long_unsigned_type_node;
906 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
907 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
909 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
910 return long_long_unsigned_type_node;
911 else
912 return long_long_integer_type_node;
915 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
916 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
917 return long_unsigned_type_node;
919 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
920 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
922 /* But preserve unsignedness from the other type,
923 since long cannot hold all the values of an unsigned int. */
924 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
925 return long_unsigned_type_node;
926 else
927 return long_integer_type_node;
930 /* For floating types of the same TYPE_PRECISION (which we here
931 assume means either the same set of values, or sets of values
932 neither a subset of the other, with behavior being undefined in
933 the latter case), follow the rules from TS 18661-3: prefer
934 interchange types _FloatN, then standard types long double,
935 double, float, then extended types _FloatNx. For extended types,
936 check them starting with _Float128x as that seems most consistent
937 in spirit with preferring long double to double; for interchange
938 types, also check in that order for consistency although it's not
939 possible for more than one of them to have the same
940 precision. */
941 tree mv1 = TYPE_MAIN_VARIANT (t1);
942 tree mv2 = TYPE_MAIN_VARIANT (t2);
944 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
945 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
946 return FLOATN_TYPE_NODE (i);
948 /* Likewise, prefer long double to double even if same size. */
949 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
950 return long_double_type_node;
952 /* Likewise, prefer double to float even if same size.
953 We got a couple of embedded targets with 32 bit doubles, and the
954 pdp11 might have 64 bit floats. */
955 if (mv1 == double_type_node || mv2 == double_type_node)
956 return double_type_node;
958 if (mv1 == float_type_node || mv2 == float_type_node)
959 return float_type_node;
961 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
962 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
963 return FLOATNX_TYPE_NODE (i);
965 /* Otherwise prefer the unsigned one. */
967 if (TYPE_UNSIGNED (t1))
968 return t1;
969 else
970 return t2;
973 /* Wrapper around c_common_type that is used by c-common.c and other
974 front end optimizations that remove promotions. ENUMERAL_TYPEs
975 are allowed here and are converted to their compatible integer types.
976 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
977 preferably a non-Boolean type as the common type. */
978 tree
979 common_type (tree t1, tree t2)
981 if (TREE_CODE (t1) == ENUMERAL_TYPE)
982 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
983 if (TREE_CODE (t2) == ENUMERAL_TYPE)
984 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
986 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
987 if (TREE_CODE (t1) == BOOLEAN_TYPE
988 && TREE_CODE (t2) == BOOLEAN_TYPE)
989 return boolean_type_node;
991 /* If either type is BOOLEAN_TYPE, then return the other. */
992 if (TREE_CODE (t1) == BOOLEAN_TYPE)
993 return t2;
994 if (TREE_CODE (t2) == BOOLEAN_TYPE)
995 return t1;
997 return c_common_type (t1, t2);
1000 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1001 or various other operations. Return 2 if they are compatible
1002 but a warning may be needed if you use them together. */
1005 comptypes (tree type1, tree type2)
1007 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1008 int val;
1010 val = comptypes_internal (type1, type2, NULL, NULL);
1011 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1013 return val;
1016 /* Like comptypes, but if it returns non-zero because enum and int are
1017 compatible, it sets *ENUM_AND_INT_P to true. */
1019 static int
1020 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1022 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1023 int val;
1025 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1026 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1028 return val;
1031 /* Like comptypes, but if it returns nonzero for different types, it
1032 sets *DIFFERENT_TYPES_P to true. */
1035 comptypes_check_different_types (tree type1, tree type2,
1036 bool *different_types_p)
1038 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1039 int val;
1041 val = comptypes_internal (type1, type2, NULL, different_types_p);
1042 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1044 return val;
1047 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1048 or various other operations. Return 2 if they are compatible
1049 but a warning may be needed if you use them together. If
1050 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1051 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1052 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1053 NULL, and the types are compatible but different enough not to be
1054 permitted in C11 typedef redeclarations, then this sets
1055 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1056 false, but may or may not be set if the types are incompatible.
1057 This differs from comptypes, in that we don't free the seen
1058 types. */
1060 static int
1061 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1062 bool *different_types_p)
1064 const_tree t1 = type1;
1065 const_tree t2 = type2;
1066 int attrval, val;
1068 /* Suppress errors caused by previously reported errors. */
1070 if (t1 == t2 || !t1 || !t2
1071 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1072 return 1;
1074 /* Enumerated types are compatible with integer types, but this is
1075 not transitive: two enumerated types in the same translation unit
1076 are compatible with each other only if they are the same type. */
1078 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1080 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1081 if (TREE_CODE (t2) != VOID_TYPE)
1083 if (enum_and_int_p != NULL)
1084 *enum_and_int_p = true;
1085 if (different_types_p != NULL)
1086 *different_types_p = true;
1089 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1091 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1092 if (TREE_CODE (t1) != VOID_TYPE)
1094 if (enum_and_int_p != NULL)
1095 *enum_and_int_p = true;
1096 if (different_types_p != NULL)
1097 *different_types_p = true;
1101 if (t1 == t2)
1102 return 1;
1104 /* Different classes of types can't be compatible. */
1106 if (TREE_CODE (t1) != TREE_CODE (t2))
1107 return 0;
1109 /* Qualifiers must match. C99 6.7.3p9 */
1111 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1112 return 0;
1114 /* Allow for two different type nodes which have essentially the same
1115 definition. Note that we already checked for equality of the type
1116 qualifiers (just above). */
1118 if (TREE_CODE (t1) != ARRAY_TYPE
1119 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1120 return 1;
1122 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1123 if (!(attrval = comp_type_attributes (t1, t2)))
1124 return 0;
1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1127 val = 0;
1129 switch (TREE_CODE (t1))
1131 case INTEGER_TYPE:
1132 case FIXED_POINT_TYPE:
1133 case REAL_TYPE:
1134 /* With these nodes, we can't determine type equivalence by
1135 looking at what is stored in the nodes themselves, because
1136 two nodes might have different TYPE_MAIN_VARIANTs but still
1137 represent the same type. For example, wchar_t and int could
1138 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1139 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1140 and are distinct types. On the other hand, int and the
1141 following typedef
1143 typedef int INT __attribute((may_alias));
1145 have identical properties, different TYPE_MAIN_VARIANTs, but
1146 represent the same type. The canonical type system keeps
1147 track of equivalence in this case, so we fall back on it. */
1148 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1150 case POINTER_TYPE:
1151 /* Do not remove mode information. */
1152 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1153 break;
1154 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1155 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1156 enum_and_int_p, different_types_p));
1157 break;
1159 case FUNCTION_TYPE:
1160 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1161 different_types_p);
1162 break;
1164 case ARRAY_TYPE:
1166 tree d1 = TYPE_DOMAIN (t1);
1167 tree d2 = TYPE_DOMAIN (t2);
1168 bool d1_variable, d2_variable;
1169 bool d1_zero, d2_zero;
1170 val = 1;
1172 /* Target types must match incl. qualifiers. */
1173 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1174 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1175 enum_and_int_p,
1176 different_types_p)))
1177 return 0;
1179 if (different_types_p != NULL
1180 && (d1 == 0) != (d2 == 0))
1181 *different_types_p = true;
1182 /* Sizes must match unless one is missing or variable. */
1183 if (d1 == 0 || d2 == 0 || d1 == d2)
1184 break;
1186 d1_zero = !TYPE_MAX_VALUE (d1);
1187 d2_zero = !TYPE_MAX_VALUE (d2);
1189 d1_variable = (!d1_zero
1190 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1191 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1192 d2_variable = (!d2_zero
1193 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1194 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1195 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1196 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1198 if (different_types_p != NULL
1199 && d1_variable != d2_variable)
1200 *different_types_p = true;
1201 if (d1_variable || d2_variable)
1202 break;
1203 if (d1_zero && d2_zero)
1204 break;
1205 if (d1_zero || d2_zero
1206 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1207 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1208 val = 0;
1210 break;
1213 case ENUMERAL_TYPE:
1214 case RECORD_TYPE:
1215 case UNION_TYPE:
1216 if (val != 1 && !same_translation_unit_p (t1, t2))
1218 tree a1 = TYPE_ATTRIBUTES (t1);
1219 tree a2 = TYPE_ATTRIBUTES (t2);
1221 if (! attribute_list_contained (a1, a2)
1222 && ! attribute_list_contained (a2, a1))
1223 break;
1225 if (attrval != 2)
1226 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1227 different_types_p);
1228 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1229 different_types_p);
1231 break;
1233 case VECTOR_TYPE:
1234 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1235 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1236 enum_and_int_p, different_types_p));
1237 break;
1239 default:
1240 break;
1242 return attrval == 2 && val == 1 ? 2 : val;
1245 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1246 their qualifiers, except for named address spaces. If the pointers point to
1247 different named addresses, then we must determine if one address space is a
1248 subset of the other. */
1250 static int
1251 comp_target_types (location_t location, tree ttl, tree ttr)
1253 int val;
1254 int val_ped;
1255 tree mvl = TREE_TYPE (ttl);
1256 tree mvr = TREE_TYPE (ttr);
1257 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1258 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1259 addr_space_t as_common;
1260 bool enum_and_int_p;
1262 /* Fail if pointers point to incompatible address spaces. */
1263 if (!addr_space_superset (asl, asr, &as_common))
1264 return 0;
1266 /* For pedantic record result of comptypes on arrays before losing
1267 qualifiers on the element type below. */
1268 val_ped = 1;
1270 if (TREE_CODE (mvl) == ARRAY_TYPE
1271 && TREE_CODE (mvr) == ARRAY_TYPE)
1272 val_ped = comptypes (mvl, mvr);
1274 /* Qualifiers on element types of array types that are
1275 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1277 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1278 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1279 : TYPE_MAIN_VARIANT (mvl));
1281 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1282 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1283 : TYPE_MAIN_VARIANT (mvr));
1285 enum_and_int_p = false;
1286 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1288 if (val == 1 && val_ped != 1)
1289 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1290 "are incompatible in ISO C");
1292 if (val == 2)
1293 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1295 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1296 warning_at (location, OPT_Wc___compat,
1297 "pointer target types incompatible in C++");
1299 return val;
1302 /* Subroutines of `comptypes'. */
1304 /* Determine whether two trees derive from the same translation unit.
1305 If the CONTEXT chain ends in a null, that tree's context is still
1306 being parsed, so if two trees have context chains ending in null,
1307 they're in the same translation unit. */
1309 same_translation_unit_p (const_tree t1, const_tree t2)
1311 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1312 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1314 case tcc_declaration:
1315 t1 = DECL_CONTEXT (t1); break;
1316 case tcc_type:
1317 t1 = TYPE_CONTEXT (t1); break;
1318 case tcc_exceptional:
1319 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1320 default: gcc_unreachable ();
1323 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1324 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1326 case tcc_declaration:
1327 t2 = DECL_CONTEXT (t2); break;
1328 case tcc_type:
1329 t2 = TYPE_CONTEXT (t2); break;
1330 case tcc_exceptional:
1331 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1332 default: gcc_unreachable ();
1335 return t1 == t2;
1338 /* Allocate the seen two types, assuming that they are compatible. */
1340 static struct tagged_tu_seen_cache *
1341 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1343 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1344 tu->next = tagged_tu_seen_base;
1345 tu->t1 = t1;
1346 tu->t2 = t2;
1348 tagged_tu_seen_base = tu;
1350 /* The C standard says that two structures in different translation
1351 units are compatible with each other only if the types of their
1352 fields are compatible (among other things). We assume that they
1353 are compatible until proven otherwise when building the cache.
1354 An example where this can occur is:
1355 struct a
1357 struct a *next;
1359 If we are comparing this against a similar struct in another TU,
1360 and did not assume they were compatible, we end up with an infinite
1361 loop. */
1362 tu->val = 1;
1363 return tu;
1366 /* Free the seen types until we get to TU_TIL. */
1368 static void
1369 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1371 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1372 while (tu != tu_til)
1374 const struct tagged_tu_seen_cache *const tu1
1375 = (const struct tagged_tu_seen_cache *) tu;
1376 tu = tu1->next;
1377 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1379 tagged_tu_seen_base = tu_til;
1382 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1383 compatible. If the two types are not the same (which has been
1384 checked earlier), this can only happen when multiple translation
1385 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1386 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1387 comptypes_internal. */
1389 static int
1390 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1391 bool *enum_and_int_p, bool *different_types_p)
1393 tree s1, s2;
1394 bool needs_warning = false;
1396 /* We have to verify that the tags of the types are the same. This
1397 is harder than it looks because this may be a typedef, so we have
1398 to go look at the original type. It may even be a typedef of a
1399 typedef...
1400 In the case of compiler-created builtin structs the TYPE_DECL
1401 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1402 while (TYPE_NAME (t1)
1403 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1404 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1405 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1407 while (TYPE_NAME (t2)
1408 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1409 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1410 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1412 /* C90 didn't have the requirement that the two tags be the same. */
1413 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1414 return 0;
1416 /* C90 didn't say what happened if one or both of the types were
1417 incomplete; we choose to follow C99 rules here, which is that they
1418 are compatible. */
1419 if (TYPE_SIZE (t1) == NULL
1420 || TYPE_SIZE (t2) == NULL)
1421 return 1;
1424 const struct tagged_tu_seen_cache * tts_i;
1425 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1426 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1427 return tts_i->val;
1430 switch (TREE_CODE (t1))
1432 case ENUMERAL_TYPE:
1434 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1435 /* Speed up the case where the type values are in the same order. */
1436 tree tv1 = TYPE_VALUES (t1);
1437 tree tv2 = TYPE_VALUES (t2);
1439 if (tv1 == tv2)
1441 return 1;
1444 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1446 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1447 break;
1448 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1450 tu->val = 0;
1451 return 0;
1455 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1457 return 1;
1459 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1461 tu->val = 0;
1462 return 0;
1465 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1467 tu->val = 0;
1468 return 0;
1471 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1473 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1474 if (s2 == NULL
1475 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1477 tu->val = 0;
1478 return 0;
1481 return 1;
1484 case UNION_TYPE:
1486 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1487 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1489 tu->val = 0;
1490 return 0;
1493 /* Speed up the common case where the fields are in the same order. */
1494 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1495 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1497 int result;
1499 if (DECL_NAME (s1) != DECL_NAME (s2))
1500 break;
1501 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1502 enum_and_int_p, different_types_p);
1504 if (result != 1 && !DECL_NAME (s1))
1505 break;
1506 if (result == 0)
1508 tu->val = 0;
1509 return 0;
1511 if (result == 2)
1512 needs_warning = true;
1514 if (TREE_CODE (s1) == FIELD_DECL
1515 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1516 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518 tu->val = 0;
1519 return 0;
1522 if (!s1 && !s2)
1524 tu->val = needs_warning ? 2 : 1;
1525 return tu->val;
1528 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1530 bool ok = false;
1532 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1533 if (DECL_NAME (s1) == DECL_NAME (s2))
1535 int result;
1537 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1538 enum_and_int_p,
1539 different_types_p);
1541 if (result != 1 && !DECL_NAME (s1))
1542 continue;
1543 if (result == 0)
1545 tu->val = 0;
1546 return 0;
1548 if (result == 2)
1549 needs_warning = true;
1551 if (TREE_CODE (s1) == FIELD_DECL
1552 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1553 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1554 break;
1556 ok = true;
1557 break;
1559 if (!ok)
1561 tu->val = 0;
1562 return 0;
1565 tu->val = needs_warning ? 2 : 10;
1566 return tu->val;
1569 case RECORD_TYPE:
1571 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1573 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1574 s1 && s2;
1575 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1577 int result;
1578 if (TREE_CODE (s1) != TREE_CODE (s2)
1579 || DECL_NAME (s1) != DECL_NAME (s2))
1580 break;
1581 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1582 enum_and_int_p, different_types_p);
1583 if (result == 0)
1584 break;
1585 if (result == 2)
1586 needs_warning = true;
1588 if (TREE_CODE (s1) == FIELD_DECL
1589 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1590 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1591 break;
1593 if (s1 && s2)
1594 tu->val = 0;
1595 else
1596 tu->val = needs_warning ? 2 : 1;
1597 return tu->val;
1600 default:
1601 gcc_unreachable ();
1605 /* Return 1 if two function types F1 and F2 are compatible.
1606 If either type specifies no argument types,
1607 the other must specify a fixed number of self-promoting arg types.
1608 Otherwise, if one type specifies only the number of arguments,
1609 the other must specify that number of self-promoting arg types.
1610 Otherwise, the argument types must match.
1611 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1613 static int
1614 function_types_compatible_p (const_tree f1, const_tree f2,
1615 bool *enum_and_int_p, bool *different_types_p)
1617 tree args1, args2;
1618 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1619 int val = 1;
1620 int val1;
1621 tree ret1, ret2;
1623 ret1 = TREE_TYPE (f1);
1624 ret2 = TREE_TYPE (f2);
1626 /* 'volatile' qualifiers on a function's return type used to mean
1627 the function is noreturn. */
1628 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1629 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1630 if (TYPE_VOLATILE (ret1))
1631 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1632 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1633 if (TYPE_VOLATILE (ret2))
1634 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1635 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1636 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1637 if (val == 0)
1638 return 0;
1640 args1 = TYPE_ARG_TYPES (f1);
1641 args2 = TYPE_ARG_TYPES (f2);
1643 if (different_types_p != NULL
1644 && (args1 == 0) != (args2 == 0))
1645 *different_types_p = true;
1647 /* An unspecified parmlist matches any specified parmlist
1648 whose argument types don't need default promotions. */
1650 if (args1 == 0)
1652 if (!self_promoting_args_p (args2))
1653 return 0;
1654 /* If one of these types comes from a non-prototype fn definition,
1655 compare that with the other type's arglist.
1656 If they don't match, ask for a warning (but no error). */
1657 if (TYPE_ACTUAL_ARG_TYPES (f1)
1658 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1659 enum_and_int_p, different_types_p))
1660 val = 2;
1661 return val;
1663 if (args2 == 0)
1665 if (!self_promoting_args_p (args1))
1666 return 0;
1667 if (TYPE_ACTUAL_ARG_TYPES (f2)
1668 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1669 enum_and_int_p, different_types_p))
1670 val = 2;
1671 return val;
1674 /* Both types have argument lists: compare them and propagate results. */
1675 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1676 different_types_p);
1677 return val1 != 1 ? val1 : val;
1680 /* Check two lists of types for compatibility, returning 0 for
1681 incompatible, 1 for compatible, or 2 for compatible with
1682 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1683 comptypes_internal. */
1685 static int
1686 type_lists_compatible_p (const_tree args1, const_tree args2,
1687 bool *enum_and_int_p, bool *different_types_p)
1689 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1690 int val = 1;
1691 int newval = 0;
1693 while (1)
1695 tree a1, mv1, a2, mv2;
1696 if (args1 == 0 && args2 == 0)
1697 return val;
1698 /* If one list is shorter than the other,
1699 they fail to match. */
1700 if (args1 == 0 || args2 == 0)
1701 return 0;
1702 mv1 = a1 = TREE_VALUE (args1);
1703 mv2 = a2 = TREE_VALUE (args2);
1704 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1705 mv1 = (TYPE_ATOMIC (mv1)
1706 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1707 TYPE_QUAL_ATOMIC)
1708 : TYPE_MAIN_VARIANT (mv1));
1709 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1710 mv2 = (TYPE_ATOMIC (mv2)
1711 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1712 TYPE_QUAL_ATOMIC)
1713 : TYPE_MAIN_VARIANT (mv2));
1714 /* A null pointer instead of a type
1715 means there is supposed to be an argument
1716 but nothing is specified about what type it has.
1717 So match anything that self-promotes. */
1718 if (different_types_p != NULL
1719 && (a1 == 0) != (a2 == 0))
1720 *different_types_p = true;
1721 if (a1 == 0)
1723 if (c_type_promotes_to (a2) != a2)
1724 return 0;
1726 else if (a2 == 0)
1728 if (c_type_promotes_to (a1) != a1)
1729 return 0;
1731 /* If one of the lists has an error marker, ignore this arg. */
1732 else if (TREE_CODE (a1) == ERROR_MARK
1733 || TREE_CODE (a2) == ERROR_MARK)
1735 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1736 different_types_p)))
1738 if (different_types_p != NULL)
1739 *different_types_p = true;
1740 /* Allow wait (union {union wait *u; int *i} *)
1741 and wait (union wait *) to be compatible. */
1742 if (TREE_CODE (a1) == UNION_TYPE
1743 && (TYPE_NAME (a1) == 0
1744 || TYPE_TRANSPARENT_AGGR (a1))
1745 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1746 && tree_int_cst_equal (TYPE_SIZE (a1),
1747 TYPE_SIZE (a2)))
1749 tree memb;
1750 for (memb = TYPE_FIELDS (a1);
1751 memb; memb = DECL_CHAIN (memb))
1753 tree mv3 = TREE_TYPE (memb);
1754 if (mv3 && mv3 != error_mark_node
1755 && TREE_CODE (mv3) != ARRAY_TYPE)
1756 mv3 = (TYPE_ATOMIC (mv3)
1757 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1758 TYPE_QUAL_ATOMIC)
1759 : TYPE_MAIN_VARIANT (mv3));
1760 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1761 different_types_p))
1762 break;
1764 if (memb == 0)
1765 return 0;
1767 else if (TREE_CODE (a2) == UNION_TYPE
1768 && (TYPE_NAME (a2) == 0
1769 || TYPE_TRANSPARENT_AGGR (a2))
1770 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1771 && tree_int_cst_equal (TYPE_SIZE (a2),
1772 TYPE_SIZE (a1)))
1774 tree memb;
1775 for (memb = TYPE_FIELDS (a2);
1776 memb; memb = DECL_CHAIN (memb))
1778 tree mv3 = TREE_TYPE (memb);
1779 if (mv3 && mv3 != error_mark_node
1780 && TREE_CODE (mv3) != ARRAY_TYPE)
1781 mv3 = (TYPE_ATOMIC (mv3)
1782 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1783 TYPE_QUAL_ATOMIC)
1784 : TYPE_MAIN_VARIANT (mv3));
1785 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1786 different_types_p))
1787 break;
1789 if (memb == 0)
1790 return 0;
1792 else
1793 return 0;
1796 /* comptypes said ok, but record if it said to warn. */
1797 if (newval > val)
1798 val = newval;
1800 args1 = TREE_CHAIN (args1);
1801 args2 = TREE_CHAIN (args2);
1805 /* Compute the size to increment a pointer by. When a function type or void
1806 type or incomplete type is passed, size_one_node is returned.
1807 This function does not emit any diagnostics; the caller is responsible
1808 for that. */
1810 static tree
1811 c_size_in_bytes (const_tree type)
1813 enum tree_code code = TREE_CODE (type);
1815 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1816 || !COMPLETE_TYPE_P (type))
1817 return size_one_node;
1819 /* Convert in case a char is more than one unit. */
1820 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1821 size_int (TYPE_PRECISION (char_type_node)
1822 / BITS_PER_UNIT));
1825 /* Return either DECL or its known constant value (if it has one). */
1827 tree
1828 decl_constant_value (tree decl)
1830 if (/* Don't change a variable array bound or initial value to a constant
1831 in a place where a variable is invalid. Note that DECL_INITIAL
1832 isn't valid for a PARM_DECL. */
1833 current_function_decl != 0
1834 && TREE_CODE (decl) != PARM_DECL
1835 && !TREE_THIS_VOLATILE (decl)
1836 && TREE_READONLY (decl)
1837 && DECL_INITIAL (decl) != 0
1838 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1839 /* This is invalid if initial value is not constant.
1840 If it has either a function call, a memory reference,
1841 or a variable, then re-evaluating it could give different results. */
1842 && TREE_CONSTANT (DECL_INITIAL (decl))
1843 /* Check for cases where this is sub-optimal, even though valid. */
1844 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1845 return DECL_INITIAL (decl);
1846 return decl;
1849 /* Convert the array expression EXP to a pointer. */
1850 static tree
1851 array_to_pointer_conversion (location_t loc, tree exp)
1853 tree orig_exp = exp;
1854 tree type = TREE_TYPE (exp);
1855 tree adr;
1856 tree restype = TREE_TYPE (type);
1857 tree ptrtype;
1859 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1861 STRIP_TYPE_NOPS (exp);
1863 if (TREE_NO_WARNING (orig_exp))
1864 TREE_NO_WARNING (exp) = 1;
1866 ptrtype = build_pointer_type (restype);
1868 if (INDIRECT_REF_P (exp))
1869 return convert (ptrtype, TREE_OPERAND (exp, 0));
1871 /* In C++ array compound literals are temporary objects unless they are
1872 const or appear in namespace scope, so they are destroyed too soon
1873 to use them for much of anything (c++/53220). */
1874 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1876 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1877 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1878 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1879 "converting an array compound literal to a pointer "
1880 "is ill-formed in C++");
1883 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1884 return convert (ptrtype, adr);
1887 /* Convert the function expression EXP to a pointer. */
1888 static tree
1889 function_to_pointer_conversion (location_t loc, tree exp)
1891 tree orig_exp = exp;
1893 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1895 STRIP_TYPE_NOPS (exp);
1897 if (TREE_NO_WARNING (orig_exp))
1898 TREE_NO_WARNING (exp) = 1;
1900 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1903 /* Mark EXP as read, not just set, for set but not used -Wunused
1904 warning purposes. */
1906 void
1907 mark_exp_read (tree exp)
1909 switch (TREE_CODE (exp))
1911 case VAR_DECL:
1912 case PARM_DECL:
1913 DECL_READ_P (exp) = 1;
1914 break;
1915 case ARRAY_REF:
1916 case COMPONENT_REF:
1917 case MODIFY_EXPR:
1918 case REALPART_EXPR:
1919 case IMAGPART_EXPR:
1920 CASE_CONVERT:
1921 case ADDR_EXPR:
1922 case VIEW_CONVERT_EXPR:
1923 mark_exp_read (TREE_OPERAND (exp, 0));
1924 break;
1925 case COMPOUND_EXPR:
1926 case C_MAYBE_CONST_EXPR:
1927 mark_exp_read (TREE_OPERAND (exp, 1));
1928 break;
1929 default:
1930 break;
1934 /* Perform the default conversion of arrays and functions to pointers.
1935 Return the result of converting EXP. For any other expression, just
1936 return EXP.
1938 LOC is the location of the expression. */
1940 struct c_expr
1941 default_function_array_conversion (location_t loc, struct c_expr exp)
1943 tree orig_exp = exp.value;
1944 tree type = TREE_TYPE (exp.value);
1945 enum tree_code code = TREE_CODE (type);
1947 switch (code)
1949 case ARRAY_TYPE:
1951 bool not_lvalue = false;
1952 bool lvalue_array_p;
1954 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1955 || CONVERT_EXPR_P (exp.value))
1956 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1958 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1959 not_lvalue = true;
1960 exp.value = TREE_OPERAND (exp.value, 0);
1963 if (TREE_NO_WARNING (orig_exp))
1964 TREE_NO_WARNING (exp.value) = 1;
1966 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1967 if (!flag_isoc99 && !lvalue_array_p)
1969 /* Before C99, non-lvalue arrays do not decay to pointers.
1970 Normally, using such an array would be invalid; but it can
1971 be used correctly inside sizeof or as a statement expression.
1972 Thus, do not give an error here; an error will result later. */
1973 return exp;
1976 exp.value = array_to_pointer_conversion (loc, exp.value);
1978 break;
1979 case FUNCTION_TYPE:
1980 exp.value = function_to_pointer_conversion (loc, exp.value);
1981 break;
1982 default:
1983 break;
1986 return exp;
1989 struct c_expr
1990 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1992 mark_exp_read (exp.value);
1993 return default_function_array_conversion (loc, exp);
1996 /* Return whether EXPR should be treated as an atomic lvalue for the
1997 purposes of load and store handling. */
1999 static bool
2000 really_atomic_lvalue (tree expr)
2002 if (error_operand_p (expr))
2003 return false;
2004 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2005 return false;
2006 if (!lvalue_p (expr))
2007 return false;
2009 /* Ignore _Atomic on register variables, since their addresses can't
2010 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2011 sequences wouldn't work. Ignore _Atomic on structures containing
2012 bit-fields, since accessing elements of atomic structures or
2013 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2014 it's undefined at translation time or execution time, and the
2015 normal atomic sequences again wouldn't work. */
2016 while (handled_component_p (expr))
2018 if (TREE_CODE (expr) == COMPONENT_REF
2019 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2020 return false;
2021 expr = TREE_OPERAND (expr, 0);
2023 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2024 return false;
2025 return true;
2028 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2029 including converting functions and arrays to pointers if CONVERT_P.
2030 If READ_P, also mark the expression as having been read. */
2032 struct c_expr
2033 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2034 bool convert_p, bool read_p)
2036 if (read_p)
2037 mark_exp_read (exp.value);
2038 if (convert_p)
2039 exp = default_function_array_conversion (loc, exp);
2040 if (really_atomic_lvalue (exp.value))
2042 vec<tree, va_gc> *params;
2043 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2044 tree expr_type = TREE_TYPE (exp.value);
2045 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2046 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2048 gcc_assert (TYPE_ATOMIC (expr_type));
2050 /* Expansion of a generic atomic load may require an addition
2051 element, so allocate enough to prevent a resize. */
2052 vec_alloc (params, 4);
2054 /* Remove the qualifiers for the rest of the expressions and
2055 create the VAL temp variable to hold the RHS. */
2056 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2057 tmp = create_tmp_var_raw (nonatomic_type);
2058 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2059 TREE_ADDRESSABLE (tmp) = 1;
2060 TREE_NO_WARNING (tmp) = 1;
2062 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2063 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2064 params->quick_push (expr_addr);
2065 params->quick_push (tmp_addr);
2066 params->quick_push (seq_cst);
2067 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2069 /* EXPR is always read. */
2070 mark_exp_read (exp.value);
2072 /* Return tmp which contains the value loaded. */
2073 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2074 NULL_TREE, NULL_TREE);
2076 return exp;
2079 /* EXP is an expression of integer type. Apply the integer promotions
2080 to it and return the promoted value. */
2082 tree
2083 perform_integral_promotions (tree exp)
2085 tree type = TREE_TYPE (exp);
2086 enum tree_code code = TREE_CODE (type);
2088 gcc_assert (INTEGRAL_TYPE_P (type));
2090 /* Normally convert enums to int,
2091 but convert wide enums to something wider. */
2092 if (code == ENUMERAL_TYPE)
2094 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2095 TYPE_PRECISION (integer_type_node)),
2096 ((TYPE_PRECISION (type)
2097 >= TYPE_PRECISION (integer_type_node))
2098 && TYPE_UNSIGNED (type)));
2100 return convert (type, exp);
2103 /* ??? This should no longer be needed now bit-fields have their
2104 proper types. */
2105 if (TREE_CODE (exp) == COMPONENT_REF
2106 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2107 /* If it's thinner than an int, promote it like a
2108 c_promoting_integer_type_p, otherwise leave it alone. */
2109 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2110 TYPE_PRECISION (integer_type_node)))
2111 return convert (integer_type_node, exp);
2113 if (c_promoting_integer_type_p (type))
2115 /* Preserve unsignedness if not really getting any wider. */
2116 if (TYPE_UNSIGNED (type)
2117 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2118 return convert (unsigned_type_node, exp);
2120 return convert (integer_type_node, exp);
2123 return exp;
2127 /* Perform default promotions for C data used in expressions.
2128 Enumeral types or short or char are converted to int.
2129 In addition, manifest constants symbols are replaced by their values. */
2131 tree
2132 default_conversion (tree exp)
2134 tree orig_exp;
2135 tree type = TREE_TYPE (exp);
2136 enum tree_code code = TREE_CODE (type);
2137 tree promoted_type;
2139 mark_exp_read (exp);
2141 /* Functions and arrays have been converted during parsing. */
2142 gcc_assert (code != FUNCTION_TYPE);
2143 if (code == ARRAY_TYPE)
2144 return exp;
2146 /* Constants can be used directly unless they're not loadable. */
2147 if (TREE_CODE (exp) == CONST_DECL)
2148 exp = DECL_INITIAL (exp);
2150 /* Strip no-op conversions. */
2151 orig_exp = exp;
2152 STRIP_TYPE_NOPS (exp);
2154 if (TREE_NO_WARNING (orig_exp))
2155 TREE_NO_WARNING (exp) = 1;
2157 if (code == VOID_TYPE)
2159 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2160 "void value not ignored as it ought to be");
2161 return error_mark_node;
2164 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2165 if (exp == error_mark_node)
2166 return error_mark_node;
2168 promoted_type = targetm.promoted_type (type);
2169 if (promoted_type)
2170 return convert (promoted_type, exp);
2172 if (INTEGRAL_TYPE_P (type))
2173 return perform_integral_promotions (exp);
2175 return exp;
2178 /* Look up COMPONENT in a structure or union TYPE.
2180 If the component name is not found, returns NULL_TREE. Otherwise,
2181 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2182 stepping down the chain to the component, which is in the last
2183 TREE_VALUE of the list. Normally the list is of length one, but if
2184 the component is embedded within (nested) anonymous structures or
2185 unions, the list steps down the chain to the component. */
2187 static tree
2188 lookup_field (tree type, tree component)
2190 tree field;
2192 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2193 to the field elements. Use a binary search on this array to quickly
2194 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2195 will always be set for structures which have many elements. */
2197 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2199 int bot, top, half;
2200 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2202 field = TYPE_FIELDS (type);
2203 bot = 0;
2204 top = TYPE_LANG_SPECIFIC (type)->s->len;
2205 while (top - bot > 1)
2207 half = (top - bot + 1) >> 1;
2208 field = field_array[bot+half];
2210 if (DECL_NAME (field) == NULL_TREE)
2212 /* Step through all anon unions in linear fashion. */
2213 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2215 field = field_array[bot++];
2216 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2218 tree anon = lookup_field (TREE_TYPE (field), component);
2220 if (anon)
2221 return tree_cons (NULL_TREE, field, anon);
2223 /* The Plan 9 compiler permits referring
2224 directly to an anonymous struct/union field
2225 using a typedef name. */
2226 if (flag_plan9_extensions
2227 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2228 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2229 == TYPE_DECL)
2230 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2231 == component))
2232 break;
2236 /* Entire record is only anon unions. */
2237 if (bot > top)
2238 return NULL_TREE;
2240 /* Restart the binary search, with new lower bound. */
2241 continue;
2244 if (DECL_NAME (field) == component)
2245 break;
2246 if (DECL_NAME (field) < component)
2247 bot += half;
2248 else
2249 top = bot + half;
2252 if (DECL_NAME (field_array[bot]) == component)
2253 field = field_array[bot];
2254 else if (DECL_NAME (field) != component)
2255 return NULL_TREE;
2257 else
2259 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2261 if (DECL_NAME (field) == NULL_TREE
2262 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2264 tree anon = lookup_field (TREE_TYPE (field), component);
2266 if (anon)
2267 return tree_cons (NULL_TREE, field, anon);
2269 /* The Plan 9 compiler permits referring directly to an
2270 anonymous struct/union field using a typedef
2271 name. */
2272 if (flag_plan9_extensions
2273 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2274 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2275 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2276 == component))
2277 break;
2280 if (DECL_NAME (field) == component)
2281 break;
2284 if (field == NULL_TREE)
2285 return NULL_TREE;
2288 return tree_cons (NULL_TREE, field, NULL_TREE);
2291 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2293 static void
2294 lookup_field_fuzzy_find_candidates (tree type, tree component,
2295 vec<tree> *candidates)
2297 tree field;
2298 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2300 if (DECL_NAME (field) == NULL_TREE
2301 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2302 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2303 candidates);
2305 if (DECL_NAME (field))
2306 candidates->safe_push (DECL_NAME (field));
2310 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2311 rather than returning a TREE_LIST for an exact match. */
2313 static tree
2314 lookup_field_fuzzy (tree type, tree component)
2316 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2318 /* First, gather a list of candidates. */
2319 auto_vec <tree> candidates;
2321 lookup_field_fuzzy_find_candidates (type, component,
2322 &candidates);
2324 return find_closest_identifier (component, &candidates);
2327 /* Support function for build_component_ref's error-handling.
2329 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2330 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2332 static bool
2333 should_suggest_deref_p (tree datum_type)
2335 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2336 allows "." for ptrs; we could be handling a failed attempt
2337 to access a property. */
2338 if (c_dialect_objc ())
2339 return false;
2341 /* Only suggest it for pointers... */
2342 if (TREE_CODE (datum_type) != POINTER_TYPE)
2343 return false;
2345 /* ...to structs/unions. */
2346 tree underlying_type = TREE_TYPE (datum_type);
2347 enum tree_code code = TREE_CODE (underlying_type);
2348 if (code == RECORD_TYPE || code == UNION_TYPE)
2349 return true;
2350 else
2351 return false;
2354 /* Make an expression to refer to the COMPONENT field of structure or
2355 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2356 location of the COMPONENT_REF. COMPONENT_LOC is the location
2357 of COMPONENT. */
2359 tree
2360 build_component_ref (location_t loc, tree datum, tree component,
2361 location_t component_loc)
2363 tree type = TREE_TYPE (datum);
2364 enum tree_code code = TREE_CODE (type);
2365 tree field = NULL;
2366 tree ref;
2367 bool datum_lvalue = lvalue_p (datum);
2369 if (!objc_is_public (datum, component))
2370 return error_mark_node;
2372 /* Detect Objective-C property syntax object.property. */
2373 if (c_dialect_objc ()
2374 && (ref = objc_maybe_build_component_ref (datum, component)))
2375 return ref;
2377 /* See if there is a field or component with name COMPONENT. */
2379 if (code == RECORD_TYPE || code == UNION_TYPE)
2381 if (!COMPLETE_TYPE_P (type))
2383 c_incomplete_type_error (loc, NULL_TREE, type);
2384 return error_mark_node;
2387 field = lookup_field (type, component);
2389 if (!field)
2391 tree guessed_id = lookup_field_fuzzy (type, component);
2392 if (guessed_id)
2394 /* Attempt to provide a fixit replacement hint, if
2395 we have a valid range for the component. */
2396 location_t reported_loc
2397 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2398 gcc_rich_location rich_loc (reported_loc);
2399 if (component_loc != UNKNOWN_LOCATION)
2400 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2401 error_at_rich_loc
2402 (&rich_loc,
2403 "%qT has no member named %qE; did you mean %qE?",
2404 type, component, guessed_id);
2406 else
2407 error_at (loc, "%qT has no member named %qE", type, component);
2408 return error_mark_node;
2411 /* Accessing elements of atomic structures or unions is undefined
2412 behavior (C11 6.5.2.3#5). */
2413 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2415 if (code == RECORD_TYPE)
2416 warning_at (loc, 0, "accessing a member %qE of an atomic "
2417 "structure %qE", component, datum);
2418 else
2419 warning_at (loc, 0, "accessing a member %qE of an atomic "
2420 "union %qE", component, datum);
2423 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2424 This might be better solved in future the way the C++ front
2425 end does it - by giving the anonymous entities each a
2426 separate name and type, and then have build_component_ref
2427 recursively call itself. We can't do that here. */
2430 tree subdatum = TREE_VALUE (field);
2431 int quals;
2432 tree subtype;
2433 bool use_datum_quals;
2435 if (TREE_TYPE (subdatum) == error_mark_node)
2436 return error_mark_node;
2438 /* If this is an rvalue, it does not have qualifiers in C
2439 standard terms and we must avoid propagating such
2440 qualifiers down to a non-lvalue array that is then
2441 converted to a pointer. */
2442 use_datum_quals = (datum_lvalue
2443 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2445 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2446 if (use_datum_quals)
2447 quals |= TYPE_QUALS (TREE_TYPE (datum));
2448 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2450 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2451 NULL_TREE);
2452 SET_EXPR_LOCATION (ref, loc);
2453 if (TREE_READONLY (subdatum)
2454 || (use_datum_quals && TREE_READONLY (datum)))
2455 TREE_READONLY (ref) = 1;
2456 if (TREE_THIS_VOLATILE (subdatum)
2457 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2458 TREE_THIS_VOLATILE (ref) = 1;
2460 if (TREE_DEPRECATED (subdatum))
2461 warn_deprecated_use (subdatum, NULL_TREE);
2463 datum = ref;
2465 field = TREE_CHAIN (field);
2467 while (field);
2469 return ref;
2471 else if (should_suggest_deref_p (type))
2473 /* Special-case the error message for "ptr.field" for the case
2474 where the user has confused "." vs "->". */
2475 rich_location richloc (line_table, loc);
2476 /* "loc" should be the "." token. */
2477 richloc.add_fixit_replace (source_range::from_location (loc), "->");
2478 error_at_rich_loc (&richloc,
2479 "%qE is a pointer; did you mean to use %<->%>?",
2480 datum);
2481 return error_mark_node;
2483 else if (code != ERROR_MARK)
2484 error_at (loc,
2485 "request for member %qE in something not a structure or union",
2486 component);
2488 return error_mark_node;
2491 /* Given an expression PTR for a pointer, return an expression
2492 for the value pointed to.
2493 ERRORSTRING is the name of the operator to appear in error messages.
2495 LOC is the location to use for the generated tree. */
2497 tree
2498 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2500 tree pointer = default_conversion (ptr);
2501 tree type = TREE_TYPE (pointer);
2502 tree ref;
2504 if (TREE_CODE (type) == POINTER_TYPE)
2506 if (CONVERT_EXPR_P (pointer)
2507 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2509 /* If a warning is issued, mark it to avoid duplicates from
2510 the backend. This only needs to be done at
2511 warn_strict_aliasing > 2. */
2512 if (warn_strict_aliasing > 2)
2513 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2514 type, TREE_OPERAND (pointer, 0)))
2515 TREE_NO_WARNING (pointer) = 1;
2518 if (TREE_CODE (pointer) == ADDR_EXPR
2519 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2520 == TREE_TYPE (type)))
2522 ref = TREE_OPERAND (pointer, 0);
2523 protected_set_expr_location (ref, loc);
2524 return ref;
2526 else
2528 tree t = TREE_TYPE (type);
2530 ref = build1 (INDIRECT_REF, t, pointer);
2532 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2534 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2536 error_at (loc, "dereferencing pointer to incomplete type "
2537 "%qT", t);
2538 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2540 return error_mark_node;
2542 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2543 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2545 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2546 so that we get the proper error message if the result is used
2547 to assign to. Also, &* is supposed to be a no-op.
2548 And ANSI C seems to specify that the type of the result
2549 should be the const type. */
2550 /* A de-reference of a pointer to const is not a const. It is valid
2551 to change it via some other pointer. */
2552 TREE_READONLY (ref) = TYPE_READONLY (t);
2553 TREE_SIDE_EFFECTS (ref)
2554 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2555 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2556 protected_set_expr_location (ref, loc);
2557 return ref;
2560 else if (TREE_CODE (pointer) != ERROR_MARK)
2561 invalid_indirection_error (loc, type, errstring);
2563 return error_mark_node;
2566 /* This handles expressions of the form "a[i]", which denotes
2567 an array reference.
2569 This is logically equivalent in C to *(a+i), but we may do it differently.
2570 If A is a variable or a member, we generate a primitive ARRAY_REF.
2571 This avoids forcing the array out of registers, and can work on
2572 arrays that are not lvalues (for example, members of structures returned
2573 by functions).
2575 For vector types, allow vector[i] but not i[vector], and create
2576 *(((type*)&vectortype) + i) for the expression.
2578 LOC is the location to use for the returned expression. */
2580 tree
2581 build_array_ref (location_t loc, tree array, tree index)
2583 tree ret;
2584 bool swapped = false;
2585 if (TREE_TYPE (array) == error_mark_node
2586 || TREE_TYPE (index) == error_mark_node)
2587 return error_mark_node;
2589 if (flag_cilkplus && contains_array_notation_expr (index))
2591 size_t rank = 0;
2592 if (!find_rank (loc, index, index, true, &rank))
2593 return error_mark_node;
2594 if (rank > 1)
2596 error_at (loc, "rank of the array's index is greater than 1");
2597 return error_mark_node;
2600 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2601 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2602 /* Allow vector[index] but not index[vector]. */
2603 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2605 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2606 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2608 error_at (loc,
2609 "subscripted value is neither array nor pointer nor vector");
2611 return error_mark_node;
2613 std::swap (array, index);
2614 swapped = true;
2617 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2619 error_at (loc, "array subscript is not an integer");
2620 return error_mark_node;
2623 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2625 error_at (loc, "subscripted value is pointer to function");
2626 return error_mark_node;
2629 /* ??? Existing practice has been to warn only when the char
2630 index is syntactically the index, not for char[array]. */
2631 if (!swapped)
2632 warn_array_subscript_with_type_char (loc, index);
2634 /* Apply default promotions *after* noticing character types. */
2635 index = default_conversion (index);
2636 if (index == error_mark_node)
2637 return error_mark_node;
2639 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2641 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2642 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2644 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2646 tree rval, type;
2648 /* An array that is indexed by a non-constant
2649 cannot be stored in a register; we must be able to do
2650 address arithmetic on its address.
2651 Likewise an array of elements of variable size. */
2652 if (TREE_CODE (index) != INTEGER_CST
2653 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2654 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2656 if (!c_mark_addressable (array))
2657 return error_mark_node;
2659 /* An array that is indexed by a constant value which is not within
2660 the array bounds cannot be stored in a register either; because we
2661 would get a crash in store_bit_field/extract_bit_field when trying
2662 to access a non-existent part of the register. */
2663 if (TREE_CODE (index) == INTEGER_CST
2664 && TYPE_DOMAIN (TREE_TYPE (array))
2665 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2667 if (!c_mark_addressable (array))
2668 return error_mark_node;
2671 if ((pedantic || warn_c90_c99_compat)
2672 && ! was_vector)
2674 tree foo = array;
2675 while (TREE_CODE (foo) == COMPONENT_REF)
2676 foo = TREE_OPERAND (foo, 0);
2677 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2678 pedwarn (loc, OPT_Wpedantic,
2679 "ISO C forbids subscripting %<register%> array");
2680 else if (!lvalue_p (foo))
2681 pedwarn_c90 (loc, OPT_Wpedantic,
2682 "ISO C90 forbids subscripting non-lvalue "
2683 "array");
2686 type = TREE_TYPE (TREE_TYPE (array));
2687 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2688 /* Array ref is const/volatile if the array elements are
2689 or if the array is. */
2690 TREE_READONLY (rval)
2691 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2692 | TREE_READONLY (array));
2693 TREE_SIDE_EFFECTS (rval)
2694 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2695 | TREE_SIDE_EFFECTS (array));
2696 TREE_THIS_VOLATILE (rval)
2697 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2698 /* This was added by rms on 16 Nov 91.
2699 It fixes vol struct foo *a; a->elts[1]
2700 in an inline function.
2701 Hope it doesn't break something else. */
2702 | TREE_THIS_VOLATILE (array));
2703 ret = require_complete_type (loc, rval);
2704 protected_set_expr_location (ret, loc);
2705 if (non_lvalue)
2706 ret = non_lvalue_loc (loc, ret);
2707 return ret;
2709 else
2711 tree ar = default_conversion (array);
2713 if (ar == error_mark_node)
2714 return ar;
2716 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2717 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2719 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2720 index, 0),
2721 RO_ARRAY_INDEXING);
2722 if (non_lvalue)
2723 ret = non_lvalue_loc (loc, ret);
2724 return ret;
2728 /* Build an external reference to identifier ID. FUN indicates
2729 whether this will be used for a function call. LOC is the source
2730 location of the identifier. This sets *TYPE to the type of the
2731 identifier, which is not the same as the type of the returned value
2732 for CONST_DECLs defined as enum constants. If the type of the
2733 identifier is not available, *TYPE is set to NULL. */
2734 tree
2735 build_external_ref (location_t loc, tree id, int fun, tree *type)
2737 tree ref;
2738 tree decl = lookup_name (id);
2740 /* In Objective-C, an instance variable (ivar) may be preferred to
2741 whatever lookup_name() found. */
2742 decl = objc_lookup_ivar (decl, id);
2744 *type = NULL;
2745 if (decl && decl != error_mark_node)
2747 ref = decl;
2748 *type = TREE_TYPE (ref);
2750 else if (fun)
2751 /* Implicit function declaration. */
2752 ref = implicitly_declare (loc, id);
2753 else if (decl == error_mark_node)
2754 /* Don't complain about something that's already been
2755 complained about. */
2756 return error_mark_node;
2757 else
2759 undeclared_variable (loc, id);
2760 return error_mark_node;
2763 if (TREE_TYPE (ref) == error_mark_node)
2764 return error_mark_node;
2766 if (TREE_DEPRECATED (ref))
2767 warn_deprecated_use (ref, NULL_TREE);
2769 /* Recursive call does not count as usage. */
2770 if (ref != current_function_decl)
2772 TREE_USED (ref) = 1;
2775 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2777 if (!in_sizeof && !in_typeof)
2778 C_DECL_USED (ref) = 1;
2779 else if (DECL_INITIAL (ref) == 0
2780 && DECL_EXTERNAL (ref)
2781 && !TREE_PUBLIC (ref))
2782 record_maybe_used_decl (ref);
2785 if (TREE_CODE (ref) == CONST_DECL)
2787 used_types_insert (TREE_TYPE (ref));
2789 if (warn_cxx_compat
2790 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2791 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2793 warning_at (loc, OPT_Wc___compat,
2794 ("enum constant defined in struct or union "
2795 "is not visible in C++"));
2796 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2799 ref = DECL_INITIAL (ref);
2800 TREE_CONSTANT (ref) = 1;
2802 else if (current_function_decl != 0
2803 && !DECL_FILE_SCOPE_P (current_function_decl)
2804 && (VAR_OR_FUNCTION_DECL_P (ref)
2805 || TREE_CODE (ref) == PARM_DECL))
2807 tree context = decl_function_context (ref);
2809 if (context != 0 && context != current_function_decl)
2810 DECL_NONLOCAL (ref) = 1;
2812 /* C99 6.7.4p3: An inline definition of a function with external
2813 linkage ... shall not contain a reference to an identifier with
2814 internal linkage. */
2815 else if (current_function_decl != 0
2816 && DECL_DECLARED_INLINE_P (current_function_decl)
2817 && DECL_EXTERNAL (current_function_decl)
2818 && VAR_OR_FUNCTION_DECL_P (ref)
2819 && (!VAR_P (ref) || TREE_STATIC (ref))
2820 && ! TREE_PUBLIC (ref)
2821 && DECL_CONTEXT (ref) != current_function_decl)
2822 record_inline_static (loc, current_function_decl, ref,
2823 csi_internal);
2825 return ref;
2828 /* Record details of decls possibly used inside sizeof or typeof. */
2829 struct maybe_used_decl
2831 /* The decl. */
2832 tree decl;
2833 /* The level seen at (in_sizeof + in_typeof). */
2834 int level;
2835 /* The next one at this level or above, or NULL. */
2836 struct maybe_used_decl *next;
2839 static struct maybe_used_decl *maybe_used_decls;
2841 /* Record that DECL, an undefined static function reference seen
2842 inside sizeof or typeof, might be used if the operand of sizeof is
2843 a VLA type or the operand of typeof is a variably modified
2844 type. */
2846 static void
2847 record_maybe_used_decl (tree decl)
2849 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2850 t->decl = decl;
2851 t->level = in_sizeof + in_typeof;
2852 t->next = maybe_used_decls;
2853 maybe_used_decls = t;
2856 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2857 USED is false, just discard them. If it is true, mark them used
2858 (if no longer inside sizeof or typeof) or move them to the next
2859 level up (if still inside sizeof or typeof). */
2861 void
2862 pop_maybe_used (bool used)
2864 struct maybe_used_decl *p = maybe_used_decls;
2865 int cur_level = in_sizeof + in_typeof;
2866 while (p && p->level > cur_level)
2868 if (used)
2870 if (cur_level == 0)
2871 C_DECL_USED (p->decl) = 1;
2872 else
2873 p->level = cur_level;
2875 p = p->next;
2877 if (!used || cur_level == 0)
2878 maybe_used_decls = p;
2881 /* Return the result of sizeof applied to EXPR. */
2883 struct c_expr
2884 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2886 struct c_expr ret;
2887 if (expr.value == error_mark_node)
2889 ret.value = error_mark_node;
2890 ret.original_code = ERROR_MARK;
2891 ret.original_type = NULL;
2892 pop_maybe_used (false);
2894 else
2896 bool expr_const_operands = true;
2898 if (TREE_CODE (expr.value) == PARM_DECL
2899 && C_ARRAY_PARAMETER (expr.value))
2901 if (warning_at (loc, OPT_Wsizeof_array_argument,
2902 "%<sizeof%> on array function parameter %qE will "
2903 "return size of %qT", expr.value,
2904 expr.original_type))
2905 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2907 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2908 &expr_const_operands);
2909 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2910 c_last_sizeof_arg = expr.value;
2911 ret.original_code = SIZEOF_EXPR;
2912 ret.original_type = NULL;
2913 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2915 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2916 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2917 folded_expr, ret.value);
2918 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2919 SET_EXPR_LOCATION (ret.value, loc);
2921 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2923 return ret;
2926 /* Return the result of sizeof applied to T, a structure for the type
2927 name passed to sizeof (rather than the type itself). LOC is the
2928 location of the original expression. */
2930 struct c_expr
2931 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2933 tree type;
2934 struct c_expr ret;
2935 tree type_expr = NULL_TREE;
2936 bool type_expr_const = true;
2937 type = groktypename (t, &type_expr, &type_expr_const);
2938 ret.value = c_sizeof (loc, type);
2939 c_last_sizeof_arg = type;
2940 ret.original_code = SIZEOF_EXPR;
2941 ret.original_type = NULL;
2942 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2943 && c_vla_type_p (type))
2945 /* If the type is a [*] array, it is a VLA but is represented as
2946 having a size of zero. In such a case we must ensure that
2947 the result of sizeof does not get folded to a constant by
2948 c_fully_fold, because if the size is evaluated the result is
2949 not constant and so constraints on zero or negative size
2950 arrays must not be applied when this sizeof call is inside
2951 another array declarator. */
2952 if (!type_expr)
2953 type_expr = integer_zero_node;
2954 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2955 type_expr, ret.value);
2956 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2958 pop_maybe_used (type != error_mark_node
2959 ? C_TYPE_VARIABLE_SIZE (type) : false);
2960 return ret;
2963 /* Build a function call to function FUNCTION with parameters PARAMS.
2964 The function call is at LOC.
2965 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2966 TREE_VALUE of each node is a parameter-expression.
2967 FUNCTION's data type may be a function type or a pointer-to-function. */
2969 tree
2970 build_function_call (location_t loc, tree function, tree params)
2972 vec<tree, va_gc> *v;
2973 tree ret;
2975 vec_alloc (v, list_length (params));
2976 for (; params; params = TREE_CHAIN (params))
2977 v->quick_push (TREE_VALUE (params));
2978 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2979 vec_free (v);
2980 return ret;
2983 /* Give a note about the location of the declaration of DECL. */
2985 static void
2986 inform_declaration (tree decl)
2988 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2989 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2992 /* Build a function call to function FUNCTION with parameters PARAMS.
2993 ORIGTYPES, if not NULL, is a vector of types; each element is
2994 either NULL or the original type of the corresponding element in
2995 PARAMS. The original type may differ from TREE_TYPE of the
2996 parameter for enums. FUNCTION's data type may be a function type
2997 or pointer-to-function. This function changes the elements of
2998 PARAMS. */
3000 tree
3001 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3002 tree function, vec<tree, va_gc> *params,
3003 vec<tree, va_gc> *origtypes)
3005 tree fntype, fundecl = 0;
3006 tree name = NULL_TREE, result;
3007 tree tem;
3008 int nargs;
3009 tree *argarray;
3012 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3013 STRIP_TYPE_NOPS (function);
3015 /* Convert anything with function type to a pointer-to-function. */
3016 if (TREE_CODE (function) == FUNCTION_DECL)
3018 name = DECL_NAME (function);
3020 if (flag_tm)
3021 tm_malloc_replacement (function);
3022 fundecl = function;
3023 /* Atomic functions have type checking/casting already done. They are
3024 often rewritten and don't match the original parameter list. */
3025 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3026 origtypes = NULL;
3028 if (flag_cilkplus
3029 && is_cilkplus_reduce_builtin (function))
3030 origtypes = NULL;
3032 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3033 function = function_to_pointer_conversion (loc, function);
3035 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3036 expressions, like those used for ObjC messenger dispatches. */
3037 if (params && !params->is_empty ())
3038 function = objc_rewrite_function_call (function, (*params)[0]);
3040 function = c_fully_fold (function, false, NULL);
3042 fntype = TREE_TYPE (function);
3044 if (TREE_CODE (fntype) == ERROR_MARK)
3045 return error_mark_node;
3047 if (!(TREE_CODE (fntype) == POINTER_TYPE
3048 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3050 if (!flag_diagnostics_show_caret)
3051 error_at (loc,
3052 "called object %qE is not a function or function pointer",
3053 function);
3054 else if (DECL_P (function))
3056 error_at (loc,
3057 "called object %qD is not a function or function pointer",
3058 function);
3059 inform_declaration (function);
3061 else
3062 error_at (loc,
3063 "called object is not a function or function pointer");
3064 return error_mark_node;
3067 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3068 current_function_returns_abnormally = 1;
3070 /* fntype now gets the type of function pointed to. */
3071 fntype = TREE_TYPE (fntype);
3073 /* Convert the parameters to the types declared in the
3074 function prototype, or apply default promotions. */
3076 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3077 origtypes, function, fundecl);
3078 if (nargs < 0)
3079 return error_mark_node;
3081 /* Check that the function is called through a compatible prototype.
3082 If it is not, warn. */
3083 if (CONVERT_EXPR_P (function)
3084 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3085 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3086 && !comptypes (fntype, TREE_TYPE (tem)))
3088 tree return_type = TREE_TYPE (fntype);
3090 /* This situation leads to run-time undefined behavior. We can't,
3091 therefore, simply error unless we can prove that all possible
3092 executions of the program must execute the code. */
3093 warning_at (loc, 0, "function called through a non-compatible type");
3095 if (VOID_TYPE_P (return_type)
3096 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3097 pedwarn (loc, 0,
3098 "function with qualified void return type called");
3101 argarray = vec_safe_address (params);
3103 /* Check that arguments to builtin functions match the expectations. */
3104 if (fundecl
3105 && DECL_BUILT_IN (fundecl)
3106 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3107 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3108 argarray))
3109 return error_mark_node;
3111 /* Check that the arguments to the function are valid. */
3112 check_function_arguments (loc, fntype, nargs, argarray);
3114 if (name != NULL_TREE
3115 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3117 if (require_constant_value)
3118 result =
3119 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3120 function, nargs, argarray);
3121 else
3122 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3123 function, nargs, argarray);
3124 if (TREE_CODE (result) == NOP_EXPR
3125 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3126 STRIP_TYPE_NOPS (result);
3128 else
3129 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3130 function, nargs, argarray);
3132 /* In this improbable scenario, a nested function returns a VM type.
3133 Create a TARGET_EXPR so that the call always has a LHS, much as
3134 what the C++ FE does for functions returning non-PODs. */
3135 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3137 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3138 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3139 NULL_TREE, NULL_TREE);
3142 if (VOID_TYPE_P (TREE_TYPE (result)))
3144 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3145 pedwarn (loc, 0,
3146 "function with qualified void return type called");
3147 return result;
3149 return require_complete_type (loc, result);
3152 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3154 tree
3155 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3156 tree function, vec<tree, va_gc> *params,
3157 vec<tree, va_gc> *origtypes)
3159 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3160 STRIP_TYPE_NOPS (function);
3162 /* Convert anything with function type to a pointer-to-function. */
3163 if (TREE_CODE (function) == FUNCTION_DECL)
3165 /* Implement type-directed function overloading for builtins.
3166 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3167 handle all the type checking. The result is a complete expression
3168 that implements this function call. */
3169 tree tem = resolve_overloaded_builtin (loc, function, params);
3170 if (tem)
3171 return tem;
3173 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3176 /* Convert the argument expressions in the vector VALUES
3177 to the types in the list TYPELIST.
3179 If TYPELIST is exhausted, or when an element has NULL as its type,
3180 perform the default conversions.
3182 ORIGTYPES is the original types of the expressions in VALUES. This
3183 holds the type of enum values which have been converted to integral
3184 types. It may be NULL.
3186 FUNCTION is a tree for the called function. It is used only for
3187 error messages, where it is formatted with %qE.
3189 This is also where warnings about wrong number of args are generated.
3191 ARG_LOC are locations of function arguments (if any).
3193 Returns the actual number of arguments processed (which may be less
3194 than the length of VALUES in some error situations), or -1 on
3195 failure. */
3197 static int
3198 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3199 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3200 tree function, tree fundecl)
3202 tree typetail, val;
3203 unsigned int parmnum;
3204 bool error_args = false;
3205 const bool type_generic = fundecl
3206 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3207 bool type_generic_remove_excess_precision = false;
3208 bool type_generic_overflow_p = false;
3209 tree selector;
3211 /* Change pointer to function to the function itself for
3212 diagnostics. */
3213 if (TREE_CODE (function) == ADDR_EXPR
3214 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3215 function = TREE_OPERAND (function, 0);
3217 /* Handle an ObjC selector specially for diagnostics. */
3218 selector = objc_message_selector ();
3220 /* For type-generic built-in functions, determine whether excess
3221 precision should be removed (classification) or not
3222 (comparison). */
3223 if (type_generic
3224 && DECL_BUILT_IN (fundecl)
3225 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3227 switch (DECL_FUNCTION_CODE (fundecl))
3229 case BUILT_IN_ISFINITE:
3230 case BUILT_IN_ISINF:
3231 case BUILT_IN_ISINF_SIGN:
3232 case BUILT_IN_ISNAN:
3233 case BUILT_IN_ISNORMAL:
3234 case BUILT_IN_FPCLASSIFY:
3235 type_generic_remove_excess_precision = true;
3236 break;
3238 case BUILT_IN_ADD_OVERFLOW_P:
3239 case BUILT_IN_SUB_OVERFLOW_P:
3240 case BUILT_IN_MUL_OVERFLOW_P:
3241 /* The last argument of these type-generic builtins
3242 should not be promoted. */
3243 type_generic_overflow_p = true;
3244 break;
3246 default:
3247 break;
3250 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3251 return vec_safe_length (values);
3253 /* Scan the given expressions and types, producing individual
3254 converted arguments. */
3256 for (typetail = typelist, parmnum = 0;
3257 values && values->iterate (parmnum, &val);
3258 ++parmnum)
3260 tree type = typetail ? TREE_VALUE (typetail) : 0;
3261 tree valtype = TREE_TYPE (val);
3262 tree rname = function;
3263 int argnum = parmnum + 1;
3264 const char *invalid_func_diag;
3265 bool excess_precision = false;
3266 bool npc;
3267 tree parmval;
3268 /* Some __atomic_* builtins have additional hidden argument at
3269 position 0. */
3270 location_t ploc
3271 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3272 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3273 : input_location;
3275 if (type == void_type_node)
3277 if (selector)
3278 error_at (loc, "too many arguments to method %qE", selector);
3279 else
3280 error_at (loc, "too many arguments to function %qE", function);
3281 inform_declaration (fundecl);
3282 return error_args ? -1 : (int) parmnum;
3285 if (selector && argnum > 2)
3287 rname = selector;
3288 argnum -= 2;
3291 npc = null_pointer_constant_p (val);
3293 /* If there is excess precision and a prototype, convert once to
3294 the required type rather than converting via the semantic
3295 type. Likewise without a prototype a float value represented
3296 as long double should be converted once to double. But for
3297 type-generic classification functions excess precision must
3298 be removed here. */
3299 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3300 && (type || !type_generic || !type_generic_remove_excess_precision))
3302 val = TREE_OPERAND (val, 0);
3303 excess_precision = true;
3305 val = c_fully_fold (val, false, NULL);
3306 STRIP_TYPE_NOPS (val);
3308 val = require_complete_type (ploc, val);
3310 /* Some floating-point arguments must be promoted to double when
3311 no type is specified by a prototype. This applies to
3312 arguments of type float, and to architecture-specific types
3313 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3314 bool promote_float_arg = false;
3315 if (type == NULL_TREE
3316 && TREE_CODE (valtype) == REAL_TYPE
3317 && (TYPE_PRECISION (valtype)
3318 <= TYPE_PRECISION (double_type_node))
3319 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3320 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3321 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3323 /* Promote this argument, unless it has a _FloatN or
3324 _FloatNx type. */
3325 promote_float_arg = true;
3326 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3327 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3329 promote_float_arg = false;
3330 break;
3334 if (type != 0)
3336 /* Formal parm type is specified by a function prototype. */
3338 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3340 error_at (ploc, "type of formal parameter %d is incomplete",
3341 parmnum + 1);
3342 parmval = val;
3344 else
3346 tree origtype;
3348 /* Optionally warn about conversions that
3349 differ from the default conversions. */
3350 if (warn_traditional_conversion || warn_traditional)
3352 unsigned int formal_prec = TYPE_PRECISION (type);
3354 if (INTEGRAL_TYPE_P (type)
3355 && TREE_CODE (valtype) == REAL_TYPE)
3356 warning_at (ploc, OPT_Wtraditional_conversion,
3357 "passing argument %d of %qE as integer rather "
3358 "than floating due to prototype",
3359 argnum, rname);
3360 if (INTEGRAL_TYPE_P (type)
3361 && TREE_CODE (valtype) == COMPLEX_TYPE)
3362 warning_at (ploc, OPT_Wtraditional_conversion,
3363 "passing argument %d of %qE as integer rather "
3364 "than complex due to prototype",
3365 argnum, rname);
3366 else if (TREE_CODE (type) == COMPLEX_TYPE
3367 && TREE_CODE (valtype) == REAL_TYPE)
3368 warning_at (ploc, OPT_Wtraditional_conversion,
3369 "passing argument %d of %qE as complex rather "
3370 "than floating due to prototype",
3371 argnum, rname);
3372 else if (TREE_CODE (type) == REAL_TYPE
3373 && INTEGRAL_TYPE_P (valtype))
3374 warning_at (ploc, OPT_Wtraditional_conversion,
3375 "passing argument %d of %qE as floating rather "
3376 "than integer due to prototype",
3377 argnum, rname);
3378 else if (TREE_CODE (type) == COMPLEX_TYPE
3379 && INTEGRAL_TYPE_P (valtype))
3380 warning_at (ploc, OPT_Wtraditional_conversion,
3381 "passing argument %d of %qE as complex rather "
3382 "than integer due to prototype",
3383 argnum, rname);
3384 else if (TREE_CODE (type) == REAL_TYPE
3385 && TREE_CODE (valtype) == COMPLEX_TYPE)
3386 warning_at (ploc, OPT_Wtraditional_conversion,
3387 "passing argument %d of %qE as floating rather "
3388 "than complex due to prototype",
3389 argnum, rname);
3390 /* ??? At some point, messages should be written about
3391 conversions between complex types, but that's too messy
3392 to do now. */
3393 else if (TREE_CODE (type) == REAL_TYPE
3394 && TREE_CODE (valtype) == REAL_TYPE)
3396 /* Warn if any argument is passed as `float',
3397 since without a prototype it would be `double'. */
3398 if (formal_prec == TYPE_PRECISION (float_type_node)
3399 && type != dfloat32_type_node)
3400 warning_at (ploc, 0,
3401 "passing argument %d of %qE as %<float%> "
3402 "rather than %<double%> due to prototype",
3403 argnum, rname);
3405 /* Warn if mismatch between argument and prototype
3406 for decimal float types. Warn of conversions with
3407 binary float types and of precision narrowing due to
3408 prototype. */
3409 else if (type != valtype
3410 && (type == dfloat32_type_node
3411 || type == dfloat64_type_node
3412 || type == dfloat128_type_node
3413 || valtype == dfloat32_type_node
3414 || valtype == dfloat64_type_node
3415 || valtype == dfloat128_type_node)
3416 && (formal_prec
3417 <= TYPE_PRECISION (valtype)
3418 || (type == dfloat128_type_node
3419 && (valtype
3420 != dfloat64_type_node
3421 && (valtype
3422 != dfloat32_type_node)))
3423 || (type == dfloat64_type_node
3424 && (valtype
3425 != dfloat32_type_node))))
3426 warning_at (ploc, 0,
3427 "passing argument %d of %qE as %qT "
3428 "rather than %qT due to prototype",
3429 argnum, rname, type, valtype);
3432 /* Detect integer changing in width or signedness.
3433 These warnings are only activated with
3434 -Wtraditional-conversion, not with -Wtraditional. */
3435 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3436 && INTEGRAL_TYPE_P (valtype))
3438 tree would_have_been = default_conversion (val);
3439 tree type1 = TREE_TYPE (would_have_been);
3441 if (TREE_CODE (type) == ENUMERAL_TYPE
3442 && (TYPE_MAIN_VARIANT (type)
3443 == TYPE_MAIN_VARIANT (valtype)))
3444 /* No warning if function asks for enum
3445 and the actual arg is that enum type. */
3447 else if (formal_prec != TYPE_PRECISION (type1))
3448 warning_at (ploc, OPT_Wtraditional_conversion,
3449 "passing argument %d of %qE "
3450 "with different width due to prototype",
3451 argnum, rname);
3452 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3454 /* Don't complain if the formal parameter type
3455 is an enum, because we can't tell now whether
3456 the value was an enum--even the same enum. */
3457 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3459 else if (TREE_CODE (val) == INTEGER_CST
3460 && int_fits_type_p (val, type))
3461 /* Change in signedness doesn't matter
3462 if a constant value is unaffected. */
3464 /* If the value is extended from a narrower
3465 unsigned type, it doesn't matter whether we
3466 pass it as signed or unsigned; the value
3467 certainly is the same either way. */
3468 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3469 && TYPE_UNSIGNED (valtype))
3471 else if (TYPE_UNSIGNED (type))
3472 warning_at (ploc, OPT_Wtraditional_conversion,
3473 "passing argument %d of %qE "
3474 "as unsigned due to prototype",
3475 argnum, rname);
3476 else
3477 warning_at (ploc, OPT_Wtraditional_conversion,
3478 "passing argument %d of %qE "
3479 "as signed due to prototype",
3480 argnum, rname);
3484 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3485 sake of better warnings from convert_and_check. */
3486 if (excess_precision)
3487 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3488 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3489 parmval = convert_for_assignment (loc, ploc, type,
3490 val, origtype, ic_argpass,
3491 npc, fundecl, function,
3492 parmnum + 1);
3494 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3495 && INTEGRAL_TYPE_P (type)
3496 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3497 parmval = default_conversion (parmval);
3500 else if (promote_float_arg)
3502 if (type_generic)
3503 parmval = val;
3504 else
3506 /* Convert `float' to `double'. */
3507 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3508 warning_at (ploc, OPT_Wdouble_promotion,
3509 "implicit conversion from %qT to %qT when passing "
3510 "argument to function",
3511 valtype, double_type_node);
3512 parmval = convert (double_type_node, val);
3515 else if ((excess_precision && !type_generic)
3516 || (type_generic_overflow_p && parmnum == 2))
3517 /* A "double" argument with excess precision being passed
3518 without a prototype or in variable arguments.
3519 The last argument of __builtin_*_overflow_p should not be
3520 promoted. */
3521 parmval = convert (valtype, val);
3522 else if ((invalid_func_diag =
3523 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3525 error (invalid_func_diag);
3526 return -1;
3528 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3530 return -1;
3532 else
3533 /* Convert `short' and `char' to full-size `int'. */
3534 parmval = default_conversion (val);
3536 (*values)[parmnum] = parmval;
3537 if (parmval == error_mark_node)
3538 error_args = true;
3540 if (typetail)
3541 typetail = TREE_CHAIN (typetail);
3544 gcc_assert (parmnum == vec_safe_length (values));
3546 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3548 error_at (loc, "too few arguments to function %qE", function);
3549 inform_declaration (fundecl);
3550 return -1;
3553 return error_args ? -1 : (int) parmnum;
3556 /* This is the entry point used by the parser to build unary operators
3557 in the input. CODE, a tree_code, specifies the unary operator, and
3558 ARG is the operand. For unary plus, the C parser currently uses
3559 CONVERT_EXPR for code.
3561 LOC is the location to use for the tree generated.
3564 struct c_expr
3565 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3567 struct c_expr result;
3569 result.original_code = code;
3570 result.original_type = NULL;
3572 if (reject_gcc_builtin (arg.value))
3574 result.value = error_mark_node;
3576 else
3578 result.value = build_unary_op (loc, code, arg.value, 0);
3580 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3581 overflow_warning (loc, result.value);
3584 /* We are typically called when parsing a prefix token at LOC acting on
3585 ARG. Reflect this by updating the source range of the result to
3586 start at LOC and end at the end of ARG. */
3587 set_c_expr_source_range (&result,
3588 loc, arg.get_finish ());
3590 return result;
3593 /* This is the entry point used by the parser to build binary operators
3594 in the input. CODE, a tree_code, specifies the binary operator, and
3595 ARG1 and ARG2 are the operands. In addition to constructing the
3596 expression, we check for operands that were written with other binary
3597 operators in a way that is likely to confuse the user.
3599 LOCATION is the location of the binary operator. */
3601 struct c_expr
3602 parser_build_binary_op (location_t location, enum tree_code code,
3603 struct c_expr arg1, struct c_expr arg2)
3605 struct c_expr result;
3607 enum tree_code code1 = arg1.original_code;
3608 enum tree_code code2 = arg2.original_code;
3609 tree type1 = (arg1.original_type
3610 ? arg1.original_type
3611 : TREE_TYPE (arg1.value));
3612 tree type2 = (arg2.original_type
3613 ? arg2.original_type
3614 : TREE_TYPE (arg2.value));
3616 result.value = build_binary_op (location, code,
3617 arg1.value, arg2.value, 1);
3618 result.original_code = code;
3619 result.original_type = NULL;
3621 if (TREE_CODE (result.value) == ERROR_MARK)
3623 set_c_expr_source_range (&result,
3624 arg1.get_start (),
3625 arg2.get_finish ());
3626 return result;
3629 if (location != UNKNOWN_LOCATION)
3630 protected_set_expr_location (result.value, location);
3632 set_c_expr_source_range (&result,
3633 arg1.get_start (),
3634 arg2.get_finish ());
3636 /* Check for cases such as x+y<<z which users are likely
3637 to misinterpret. */
3638 if (warn_parentheses)
3639 warn_about_parentheses (location, code, code1, arg1.value, code2,
3640 arg2.value);
3642 if (warn_logical_op)
3643 warn_logical_operator (location, code, TREE_TYPE (result.value),
3644 code1, arg1.value, code2, arg2.value);
3646 if (warn_tautological_compare)
3648 tree lhs = arg1.value;
3649 tree rhs = arg2.value;
3650 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3652 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3653 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3654 lhs = NULL_TREE;
3655 else
3656 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3658 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3660 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3661 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3662 rhs = NULL_TREE;
3663 else
3664 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3666 if (lhs != NULL_TREE && rhs != NULL_TREE)
3667 warn_tautological_cmp (location, code, lhs, rhs);
3670 if (warn_logical_not_paren
3671 && TREE_CODE_CLASS (code) == tcc_comparison
3672 && code1 == TRUTH_NOT_EXPR
3673 && code2 != TRUTH_NOT_EXPR
3674 /* Avoid warning for !!x == y. */
3675 && (TREE_CODE (arg1.value) != NE_EXPR
3676 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3678 /* Avoid warning for !b == y where b has _Bool type. */
3679 tree t = integer_zero_node;
3680 if (TREE_CODE (arg1.value) == EQ_EXPR
3681 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3682 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3684 t = TREE_OPERAND (arg1.value, 0);
3687 if (TREE_TYPE (t) != integer_type_node)
3688 break;
3689 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3690 t = C_MAYBE_CONST_EXPR_EXPR (t);
3691 else if (CONVERT_EXPR_P (t))
3692 t = TREE_OPERAND (t, 0);
3693 else
3694 break;
3696 while (1);
3698 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3699 warn_logical_not_parentheses (location, code, arg2.value);
3702 /* Warn about comparisons against string literals, with the exception
3703 of testing for equality or inequality of a string literal with NULL. */
3704 if (code == EQ_EXPR || code == NE_EXPR)
3706 if ((code1 == STRING_CST
3707 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3708 || (code2 == STRING_CST
3709 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3710 warning_at (location, OPT_Waddress,
3711 "comparison with string literal results in unspecified behavior");
3713 else if (TREE_CODE_CLASS (code) == tcc_comparison
3714 && (code1 == STRING_CST || code2 == STRING_CST))
3715 warning_at (location, OPT_Waddress,
3716 "comparison with string literal results in unspecified behavior");
3718 if (TREE_OVERFLOW_P (result.value)
3719 && !TREE_OVERFLOW_P (arg1.value)
3720 && !TREE_OVERFLOW_P (arg2.value))
3721 overflow_warning (location, result.value);
3723 /* Warn about comparisons of different enum types. */
3724 if (warn_enum_compare
3725 && TREE_CODE_CLASS (code) == tcc_comparison
3726 && TREE_CODE (type1) == ENUMERAL_TYPE
3727 && TREE_CODE (type2) == ENUMERAL_TYPE
3728 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3729 warning_at (location, OPT_Wenum_compare,
3730 "comparison between %qT and %qT",
3731 type1, type2);
3733 return result;
3736 /* Return a tree for the difference of pointers OP0 and OP1.
3737 The resulting tree has type int. */
3739 static tree
3740 pointer_diff (location_t loc, tree op0, tree op1)
3742 tree restype = ptrdiff_type_node;
3743 tree result, inttype;
3745 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3746 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3747 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3748 tree orig_op1 = op1;
3750 /* If the operands point into different address spaces, we need to
3751 explicitly convert them to pointers into the common address space
3752 before we can subtract the numerical address values. */
3753 if (as0 != as1)
3755 addr_space_t as_common;
3756 tree common_type;
3758 /* Determine the common superset address space. This is guaranteed
3759 to exist because the caller verified that comp_target_types
3760 returned non-zero. */
3761 if (!addr_space_superset (as0, as1, &as_common))
3762 gcc_unreachable ();
3764 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3765 op0 = convert (common_type, op0);
3766 op1 = convert (common_type, op1);
3769 /* Determine integer type to perform computations in. This will usually
3770 be the same as the result type (ptrdiff_t), but may need to be a wider
3771 type if pointers for the address space are wider than ptrdiff_t. */
3772 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3773 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3774 else
3775 inttype = restype;
3777 if (TREE_CODE (target_type) == VOID_TYPE)
3778 pedwarn (loc, OPT_Wpointer_arith,
3779 "pointer of type %<void *%> used in subtraction");
3780 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3781 pedwarn (loc, OPT_Wpointer_arith,
3782 "pointer to a function used in subtraction");
3784 /* First do the subtraction as integers;
3785 then drop through to build the divide operator.
3786 Do not do default conversions on the minus operator
3787 in case restype is a short type. */
3789 op0 = build_binary_op (loc,
3790 MINUS_EXPR, convert (inttype, op0),
3791 convert (inttype, op1), 0);
3792 /* This generates an error if op1 is pointer to incomplete type. */
3793 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3794 error_at (loc, "arithmetic on pointer to an incomplete type");
3796 op1 = c_size_in_bytes (target_type);
3798 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3799 error_at (loc, "arithmetic on pointer to an empty aggregate");
3801 /* Divide by the size, in easiest possible way. */
3802 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3803 op0, convert (inttype, op1));
3805 /* Convert to final result type if necessary. */
3806 return convert (restype, result);
3809 /* Expand atomic compound assignments into an appropriate sequence as
3810 specified by the C11 standard section 6.5.16.2.
3812 _Atomic T1 E1
3813 T2 E2
3814 E1 op= E2
3816 This sequence is used for all types for which these operations are
3817 supported.
3819 In addition, built-in versions of the 'fe' prefixed routines may
3820 need to be invoked for floating point (real, complex or vector) when
3821 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3823 T1 newval;
3824 T1 old;
3825 T1 *addr
3826 T2 val
3827 fenv_t fenv
3829 addr = &E1;
3830 val = (E2);
3831 __atomic_load (addr, &old, SEQ_CST);
3832 feholdexcept (&fenv);
3833 loop:
3834 newval = old op val;
3835 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3836 SEQ_CST))
3837 goto done;
3838 feclearexcept (FE_ALL_EXCEPT);
3839 goto loop:
3840 done:
3841 feupdateenv (&fenv);
3843 The compiler will issue the __atomic_fetch_* built-in when possible,
3844 otherwise it will generate the generic form of the atomic operations.
3845 This requires temp(s) and has their address taken. The atomic processing
3846 is smart enough to figure out when the size of an object can utilize
3847 a lock-free version, and convert the built-in call to the appropriate
3848 lock-free routine. The optimizers will then dispose of any temps that
3849 are no longer required, and lock-free implementations are utilized as
3850 long as there is target support for the required size.
3852 If the operator is NOP_EXPR, then this is a simple assignment, and
3853 an __atomic_store is issued to perform the assignment rather than
3854 the above loop. */
3856 /* Build an atomic assignment at LOC, expanding into the proper
3857 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3858 the result of the operation, unless RETURN_OLD_P, in which case
3859 return the old value of LHS (this is only for postincrement and
3860 postdecrement). */
3862 static tree
3863 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3864 tree rhs, bool return_old_p)
3866 tree fndecl, func_call;
3867 vec<tree, va_gc> *params;
3868 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3869 tree old, old_addr;
3870 tree compound_stmt;
3871 tree stmt, goto_stmt;
3872 tree loop_label, loop_decl, done_label, done_decl;
3874 tree lhs_type = TREE_TYPE (lhs);
3875 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3876 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3877 tree rhs_type = TREE_TYPE (rhs);
3879 gcc_assert (TYPE_ATOMIC (lhs_type));
3881 if (return_old_p)
3882 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3884 /* Allocate enough vector items for a compare_exchange. */
3885 vec_alloc (params, 6);
3887 /* Create a compound statement to hold the sequence of statements
3888 with a loop. */
3889 compound_stmt = c_begin_compound_stmt (false);
3891 /* Fold the RHS if it hasn't already been folded. */
3892 if (modifycode != NOP_EXPR)
3893 rhs = c_fully_fold (rhs, false, NULL);
3895 /* Remove the qualifiers for the rest of the expressions and create
3896 the VAL temp variable to hold the RHS. */
3897 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3898 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3899 val = create_tmp_var_raw (nonatomic_rhs_type);
3900 TREE_ADDRESSABLE (val) = 1;
3901 TREE_NO_WARNING (val) = 1;
3902 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3903 NULL_TREE);
3904 SET_EXPR_LOCATION (rhs, loc);
3905 add_stmt (rhs);
3907 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3908 an atomic_store. */
3909 if (modifycode == NOP_EXPR)
3911 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3912 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3913 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3914 params->quick_push (lhs_addr);
3915 params->quick_push (rhs);
3916 params->quick_push (seq_cst);
3917 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3918 add_stmt (func_call);
3920 /* Finish the compound statement. */
3921 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3923 /* VAL is the value which was stored, return a COMPOUND_STMT of
3924 the statement and that value. */
3925 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3928 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3929 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3930 isn't applicable for such builtins. ??? Do we want to handle enums? */
3931 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3932 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3934 built_in_function fncode;
3935 switch (modifycode)
3937 case PLUS_EXPR:
3938 case POINTER_PLUS_EXPR:
3939 fncode = (return_old_p
3940 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3941 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3942 break;
3943 case MINUS_EXPR:
3944 fncode = (return_old_p
3945 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3946 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3947 break;
3948 case BIT_AND_EXPR:
3949 fncode = (return_old_p
3950 ? BUILT_IN_ATOMIC_FETCH_AND_N
3951 : BUILT_IN_ATOMIC_AND_FETCH_N);
3952 break;
3953 case BIT_IOR_EXPR:
3954 fncode = (return_old_p
3955 ? BUILT_IN_ATOMIC_FETCH_OR_N
3956 : BUILT_IN_ATOMIC_OR_FETCH_N);
3957 break;
3958 case BIT_XOR_EXPR:
3959 fncode = (return_old_p
3960 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3961 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3962 break;
3963 default:
3964 goto cas_loop;
3967 /* We can only use "_1" through "_16" variants of the atomic fetch
3968 built-ins. */
3969 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3970 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3971 goto cas_loop;
3973 /* If this is a pointer type, we need to multiply by the size of
3974 the pointer target type. */
3975 if (POINTER_TYPE_P (lhs_type))
3977 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3978 /* ??? This would introduce -Wdiscarded-qualifiers
3979 warning: __atomic_fetch_* expect volatile void *
3980 type as the first argument. (Assignments between
3981 atomic and non-atomic objects are OK.) */
3982 || TYPE_RESTRICT (lhs_type))
3983 goto cas_loop;
3984 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3985 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3986 convert (ptrdiff_type_node, rhs),
3987 convert (ptrdiff_type_node, sz));
3990 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3991 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3992 fndecl = builtin_decl_explicit (fncode);
3993 params->quick_push (lhs_addr);
3994 params->quick_push (rhs);
3995 params->quick_push (seq_cst);
3996 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3998 newval = create_tmp_var_raw (nonatomic_lhs_type);
3999 TREE_ADDRESSABLE (newval) = 1;
4000 TREE_NO_WARNING (newval) = 1;
4001 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4002 NULL_TREE, NULL_TREE);
4003 SET_EXPR_LOCATION (rhs, loc);
4004 add_stmt (rhs);
4006 /* Finish the compound statement. */
4007 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4009 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4010 the statement and that value. */
4011 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4014 cas_loop:
4015 /* Create the variables and labels required for the op= form. */
4016 old = create_tmp_var_raw (nonatomic_lhs_type);
4017 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
4018 TREE_ADDRESSABLE (old) = 1;
4019 TREE_NO_WARNING (old) = 1;
4021 newval = create_tmp_var_raw (nonatomic_lhs_type);
4022 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
4023 TREE_ADDRESSABLE (newval) = 1;
4024 TREE_NO_WARNING (newval) = 1;
4026 loop_decl = create_artificial_label (loc);
4027 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4029 done_decl = create_artificial_label (loc);
4030 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4032 /* __atomic_load (addr, &old, SEQ_CST). */
4033 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4034 params->quick_push (lhs_addr);
4035 params->quick_push (old_addr);
4036 params->quick_push (seq_cst);
4037 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4038 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4039 NULL_TREE);
4040 add_stmt (old);
4041 params->truncate (0);
4043 /* Create the expressions for floating-point environment
4044 manipulation, if required. */
4045 bool need_fenv = (flag_trapping_math
4046 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4047 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4048 if (need_fenv)
4049 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4051 if (hold_call)
4052 add_stmt (hold_call);
4054 /* loop: */
4055 add_stmt (loop_label);
4057 /* newval = old + val; */
4058 rhs = build_binary_op (loc, modifycode, old, val, 1);
4059 rhs = c_fully_fold (rhs, false, NULL);
4060 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4061 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4062 NULL_TREE, 0);
4063 if (rhs != error_mark_node)
4065 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4066 NULL_TREE);
4067 SET_EXPR_LOCATION (rhs, loc);
4068 add_stmt (rhs);
4071 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4072 goto done; */
4073 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4074 params->quick_push (lhs_addr);
4075 params->quick_push (old_addr);
4076 params->quick_push (newval_addr);
4077 params->quick_push (integer_zero_node);
4078 params->quick_push (seq_cst);
4079 params->quick_push (seq_cst);
4080 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4082 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4083 SET_EXPR_LOCATION (goto_stmt, loc);
4085 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4086 SET_EXPR_LOCATION (stmt, loc);
4087 add_stmt (stmt);
4089 if (clear_call)
4090 add_stmt (clear_call);
4092 /* goto loop; */
4093 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4094 SET_EXPR_LOCATION (goto_stmt, loc);
4095 add_stmt (goto_stmt);
4097 /* done: */
4098 add_stmt (done_label);
4100 if (update_call)
4101 add_stmt (update_call);
4103 /* Finish the compound statement. */
4104 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4106 /* NEWVAL is the value that was successfully stored, return a
4107 COMPOUND_EXPR of the statement and the appropriate value. */
4108 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4109 return_old_p ? old : newval);
4112 /* Construct and perhaps optimize a tree representation
4113 for a unary operation. CODE, a tree_code, specifies the operation
4114 and XARG is the operand.
4115 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
4116 the default promotions (such as from short to int).
4117 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
4118 allows non-lvalues; this is only used to handle conversion of non-lvalue
4119 arrays to pointers in C99.
4121 LOCATION is the location of the operator. */
4123 tree
4124 build_unary_op (location_t location,
4125 enum tree_code code, tree xarg, int flag)
4127 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4128 tree arg = xarg;
4129 tree argtype = 0;
4130 enum tree_code typecode;
4131 tree val;
4132 tree ret = error_mark_node;
4133 tree eptype = NULL_TREE;
4134 int noconvert = flag;
4135 const char *invalid_op_diag;
4136 bool int_operands;
4138 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4139 if (int_operands)
4140 arg = remove_c_maybe_const_expr (arg);
4142 if (code != ADDR_EXPR)
4143 arg = require_complete_type (location, arg);
4145 typecode = TREE_CODE (TREE_TYPE (arg));
4146 if (typecode == ERROR_MARK)
4147 return error_mark_node;
4148 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4149 typecode = INTEGER_TYPE;
4151 if ((invalid_op_diag
4152 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4154 error_at (location, invalid_op_diag);
4155 return error_mark_node;
4158 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4160 eptype = TREE_TYPE (arg);
4161 arg = TREE_OPERAND (arg, 0);
4164 switch (code)
4166 case CONVERT_EXPR:
4167 /* This is used for unary plus, because a CONVERT_EXPR
4168 is enough to prevent anybody from looking inside for
4169 associativity, but won't generate any code. */
4170 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4171 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4172 || typecode == VECTOR_TYPE))
4174 error_at (location, "wrong type argument to unary plus");
4175 return error_mark_node;
4177 else if (!noconvert)
4178 arg = default_conversion (arg);
4179 arg = non_lvalue_loc (location, arg);
4180 break;
4182 case NEGATE_EXPR:
4183 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4184 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4185 || typecode == VECTOR_TYPE))
4187 error_at (location, "wrong type argument to unary minus");
4188 return error_mark_node;
4190 else if (!noconvert)
4191 arg = default_conversion (arg);
4192 break;
4194 case BIT_NOT_EXPR:
4195 /* ~ works on integer types and non float vectors. */
4196 if (typecode == INTEGER_TYPE
4197 || (typecode == VECTOR_TYPE
4198 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4200 if (!noconvert)
4201 arg = default_conversion (arg);
4203 else if (typecode == COMPLEX_TYPE)
4205 code = CONJ_EXPR;
4206 pedwarn (location, OPT_Wpedantic,
4207 "ISO C does not support %<~%> for complex conjugation");
4208 if (!noconvert)
4209 arg = default_conversion (arg);
4211 else
4213 error_at (location, "wrong type argument to bit-complement");
4214 return error_mark_node;
4216 break;
4218 case ABS_EXPR:
4219 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4221 error_at (location, "wrong type argument to abs");
4222 return error_mark_node;
4224 else if (!noconvert)
4225 arg = default_conversion (arg);
4226 break;
4228 case CONJ_EXPR:
4229 /* Conjugating a real value is a no-op, but allow it anyway. */
4230 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4231 || typecode == COMPLEX_TYPE))
4233 error_at (location, "wrong type argument to conjugation");
4234 return error_mark_node;
4236 else if (!noconvert)
4237 arg = default_conversion (arg);
4238 break;
4240 case TRUTH_NOT_EXPR:
4241 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4242 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4243 && typecode != COMPLEX_TYPE)
4245 error_at (location,
4246 "wrong type argument to unary exclamation mark");
4247 return error_mark_node;
4249 if (int_operands)
4251 arg = c_objc_common_truthvalue_conversion (location, xarg);
4252 arg = remove_c_maybe_const_expr (arg);
4254 else
4255 arg = c_objc_common_truthvalue_conversion (location, arg);
4256 ret = invert_truthvalue_loc (location, arg);
4257 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4258 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4259 location = EXPR_LOCATION (ret);
4260 goto return_build_unary_op;
4262 case REALPART_EXPR:
4263 case IMAGPART_EXPR:
4264 ret = build_real_imag_expr (location, code, arg);
4265 if (ret == error_mark_node)
4266 return error_mark_node;
4267 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4268 eptype = TREE_TYPE (eptype);
4269 goto return_build_unary_op;
4271 case PREINCREMENT_EXPR:
4272 case POSTINCREMENT_EXPR:
4273 case PREDECREMENT_EXPR:
4274 case POSTDECREMENT_EXPR:
4276 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4278 tree inner = build_unary_op (location, code,
4279 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4280 if (inner == error_mark_node)
4281 return error_mark_node;
4282 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4283 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4284 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4285 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4286 goto return_build_unary_op;
4289 /* Complain about anything that is not a true lvalue. In
4290 Objective-C, skip this check for property_refs. */
4291 if (!objc_is_property_ref (arg)
4292 && !lvalue_or_else (location,
4293 arg, ((code == PREINCREMENT_EXPR
4294 || code == POSTINCREMENT_EXPR)
4295 ? lv_increment
4296 : lv_decrement)))
4297 return error_mark_node;
4299 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4301 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4302 warning_at (location, OPT_Wc___compat,
4303 "increment of enumeration value is invalid in C++");
4304 else
4305 warning_at (location, OPT_Wc___compat,
4306 "decrement of enumeration value is invalid in C++");
4309 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4310 arg = c_fully_fold (arg, false, NULL);
4312 bool atomic_op;
4313 atomic_op = really_atomic_lvalue (arg);
4315 /* Increment or decrement the real part of the value,
4316 and don't change the imaginary part. */
4317 if (typecode == COMPLEX_TYPE)
4319 tree real, imag;
4321 pedwarn (location, OPT_Wpedantic,
4322 "ISO C does not support %<++%> and %<--%> on complex types");
4324 if (!atomic_op)
4326 arg = stabilize_reference (arg);
4327 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4328 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4329 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4330 if (real == error_mark_node || imag == error_mark_node)
4331 return error_mark_node;
4332 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4333 real, imag);
4334 goto return_build_unary_op;
4338 /* Report invalid types. */
4340 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4341 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4342 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4344 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4345 error_at (location, "wrong type argument to increment");
4346 else
4347 error_at (location, "wrong type argument to decrement");
4349 return error_mark_node;
4353 tree inc;
4355 argtype = TREE_TYPE (arg);
4357 /* Compute the increment. */
4359 if (typecode == POINTER_TYPE)
4361 /* If pointer target is an incomplete type,
4362 we just cannot know how to do the arithmetic. */
4363 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4365 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4366 error_at (location,
4367 "increment of pointer to an incomplete type %qT",
4368 TREE_TYPE (argtype));
4369 else
4370 error_at (location,
4371 "decrement of pointer to an incomplete type %qT",
4372 TREE_TYPE (argtype));
4374 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4375 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4377 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4378 pedwarn (location, OPT_Wpointer_arith,
4379 "wrong type argument to increment");
4380 else
4381 pedwarn (location, OPT_Wpointer_arith,
4382 "wrong type argument to decrement");
4385 inc = c_size_in_bytes (TREE_TYPE (argtype));
4386 inc = convert_to_ptrofftype_loc (location, inc);
4388 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4390 /* For signed fract types, we invert ++ to -- or
4391 -- to ++, and change inc from 1 to -1, because
4392 it is not possible to represent 1 in signed fract constants.
4393 For unsigned fract types, the result always overflows and
4394 we get an undefined (original) or the maximum value. */
4395 if (code == PREINCREMENT_EXPR)
4396 code = PREDECREMENT_EXPR;
4397 else if (code == PREDECREMENT_EXPR)
4398 code = PREINCREMENT_EXPR;
4399 else if (code == POSTINCREMENT_EXPR)
4400 code = POSTDECREMENT_EXPR;
4401 else /* code == POSTDECREMENT_EXPR */
4402 code = POSTINCREMENT_EXPR;
4404 inc = integer_minus_one_node;
4405 inc = convert (argtype, inc);
4407 else
4409 inc = VECTOR_TYPE_P (argtype)
4410 ? build_one_cst (argtype)
4411 : integer_one_node;
4412 inc = convert (argtype, inc);
4415 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4416 need to ask Objective-C to build the increment or decrement
4417 expression for it. */
4418 if (objc_is_property_ref (arg))
4419 return objc_build_incr_expr_for_property_ref (location, code,
4420 arg, inc);
4422 /* Report a read-only lvalue. */
4423 if (TYPE_READONLY (argtype))
4425 readonly_error (location, arg,
4426 ((code == PREINCREMENT_EXPR
4427 || code == POSTINCREMENT_EXPR)
4428 ? lv_increment : lv_decrement));
4429 return error_mark_node;
4431 else if (TREE_READONLY (arg))
4432 readonly_warning (arg,
4433 ((code == PREINCREMENT_EXPR
4434 || code == POSTINCREMENT_EXPR)
4435 ? lv_increment : lv_decrement));
4437 /* If the argument is atomic, use the special code sequences for
4438 atomic compound assignment. */
4439 if (atomic_op)
4441 arg = stabilize_reference (arg);
4442 ret = build_atomic_assign (location, arg,
4443 ((code == PREINCREMENT_EXPR
4444 || code == POSTINCREMENT_EXPR)
4445 ? PLUS_EXPR
4446 : MINUS_EXPR),
4447 (FRACT_MODE_P (TYPE_MODE (argtype))
4448 ? inc
4449 : integer_one_node),
4450 (code == POSTINCREMENT_EXPR
4451 || code == POSTDECREMENT_EXPR));
4452 goto return_build_unary_op;
4455 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4456 val = boolean_increment (code, arg);
4457 else
4458 val = build2 (code, TREE_TYPE (arg), arg, inc);
4459 TREE_SIDE_EFFECTS (val) = 1;
4460 if (TREE_CODE (val) != code)
4461 TREE_NO_WARNING (val) = 1;
4462 ret = val;
4463 goto return_build_unary_op;
4466 case ADDR_EXPR:
4467 /* Note that this operation never does default_conversion. */
4469 /* The operand of unary '&' must be an lvalue (which excludes
4470 expressions of type void), or, in C99, the result of a [] or
4471 unary '*' operator. */
4472 if (VOID_TYPE_P (TREE_TYPE (arg))
4473 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4474 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4475 pedwarn (location, 0, "taking address of expression of type %<void%>");
4477 /* Let &* cancel out to simplify resulting code. */
4478 if (INDIRECT_REF_P (arg))
4480 /* Don't let this be an lvalue. */
4481 if (lvalue_p (TREE_OPERAND (arg, 0)))
4482 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4483 ret = TREE_OPERAND (arg, 0);
4484 goto return_build_unary_op;
4487 /* Anything not already handled and not a true memory reference
4488 or a non-lvalue array is an error. */
4489 if (typecode != FUNCTION_TYPE && !flag
4490 && !lvalue_or_else (location, arg, lv_addressof))
4491 return error_mark_node;
4493 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4494 folding later. */
4495 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4497 tree inner = build_unary_op (location, code,
4498 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4499 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4500 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4501 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4502 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4503 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4504 goto return_build_unary_op;
4507 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4508 argtype = TREE_TYPE (arg);
4510 /* If the lvalue is const or volatile, merge that into the type
4511 to which the address will point. This is only needed
4512 for function types. */
4513 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4514 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4515 && TREE_CODE (argtype) == FUNCTION_TYPE)
4517 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4518 int quals = orig_quals;
4520 if (TREE_READONLY (arg))
4521 quals |= TYPE_QUAL_CONST;
4522 if (TREE_THIS_VOLATILE (arg))
4523 quals |= TYPE_QUAL_VOLATILE;
4525 argtype = c_build_qualified_type (argtype, quals);
4528 switch (TREE_CODE (arg))
4530 case COMPONENT_REF:
4531 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4533 error_at (location, "cannot take address of bit-field %qD",
4534 TREE_OPERAND (arg, 1));
4535 return error_mark_node;
4538 /* fall through */
4540 case ARRAY_REF:
4541 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4543 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4544 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4546 error_at (location, "cannot take address of scalar with "
4547 "reverse storage order");
4548 return error_mark_node;
4551 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4552 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4553 warning_at (location, OPT_Wscalar_storage_order,
4554 "address of array with reverse scalar storage "
4555 "order requested");
4558 default:
4559 break;
4562 if (!c_mark_addressable (arg))
4563 return error_mark_node;
4565 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4566 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4568 argtype = build_pointer_type (argtype);
4570 /* ??? Cope with user tricks that amount to offsetof. Delete this
4571 when we have proper support for integer constant expressions. */
4572 val = get_base_address (arg);
4573 if (val && INDIRECT_REF_P (val)
4574 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4576 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4577 goto return_build_unary_op;
4580 val = build1 (ADDR_EXPR, argtype, arg);
4582 ret = val;
4583 goto return_build_unary_op;
4585 default:
4586 gcc_unreachable ();
4589 if (argtype == 0)
4590 argtype = TREE_TYPE (arg);
4591 if (TREE_CODE (arg) == INTEGER_CST)
4592 ret = (require_constant_value
4593 ? fold_build1_initializer_loc (location, code, argtype, arg)
4594 : fold_build1_loc (location, code, argtype, arg));
4595 else
4596 ret = build1 (code, argtype, arg);
4597 return_build_unary_op:
4598 gcc_assert (ret != error_mark_node);
4599 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4600 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4601 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4602 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4603 ret = note_integer_operands (ret);
4604 if (eptype)
4605 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4606 protected_set_expr_location (ret, location);
4607 return ret;
4610 /* Return nonzero if REF is an lvalue valid for this language.
4611 Lvalues can be assigned, unless their type has TYPE_READONLY.
4612 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4614 bool
4615 lvalue_p (const_tree ref)
4617 const enum tree_code code = TREE_CODE (ref);
4619 switch (code)
4621 case REALPART_EXPR:
4622 case IMAGPART_EXPR:
4623 case COMPONENT_REF:
4624 return lvalue_p (TREE_OPERAND (ref, 0));
4626 case C_MAYBE_CONST_EXPR:
4627 return lvalue_p (TREE_OPERAND (ref, 1));
4629 case COMPOUND_LITERAL_EXPR:
4630 case STRING_CST:
4631 return 1;
4633 case INDIRECT_REF:
4634 case ARRAY_REF:
4635 case ARRAY_NOTATION_REF:
4636 case VAR_DECL:
4637 case PARM_DECL:
4638 case RESULT_DECL:
4639 case ERROR_MARK:
4640 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4641 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4643 case BIND_EXPR:
4644 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4646 default:
4647 return 0;
4651 /* Give a warning for storing in something that is read-only in GCC
4652 terms but not const in ISO C terms. */
4654 static void
4655 readonly_warning (tree arg, enum lvalue_use use)
4657 switch (use)
4659 case lv_assign:
4660 warning (0, "assignment of read-only location %qE", arg);
4661 break;
4662 case lv_increment:
4663 warning (0, "increment of read-only location %qE", arg);
4664 break;
4665 case lv_decrement:
4666 warning (0, "decrement of read-only location %qE", arg);
4667 break;
4668 default:
4669 gcc_unreachable ();
4671 return;
4675 /* Return nonzero if REF is an lvalue valid for this language;
4676 otherwise, print an error message and return zero. USE says
4677 how the lvalue is being used and so selects the error message.
4678 LOCATION is the location at which any error should be reported. */
4680 static int
4681 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4683 int win = lvalue_p (ref);
4685 if (!win)
4686 lvalue_error (loc, use);
4688 return win;
4691 /* Mark EXP saying that we need to be able to take the
4692 address of it; it should not be allocated in a register.
4693 Returns true if successful. */
4695 bool
4696 c_mark_addressable (tree exp)
4698 tree x = exp;
4700 while (1)
4701 switch (TREE_CODE (x))
4703 case COMPONENT_REF:
4704 case ADDR_EXPR:
4705 case ARRAY_REF:
4706 case REALPART_EXPR:
4707 case IMAGPART_EXPR:
4708 x = TREE_OPERAND (x, 0);
4709 break;
4711 case COMPOUND_LITERAL_EXPR:
4712 case CONSTRUCTOR:
4713 TREE_ADDRESSABLE (x) = 1;
4714 return true;
4716 case VAR_DECL:
4717 case CONST_DECL:
4718 case PARM_DECL:
4719 case RESULT_DECL:
4720 if (C_DECL_REGISTER (x)
4721 && DECL_NONLOCAL (x))
4723 if (TREE_PUBLIC (x) || is_global_var (x))
4725 error
4726 ("global register variable %qD used in nested function", x);
4727 return false;
4729 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4731 else if (C_DECL_REGISTER (x))
4733 if (TREE_PUBLIC (x) || is_global_var (x))
4734 error ("address of global register variable %qD requested", x);
4735 else
4736 error ("address of register variable %qD requested", x);
4737 return false;
4740 /* FALLTHRU */
4741 case FUNCTION_DECL:
4742 TREE_ADDRESSABLE (x) = 1;
4743 /* FALLTHRU */
4744 default:
4745 return true;
4749 /* Convert EXPR to TYPE, warning about conversion problems with
4750 constants. SEMANTIC_TYPE is the type this conversion would use
4751 without excess precision. If SEMANTIC_TYPE is NULL, this function
4752 is equivalent to convert_and_check. This function is a wrapper that
4753 handles conversions that may be different than
4754 the usual ones because of excess precision. */
4756 static tree
4757 ep_convert_and_check (location_t loc, tree type, tree expr,
4758 tree semantic_type)
4760 if (TREE_TYPE (expr) == type)
4761 return expr;
4763 if (!semantic_type)
4764 return convert_and_check (loc, type, expr);
4766 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4767 && TREE_TYPE (expr) != semantic_type)
4769 /* For integers, we need to check the real conversion, not
4770 the conversion to the excess precision type. */
4771 expr = convert_and_check (loc, semantic_type, expr);
4773 /* Result type is the excess precision type, which should be
4774 large enough, so do not check. */
4775 return convert (type, expr);
4778 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4779 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4780 if folded to an integer constant then the unselected half may
4781 contain arbitrary operations not normally permitted in constant
4782 expressions. Set the location of the expression to LOC. */
4784 tree
4785 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4786 tree op1, tree op1_original_type, tree op2,
4787 tree op2_original_type)
4789 tree type1;
4790 tree type2;
4791 enum tree_code code1;
4792 enum tree_code code2;
4793 tree result_type = NULL;
4794 tree semantic_result_type = NULL;
4795 tree orig_op1 = op1, orig_op2 = op2;
4796 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4797 bool ifexp_int_operands;
4798 tree ret;
4800 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4801 if (op1_int_operands)
4802 op1 = remove_c_maybe_const_expr (op1);
4803 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4804 if (op2_int_operands)
4805 op2 = remove_c_maybe_const_expr (op2);
4806 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4807 if (ifexp_int_operands)
4808 ifexp = remove_c_maybe_const_expr (ifexp);
4810 /* Promote both alternatives. */
4812 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4813 op1 = default_conversion (op1);
4814 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4815 op2 = default_conversion (op2);
4817 if (TREE_CODE (ifexp) == ERROR_MARK
4818 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4819 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4820 return error_mark_node;
4822 type1 = TREE_TYPE (op1);
4823 code1 = TREE_CODE (type1);
4824 type2 = TREE_TYPE (op2);
4825 code2 = TREE_CODE (type2);
4827 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4828 return error_mark_node;
4830 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4831 return error_mark_node;
4833 /* C90 does not permit non-lvalue arrays in conditional expressions.
4834 In C99 they will be pointers by now. */
4835 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4837 error_at (colon_loc, "non-lvalue array in conditional expression");
4838 return error_mark_node;
4841 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4842 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4843 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4844 || code1 == COMPLEX_TYPE)
4845 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4846 || code2 == COMPLEX_TYPE))
4848 semantic_result_type = c_common_type (type1, type2);
4849 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4851 op1 = TREE_OPERAND (op1, 0);
4852 type1 = TREE_TYPE (op1);
4853 gcc_assert (TREE_CODE (type1) == code1);
4855 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4857 op2 = TREE_OPERAND (op2, 0);
4858 type2 = TREE_TYPE (op2);
4859 gcc_assert (TREE_CODE (type2) == code2);
4863 if (warn_cxx_compat)
4865 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4866 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4868 if (TREE_CODE (t1) == ENUMERAL_TYPE
4869 && TREE_CODE (t2) == ENUMERAL_TYPE
4870 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4871 warning_at (colon_loc, OPT_Wc___compat,
4872 ("different enum types in conditional is "
4873 "invalid in C++: %qT vs %qT"),
4874 t1, t2);
4877 /* Quickly detect the usual case where op1 and op2 have the same type
4878 after promotion. */
4879 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4881 if (type1 == type2)
4882 result_type = type1;
4883 else
4884 result_type = TYPE_MAIN_VARIANT (type1);
4886 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4887 || code1 == COMPLEX_TYPE)
4888 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4889 || code2 == COMPLEX_TYPE))
4891 result_type = c_common_type (type1, type2);
4892 if (result_type == error_mark_node)
4893 return error_mark_node;
4894 do_warn_double_promotion (result_type, type1, type2,
4895 "implicit conversion from %qT to %qT to "
4896 "match other result of conditional",
4897 colon_loc);
4899 /* If -Wsign-compare, warn here if type1 and type2 have
4900 different signedness. We'll promote the signed to unsigned
4901 and later code won't know it used to be different.
4902 Do this check on the original types, so that explicit casts
4903 will be considered, but default promotions won't. */
4904 if (c_inhibit_evaluation_warnings == 0)
4906 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4907 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4909 if (unsigned_op1 ^ unsigned_op2)
4911 bool ovf;
4913 /* Do not warn if the result type is signed, since the
4914 signed type will only be chosen if it can represent
4915 all the values of the unsigned type. */
4916 if (!TYPE_UNSIGNED (result_type))
4917 /* OK */;
4918 else
4920 bool op1_maybe_const = true;
4921 bool op2_maybe_const = true;
4923 /* Do not warn if the signed quantity is an
4924 unsuffixed integer literal (or some static
4925 constant expression involving such literals) and
4926 it is non-negative. This warning requires the
4927 operands to be folded for best results, so do
4928 that folding in this case even without
4929 warn_sign_compare to avoid warning options
4930 possibly affecting code generation. */
4931 c_inhibit_evaluation_warnings
4932 += (ifexp == truthvalue_false_node);
4933 op1 = c_fully_fold (op1, require_constant_value,
4934 &op1_maybe_const);
4935 c_inhibit_evaluation_warnings
4936 -= (ifexp == truthvalue_false_node);
4938 c_inhibit_evaluation_warnings
4939 += (ifexp == truthvalue_true_node);
4940 op2 = c_fully_fold (op2, require_constant_value,
4941 &op2_maybe_const);
4942 c_inhibit_evaluation_warnings
4943 -= (ifexp == truthvalue_true_node);
4945 if (warn_sign_compare)
4947 if ((unsigned_op2
4948 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4949 || (unsigned_op1
4950 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4951 /* OK */;
4952 else
4953 warning_at (colon_loc, OPT_Wsign_compare,
4954 ("signed and unsigned type in "
4955 "conditional expression"));
4957 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4958 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4959 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4960 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4965 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4967 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4968 pedwarn (colon_loc, OPT_Wpedantic,
4969 "ISO C forbids conditional expr with only one void side");
4970 result_type = void_type_node;
4972 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4974 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4975 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4976 addr_space_t as_common;
4978 if (comp_target_types (colon_loc, type1, type2))
4979 result_type = common_pointer_type (type1, type2);
4980 else if (null_pointer_constant_p (orig_op1))
4981 result_type = type2;
4982 else if (null_pointer_constant_p (orig_op2))
4983 result_type = type1;
4984 else if (!addr_space_superset (as1, as2, &as_common))
4986 error_at (colon_loc, "pointers to disjoint address spaces "
4987 "used in conditional expression");
4988 return error_mark_node;
4990 else if (VOID_TYPE_P (TREE_TYPE (type1))
4991 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4993 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4994 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4995 & ~TYPE_QUALS (TREE_TYPE (type1))))
4996 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4997 "pointer to array loses qualifier "
4998 "in conditional expression");
5000 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5001 pedwarn (colon_loc, OPT_Wpedantic,
5002 "ISO C forbids conditional expr between "
5003 "%<void *%> and function pointer");
5004 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5005 TREE_TYPE (type2)));
5007 else if (VOID_TYPE_P (TREE_TYPE (type2))
5008 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5010 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5011 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5012 & ~TYPE_QUALS (TREE_TYPE (type2))))
5013 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5014 "pointer to array loses qualifier "
5015 "in conditional expression");
5017 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5018 pedwarn (colon_loc, OPT_Wpedantic,
5019 "ISO C forbids conditional expr between "
5020 "%<void *%> and function pointer");
5021 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5022 TREE_TYPE (type1)));
5024 /* Objective-C pointer comparisons are a bit more lenient. */
5025 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5026 result_type = objc_common_type (type1, type2);
5027 else
5029 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5031 pedwarn (colon_loc, 0,
5032 "pointer type mismatch in conditional expression");
5033 result_type = build_pointer_type
5034 (build_qualified_type (void_type_node, qual));
5037 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5039 if (!null_pointer_constant_p (orig_op2))
5040 pedwarn (colon_loc, 0,
5041 "pointer/integer type mismatch in conditional expression");
5042 else
5044 op2 = null_pointer_node;
5046 result_type = type1;
5048 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5050 if (!null_pointer_constant_p (orig_op1))
5051 pedwarn (colon_loc, 0,
5052 "pointer/integer type mismatch in conditional expression");
5053 else
5055 op1 = null_pointer_node;
5057 result_type = type2;
5060 if (!result_type)
5062 if (flag_cond_mismatch)
5063 result_type = void_type_node;
5064 else
5066 error_at (colon_loc, "type mismatch in conditional expression");
5067 return error_mark_node;
5071 /* Merge const and volatile flags of the incoming types. */
5072 result_type
5073 = build_type_variant (result_type,
5074 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5075 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5077 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5078 semantic_result_type);
5079 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5080 semantic_result_type);
5082 if (ifexp_bcp && ifexp == truthvalue_true_node)
5084 op2_int_operands = true;
5085 op1 = c_fully_fold (op1, require_constant_value, NULL);
5087 if (ifexp_bcp && ifexp == truthvalue_false_node)
5089 op1_int_operands = true;
5090 op2 = c_fully_fold (op2, require_constant_value, NULL);
5092 int_const = int_operands = (ifexp_int_operands
5093 && op1_int_operands
5094 && op2_int_operands);
5095 if (int_operands)
5097 int_const = ((ifexp == truthvalue_true_node
5098 && TREE_CODE (orig_op1) == INTEGER_CST
5099 && !TREE_OVERFLOW (orig_op1))
5100 || (ifexp == truthvalue_false_node
5101 && TREE_CODE (orig_op2) == INTEGER_CST
5102 && !TREE_OVERFLOW (orig_op2)));
5105 /* Need to convert condition operand into a vector mask. */
5106 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5108 tree vectype = TREE_TYPE (ifexp);
5109 tree elem_type = TREE_TYPE (vectype);
5110 tree zero = build_int_cst (elem_type, 0);
5111 tree zero_vec = build_vector_from_val (vectype, zero);
5112 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5113 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5116 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5117 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5118 else
5120 if (int_operands)
5122 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5123 nested inside of the expression. */
5124 op1 = c_fully_fold (op1, false, NULL);
5125 op2 = c_fully_fold (op2, false, NULL);
5127 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5128 if (int_operands)
5129 ret = note_integer_operands (ret);
5131 if (semantic_result_type)
5132 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5134 protected_set_expr_location (ret, colon_loc);
5135 return ret;
5138 /* Return a compound expression that performs two expressions and
5139 returns the value of the second of them.
5141 LOC is the location of the COMPOUND_EXPR. */
5143 tree
5144 build_compound_expr (location_t loc, tree expr1, tree expr2)
5146 bool expr1_int_operands, expr2_int_operands;
5147 tree eptype = NULL_TREE;
5148 tree ret;
5150 if (flag_cilkplus
5151 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5152 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5154 error_at (loc,
5155 "spawned function call cannot be part of a comma expression");
5156 return error_mark_node;
5158 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5159 if (expr1_int_operands)
5160 expr1 = remove_c_maybe_const_expr (expr1);
5161 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5162 if (expr2_int_operands)
5163 expr2 = remove_c_maybe_const_expr (expr2);
5165 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5166 expr1 = TREE_OPERAND (expr1, 0);
5167 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5169 eptype = TREE_TYPE (expr2);
5170 expr2 = TREE_OPERAND (expr2, 0);
5173 if (!TREE_SIDE_EFFECTS (expr1))
5175 /* The left-hand operand of a comma expression is like an expression
5176 statement: with -Wunused, we should warn if it doesn't have
5177 any side-effects, unless it was explicitly cast to (void). */
5178 if (warn_unused_value)
5180 if (VOID_TYPE_P (TREE_TYPE (expr1))
5181 && CONVERT_EXPR_P (expr1))
5182 ; /* (void) a, b */
5183 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5184 && TREE_CODE (expr1) == COMPOUND_EXPR
5185 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5186 ; /* (void) a, (void) b, c */
5187 else
5188 warning_at (loc, OPT_Wunused_value,
5189 "left-hand operand of comma expression has no effect");
5192 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5193 && warn_unused_value)
5195 tree r = expr1;
5196 location_t cloc = loc;
5197 while (TREE_CODE (r) == COMPOUND_EXPR)
5199 if (EXPR_HAS_LOCATION (r))
5200 cloc = EXPR_LOCATION (r);
5201 r = TREE_OPERAND (r, 1);
5203 if (!TREE_SIDE_EFFECTS (r)
5204 && !VOID_TYPE_P (TREE_TYPE (r))
5205 && !CONVERT_EXPR_P (r))
5206 warning_at (cloc, OPT_Wunused_value,
5207 "right-hand operand of comma expression has no effect");
5210 /* With -Wunused, we should also warn if the left-hand operand does have
5211 side-effects, but computes a value which is not used. For example, in
5212 `foo() + bar(), baz()' the result of the `+' operator is not used,
5213 so we should issue a warning. */
5214 else if (warn_unused_value)
5215 warn_if_unused_value (expr1, loc);
5217 if (expr2 == error_mark_node)
5218 return error_mark_node;
5220 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5222 if (flag_isoc99
5223 && expr1_int_operands
5224 && expr2_int_operands)
5225 ret = note_integer_operands (ret);
5227 if (eptype)
5228 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5230 protected_set_expr_location (ret, loc);
5231 return ret;
5234 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5235 which we are casting. OTYPE is the type of the expression being
5236 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5237 of the cast. -Wcast-qual appeared on the command line. Named
5238 address space qualifiers are not handled here, because they result
5239 in different warnings. */
5241 static void
5242 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5244 tree in_type = type;
5245 tree in_otype = otype;
5246 int added = 0;
5247 int discarded = 0;
5248 bool is_const;
5250 /* Check that the qualifiers on IN_TYPE are a superset of the
5251 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5252 nodes is uninteresting and we stop as soon as we hit a
5253 non-POINTER_TYPE node on either type. */
5256 in_otype = TREE_TYPE (in_otype);
5257 in_type = TREE_TYPE (in_type);
5259 /* GNU C allows cv-qualified function types. 'const' means the
5260 function is very pure, 'volatile' means it can't return. We
5261 need to warn when such qualifiers are added, not when they're
5262 taken away. */
5263 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5264 && TREE_CODE (in_type) == FUNCTION_TYPE)
5265 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5266 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5267 else
5268 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5269 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5271 while (TREE_CODE (in_type) == POINTER_TYPE
5272 && TREE_CODE (in_otype) == POINTER_TYPE);
5274 if (added)
5275 warning_at (loc, OPT_Wcast_qual,
5276 "cast adds %q#v qualifier to function type", added);
5278 if (discarded)
5279 /* There are qualifiers present in IN_OTYPE that are not present
5280 in IN_TYPE. */
5281 warning_at (loc, OPT_Wcast_qual,
5282 "cast discards %qv qualifier from pointer target type",
5283 discarded);
5285 if (added || discarded)
5286 return;
5288 /* A cast from **T to const **T is unsafe, because it can cause a
5289 const value to be changed with no additional warning. We only
5290 issue this warning if T is the same on both sides, and we only
5291 issue the warning if there are the same number of pointers on
5292 both sides, as otherwise the cast is clearly unsafe anyhow. A
5293 cast is unsafe when a qualifier is added at one level and const
5294 is not present at all outer levels.
5296 To issue this warning, we check at each level whether the cast
5297 adds new qualifiers not already seen. We don't need to special
5298 case function types, as they won't have the same
5299 TYPE_MAIN_VARIANT. */
5301 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5302 return;
5303 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5304 return;
5306 in_type = type;
5307 in_otype = otype;
5308 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5311 in_type = TREE_TYPE (in_type);
5312 in_otype = TREE_TYPE (in_otype);
5313 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5314 && !is_const)
5316 warning_at (loc, OPT_Wcast_qual,
5317 "to be safe all intermediate pointers in cast from "
5318 "%qT to %qT must be %<const%> qualified",
5319 otype, type);
5320 break;
5322 if (is_const)
5323 is_const = TYPE_READONLY (in_type);
5325 while (TREE_CODE (in_type) == POINTER_TYPE);
5328 /* Build an expression representing a cast to type TYPE of expression EXPR.
5329 LOC is the location of the cast-- typically the open paren of the cast. */
5331 tree
5332 build_c_cast (location_t loc, tree type, tree expr)
5334 tree value;
5336 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5337 expr = TREE_OPERAND (expr, 0);
5339 value = expr;
5341 if (type == error_mark_node || expr == error_mark_node)
5342 return error_mark_node;
5344 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5345 only in <protocol> qualifications. But when constructing cast expressions,
5346 the protocols do matter and must be kept around. */
5347 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5348 return build1 (NOP_EXPR, type, expr);
5350 type = TYPE_MAIN_VARIANT (type);
5352 if (TREE_CODE (type) == ARRAY_TYPE)
5354 error_at (loc, "cast specifies array type");
5355 return error_mark_node;
5358 if (TREE_CODE (type) == FUNCTION_TYPE)
5360 error_at (loc, "cast specifies function type");
5361 return error_mark_node;
5364 if (!VOID_TYPE_P (type))
5366 value = require_complete_type (loc, value);
5367 if (value == error_mark_node)
5368 return error_mark_node;
5371 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5373 if (RECORD_OR_UNION_TYPE_P (type))
5374 pedwarn (loc, OPT_Wpedantic,
5375 "ISO C forbids casting nonscalar to the same type");
5377 /* Convert to remove any qualifiers from VALUE's type. */
5378 value = convert (type, value);
5380 else if (TREE_CODE (type) == UNION_TYPE)
5382 tree field;
5384 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5385 if (TREE_TYPE (field) != error_mark_node
5386 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5387 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5388 break;
5390 if (field)
5392 tree t;
5393 bool maybe_const = true;
5395 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5396 t = c_fully_fold (value, false, &maybe_const);
5397 t = build_constructor_single (type, field, t);
5398 if (!maybe_const)
5399 t = c_wrap_maybe_const (t, true);
5400 t = digest_init (loc, type, t,
5401 NULL_TREE, false, true, 0);
5402 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5403 return t;
5405 error_at (loc, "cast to union type from type not present in union");
5406 return error_mark_node;
5408 else
5410 tree otype, ovalue;
5412 if (type == void_type_node)
5414 tree t = build1 (CONVERT_EXPR, type, value);
5415 SET_EXPR_LOCATION (t, loc);
5416 return t;
5419 otype = TREE_TYPE (value);
5421 /* Optionally warn about potentially worrisome casts. */
5422 if (warn_cast_qual
5423 && TREE_CODE (type) == POINTER_TYPE
5424 && TREE_CODE (otype) == POINTER_TYPE)
5425 handle_warn_cast_qual (loc, type, otype);
5427 /* Warn about conversions between pointers to disjoint
5428 address spaces. */
5429 if (TREE_CODE (type) == POINTER_TYPE
5430 && TREE_CODE (otype) == POINTER_TYPE
5431 && !null_pointer_constant_p (value))
5433 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5434 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5435 addr_space_t as_common;
5437 if (!addr_space_superset (as_to, as_from, &as_common))
5439 if (ADDR_SPACE_GENERIC_P (as_from))
5440 warning_at (loc, 0, "cast to %s address space pointer "
5441 "from disjoint generic address space pointer",
5442 c_addr_space_name (as_to));
5444 else if (ADDR_SPACE_GENERIC_P (as_to))
5445 warning_at (loc, 0, "cast to generic address space pointer "
5446 "from disjoint %s address space pointer",
5447 c_addr_space_name (as_from));
5449 else
5450 warning_at (loc, 0, "cast to %s address space pointer "
5451 "from disjoint %s address space pointer",
5452 c_addr_space_name (as_to),
5453 c_addr_space_name (as_from));
5457 /* Warn about possible alignment problems. */
5458 if (STRICT_ALIGNMENT
5459 && TREE_CODE (type) == POINTER_TYPE
5460 && TREE_CODE (otype) == POINTER_TYPE
5461 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5462 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5463 /* Don't warn about opaque types, where the actual alignment
5464 restriction is unknown. */
5465 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5466 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5467 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5468 warning_at (loc, OPT_Wcast_align,
5469 "cast increases required alignment of target type");
5471 if (TREE_CODE (type) == INTEGER_TYPE
5472 && TREE_CODE (otype) == POINTER_TYPE
5473 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5474 /* Unlike conversion of integers to pointers, where the
5475 warning is disabled for converting constants because
5476 of cases such as SIG_*, warn about converting constant
5477 pointers to integers. In some cases it may cause unwanted
5478 sign extension, and a warning is appropriate. */
5479 warning_at (loc, OPT_Wpointer_to_int_cast,
5480 "cast from pointer to integer of different size");
5482 if (TREE_CODE (value) == CALL_EXPR
5483 && TREE_CODE (type) != TREE_CODE (otype))
5484 warning_at (loc, OPT_Wbad_function_cast,
5485 "cast from function call of type %qT "
5486 "to non-matching type %qT", otype, type);
5488 if (TREE_CODE (type) == POINTER_TYPE
5489 && TREE_CODE (otype) == INTEGER_TYPE
5490 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5491 /* Don't warn about converting any constant. */
5492 && !TREE_CONSTANT (value))
5493 warning_at (loc,
5494 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5495 "of different size");
5497 if (warn_strict_aliasing <= 2)
5498 strict_aliasing_warning (otype, type, expr);
5500 /* If pedantic, warn for conversions between function and object
5501 pointer types, except for converting a null pointer constant
5502 to function pointer type. */
5503 if (pedantic
5504 && TREE_CODE (type) == POINTER_TYPE
5505 && TREE_CODE (otype) == POINTER_TYPE
5506 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5507 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5508 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5509 "conversion of function pointer to object pointer type");
5511 if (pedantic
5512 && TREE_CODE (type) == POINTER_TYPE
5513 && TREE_CODE (otype) == POINTER_TYPE
5514 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5515 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5516 && !null_pointer_constant_p (value))
5517 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5518 "conversion of object pointer to function pointer type");
5520 ovalue = value;
5521 value = convert (type, value);
5523 /* Ignore any integer overflow caused by the cast. */
5524 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5526 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5528 if (!TREE_OVERFLOW (value))
5530 /* Avoid clobbering a shared constant. */
5531 value = copy_node (value);
5532 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5535 else if (TREE_OVERFLOW (value))
5536 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5537 value = wide_int_to_tree (TREE_TYPE (value), value);
5541 /* Don't let a cast be an lvalue. */
5542 if (lvalue_p (value))
5543 value = non_lvalue_loc (loc, value);
5545 /* Don't allow the results of casting to floating-point or complex
5546 types be confused with actual constants, or casts involving
5547 integer and pointer types other than direct integer-to-integer
5548 and integer-to-pointer be confused with integer constant
5549 expressions and null pointer constants. */
5550 if (TREE_CODE (value) == REAL_CST
5551 || TREE_CODE (value) == COMPLEX_CST
5552 || (TREE_CODE (value) == INTEGER_CST
5553 && !((TREE_CODE (expr) == INTEGER_CST
5554 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5555 || TREE_CODE (expr) == REAL_CST
5556 || TREE_CODE (expr) == COMPLEX_CST)))
5557 value = build1 (NOP_EXPR, type, value);
5559 protected_set_expr_location (value, loc);
5560 return value;
5563 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5564 location of the open paren of the cast, or the position of the cast
5565 expr. */
5566 tree
5567 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5569 tree type;
5570 tree type_expr = NULL_TREE;
5571 bool type_expr_const = true;
5572 tree ret;
5573 int saved_wsp = warn_strict_prototypes;
5575 /* This avoids warnings about unprototyped casts on
5576 integers. E.g. "#define SIG_DFL (void(*)())0". */
5577 if (TREE_CODE (expr) == INTEGER_CST)
5578 warn_strict_prototypes = 0;
5579 type = groktypename (type_name, &type_expr, &type_expr_const);
5580 warn_strict_prototypes = saved_wsp;
5582 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5583 && reject_gcc_builtin (expr))
5584 return error_mark_node;
5586 ret = build_c_cast (loc, type, expr);
5587 if (type_expr)
5589 bool inner_expr_const = true;
5590 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5591 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5592 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5593 && inner_expr_const);
5594 SET_EXPR_LOCATION (ret, loc);
5597 if (!EXPR_HAS_LOCATION (ret))
5598 protected_set_expr_location (ret, loc);
5600 /* C++ does not permits types to be defined in a cast, but it
5601 allows references to incomplete types. */
5602 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5603 warning_at (loc, OPT_Wc___compat,
5604 "defining a type in a cast is invalid in C++");
5606 return ret;
5609 /* Build an assignment expression of lvalue LHS from value RHS.
5610 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5611 may differ from TREE_TYPE (LHS) for an enum bitfield.
5612 MODIFYCODE is the code for a binary operator that we use
5613 to combine the old value of LHS with RHS to get the new value.
5614 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5615 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5616 which may differ from TREE_TYPE (RHS) for an enum value.
5618 LOCATION is the location of the MODIFYCODE operator.
5619 RHS_LOC is the location of the RHS. */
5621 tree
5622 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5623 enum tree_code modifycode,
5624 location_t rhs_loc, tree rhs, tree rhs_origtype)
5626 tree result;
5627 tree newrhs;
5628 tree rhseval = NULL_TREE;
5629 tree rhs_semantic_type = NULL_TREE;
5630 tree lhstype = TREE_TYPE (lhs);
5631 tree olhstype = lhstype;
5632 bool npc;
5633 bool is_atomic_op;
5635 /* Types that aren't fully specified cannot be used in assignments. */
5636 lhs = require_complete_type (location, lhs);
5638 /* Avoid duplicate error messages from operands that had errors. */
5639 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5640 return error_mark_node;
5642 /* Ensure an error for assigning a non-lvalue array to an array in
5643 C90. */
5644 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5646 error_at (location, "assignment to expression with array type");
5647 return error_mark_node;
5650 /* For ObjC properties, defer this check. */
5651 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5652 return error_mark_node;
5654 is_atomic_op = really_atomic_lvalue (lhs);
5656 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5658 rhs_semantic_type = TREE_TYPE (rhs);
5659 rhs = TREE_OPERAND (rhs, 0);
5662 newrhs = rhs;
5664 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5666 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5667 lhs_origtype, modifycode, rhs_loc, rhs,
5668 rhs_origtype);
5669 if (inner == error_mark_node)
5670 return error_mark_node;
5671 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5672 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5673 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5674 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5675 protected_set_expr_location (result, location);
5676 return result;
5679 /* If a binary op has been requested, combine the old LHS value with the RHS
5680 producing the value we should actually store into the LHS. */
5682 if (modifycode != NOP_EXPR)
5684 lhs = c_fully_fold (lhs, false, NULL);
5685 lhs = stabilize_reference (lhs);
5687 /* Construct the RHS for any non-atomic compound assignemnt. */
5688 if (!is_atomic_op)
5690 /* If in LHS op= RHS the RHS has side-effects, ensure they
5691 are preevaluated before the rest of the assignment expression's
5692 side-effects, because RHS could contain e.g. function calls
5693 that modify LHS. */
5694 if (TREE_SIDE_EFFECTS (rhs))
5696 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5697 rhseval = newrhs;
5699 newrhs = build_binary_op (location,
5700 modifycode, lhs, newrhs, 1);
5702 /* The original type of the right hand side is no longer
5703 meaningful. */
5704 rhs_origtype = NULL_TREE;
5708 if (c_dialect_objc ())
5710 /* Check if we are modifying an Objective-C property reference;
5711 if so, we need to generate setter calls. */
5712 result = objc_maybe_build_modify_expr (lhs, newrhs);
5713 if (result)
5714 goto return_result;
5716 /* Else, do the check that we postponed for Objective-C. */
5717 if (!lvalue_or_else (location, lhs, lv_assign))
5718 return error_mark_node;
5721 /* Give an error for storing in something that is 'const'. */
5723 if (TYPE_READONLY (lhstype)
5724 || (RECORD_OR_UNION_TYPE_P (lhstype)
5725 && C_TYPE_FIELDS_READONLY (lhstype)))
5727 readonly_error (location, lhs, lv_assign);
5728 return error_mark_node;
5730 else if (TREE_READONLY (lhs))
5731 readonly_warning (lhs, lv_assign);
5733 /* If storing into a structure or union member,
5734 it has probably been given type `int'.
5735 Compute the type that would go with
5736 the actual amount of storage the member occupies. */
5738 if (TREE_CODE (lhs) == COMPONENT_REF
5739 && (TREE_CODE (lhstype) == INTEGER_TYPE
5740 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5741 || TREE_CODE (lhstype) == REAL_TYPE
5742 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5743 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5745 /* If storing in a field that is in actuality a short or narrower than one,
5746 we must store in the field in its actual type. */
5748 if (lhstype != TREE_TYPE (lhs))
5750 lhs = copy_node (lhs);
5751 TREE_TYPE (lhs) = lhstype;
5754 /* Issue -Wc++-compat warnings about an assignment to an enum type
5755 when LHS does not have its original type. This happens for,
5756 e.g., an enum bitfield in a struct. */
5757 if (warn_cxx_compat
5758 && lhs_origtype != NULL_TREE
5759 && lhs_origtype != lhstype
5760 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5762 tree checktype = (rhs_origtype != NULL_TREE
5763 ? rhs_origtype
5764 : TREE_TYPE (rhs));
5765 if (checktype != error_mark_node
5766 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5767 || (is_atomic_op && modifycode != NOP_EXPR)))
5768 warning_at (location, OPT_Wc___compat,
5769 "enum conversion in assignment is invalid in C++");
5772 /* If the lhs is atomic, remove that qualifier. */
5773 if (is_atomic_op)
5775 lhstype = build_qualified_type (lhstype,
5776 (TYPE_QUALS (lhstype)
5777 & ~TYPE_QUAL_ATOMIC));
5778 olhstype = build_qualified_type (olhstype,
5779 (TYPE_QUALS (lhstype)
5780 & ~TYPE_QUAL_ATOMIC));
5783 /* Convert new value to destination type. Fold it first, then
5784 restore any excess precision information, for the sake of
5785 conversion warnings. */
5787 if (!(is_atomic_op && modifycode != NOP_EXPR))
5789 npc = null_pointer_constant_p (newrhs);
5790 newrhs = c_fully_fold (newrhs, false, NULL);
5791 if (rhs_semantic_type)
5792 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5793 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5794 rhs_origtype, ic_assign, npc,
5795 NULL_TREE, NULL_TREE, 0);
5796 if (TREE_CODE (newrhs) == ERROR_MARK)
5797 return error_mark_node;
5800 /* Emit ObjC write barrier, if necessary. */
5801 if (c_dialect_objc () && flag_objc_gc)
5803 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5804 if (result)
5806 protected_set_expr_location (result, location);
5807 goto return_result;
5811 /* Scan operands. */
5813 if (is_atomic_op)
5814 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5815 else
5817 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5818 TREE_SIDE_EFFECTS (result) = 1;
5819 protected_set_expr_location (result, location);
5822 /* If we got the LHS in a different type for storing in,
5823 convert the result back to the nominal type of LHS
5824 so that the value we return always has the same type
5825 as the LHS argument. */
5827 if (olhstype == TREE_TYPE (result))
5828 goto return_result;
5830 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5831 rhs_origtype, ic_assign, false, NULL_TREE,
5832 NULL_TREE, 0);
5833 protected_set_expr_location (result, location);
5835 return_result:
5836 if (rhseval)
5837 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5838 return result;
5841 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5842 This is used to implement -fplan9-extensions. */
5844 static bool
5845 find_anonymous_field_with_type (tree struct_type, tree type)
5847 tree field;
5848 bool found;
5850 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5851 found = false;
5852 for (field = TYPE_FIELDS (struct_type);
5853 field != NULL_TREE;
5854 field = TREE_CHAIN (field))
5856 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5857 ? c_build_qualified_type (TREE_TYPE (field),
5858 TYPE_QUAL_ATOMIC)
5859 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5860 if (DECL_NAME (field) == NULL
5861 && comptypes (type, fieldtype))
5863 if (found)
5864 return false;
5865 found = true;
5867 else if (DECL_NAME (field) == NULL
5868 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5869 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5871 if (found)
5872 return false;
5873 found = true;
5876 return found;
5879 /* RHS is an expression whose type is pointer to struct. If there is
5880 an anonymous field in RHS with type TYPE, then return a pointer to
5881 that field in RHS. This is used with -fplan9-extensions. This
5882 returns NULL if no conversion could be found. */
5884 static tree
5885 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5887 tree rhs_struct_type, lhs_main_type;
5888 tree field, found_field;
5889 bool found_sub_field;
5890 tree ret;
5892 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5893 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5894 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5896 gcc_assert (POINTER_TYPE_P (type));
5897 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5898 ? c_build_qualified_type (TREE_TYPE (type),
5899 TYPE_QUAL_ATOMIC)
5900 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5902 found_field = NULL_TREE;
5903 found_sub_field = false;
5904 for (field = TYPE_FIELDS (rhs_struct_type);
5905 field != NULL_TREE;
5906 field = TREE_CHAIN (field))
5908 if (DECL_NAME (field) != NULL_TREE
5909 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5910 continue;
5911 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5912 ? c_build_qualified_type (TREE_TYPE (field),
5913 TYPE_QUAL_ATOMIC)
5914 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5915 if (comptypes (lhs_main_type, fieldtype))
5917 if (found_field != NULL_TREE)
5918 return NULL_TREE;
5919 found_field = field;
5921 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5922 lhs_main_type))
5924 if (found_field != NULL_TREE)
5925 return NULL_TREE;
5926 found_field = field;
5927 found_sub_field = true;
5931 if (found_field == NULL_TREE)
5932 return NULL_TREE;
5934 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5935 build_fold_indirect_ref (rhs), found_field,
5936 NULL_TREE);
5937 ret = build_fold_addr_expr_loc (location, ret);
5939 if (found_sub_field)
5941 ret = convert_to_anonymous_field (location, type, ret);
5942 gcc_assert (ret != NULL_TREE);
5945 return ret;
5948 /* Issue an error message for a bad initializer component.
5949 GMSGID identifies the message.
5950 The component name is taken from the spelling stack. */
5952 static void
5953 error_init (location_t loc, const char *gmsgid)
5955 char *ofwhat;
5957 /* The gmsgid may be a format string with %< and %>. */
5958 error_at (loc, gmsgid);
5959 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5960 if (*ofwhat)
5961 inform (loc, "(near initialization for %qs)", ofwhat);
5964 /* Issue a pedantic warning for a bad initializer component. OPT is
5965 the option OPT_* (from options.h) controlling this warning or 0 if
5966 it is unconditionally given. GMSGID identifies the message. The
5967 component name is taken from the spelling stack. */
5969 static void
5970 pedwarn_init (location_t loc, int opt, const char *gmsgid)
5972 char *ofwhat;
5973 bool warned;
5975 /* Use the location where a macro was expanded rather than where
5976 it was defined to make sure macros defined in system headers
5977 but used incorrectly elsewhere are diagnosed. */
5978 source_location exploc = expansion_point_location_if_in_system_header (loc);
5980 /* The gmsgid may be a format string with %< and %>. */
5981 warned = pedwarn (exploc, opt, gmsgid);
5982 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5983 if (*ofwhat && warned)
5984 inform (exploc, "(near initialization for %qs)", ofwhat);
5987 /* Issue a warning for a bad initializer component.
5989 OPT is the OPT_W* value corresponding to the warning option that
5990 controls this warning. GMSGID identifies the message. The
5991 component name is taken from the spelling stack. */
5993 static void
5994 warning_init (location_t loc, int opt, const char *gmsgid)
5996 char *ofwhat;
5997 bool warned;
5999 /* Use the location where a macro was expanded rather than where
6000 it was defined to make sure macros defined in system headers
6001 but used incorrectly elsewhere are diagnosed. */
6002 source_location exploc = expansion_point_location_if_in_system_header (loc);
6004 /* The gmsgid may be a format string with %< and %>. */
6005 warned = warning_at (exploc, opt, gmsgid);
6006 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6007 if (*ofwhat && warned)
6008 inform (exploc, "(near initialization for %qs)", ofwhat);
6011 /* If TYPE is an array type and EXPR is a parenthesized string
6012 constant, warn if pedantic that EXPR is being used to initialize an
6013 object of type TYPE. */
6015 void
6016 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6018 if (pedantic
6019 && TREE_CODE (type) == ARRAY_TYPE
6020 && TREE_CODE (expr.value) == STRING_CST
6021 && expr.original_code != STRING_CST)
6022 pedwarn_init (loc, OPT_Wpedantic,
6023 "array initialized from parenthesized string constant");
6026 /* Convert value RHS to type TYPE as preparation for an assignment to
6027 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6028 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6029 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6030 constant before any folding.
6031 The real work of conversion is done by `convert'.
6032 The purpose of this function is to generate error messages
6033 for assignments that are not allowed in C.
6034 ERRTYPE says whether it is argument passing, assignment,
6035 initialization or return.
6037 In the following example, '~' denotes where EXPR_LOC and '^' where
6038 LOCATION point to:
6040 f (var); [ic_argpass]
6041 ^ ~~~
6042 x = var; [ic_assign]
6043 ^ ~~~;
6044 int x = var; [ic_init]
6046 return x; [ic_return]
6049 FUNCTION is a tree for the function being called.
6050 PARMNUM is the number of the argument, for printing in error messages. */
6052 static tree
6053 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6054 tree rhs, tree origtype, enum impl_conv errtype,
6055 bool null_pointer_constant, tree fundecl,
6056 tree function, int parmnum)
6058 enum tree_code codel = TREE_CODE (type);
6059 tree orig_rhs = rhs;
6060 tree rhstype;
6061 enum tree_code coder;
6062 tree rname = NULL_TREE;
6063 bool objc_ok = false;
6065 /* Use the expansion point location to handle cases such as user's
6066 function returning a wrong-type macro defined in a system header. */
6067 location = expansion_point_location_if_in_system_header (location);
6069 if (errtype == ic_argpass)
6071 tree selector;
6072 /* Change pointer to function to the function itself for
6073 diagnostics. */
6074 if (TREE_CODE (function) == ADDR_EXPR
6075 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6076 function = TREE_OPERAND (function, 0);
6078 /* Handle an ObjC selector specially for diagnostics. */
6079 selector = objc_message_selector ();
6080 rname = function;
6081 if (selector && parmnum > 2)
6083 rname = selector;
6084 parmnum -= 2;
6088 /* This macro is used to emit diagnostics to ensure that all format
6089 strings are complete sentences, visible to gettext and checked at
6090 compile time. */
6091 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6092 do { \
6093 switch (errtype) \
6095 case ic_argpass: \
6096 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6097 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6098 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6099 "expected %qT but argument is of type %qT", \
6100 type, rhstype); \
6101 break; \
6102 case ic_assign: \
6103 pedwarn (LOCATION, OPT, AS); \
6104 break; \
6105 case ic_init: \
6106 pedwarn_init (LOCATION, OPT, IN); \
6107 break; \
6108 case ic_return: \
6109 pedwarn (LOCATION, OPT, RE); \
6110 break; \
6111 default: \
6112 gcc_unreachable (); \
6114 } while (0)
6116 /* This macro is used to emit diagnostics to ensure that all format
6117 strings are complete sentences, visible to gettext and checked at
6118 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6119 extra parameter to enumerate qualifiers. */
6120 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6121 do { \
6122 switch (errtype) \
6124 case ic_argpass: \
6125 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6126 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6127 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6128 "expected %qT but argument is of type %qT", \
6129 type, rhstype); \
6130 break; \
6131 case ic_assign: \
6132 pedwarn (LOCATION, OPT, AS, QUALS); \
6133 break; \
6134 case ic_init: \
6135 pedwarn (LOCATION, OPT, IN, QUALS); \
6136 break; \
6137 case ic_return: \
6138 pedwarn (LOCATION, OPT, RE, QUALS); \
6139 break; \
6140 default: \
6141 gcc_unreachable (); \
6143 } while (0)
6145 /* This macro is used to emit diagnostics to ensure that all format
6146 strings are complete sentences, visible to gettext and checked at
6147 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6148 warning_at instead of pedwarn. */
6149 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6150 do { \
6151 switch (errtype) \
6153 case ic_argpass: \
6154 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6155 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6156 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6157 "expected %qT but argument is of type %qT", \
6158 type, rhstype); \
6159 break; \
6160 case ic_assign: \
6161 warning_at (LOCATION, OPT, AS, QUALS); \
6162 break; \
6163 case ic_init: \
6164 warning_at (LOCATION, OPT, IN, QUALS); \
6165 break; \
6166 case ic_return: \
6167 warning_at (LOCATION, OPT, RE, QUALS); \
6168 break; \
6169 default: \
6170 gcc_unreachable (); \
6172 } while (0)
6174 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6175 rhs = TREE_OPERAND (rhs, 0);
6177 rhstype = TREE_TYPE (rhs);
6178 coder = TREE_CODE (rhstype);
6180 if (coder == ERROR_MARK)
6181 return error_mark_node;
6183 if (c_dialect_objc ())
6185 int parmno;
6187 switch (errtype)
6189 case ic_return:
6190 parmno = 0;
6191 break;
6193 case ic_assign:
6194 parmno = -1;
6195 break;
6197 case ic_init:
6198 parmno = -2;
6199 break;
6201 default:
6202 parmno = parmnum;
6203 break;
6206 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6209 if (warn_cxx_compat)
6211 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6212 if (checktype != error_mark_node
6213 && TREE_CODE (type) == ENUMERAL_TYPE
6214 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6216 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6217 G_("enum conversion when passing argument "
6218 "%d of %qE is invalid in C++"),
6219 G_("enum conversion in assignment is "
6220 "invalid in C++"),
6221 G_("enum conversion in initialization is "
6222 "invalid in C++"),
6223 G_("enum conversion in return is "
6224 "invalid in C++"));
6228 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6229 return rhs;
6231 if (coder == VOID_TYPE)
6233 /* Except for passing an argument to an unprototyped function,
6234 this is a constraint violation. When passing an argument to
6235 an unprototyped function, it is compile-time undefined;
6236 making it a constraint in that case was rejected in
6237 DR#252. */
6238 error_at (location, "void value not ignored as it ought to be");
6239 return error_mark_node;
6241 rhs = require_complete_type (location, rhs);
6242 if (rhs == error_mark_node)
6243 return error_mark_node;
6245 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6246 return error_mark_node;
6248 /* A non-reference type can convert to a reference. This handles
6249 va_start, va_copy and possibly port built-ins. */
6250 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6252 if (!lvalue_p (rhs))
6254 error_at (location, "cannot pass rvalue to reference parameter");
6255 return error_mark_node;
6257 if (!c_mark_addressable (rhs))
6258 return error_mark_node;
6259 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6260 SET_EXPR_LOCATION (rhs, location);
6262 rhs = convert_for_assignment (location, expr_loc,
6263 build_pointer_type (TREE_TYPE (type)),
6264 rhs, origtype, errtype,
6265 null_pointer_constant, fundecl, function,
6266 parmnum);
6267 if (rhs == error_mark_node)
6268 return error_mark_node;
6270 rhs = build1 (NOP_EXPR, type, rhs);
6271 SET_EXPR_LOCATION (rhs, location);
6272 return rhs;
6274 /* Some types can interconvert without explicit casts. */
6275 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6276 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6277 return convert (type, rhs);
6278 /* Arithmetic types all interconvert, and enum is treated like int. */
6279 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6280 || codel == FIXED_POINT_TYPE
6281 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6282 || codel == BOOLEAN_TYPE)
6283 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6284 || coder == FIXED_POINT_TYPE
6285 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6286 || coder == BOOLEAN_TYPE))
6288 tree ret;
6289 bool save = in_late_binary_op;
6290 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6291 || (coder == REAL_TYPE
6292 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6293 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6294 in_late_binary_op = true;
6295 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6296 ? expr_loc : location, type, orig_rhs);
6297 in_late_binary_op = save;
6298 return ret;
6301 /* Aggregates in different TUs might need conversion. */
6302 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6303 && codel == coder
6304 && comptypes (type, rhstype))
6305 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6306 ? expr_loc : location, type, rhs);
6308 /* Conversion to a transparent union or record from its member types.
6309 This applies only to function arguments. */
6310 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6311 && TYPE_TRANSPARENT_AGGR (type))
6312 && errtype == ic_argpass)
6314 tree memb, marginal_memb = NULL_TREE;
6316 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6318 tree memb_type = TREE_TYPE (memb);
6320 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6321 TYPE_MAIN_VARIANT (rhstype)))
6322 break;
6324 if (TREE_CODE (memb_type) != POINTER_TYPE)
6325 continue;
6327 if (coder == POINTER_TYPE)
6329 tree ttl = TREE_TYPE (memb_type);
6330 tree ttr = TREE_TYPE (rhstype);
6332 /* Any non-function converts to a [const][volatile] void *
6333 and vice versa; otherwise, targets must be the same.
6334 Meanwhile, the lhs target must have all the qualifiers of
6335 the rhs. */
6336 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6337 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6338 || comp_target_types (location, memb_type, rhstype))
6340 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6341 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6342 /* If this type won't generate any warnings, use it. */
6343 if (lquals == rquals
6344 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6345 && TREE_CODE (ttl) == FUNCTION_TYPE)
6346 ? ((lquals | rquals) == rquals)
6347 : ((lquals | rquals) == lquals)))
6348 break;
6350 /* Keep looking for a better type, but remember this one. */
6351 if (!marginal_memb)
6352 marginal_memb = memb;
6356 /* Can convert integer zero to any pointer type. */
6357 if (null_pointer_constant)
6359 rhs = null_pointer_node;
6360 break;
6364 if (memb || marginal_memb)
6366 if (!memb)
6368 /* We have only a marginally acceptable member type;
6369 it needs a warning. */
6370 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6371 tree ttr = TREE_TYPE (rhstype);
6373 /* Const and volatile mean something different for function
6374 types, so the usual warnings are not appropriate. */
6375 if (TREE_CODE (ttr) == FUNCTION_TYPE
6376 && TREE_CODE (ttl) == FUNCTION_TYPE)
6378 /* Because const and volatile on functions are
6379 restrictions that say the function will not do
6380 certain things, it is okay to use a const or volatile
6381 function where an ordinary one is wanted, but not
6382 vice-versa. */
6383 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6384 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6385 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6386 OPT_Wdiscarded_qualifiers,
6387 G_("passing argument %d of %qE "
6388 "makes %q#v qualified function "
6389 "pointer from unqualified"),
6390 G_("assignment makes %q#v qualified "
6391 "function pointer from "
6392 "unqualified"),
6393 G_("initialization makes %q#v qualified "
6394 "function pointer from "
6395 "unqualified"),
6396 G_("return makes %q#v qualified function "
6397 "pointer from unqualified"),
6398 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6400 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6401 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6402 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6403 OPT_Wdiscarded_qualifiers,
6404 G_("passing argument %d of %qE discards "
6405 "%qv qualifier from pointer target type"),
6406 G_("assignment discards %qv qualifier "
6407 "from pointer target type"),
6408 G_("initialization discards %qv qualifier "
6409 "from pointer target type"),
6410 G_("return discards %qv qualifier from "
6411 "pointer target type"),
6412 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6414 memb = marginal_memb;
6417 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6418 pedwarn (location, OPT_Wpedantic,
6419 "ISO C prohibits argument conversion to union type");
6421 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6422 return build_constructor_single (type, memb, rhs);
6426 /* Conversions among pointers */
6427 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6428 && (coder == codel))
6430 tree ttl = TREE_TYPE (type);
6431 tree ttr = TREE_TYPE (rhstype);
6432 tree mvl = ttl;
6433 tree mvr = ttr;
6434 bool is_opaque_pointer;
6435 int target_cmp = 0; /* Cache comp_target_types () result. */
6436 addr_space_t asl;
6437 addr_space_t asr;
6439 if (TREE_CODE (mvl) != ARRAY_TYPE)
6440 mvl = (TYPE_ATOMIC (mvl)
6441 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6442 TYPE_QUAL_ATOMIC)
6443 : TYPE_MAIN_VARIANT (mvl));
6444 if (TREE_CODE (mvr) != ARRAY_TYPE)
6445 mvr = (TYPE_ATOMIC (mvr)
6446 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6447 TYPE_QUAL_ATOMIC)
6448 : TYPE_MAIN_VARIANT (mvr));
6449 /* Opaque pointers are treated like void pointers. */
6450 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6452 /* The Plan 9 compiler permits a pointer to a struct to be
6453 automatically converted into a pointer to an anonymous field
6454 within the struct. */
6455 if (flag_plan9_extensions
6456 && RECORD_OR_UNION_TYPE_P (mvl)
6457 && RECORD_OR_UNION_TYPE_P (mvr)
6458 && mvl != mvr)
6460 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6461 if (new_rhs != NULL_TREE)
6463 rhs = new_rhs;
6464 rhstype = TREE_TYPE (rhs);
6465 coder = TREE_CODE (rhstype);
6466 ttr = TREE_TYPE (rhstype);
6467 mvr = TYPE_MAIN_VARIANT (ttr);
6471 /* C++ does not allow the implicit conversion void* -> T*. However,
6472 for the purpose of reducing the number of false positives, we
6473 tolerate the special case of
6475 int *p = NULL;
6477 where NULL is typically defined in C to be '(void *) 0'. */
6478 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6479 warning_at (errtype == ic_argpass ? expr_loc : location,
6480 OPT_Wc___compat,
6481 "request for implicit conversion "
6482 "from %qT to %qT not permitted in C++", rhstype, type);
6484 /* See if the pointers point to incompatible address spaces. */
6485 asl = TYPE_ADDR_SPACE (ttl);
6486 asr = TYPE_ADDR_SPACE (ttr);
6487 if (!null_pointer_constant_p (rhs)
6488 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6490 switch (errtype)
6492 case ic_argpass:
6493 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6494 "non-enclosed address space", parmnum, rname);
6495 break;
6496 case ic_assign:
6497 error_at (location, "assignment from pointer to "
6498 "non-enclosed address space");
6499 break;
6500 case ic_init:
6501 error_at (location, "initialization from pointer to "
6502 "non-enclosed address space");
6503 break;
6504 case ic_return:
6505 error_at (location, "return from pointer to "
6506 "non-enclosed address space");
6507 break;
6508 default:
6509 gcc_unreachable ();
6511 return error_mark_node;
6514 /* Check if the right-hand side has a format attribute but the
6515 left-hand side doesn't. */
6516 if (warn_suggest_attribute_format
6517 && check_missing_format_attribute (type, rhstype))
6519 switch (errtype)
6521 case ic_argpass:
6522 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6523 "argument %d of %qE might be "
6524 "a candidate for a format attribute",
6525 parmnum, rname);
6526 break;
6527 case ic_assign:
6528 warning_at (location, OPT_Wsuggest_attribute_format,
6529 "assignment left-hand side might be "
6530 "a candidate for a format attribute");
6531 break;
6532 case ic_init:
6533 warning_at (location, OPT_Wsuggest_attribute_format,
6534 "initialization left-hand side might be "
6535 "a candidate for a format attribute");
6536 break;
6537 case ic_return:
6538 warning_at (location, OPT_Wsuggest_attribute_format,
6539 "return type might be "
6540 "a candidate for a format attribute");
6541 break;
6542 default:
6543 gcc_unreachable ();
6547 /* Any non-function converts to a [const][volatile] void *
6548 and vice versa; otherwise, targets must be the same.
6549 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6550 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6551 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6552 || (target_cmp = comp_target_types (location, type, rhstype))
6553 || is_opaque_pointer
6554 || ((c_common_unsigned_type (mvl)
6555 == c_common_unsigned_type (mvr))
6556 && (c_common_signed_type (mvl)
6557 == c_common_signed_type (mvr))
6558 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6560 /* Warn about loss of qualifers from pointers to arrays with
6561 qualifiers on the element type. */
6562 if (TREE_CODE (ttr) == ARRAY_TYPE)
6564 ttr = strip_array_types (ttr);
6565 ttl = strip_array_types (ttl);
6567 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6568 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6569 WARNING_FOR_QUALIFIERS (location, expr_loc,
6570 OPT_Wdiscarded_array_qualifiers,
6571 G_("passing argument %d of %qE discards "
6572 "%qv qualifier from pointer target type"),
6573 G_("assignment discards %qv qualifier "
6574 "from pointer target type"),
6575 G_("initialization discards %qv qualifier "
6576 "from pointer target type"),
6577 G_("return discards %qv qualifier from "
6578 "pointer target type"),
6579 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6581 else if (pedantic
6582 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6584 (VOID_TYPE_P (ttr)
6585 && !null_pointer_constant
6586 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6587 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6588 G_("ISO C forbids passing argument %d of "
6589 "%qE between function pointer "
6590 "and %<void *%>"),
6591 G_("ISO C forbids assignment between "
6592 "function pointer and %<void *%>"),
6593 G_("ISO C forbids initialization between "
6594 "function pointer and %<void *%>"),
6595 G_("ISO C forbids return between function "
6596 "pointer and %<void *%>"));
6597 /* Const and volatile mean something different for function types,
6598 so the usual warnings are not appropriate. */
6599 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6600 && TREE_CODE (ttl) != FUNCTION_TYPE)
6602 /* Don't warn about loss of qualifier for conversions from
6603 qualified void* to pointers to arrays with corresponding
6604 qualifier on the element type. */
6605 if (!pedantic)
6606 ttl = strip_array_types (ttl);
6608 /* Assignments between atomic and non-atomic objects are OK. */
6609 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6610 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6612 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6613 OPT_Wdiscarded_qualifiers,
6614 G_("passing argument %d of %qE discards "
6615 "%qv qualifier from pointer target type"),
6616 G_("assignment discards %qv qualifier "
6617 "from pointer target type"),
6618 G_("initialization discards %qv qualifier "
6619 "from pointer target type"),
6620 G_("return discards %qv qualifier from "
6621 "pointer target type"),
6622 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6624 /* If this is not a case of ignoring a mismatch in signedness,
6625 no warning. */
6626 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6627 || target_cmp)
6629 /* If there is a mismatch, do warn. */
6630 else if (warn_pointer_sign)
6631 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6632 G_("pointer targets in passing argument "
6633 "%d of %qE differ in signedness"),
6634 G_("pointer targets in assignment "
6635 "differ in signedness"),
6636 G_("pointer targets in initialization "
6637 "differ in signedness"),
6638 G_("pointer targets in return differ "
6639 "in signedness"));
6641 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6642 && TREE_CODE (ttr) == FUNCTION_TYPE)
6644 /* Because const and volatile on functions are restrictions
6645 that say the function will not do certain things,
6646 it is okay to use a const or volatile function
6647 where an ordinary one is wanted, but not vice-versa. */
6648 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6649 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6650 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6651 OPT_Wdiscarded_qualifiers,
6652 G_("passing argument %d of %qE makes "
6653 "%q#v qualified function pointer "
6654 "from unqualified"),
6655 G_("assignment makes %q#v qualified function "
6656 "pointer from unqualified"),
6657 G_("initialization makes %q#v qualified "
6658 "function pointer from unqualified"),
6659 G_("return makes %q#v qualified function "
6660 "pointer from unqualified"),
6661 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6664 else
6665 /* Avoid warning about the volatile ObjC EH puts on decls. */
6666 if (!objc_ok)
6667 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6668 OPT_Wincompatible_pointer_types,
6669 G_("passing argument %d of %qE from "
6670 "incompatible pointer type"),
6671 G_("assignment from incompatible pointer type"),
6672 G_("initialization from incompatible "
6673 "pointer type"),
6674 G_("return from incompatible pointer type"));
6676 return convert (type, rhs);
6678 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6680 /* ??? This should not be an error when inlining calls to
6681 unprototyped functions. */
6682 error_at (location, "invalid use of non-lvalue array");
6683 return error_mark_node;
6685 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6687 /* An explicit constant 0 can convert to a pointer,
6688 or one that results from arithmetic, even including
6689 a cast to integer type. */
6690 if (!null_pointer_constant)
6691 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6692 OPT_Wint_conversion,
6693 G_("passing argument %d of %qE makes "
6694 "pointer from integer without a cast"),
6695 G_("assignment makes pointer from integer "
6696 "without a cast"),
6697 G_("initialization makes pointer from "
6698 "integer without a cast"),
6699 G_("return makes pointer from integer "
6700 "without a cast"));
6702 return convert (type, rhs);
6704 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6706 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6707 OPT_Wint_conversion,
6708 G_("passing argument %d of %qE makes integer "
6709 "from pointer without a cast"),
6710 G_("assignment makes integer from pointer "
6711 "without a cast"),
6712 G_("initialization makes integer from pointer "
6713 "without a cast"),
6714 G_("return makes integer from pointer "
6715 "without a cast"));
6716 return convert (type, rhs);
6718 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6720 tree ret;
6721 bool save = in_late_binary_op;
6722 in_late_binary_op = true;
6723 ret = convert (type, rhs);
6724 in_late_binary_op = save;
6725 return ret;
6728 switch (errtype)
6730 case ic_argpass:
6731 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6732 rname);
6733 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6734 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6735 "expected %qT but argument is of type %qT", type, rhstype);
6736 break;
6737 case ic_assign:
6738 error_at (location, "incompatible types when assigning to type %qT from "
6739 "type %qT", type, rhstype);
6740 break;
6741 case ic_init:
6742 error_at (location,
6743 "incompatible types when initializing type %qT using type %qT",
6744 type, rhstype);
6745 break;
6746 case ic_return:
6747 error_at (location,
6748 "incompatible types when returning type %qT but %qT was "
6749 "expected", rhstype, type);
6750 break;
6751 default:
6752 gcc_unreachable ();
6755 return error_mark_node;
6758 /* If VALUE is a compound expr all of whose expressions are constant, then
6759 return its value. Otherwise, return error_mark_node.
6761 This is for handling COMPOUND_EXPRs as initializer elements
6762 which is allowed with a warning when -pedantic is specified. */
6764 static tree
6765 valid_compound_expr_initializer (tree value, tree endtype)
6767 if (TREE_CODE (value) == COMPOUND_EXPR)
6769 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6770 == error_mark_node)
6771 return error_mark_node;
6772 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6773 endtype);
6775 else if (!initializer_constant_valid_p (value, endtype))
6776 return error_mark_node;
6777 else
6778 return value;
6781 /* Perform appropriate conversions on the initial value of a variable,
6782 store it in the declaration DECL,
6783 and print any error messages that are appropriate.
6784 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6785 If the init is invalid, store an ERROR_MARK.
6787 INIT_LOC is the location of the initial value. */
6789 void
6790 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6792 tree value, type;
6793 bool npc = false;
6795 /* If variable's type was invalidly declared, just ignore it. */
6797 type = TREE_TYPE (decl);
6798 if (TREE_CODE (type) == ERROR_MARK)
6799 return;
6801 /* Digest the specified initializer into an expression. */
6803 if (init)
6804 npc = null_pointer_constant_p (init);
6805 value = digest_init (init_loc, type, init, origtype, npc,
6806 true, TREE_STATIC (decl));
6808 /* Store the expression if valid; else report error. */
6810 if (!in_system_header_at (input_location)
6811 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6812 warning (OPT_Wtraditional, "traditional C rejects automatic "
6813 "aggregate initialization");
6815 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6816 DECL_INITIAL (decl) = value;
6818 /* ANSI wants warnings about out-of-range constant initializers. */
6819 STRIP_TYPE_NOPS (value);
6820 if (TREE_STATIC (decl))
6821 constant_expression_warning (value);
6823 /* Check if we need to set array size from compound literal size. */
6824 if (TREE_CODE (type) == ARRAY_TYPE
6825 && TYPE_DOMAIN (type) == 0
6826 && value != error_mark_node)
6828 tree inside_init = init;
6830 STRIP_TYPE_NOPS (inside_init);
6831 inside_init = fold (inside_init);
6833 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6835 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6837 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6839 /* For int foo[] = (int [3]){1}; we need to set array size
6840 now since later on array initializer will be just the
6841 brace enclosed list of the compound literal. */
6842 tree etype = strip_array_types (TREE_TYPE (decl));
6843 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6844 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6845 layout_type (type);
6846 layout_decl (cldecl, 0);
6847 TREE_TYPE (decl)
6848 = c_build_qualified_type (type, TYPE_QUALS (etype));
6854 /* Methods for storing and printing names for error messages. */
6856 /* Implement a spelling stack that allows components of a name to be pushed
6857 and popped. Each element on the stack is this structure. */
6859 struct spelling
6861 int kind;
6862 union
6864 unsigned HOST_WIDE_INT i;
6865 const char *s;
6866 } u;
6869 #define SPELLING_STRING 1
6870 #define SPELLING_MEMBER 2
6871 #define SPELLING_BOUNDS 3
6873 static struct spelling *spelling; /* Next stack element (unused). */
6874 static struct spelling *spelling_base; /* Spelling stack base. */
6875 static int spelling_size; /* Size of the spelling stack. */
6877 /* Macros to save and restore the spelling stack around push_... functions.
6878 Alternative to SAVE_SPELLING_STACK. */
6880 #define SPELLING_DEPTH() (spelling - spelling_base)
6881 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6883 /* Push an element on the spelling stack with type KIND and assign VALUE
6884 to MEMBER. */
6886 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6888 int depth = SPELLING_DEPTH (); \
6890 if (depth >= spelling_size) \
6892 spelling_size += 10; \
6893 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6894 spelling_size); \
6895 RESTORE_SPELLING_DEPTH (depth); \
6898 spelling->kind = (KIND); \
6899 spelling->MEMBER = (VALUE); \
6900 spelling++; \
6903 /* Push STRING on the stack. Printed literally. */
6905 static void
6906 push_string (const char *string)
6908 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6911 /* Push a member name on the stack. Printed as '.' STRING. */
6913 static void
6914 push_member_name (tree decl)
6916 const char *const string
6917 = (DECL_NAME (decl)
6918 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6919 : _("<anonymous>"));
6920 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6923 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6925 static void
6926 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6928 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6931 /* Compute the maximum size in bytes of the printed spelling. */
6933 static int
6934 spelling_length (void)
6936 int size = 0;
6937 struct spelling *p;
6939 for (p = spelling_base; p < spelling; p++)
6941 if (p->kind == SPELLING_BOUNDS)
6942 size += 25;
6943 else
6944 size += strlen (p->u.s) + 1;
6947 return size;
6950 /* Print the spelling to BUFFER and return it. */
6952 static char *
6953 print_spelling (char *buffer)
6955 char *d = buffer;
6956 struct spelling *p;
6958 for (p = spelling_base; p < spelling; p++)
6959 if (p->kind == SPELLING_BOUNDS)
6961 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6962 d += strlen (d);
6964 else
6966 const char *s;
6967 if (p->kind == SPELLING_MEMBER)
6968 *d++ = '.';
6969 for (s = p->u.s; (*d = *s++); d++)
6972 *d++ = '\0';
6973 return buffer;
6976 /* Digest the parser output INIT as an initializer for type TYPE.
6977 Return a C expression of type TYPE to represent the initial value.
6979 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6981 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6983 If INIT is a string constant, STRICT_STRING is true if it is
6984 unparenthesized or we should not warn here for it being parenthesized.
6985 For other types of INIT, STRICT_STRING is not used.
6987 INIT_LOC is the location of the INIT.
6989 REQUIRE_CONSTANT requests an error if non-constant initializers or
6990 elements are seen. */
6992 static tree
6993 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6994 bool null_pointer_constant, bool strict_string,
6995 int require_constant)
6997 enum tree_code code = TREE_CODE (type);
6998 tree inside_init = init;
6999 tree semantic_type = NULL_TREE;
7000 bool maybe_const = true;
7002 if (type == error_mark_node
7003 || !init
7004 || error_operand_p (init))
7005 return error_mark_node;
7007 STRIP_TYPE_NOPS (inside_init);
7009 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7011 semantic_type = TREE_TYPE (inside_init);
7012 inside_init = TREE_OPERAND (inside_init, 0);
7014 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7015 inside_init = decl_constant_value_for_optimization (inside_init);
7017 /* Initialization of an array of chars from a string constant
7018 optionally enclosed in braces. */
7020 if (code == ARRAY_TYPE && inside_init
7021 && TREE_CODE (inside_init) == STRING_CST)
7023 tree typ1
7024 = (TYPE_ATOMIC (TREE_TYPE (type))
7025 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7026 TYPE_QUAL_ATOMIC)
7027 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7028 /* Note that an array could be both an array of character type
7029 and an array of wchar_t if wchar_t is signed char or unsigned
7030 char. */
7031 bool char_array = (typ1 == char_type_node
7032 || typ1 == signed_char_type_node
7033 || typ1 == unsigned_char_type_node);
7034 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7035 bool char16_array = !!comptypes (typ1, char16_type_node);
7036 bool char32_array = !!comptypes (typ1, char32_type_node);
7038 if (char_array || wchar_array || char16_array || char32_array)
7040 struct c_expr expr;
7041 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7042 expr.value = inside_init;
7043 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7044 expr.original_type = NULL;
7045 maybe_warn_string_init (init_loc, type, expr);
7047 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7048 pedwarn_init (init_loc, OPT_Wpedantic,
7049 "initialization of a flexible array member");
7051 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7052 TYPE_MAIN_VARIANT (type)))
7053 return inside_init;
7055 if (char_array)
7057 if (typ2 != char_type_node)
7059 error_init (init_loc, "char-array initialized from wide "
7060 "string");
7061 return error_mark_node;
7064 else
7066 if (typ2 == char_type_node)
7068 error_init (init_loc, "wide character array initialized "
7069 "from non-wide string");
7070 return error_mark_node;
7072 else if (!comptypes(typ1, typ2))
7074 error_init (init_loc, "wide character array initialized "
7075 "from incompatible wide string");
7076 return error_mark_node;
7080 TREE_TYPE (inside_init) = type;
7081 if (TYPE_DOMAIN (type) != 0
7082 && TYPE_SIZE (type) != 0
7083 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7085 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7087 /* Subtract the size of a single (possibly wide) character
7088 because it's ok to ignore the terminating null char
7089 that is counted in the length of the constant. */
7090 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7091 (len
7092 - (TYPE_PRECISION (typ1)
7093 / BITS_PER_UNIT))))
7094 pedwarn_init (init_loc, 0,
7095 ("initializer-string for array of chars "
7096 "is too long"));
7097 else if (warn_cxx_compat
7098 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7099 warning_at (init_loc, OPT_Wc___compat,
7100 ("initializer-string for array chars "
7101 "is too long for C++"));
7104 return inside_init;
7106 else if (INTEGRAL_TYPE_P (typ1))
7108 error_init (init_loc, "array of inappropriate type initialized "
7109 "from string constant");
7110 return error_mark_node;
7114 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7115 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7116 below and handle as a constructor. */
7117 if (code == VECTOR_TYPE
7118 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7119 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7120 && TREE_CONSTANT (inside_init))
7122 if (TREE_CODE (inside_init) == VECTOR_CST
7123 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7124 TYPE_MAIN_VARIANT (type)))
7125 return inside_init;
7127 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7129 unsigned HOST_WIDE_INT ix;
7130 tree value;
7131 bool constant_p = true;
7133 /* Iterate through elements and check if all constructor
7134 elements are *_CSTs. */
7135 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7136 if (!CONSTANT_CLASS_P (value))
7138 constant_p = false;
7139 break;
7142 if (constant_p)
7143 return build_vector_from_ctor (type,
7144 CONSTRUCTOR_ELTS (inside_init));
7148 if (warn_sequence_point)
7149 verify_sequence_points (inside_init);
7151 /* Any type can be initialized
7152 from an expression of the same type, optionally with braces. */
7154 if (inside_init && TREE_TYPE (inside_init) != 0
7155 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7156 TYPE_MAIN_VARIANT (type))
7157 || (code == ARRAY_TYPE
7158 && comptypes (TREE_TYPE (inside_init), type))
7159 || (code == VECTOR_TYPE
7160 && comptypes (TREE_TYPE (inside_init), type))
7161 || (code == POINTER_TYPE
7162 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7163 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7164 TREE_TYPE (type)))))
7166 if (code == POINTER_TYPE)
7168 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7170 if (TREE_CODE (inside_init) == STRING_CST
7171 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7172 inside_init = array_to_pointer_conversion
7173 (init_loc, inside_init);
7174 else
7176 error_init (init_loc, "invalid use of non-lvalue array");
7177 return error_mark_node;
7182 if (code == VECTOR_TYPE)
7183 /* Although the types are compatible, we may require a
7184 conversion. */
7185 inside_init = convert (type, inside_init);
7187 if (require_constant
7188 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7190 /* As an extension, allow initializing objects with static storage
7191 duration with compound literals (which are then treated just as
7192 the brace enclosed list they contain). Also allow this for
7193 vectors, as we can only assign them with compound literals. */
7194 if (flag_isoc99 && code != VECTOR_TYPE)
7195 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7196 "is not constant");
7197 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7198 inside_init = DECL_INITIAL (decl);
7201 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7202 && TREE_CODE (inside_init) != CONSTRUCTOR)
7204 error_init (init_loc, "array initialized from non-constant array "
7205 "expression");
7206 return error_mark_node;
7209 /* Compound expressions can only occur here if -Wpedantic or
7210 -pedantic-errors is specified. In the later case, we always want
7211 an error. In the former case, we simply want a warning. */
7212 if (require_constant && pedantic
7213 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7215 inside_init
7216 = valid_compound_expr_initializer (inside_init,
7217 TREE_TYPE (inside_init));
7218 if (inside_init == error_mark_node)
7219 error_init (init_loc, "initializer element is not constant");
7220 else
7221 pedwarn_init (init_loc, OPT_Wpedantic,
7222 "initializer element is not constant");
7223 if (flag_pedantic_errors)
7224 inside_init = error_mark_node;
7226 else if (require_constant
7227 && !initializer_constant_valid_p (inside_init,
7228 TREE_TYPE (inside_init)))
7230 error_init (init_loc, "initializer element is not constant");
7231 inside_init = error_mark_node;
7233 else if (require_constant && !maybe_const)
7234 pedwarn_init (init_loc, OPT_Wpedantic,
7235 "initializer element is not a constant expression");
7237 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7238 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7239 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7240 type, inside_init, origtype,
7241 ic_init, null_pointer_constant,
7242 NULL_TREE, NULL_TREE, 0);
7243 return inside_init;
7246 /* Handle scalar types, including conversions. */
7248 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7249 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7250 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7252 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7253 && (TREE_CODE (init) == STRING_CST
7254 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7255 inside_init = init = array_to_pointer_conversion (init_loc, init);
7256 if (semantic_type)
7257 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7258 inside_init);
7259 inside_init
7260 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7261 inside_init, origtype, ic_init,
7262 null_pointer_constant, NULL_TREE, NULL_TREE,
7265 /* Check to see if we have already given an error message. */
7266 if (inside_init == error_mark_node)
7268 else if (require_constant && !TREE_CONSTANT (inside_init))
7270 error_init (init_loc, "initializer element is not constant");
7271 inside_init = error_mark_node;
7273 else if (require_constant
7274 && !initializer_constant_valid_p (inside_init,
7275 TREE_TYPE (inside_init)))
7277 error_init (init_loc, "initializer element is not computable at "
7278 "load time");
7279 inside_init = error_mark_node;
7281 else if (require_constant && !maybe_const)
7282 pedwarn_init (init_loc, OPT_Wpedantic,
7283 "initializer element is not a constant expression");
7285 return inside_init;
7288 /* Come here only for records and arrays. */
7290 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7292 error_init (init_loc, "variable-sized object may not be initialized");
7293 return error_mark_node;
7296 error_init (init_loc, "invalid initializer");
7297 return error_mark_node;
7300 /* Handle initializers that use braces. */
7302 /* Type of object we are accumulating a constructor for.
7303 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7304 static tree constructor_type;
7306 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7307 left to fill. */
7308 static tree constructor_fields;
7310 /* For an ARRAY_TYPE, this is the specified index
7311 at which to store the next element we get. */
7312 static tree constructor_index;
7314 /* For an ARRAY_TYPE, this is the maximum index. */
7315 static tree constructor_max_index;
7317 /* For a RECORD_TYPE, this is the first field not yet written out. */
7318 static tree constructor_unfilled_fields;
7320 /* For an ARRAY_TYPE, this is the index of the first element
7321 not yet written out. */
7322 static tree constructor_unfilled_index;
7324 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7325 This is so we can generate gaps between fields, when appropriate. */
7326 static tree constructor_bit_index;
7328 /* If we are saving up the elements rather than allocating them,
7329 this is the list of elements so far (in reverse order,
7330 most recent first). */
7331 static vec<constructor_elt, va_gc> *constructor_elements;
7333 /* 1 if constructor should be incrementally stored into a constructor chain,
7334 0 if all the elements should be kept in AVL tree. */
7335 static int constructor_incremental;
7337 /* 1 if so far this constructor's elements are all compile-time constants. */
7338 static int constructor_constant;
7340 /* 1 if so far this constructor's elements are all valid address constants. */
7341 static int constructor_simple;
7343 /* 1 if this constructor has an element that cannot be part of a
7344 constant expression. */
7345 static int constructor_nonconst;
7347 /* 1 if this constructor is erroneous so far. */
7348 static int constructor_erroneous;
7350 /* 1 if this constructor is the universal zero initializer { 0 }. */
7351 static int constructor_zeroinit;
7353 /* Structure for managing pending initializer elements, organized as an
7354 AVL tree. */
7356 struct init_node
7358 struct init_node *left, *right;
7359 struct init_node *parent;
7360 int balance;
7361 tree purpose;
7362 tree value;
7363 tree origtype;
7366 /* Tree of pending elements at this constructor level.
7367 These are elements encountered out of order
7368 which belong at places we haven't reached yet in actually
7369 writing the output.
7370 Will never hold tree nodes across GC runs. */
7371 static struct init_node *constructor_pending_elts;
7373 /* The SPELLING_DEPTH of this constructor. */
7374 static int constructor_depth;
7376 /* DECL node for which an initializer is being read.
7377 0 means we are reading a constructor expression
7378 such as (struct foo) {...}. */
7379 static tree constructor_decl;
7381 /* Nonzero if this is an initializer for a top-level decl. */
7382 static int constructor_top_level;
7384 /* Nonzero if there were any member designators in this initializer. */
7385 static int constructor_designated;
7387 /* Nesting depth of designator list. */
7388 static int designator_depth;
7390 /* Nonzero if there were diagnosed errors in this designator list. */
7391 static int designator_erroneous;
7394 /* This stack has a level for each implicit or explicit level of
7395 structuring in the initializer, including the outermost one. It
7396 saves the values of most of the variables above. */
7398 struct constructor_range_stack;
7400 struct constructor_stack
7402 struct constructor_stack *next;
7403 tree type;
7404 tree fields;
7405 tree index;
7406 tree max_index;
7407 tree unfilled_index;
7408 tree unfilled_fields;
7409 tree bit_index;
7410 vec<constructor_elt, va_gc> *elements;
7411 struct init_node *pending_elts;
7412 int offset;
7413 int depth;
7414 /* If value nonzero, this value should replace the entire
7415 constructor at this level. */
7416 struct c_expr replacement_value;
7417 struct constructor_range_stack *range_stack;
7418 char constant;
7419 char simple;
7420 char nonconst;
7421 char implicit;
7422 char erroneous;
7423 char outer;
7424 char incremental;
7425 char designated;
7426 int designator_depth;
7429 static struct constructor_stack *constructor_stack;
7431 /* This stack represents designators from some range designator up to
7432 the last designator in the list. */
7434 struct constructor_range_stack
7436 struct constructor_range_stack *next, *prev;
7437 struct constructor_stack *stack;
7438 tree range_start;
7439 tree index;
7440 tree range_end;
7441 tree fields;
7444 static struct constructor_range_stack *constructor_range_stack;
7446 /* This stack records separate initializers that are nested.
7447 Nested initializers can't happen in ANSI C, but GNU C allows them
7448 in cases like { ... (struct foo) { ... } ... }. */
7450 struct initializer_stack
7452 struct initializer_stack *next;
7453 tree decl;
7454 struct constructor_stack *constructor_stack;
7455 struct constructor_range_stack *constructor_range_stack;
7456 vec<constructor_elt, va_gc> *elements;
7457 struct spelling *spelling;
7458 struct spelling *spelling_base;
7459 int spelling_size;
7460 char top_level;
7461 char require_constant_value;
7462 char require_constant_elements;
7465 static struct initializer_stack *initializer_stack;
7467 /* Prepare to parse and output the initializer for variable DECL. */
7469 void
7470 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7472 const char *locus;
7473 struct initializer_stack *p = XNEW (struct initializer_stack);
7475 p->decl = constructor_decl;
7476 p->require_constant_value = require_constant_value;
7477 p->require_constant_elements = require_constant_elements;
7478 p->constructor_stack = constructor_stack;
7479 p->constructor_range_stack = constructor_range_stack;
7480 p->elements = constructor_elements;
7481 p->spelling = spelling;
7482 p->spelling_base = spelling_base;
7483 p->spelling_size = spelling_size;
7484 p->top_level = constructor_top_level;
7485 p->next = initializer_stack;
7486 initializer_stack = p;
7488 constructor_decl = decl;
7489 constructor_designated = 0;
7490 constructor_top_level = top_level;
7492 if (decl != 0 && decl != error_mark_node)
7494 require_constant_value = TREE_STATIC (decl);
7495 require_constant_elements
7496 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7497 /* For a scalar, you can always use any value to initialize,
7498 even within braces. */
7499 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7500 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7502 else
7504 require_constant_value = 0;
7505 require_constant_elements = 0;
7506 locus = _("(anonymous)");
7509 constructor_stack = 0;
7510 constructor_range_stack = 0;
7512 found_missing_braces = 0;
7514 spelling_base = 0;
7515 spelling_size = 0;
7516 RESTORE_SPELLING_DEPTH (0);
7518 if (locus)
7519 push_string (locus);
7522 void
7523 finish_init (void)
7525 struct initializer_stack *p = initializer_stack;
7527 /* Free the whole constructor stack of this initializer. */
7528 while (constructor_stack)
7530 struct constructor_stack *q = constructor_stack;
7531 constructor_stack = q->next;
7532 free (q);
7535 gcc_assert (!constructor_range_stack);
7537 /* Pop back to the data of the outer initializer (if any). */
7538 free (spelling_base);
7540 constructor_decl = p->decl;
7541 require_constant_value = p->require_constant_value;
7542 require_constant_elements = p->require_constant_elements;
7543 constructor_stack = p->constructor_stack;
7544 constructor_range_stack = p->constructor_range_stack;
7545 constructor_elements = p->elements;
7546 spelling = p->spelling;
7547 spelling_base = p->spelling_base;
7548 spelling_size = p->spelling_size;
7549 constructor_top_level = p->top_level;
7550 initializer_stack = p->next;
7551 free (p);
7554 /* Call here when we see the initializer is surrounded by braces.
7555 This is instead of a call to push_init_level;
7556 it is matched by a call to pop_init_level.
7558 TYPE is the type to initialize, for a constructor expression.
7559 For an initializer for a decl, TYPE is zero. */
7561 void
7562 really_start_incremental_init (tree type)
7564 struct constructor_stack *p = XNEW (struct constructor_stack);
7566 if (type == 0)
7567 type = TREE_TYPE (constructor_decl);
7569 if (VECTOR_TYPE_P (type)
7570 && TYPE_VECTOR_OPAQUE (type))
7571 error ("opaque vector types cannot be initialized");
7573 p->type = constructor_type;
7574 p->fields = constructor_fields;
7575 p->index = constructor_index;
7576 p->max_index = constructor_max_index;
7577 p->unfilled_index = constructor_unfilled_index;
7578 p->unfilled_fields = constructor_unfilled_fields;
7579 p->bit_index = constructor_bit_index;
7580 p->elements = constructor_elements;
7581 p->constant = constructor_constant;
7582 p->simple = constructor_simple;
7583 p->nonconst = constructor_nonconst;
7584 p->erroneous = constructor_erroneous;
7585 p->pending_elts = constructor_pending_elts;
7586 p->depth = constructor_depth;
7587 p->replacement_value.value = 0;
7588 p->replacement_value.original_code = ERROR_MARK;
7589 p->replacement_value.original_type = NULL;
7590 p->implicit = 0;
7591 p->range_stack = 0;
7592 p->outer = 0;
7593 p->incremental = constructor_incremental;
7594 p->designated = constructor_designated;
7595 p->designator_depth = designator_depth;
7596 p->next = 0;
7597 constructor_stack = p;
7599 constructor_constant = 1;
7600 constructor_simple = 1;
7601 constructor_nonconst = 0;
7602 constructor_depth = SPELLING_DEPTH ();
7603 constructor_elements = NULL;
7604 constructor_pending_elts = 0;
7605 constructor_type = type;
7606 constructor_incremental = 1;
7607 constructor_designated = 0;
7608 constructor_zeroinit = 1;
7609 designator_depth = 0;
7610 designator_erroneous = 0;
7612 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7614 constructor_fields = TYPE_FIELDS (constructor_type);
7615 /* Skip any nameless bit fields at the beginning. */
7616 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7617 && DECL_NAME (constructor_fields) == 0)
7618 constructor_fields = DECL_CHAIN (constructor_fields);
7620 constructor_unfilled_fields = constructor_fields;
7621 constructor_bit_index = bitsize_zero_node;
7623 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7625 if (TYPE_DOMAIN (constructor_type))
7627 constructor_max_index
7628 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7630 /* Detect non-empty initializations of zero-length arrays. */
7631 if (constructor_max_index == NULL_TREE
7632 && TYPE_SIZE (constructor_type))
7633 constructor_max_index = integer_minus_one_node;
7635 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7636 to initialize VLAs will cause a proper error; avoid tree
7637 checking errors as well by setting a safe value. */
7638 if (constructor_max_index
7639 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7640 constructor_max_index = integer_minus_one_node;
7642 constructor_index
7643 = convert (bitsizetype,
7644 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7646 else
7648 constructor_index = bitsize_zero_node;
7649 constructor_max_index = NULL_TREE;
7652 constructor_unfilled_index = constructor_index;
7654 else if (VECTOR_TYPE_P (constructor_type))
7656 /* Vectors are like simple fixed-size arrays. */
7657 constructor_max_index =
7658 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7659 constructor_index = bitsize_zero_node;
7660 constructor_unfilled_index = constructor_index;
7662 else
7664 /* Handle the case of int x = {5}; */
7665 constructor_fields = constructor_type;
7666 constructor_unfilled_fields = constructor_type;
7670 /* Called when we see an open brace for a nested initializer. Finish
7671 off any pending levels with implicit braces. */
7672 void
7673 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7675 while (constructor_stack->implicit)
7677 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7678 && constructor_fields == 0)
7679 process_init_element (input_location,
7680 pop_init_level (loc, 1, braced_init_obstack),
7681 true, braced_init_obstack);
7682 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7683 && constructor_max_index
7684 && tree_int_cst_lt (constructor_max_index,
7685 constructor_index))
7686 process_init_element (input_location,
7687 pop_init_level (loc, 1, braced_init_obstack),
7688 true, braced_init_obstack);
7689 else
7690 break;
7694 /* Push down into a subobject, for initialization.
7695 If this is for an explicit set of braces, IMPLICIT is 0.
7696 If it is because the next element belongs at a lower level,
7697 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7699 void
7700 push_init_level (location_t loc, int implicit,
7701 struct obstack *braced_init_obstack)
7703 struct constructor_stack *p;
7704 tree value = NULL_TREE;
7706 /* Unless this is an explicit brace, we need to preserve previous
7707 content if any. */
7708 if (implicit)
7710 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7711 value = find_init_member (constructor_fields, braced_init_obstack);
7712 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7713 value = find_init_member (constructor_index, braced_init_obstack);
7716 p = XNEW (struct constructor_stack);
7717 p->type = constructor_type;
7718 p->fields = constructor_fields;
7719 p->index = constructor_index;
7720 p->max_index = constructor_max_index;
7721 p->unfilled_index = constructor_unfilled_index;
7722 p->unfilled_fields = constructor_unfilled_fields;
7723 p->bit_index = constructor_bit_index;
7724 p->elements = constructor_elements;
7725 p->constant = constructor_constant;
7726 p->simple = constructor_simple;
7727 p->nonconst = constructor_nonconst;
7728 p->erroneous = constructor_erroneous;
7729 p->pending_elts = constructor_pending_elts;
7730 p->depth = constructor_depth;
7731 p->replacement_value.value = 0;
7732 p->replacement_value.original_code = ERROR_MARK;
7733 p->replacement_value.original_type = NULL;
7734 p->implicit = implicit;
7735 p->outer = 0;
7736 p->incremental = constructor_incremental;
7737 p->designated = constructor_designated;
7738 p->designator_depth = designator_depth;
7739 p->next = constructor_stack;
7740 p->range_stack = 0;
7741 constructor_stack = p;
7743 constructor_constant = 1;
7744 constructor_simple = 1;
7745 constructor_nonconst = 0;
7746 constructor_depth = SPELLING_DEPTH ();
7747 constructor_elements = NULL;
7748 constructor_incremental = 1;
7749 constructor_designated = 0;
7750 constructor_pending_elts = 0;
7751 if (!implicit)
7753 p->range_stack = constructor_range_stack;
7754 constructor_range_stack = 0;
7755 designator_depth = 0;
7756 designator_erroneous = 0;
7759 /* Don't die if an entire brace-pair level is superfluous
7760 in the containing level. */
7761 if (constructor_type == 0)
7763 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7765 /* Don't die if there are extra init elts at the end. */
7766 if (constructor_fields == 0)
7767 constructor_type = 0;
7768 else
7770 constructor_type = TREE_TYPE (constructor_fields);
7771 push_member_name (constructor_fields);
7772 constructor_depth++;
7774 /* If upper initializer is designated, then mark this as
7775 designated too to prevent bogus warnings. */
7776 constructor_designated = p->designated;
7778 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7780 constructor_type = TREE_TYPE (constructor_type);
7781 push_array_bounds (tree_to_uhwi (constructor_index));
7782 constructor_depth++;
7785 if (constructor_type == 0)
7787 error_init (loc, "extra brace group at end of initializer");
7788 constructor_fields = 0;
7789 constructor_unfilled_fields = 0;
7790 return;
7793 if (value && TREE_CODE (value) == CONSTRUCTOR)
7795 constructor_constant = TREE_CONSTANT (value);
7796 constructor_simple = TREE_STATIC (value);
7797 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7798 constructor_elements = CONSTRUCTOR_ELTS (value);
7799 if (!vec_safe_is_empty (constructor_elements)
7800 && (TREE_CODE (constructor_type) == RECORD_TYPE
7801 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7802 set_nonincremental_init (braced_init_obstack);
7805 if (implicit == 1)
7806 found_missing_braces = 1;
7808 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7810 constructor_fields = TYPE_FIELDS (constructor_type);
7811 /* Skip any nameless bit fields at the beginning. */
7812 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7813 && DECL_NAME (constructor_fields) == 0)
7814 constructor_fields = DECL_CHAIN (constructor_fields);
7816 constructor_unfilled_fields = constructor_fields;
7817 constructor_bit_index = bitsize_zero_node;
7819 else if (VECTOR_TYPE_P (constructor_type))
7821 /* Vectors are like simple fixed-size arrays. */
7822 constructor_max_index =
7823 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7824 constructor_index = bitsize_int (0);
7825 constructor_unfilled_index = constructor_index;
7827 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7829 if (TYPE_DOMAIN (constructor_type))
7831 constructor_max_index
7832 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7834 /* Detect non-empty initializations of zero-length arrays. */
7835 if (constructor_max_index == NULL_TREE
7836 && TYPE_SIZE (constructor_type))
7837 constructor_max_index = integer_minus_one_node;
7839 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7840 to initialize VLAs will cause a proper error; avoid tree
7841 checking errors as well by setting a safe value. */
7842 if (constructor_max_index
7843 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7844 constructor_max_index = integer_minus_one_node;
7846 constructor_index
7847 = convert (bitsizetype,
7848 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7850 else
7851 constructor_index = bitsize_zero_node;
7853 constructor_unfilled_index = constructor_index;
7854 if (value && TREE_CODE (value) == STRING_CST)
7856 /* We need to split the char/wchar array into individual
7857 characters, so that we don't have to special case it
7858 everywhere. */
7859 set_nonincremental_init_from_string (value, braced_init_obstack);
7862 else
7864 if (constructor_type != error_mark_node)
7865 warning_init (input_location, 0, "braces around scalar initializer");
7866 constructor_fields = constructor_type;
7867 constructor_unfilled_fields = constructor_type;
7871 /* At the end of an implicit or explicit brace level,
7872 finish up that level of constructor. If a single expression
7873 with redundant braces initialized that level, return the
7874 c_expr structure for that expression. Otherwise, the original_code
7875 element is set to ERROR_MARK.
7876 If we were outputting the elements as they are read, return 0 as the value
7877 from inner levels (process_init_element ignores that),
7878 but return error_mark_node as the value from the outermost level
7879 (that's what we want to put in DECL_INITIAL).
7880 Otherwise, return a CONSTRUCTOR expression as the value. */
7882 struct c_expr
7883 pop_init_level (location_t loc, int implicit,
7884 struct obstack *braced_init_obstack)
7886 struct constructor_stack *p;
7887 struct c_expr ret;
7888 ret.value = 0;
7889 ret.original_code = ERROR_MARK;
7890 ret.original_type = NULL;
7892 if (implicit == 0)
7894 /* When we come to an explicit close brace,
7895 pop any inner levels that didn't have explicit braces. */
7896 while (constructor_stack->implicit)
7897 process_init_element (input_location,
7898 pop_init_level (loc, 1, braced_init_obstack),
7899 true, braced_init_obstack);
7900 gcc_assert (!constructor_range_stack);
7903 /* Now output all pending elements. */
7904 constructor_incremental = 1;
7905 output_pending_init_elements (1, braced_init_obstack);
7907 p = constructor_stack;
7909 /* Error for initializing a flexible array member, or a zero-length
7910 array member in an inappropriate context. */
7911 if (constructor_type && constructor_fields
7912 && TREE_CODE (constructor_type) == ARRAY_TYPE
7913 && TYPE_DOMAIN (constructor_type)
7914 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7916 /* Silently discard empty initializations. The parser will
7917 already have pedwarned for empty brackets. */
7918 if (integer_zerop (constructor_unfilled_index))
7919 constructor_type = NULL_TREE;
7920 else
7922 gcc_assert (!TYPE_SIZE (constructor_type));
7924 if (constructor_depth > 2)
7925 error_init (loc, "initialization of flexible array member in a nested context");
7926 else
7927 pedwarn_init (loc, OPT_Wpedantic,
7928 "initialization of a flexible array member");
7930 /* We have already issued an error message for the existence
7931 of a flexible array member not at the end of the structure.
7932 Discard the initializer so that we do not die later. */
7933 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7934 constructor_type = NULL_TREE;
7938 switch (vec_safe_length (constructor_elements))
7940 case 0:
7941 /* Initialization with { } counts as zeroinit. */
7942 constructor_zeroinit = 1;
7943 break;
7944 case 1:
7945 /* This might be zeroinit as well. */
7946 if (integer_zerop ((*constructor_elements)[0].value))
7947 constructor_zeroinit = 1;
7948 break;
7949 default:
7950 /* If the constructor has more than one element, it can't be { 0 }. */
7951 constructor_zeroinit = 0;
7952 break;
7955 /* Warn when some structs are initialized with direct aggregation. */
7956 if (!implicit && found_missing_braces && warn_missing_braces
7957 && !constructor_zeroinit)
7958 warning_init (loc, OPT_Wmissing_braces,
7959 "missing braces around initializer");
7961 /* Warn when some struct elements are implicitly initialized to zero. */
7962 if (warn_missing_field_initializers
7963 && constructor_type
7964 && TREE_CODE (constructor_type) == RECORD_TYPE
7965 && constructor_unfilled_fields)
7967 /* Do not warn for flexible array members or zero-length arrays. */
7968 while (constructor_unfilled_fields
7969 && (!DECL_SIZE (constructor_unfilled_fields)
7970 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7971 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7973 if (constructor_unfilled_fields
7974 /* Do not warn if this level of the initializer uses member
7975 designators; it is likely to be deliberate. */
7976 && !constructor_designated
7977 /* Do not warn about initializing with { 0 } or with { }. */
7978 && !constructor_zeroinit)
7980 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7981 "missing initializer for field %qD of %qT",
7982 constructor_unfilled_fields,
7983 constructor_type))
7984 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7985 "%qD declared here", constructor_unfilled_fields);
7989 /* Pad out the end of the structure. */
7990 if (p->replacement_value.value)
7991 /* If this closes a superfluous brace pair,
7992 just pass out the element between them. */
7993 ret = p->replacement_value;
7994 else if (constructor_type == 0)
7996 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
7997 && TREE_CODE (constructor_type) != ARRAY_TYPE
7998 && !VECTOR_TYPE_P (constructor_type))
8000 /* A nonincremental scalar initializer--just return
8001 the element, after verifying there is just one. */
8002 if (vec_safe_is_empty (constructor_elements))
8004 if (!constructor_erroneous)
8005 error_init (loc, "empty scalar initializer");
8006 ret.value = error_mark_node;
8008 else if (vec_safe_length (constructor_elements) != 1)
8010 error_init (loc, "extra elements in scalar initializer");
8011 ret.value = (*constructor_elements)[0].value;
8013 else
8014 ret.value = (*constructor_elements)[0].value;
8016 else
8018 if (constructor_erroneous)
8019 ret.value = error_mark_node;
8020 else
8022 ret.value = build_constructor (constructor_type,
8023 constructor_elements);
8024 if (constructor_constant)
8025 TREE_CONSTANT (ret.value) = 1;
8026 if (constructor_constant && constructor_simple)
8027 TREE_STATIC (ret.value) = 1;
8028 if (constructor_nonconst)
8029 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8033 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8035 if (constructor_nonconst)
8036 ret.original_code = C_MAYBE_CONST_EXPR;
8037 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8038 ret.original_code = ERROR_MARK;
8041 constructor_type = p->type;
8042 constructor_fields = p->fields;
8043 constructor_index = p->index;
8044 constructor_max_index = p->max_index;
8045 constructor_unfilled_index = p->unfilled_index;
8046 constructor_unfilled_fields = p->unfilled_fields;
8047 constructor_bit_index = p->bit_index;
8048 constructor_elements = p->elements;
8049 constructor_constant = p->constant;
8050 constructor_simple = p->simple;
8051 constructor_nonconst = p->nonconst;
8052 constructor_erroneous = p->erroneous;
8053 constructor_incremental = p->incremental;
8054 constructor_designated = p->designated;
8055 designator_depth = p->designator_depth;
8056 constructor_pending_elts = p->pending_elts;
8057 constructor_depth = p->depth;
8058 if (!p->implicit)
8059 constructor_range_stack = p->range_stack;
8060 RESTORE_SPELLING_DEPTH (constructor_depth);
8062 constructor_stack = p->next;
8063 free (p);
8065 if (ret.value == 0 && constructor_stack == 0)
8066 ret.value = error_mark_node;
8067 return ret;
8070 /* Common handling for both array range and field name designators.
8071 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8073 static int
8074 set_designator (location_t loc, int array,
8075 struct obstack *braced_init_obstack)
8077 tree subtype;
8078 enum tree_code subcode;
8080 /* Don't die if an entire brace-pair level is superfluous
8081 in the containing level. */
8082 if (constructor_type == 0)
8083 return 1;
8085 /* If there were errors in this designator list already, bail out
8086 silently. */
8087 if (designator_erroneous)
8088 return 1;
8090 if (!designator_depth)
8092 gcc_assert (!constructor_range_stack);
8094 /* Designator list starts at the level of closest explicit
8095 braces. */
8096 while (constructor_stack->implicit)
8097 process_init_element (input_location,
8098 pop_init_level (loc, 1, braced_init_obstack),
8099 true, braced_init_obstack);
8100 constructor_designated = 1;
8101 return 0;
8104 switch (TREE_CODE (constructor_type))
8106 case RECORD_TYPE:
8107 case UNION_TYPE:
8108 subtype = TREE_TYPE (constructor_fields);
8109 if (subtype != error_mark_node)
8110 subtype = TYPE_MAIN_VARIANT (subtype);
8111 break;
8112 case ARRAY_TYPE:
8113 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8114 break;
8115 default:
8116 gcc_unreachable ();
8119 subcode = TREE_CODE (subtype);
8120 if (array && subcode != ARRAY_TYPE)
8122 error_init (loc, "array index in non-array initializer");
8123 return 1;
8125 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8127 error_init (loc, "field name not in record or union initializer");
8128 return 1;
8131 constructor_designated = 1;
8132 finish_implicit_inits (loc, braced_init_obstack);
8133 push_init_level (loc, 2, braced_init_obstack);
8134 return 0;
8137 /* If there are range designators in designator list, push a new designator
8138 to constructor_range_stack. RANGE_END is end of such stack range or
8139 NULL_TREE if there is no range designator at this level. */
8141 static void
8142 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8144 struct constructor_range_stack *p;
8146 p = (struct constructor_range_stack *)
8147 obstack_alloc (braced_init_obstack,
8148 sizeof (struct constructor_range_stack));
8149 p->prev = constructor_range_stack;
8150 p->next = 0;
8151 p->fields = constructor_fields;
8152 p->range_start = constructor_index;
8153 p->index = constructor_index;
8154 p->stack = constructor_stack;
8155 p->range_end = range_end;
8156 if (constructor_range_stack)
8157 constructor_range_stack->next = p;
8158 constructor_range_stack = p;
8161 /* Within an array initializer, specify the next index to be initialized.
8162 FIRST is that index. If LAST is nonzero, then initialize a range
8163 of indices, running from FIRST through LAST. */
8165 void
8166 set_init_index (location_t loc, tree first, tree last,
8167 struct obstack *braced_init_obstack)
8169 if (set_designator (loc, 1, braced_init_obstack))
8170 return;
8172 designator_erroneous = 1;
8174 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8175 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8177 error_init (loc, "array index in initializer not of integer type");
8178 return;
8181 if (TREE_CODE (first) != INTEGER_CST)
8183 first = c_fully_fold (first, false, NULL);
8184 if (TREE_CODE (first) == INTEGER_CST)
8185 pedwarn_init (loc, OPT_Wpedantic,
8186 "array index in initializer is not "
8187 "an integer constant expression");
8190 if (last && TREE_CODE (last) != INTEGER_CST)
8192 last = c_fully_fold (last, false, NULL);
8193 if (TREE_CODE (last) == INTEGER_CST)
8194 pedwarn_init (loc, OPT_Wpedantic,
8195 "array index in initializer is not "
8196 "an integer constant expression");
8199 if (TREE_CODE (first) != INTEGER_CST)
8200 error_init (loc, "nonconstant array index in initializer");
8201 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8202 error_init (loc, "nonconstant array index in initializer");
8203 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8204 error_init (loc, "array index in non-array initializer");
8205 else if (tree_int_cst_sgn (first) == -1)
8206 error_init (loc, "array index in initializer exceeds array bounds");
8207 else if (constructor_max_index
8208 && tree_int_cst_lt (constructor_max_index, first))
8209 error_init (loc, "array index in initializer exceeds array bounds");
8210 else
8212 constant_expression_warning (first);
8213 if (last)
8214 constant_expression_warning (last);
8215 constructor_index = convert (bitsizetype, first);
8216 if (tree_int_cst_lt (constructor_index, first))
8218 constructor_index = copy_node (constructor_index);
8219 TREE_OVERFLOW (constructor_index) = 1;
8222 if (last)
8224 if (tree_int_cst_equal (first, last))
8225 last = 0;
8226 else if (tree_int_cst_lt (last, first))
8228 error_init (loc, "empty index range in initializer");
8229 last = 0;
8231 else
8233 last = convert (bitsizetype, last);
8234 if (constructor_max_index != 0
8235 && tree_int_cst_lt (constructor_max_index, last))
8237 error_init (loc, "array index range in initializer exceeds "
8238 "array bounds");
8239 last = 0;
8244 designator_depth++;
8245 designator_erroneous = 0;
8246 if (constructor_range_stack || last)
8247 push_range_stack (last, braced_init_obstack);
8251 /* Within a struct initializer, specify the next field to be initialized. */
8253 void
8254 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8255 struct obstack *braced_init_obstack)
8257 tree field;
8259 if (set_designator (loc, 0, braced_init_obstack))
8260 return;
8262 designator_erroneous = 1;
8264 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8266 error_init (loc, "field name not in record or union initializer");
8267 return;
8270 field = lookup_field (constructor_type, fieldname);
8272 if (field == 0)
8274 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8275 if (guessed_id)
8277 gcc_rich_location rich_loc (fieldname_loc);
8278 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8279 error_at_rich_loc
8280 (&rich_loc,
8281 "%qT has no member named %qE; did you mean %qE?",
8282 constructor_type, fieldname, guessed_id);
8284 else
8285 error_at (fieldname_loc, "%qT has no member named %qE",
8286 constructor_type, fieldname);
8288 else
8291 constructor_fields = TREE_VALUE (field);
8292 designator_depth++;
8293 designator_erroneous = 0;
8294 if (constructor_range_stack)
8295 push_range_stack (NULL_TREE, braced_init_obstack);
8296 field = TREE_CHAIN (field);
8297 if (field)
8299 if (set_designator (loc, 0, braced_init_obstack))
8300 return;
8303 while (field != NULL_TREE);
8306 /* Add a new initializer to the tree of pending initializers. PURPOSE
8307 identifies the initializer, either array index or field in a structure.
8308 VALUE is the value of that index or field. If ORIGTYPE is not
8309 NULL_TREE, it is the original type of VALUE.
8311 IMPLICIT is true if value comes from pop_init_level (1),
8312 the new initializer has been merged with the existing one
8313 and thus no warnings should be emitted about overriding an
8314 existing initializer. */
8316 static void
8317 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8318 bool implicit, struct obstack *braced_init_obstack)
8320 struct init_node *p, **q, *r;
8322 q = &constructor_pending_elts;
8323 p = 0;
8325 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8327 while (*q != 0)
8329 p = *q;
8330 if (tree_int_cst_lt (purpose, p->purpose))
8331 q = &p->left;
8332 else if (tree_int_cst_lt (p->purpose, purpose))
8333 q = &p->right;
8334 else
8336 if (!implicit)
8338 if (TREE_SIDE_EFFECTS (p->value))
8339 warning_init (loc, OPT_Woverride_init_side_effects,
8340 "initialized field with side-effects "
8341 "overwritten");
8342 else if (warn_override_init)
8343 warning_init (loc, OPT_Woverride_init,
8344 "initialized field overwritten");
8346 p->value = value;
8347 p->origtype = origtype;
8348 return;
8352 else
8354 tree bitpos;
8356 bitpos = bit_position (purpose);
8357 while (*q != NULL)
8359 p = *q;
8360 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8361 q = &p->left;
8362 else if (p->purpose != purpose)
8363 q = &p->right;
8364 else
8366 if (!implicit)
8368 if (TREE_SIDE_EFFECTS (p->value))
8369 warning_init (loc, OPT_Woverride_init_side_effects,
8370 "initialized field with side-effects "
8371 "overwritten");
8372 else if (warn_override_init)
8373 warning_init (loc, OPT_Woverride_init,
8374 "initialized field overwritten");
8376 p->value = value;
8377 p->origtype = origtype;
8378 return;
8383 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8384 sizeof (struct init_node));
8385 r->purpose = purpose;
8386 r->value = value;
8387 r->origtype = origtype;
8389 *q = r;
8390 r->parent = p;
8391 r->left = 0;
8392 r->right = 0;
8393 r->balance = 0;
8395 while (p)
8397 struct init_node *s;
8399 if (r == p->left)
8401 if (p->balance == 0)
8402 p->balance = -1;
8403 else if (p->balance < 0)
8405 if (r->balance < 0)
8407 /* L rotation. */
8408 p->left = r->right;
8409 if (p->left)
8410 p->left->parent = p;
8411 r->right = p;
8413 p->balance = 0;
8414 r->balance = 0;
8416 s = p->parent;
8417 p->parent = r;
8418 r->parent = s;
8419 if (s)
8421 if (s->left == p)
8422 s->left = r;
8423 else
8424 s->right = r;
8426 else
8427 constructor_pending_elts = r;
8429 else
8431 /* LR rotation. */
8432 struct init_node *t = r->right;
8434 r->right = t->left;
8435 if (r->right)
8436 r->right->parent = r;
8437 t->left = r;
8439 p->left = t->right;
8440 if (p->left)
8441 p->left->parent = p;
8442 t->right = p;
8444 p->balance = t->balance < 0;
8445 r->balance = -(t->balance > 0);
8446 t->balance = 0;
8448 s = p->parent;
8449 p->parent = t;
8450 r->parent = t;
8451 t->parent = s;
8452 if (s)
8454 if (s->left == p)
8455 s->left = t;
8456 else
8457 s->right = t;
8459 else
8460 constructor_pending_elts = t;
8462 break;
8464 else
8466 /* p->balance == +1; growth of left side balances the node. */
8467 p->balance = 0;
8468 break;
8471 else /* r == p->right */
8473 if (p->balance == 0)
8474 /* Growth propagation from right side. */
8475 p->balance++;
8476 else if (p->balance > 0)
8478 if (r->balance > 0)
8480 /* R rotation. */
8481 p->right = r->left;
8482 if (p->right)
8483 p->right->parent = p;
8484 r->left = p;
8486 p->balance = 0;
8487 r->balance = 0;
8489 s = p->parent;
8490 p->parent = r;
8491 r->parent = s;
8492 if (s)
8494 if (s->left == p)
8495 s->left = r;
8496 else
8497 s->right = r;
8499 else
8500 constructor_pending_elts = r;
8502 else /* r->balance == -1 */
8504 /* RL rotation */
8505 struct init_node *t = r->left;
8507 r->left = t->right;
8508 if (r->left)
8509 r->left->parent = r;
8510 t->right = r;
8512 p->right = t->left;
8513 if (p->right)
8514 p->right->parent = p;
8515 t->left = p;
8517 r->balance = (t->balance < 0);
8518 p->balance = -(t->balance > 0);
8519 t->balance = 0;
8521 s = p->parent;
8522 p->parent = t;
8523 r->parent = t;
8524 t->parent = s;
8525 if (s)
8527 if (s->left == p)
8528 s->left = t;
8529 else
8530 s->right = t;
8532 else
8533 constructor_pending_elts = t;
8535 break;
8537 else
8539 /* p->balance == -1; growth of right side balances the node. */
8540 p->balance = 0;
8541 break;
8545 r = p;
8546 p = p->parent;
8550 /* Build AVL tree from a sorted chain. */
8552 static void
8553 set_nonincremental_init (struct obstack * braced_init_obstack)
8555 unsigned HOST_WIDE_INT ix;
8556 tree index, value;
8558 if (TREE_CODE (constructor_type) != RECORD_TYPE
8559 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8560 return;
8562 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8563 add_pending_init (input_location, index, value, NULL_TREE, true,
8564 braced_init_obstack);
8565 constructor_elements = NULL;
8566 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8568 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8569 /* Skip any nameless bit fields at the beginning. */
8570 while (constructor_unfilled_fields != 0
8571 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8572 && DECL_NAME (constructor_unfilled_fields) == 0)
8573 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8576 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8578 if (TYPE_DOMAIN (constructor_type))
8579 constructor_unfilled_index
8580 = convert (bitsizetype,
8581 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8582 else
8583 constructor_unfilled_index = bitsize_zero_node;
8585 constructor_incremental = 0;
8588 /* Build AVL tree from a string constant. */
8590 static void
8591 set_nonincremental_init_from_string (tree str,
8592 struct obstack * braced_init_obstack)
8594 tree value, purpose, type;
8595 HOST_WIDE_INT val[2];
8596 const char *p, *end;
8597 int byte, wchar_bytes, charwidth, bitpos;
8599 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8601 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8602 charwidth = TYPE_PRECISION (char_type_node);
8603 gcc_assert ((size_t) wchar_bytes * charwidth
8604 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8605 type = TREE_TYPE (constructor_type);
8606 p = TREE_STRING_POINTER (str);
8607 end = p + TREE_STRING_LENGTH (str);
8609 for (purpose = bitsize_zero_node;
8610 p < end
8611 && !(constructor_max_index
8612 && tree_int_cst_lt (constructor_max_index, purpose));
8613 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8615 if (wchar_bytes == 1)
8617 val[0] = (unsigned char) *p++;
8618 val[1] = 0;
8620 else
8622 val[1] = 0;
8623 val[0] = 0;
8624 for (byte = 0; byte < wchar_bytes; byte++)
8626 if (BYTES_BIG_ENDIAN)
8627 bitpos = (wchar_bytes - byte - 1) * charwidth;
8628 else
8629 bitpos = byte * charwidth;
8630 val[bitpos / HOST_BITS_PER_WIDE_INT]
8631 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8632 << (bitpos % HOST_BITS_PER_WIDE_INT);
8636 if (!TYPE_UNSIGNED (type))
8638 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8639 if (bitpos < HOST_BITS_PER_WIDE_INT)
8641 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8643 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8644 val[1] = -1;
8647 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8649 if (val[0] < 0)
8650 val[1] = -1;
8652 else if (val[1] & (HOST_WIDE_INT_1
8653 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8654 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8657 value = wide_int_to_tree (type,
8658 wide_int::from_array (val, 2,
8659 HOST_BITS_PER_WIDE_INT * 2));
8660 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8661 braced_init_obstack);
8664 constructor_incremental = 0;
8667 /* Return value of FIELD in pending initializer or zero if the field was
8668 not initialized yet. */
8670 static tree
8671 find_init_member (tree field, struct obstack * braced_init_obstack)
8673 struct init_node *p;
8675 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8677 if (constructor_incremental
8678 && tree_int_cst_lt (field, constructor_unfilled_index))
8679 set_nonincremental_init (braced_init_obstack);
8681 p = constructor_pending_elts;
8682 while (p)
8684 if (tree_int_cst_lt (field, p->purpose))
8685 p = p->left;
8686 else if (tree_int_cst_lt (p->purpose, field))
8687 p = p->right;
8688 else
8689 return p->value;
8692 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8694 tree bitpos = bit_position (field);
8696 if (constructor_incremental
8697 && (!constructor_unfilled_fields
8698 || tree_int_cst_lt (bitpos,
8699 bit_position (constructor_unfilled_fields))))
8700 set_nonincremental_init (braced_init_obstack);
8702 p = constructor_pending_elts;
8703 while (p)
8705 if (field == p->purpose)
8706 return p->value;
8707 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8708 p = p->left;
8709 else
8710 p = p->right;
8713 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8715 if (!vec_safe_is_empty (constructor_elements)
8716 && (constructor_elements->last ().index == field))
8717 return constructor_elements->last ().value;
8719 return 0;
8722 /* "Output" the next constructor element.
8723 At top level, really output it to assembler code now.
8724 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8725 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8726 TYPE is the data type that the containing data type wants here.
8727 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8728 If VALUE is a string constant, STRICT_STRING is true if it is
8729 unparenthesized or we should not warn here for it being parenthesized.
8730 For other types of VALUE, STRICT_STRING is not used.
8732 PENDING if non-nil means output pending elements that belong
8733 right after this element. (PENDING is normally 1;
8734 it is 0 while outputting pending elements, to avoid recursion.)
8736 IMPLICIT is true if value comes from pop_init_level (1),
8737 the new initializer has been merged with the existing one
8738 and thus no warnings should be emitted about overriding an
8739 existing initializer. */
8741 static void
8742 output_init_element (location_t loc, tree value, tree origtype,
8743 bool strict_string, tree type, tree field, int pending,
8744 bool implicit, struct obstack * braced_init_obstack)
8746 tree semantic_type = NULL_TREE;
8747 bool maybe_const = true;
8748 bool npc;
8750 if (type == error_mark_node || value == error_mark_node)
8752 constructor_erroneous = 1;
8753 return;
8755 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8756 && (TREE_CODE (value) == STRING_CST
8757 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8758 && !(TREE_CODE (value) == STRING_CST
8759 && TREE_CODE (type) == ARRAY_TYPE
8760 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8761 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8762 TYPE_MAIN_VARIANT (type)))
8763 value = array_to_pointer_conversion (input_location, value);
8765 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8766 && require_constant_value && pending)
8768 /* As an extension, allow initializing objects with static storage
8769 duration with compound literals (which are then treated just as
8770 the brace enclosed list they contain). */
8771 if (flag_isoc99)
8772 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8773 "constant");
8774 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8775 value = DECL_INITIAL (decl);
8778 npc = null_pointer_constant_p (value);
8779 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8781 semantic_type = TREE_TYPE (value);
8782 value = TREE_OPERAND (value, 0);
8784 value = c_fully_fold (value, require_constant_value, &maybe_const);
8786 if (value == error_mark_node)
8787 constructor_erroneous = 1;
8788 else if (!TREE_CONSTANT (value))
8789 constructor_constant = 0;
8790 else if (!initializer_constant_valid_p (value,
8791 TREE_TYPE (value),
8792 AGGREGATE_TYPE_P (constructor_type)
8793 && TYPE_REVERSE_STORAGE_ORDER
8794 (constructor_type))
8795 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8796 && DECL_C_BIT_FIELD (field)
8797 && TREE_CODE (value) != INTEGER_CST))
8798 constructor_simple = 0;
8799 if (!maybe_const)
8800 constructor_nonconst = 1;
8802 /* Digest the initializer and issue any errors about incompatible
8803 types before issuing errors about non-constant initializers. */
8804 tree new_value = value;
8805 if (semantic_type)
8806 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8807 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8808 require_constant_value);
8809 if (new_value == error_mark_node)
8811 constructor_erroneous = 1;
8812 return;
8814 if (require_constant_value || require_constant_elements)
8815 constant_expression_warning (new_value);
8817 /* Proceed to check the constness of the original initializer. */
8818 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8820 if (require_constant_value)
8822 error_init (loc, "initializer element is not constant");
8823 value = error_mark_node;
8825 else if (require_constant_elements)
8826 pedwarn (loc, OPT_Wpedantic,
8827 "initializer element is not computable at load time");
8829 else if (!maybe_const
8830 && (require_constant_value || require_constant_elements))
8831 pedwarn_init (loc, OPT_Wpedantic,
8832 "initializer element is not a constant expression");
8834 /* Issue -Wc++-compat warnings about initializing a bitfield with
8835 enum type. */
8836 if (warn_cxx_compat
8837 && field != NULL_TREE
8838 && TREE_CODE (field) == FIELD_DECL
8839 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8840 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8841 != TYPE_MAIN_VARIANT (type))
8842 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8844 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8845 if (checktype != error_mark_node
8846 && (TYPE_MAIN_VARIANT (checktype)
8847 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8848 warning_init (loc, OPT_Wc___compat,
8849 "enum conversion in initialization is invalid in C++");
8852 /* If this field is empty (and not at the end of structure),
8853 don't do anything other than checking the initializer. */
8854 if (field
8855 && (TREE_TYPE (field) == error_mark_node
8856 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8857 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8858 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8859 || DECL_CHAIN (field)))))
8860 return;
8862 /* Finally, set VALUE to the initializer value digested above. */
8863 value = new_value;
8865 /* If this element doesn't come next in sequence,
8866 put it on constructor_pending_elts. */
8867 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8868 && (!constructor_incremental
8869 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8871 if (constructor_incremental
8872 && tree_int_cst_lt (field, constructor_unfilled_index))
8873 set_nonincremental_init (braced_init_obstack);
8875 add_pending_init (loc, field, value, origtype, implicit,
8876 braced_init_obstack);
8877 return;
8879 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8880 && (!constructor_incremental
8881 || field != constructor_unfilled_fields))
8883 /* We do this for records but not for unions. In a union,
8884 no matter which field is specified, it can be initialized
8885 right away since it starts at the beginning of the union. */
8886 if (constructor_incremental)
8888 if (!constructor_unfilled_fields)
8889 set_nonincremental_init (braced_init_obstack);
8890 else
8892 tree bitpos, unfillpos;
8894 bitpos = bit_position (field);
8895 unfillpos = bit_position (constructor_unfilled_fields);
8897 if (tree_int_cst_lt (bitpos, unfillpos))
8898 set_nonincremental_init (braced_init_obstack);
8902 add_pending_init (loc, field, value, origtype, implicit,
8903 braced_init_obstack);
8904 return;
8906 else if (TREE_CODE (constructor_type) == UNION_TYPE
8907 && !vec_safe_is_empty (constructor_elements))
8909 if (!implicit)
8911 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8912 warning_init (loc, OPT_Woverride_init_side_effects,
8913 "initialized field with side-effects overwritten");
8914 else if (warn_override_init)
8915 warning_init (loc, OPT_Woverride_init,
8916 "initialized field overwritten");
8919 /* We can have just one union field set. */
8920 constructor_elements = NULL;
8923 /* Otherwise, output this element either to
8924 constructor_elements or to the assembler file. */
8926 constructor_elt celt = {field, value};
8927 vec_safe_push (constructor_elements, celt);
8929 /* Advance the variable that indicates sequential elements output. */
8930 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8931 constructor_unfilled_index
8932 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8933 bitsize_one_node);
8934 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8936 constructor_unfilled_fields
8937 = DECL_CHAIN (constructor_unfilled_fields);
8939 /* Skip any nameless bit fields. */
8940 while (constructor_unfilled_fields != 0
8941 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8942 && DECL_NAME (constructor_unfilled_fields) == 0)
8943 constructor_unfilled_fields =
8944 DECL_CHAIN (constructor_unfilled_fields);
8946 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8947 constructor_unfilled_fields = 0;
8949 /* Now output any pending elements which have become next. */
8950 if (pending)
8951 output_pending_init_elements (0, braced_init_obstack);
8954 /* Output any pending elements which have become next.
8955 As we output elements, constructor_unfilled_{fields,index}
8956 advances, which may cause other elements to become next;
8957 if so, they too are output.
8959 If ALL is 0, we return when there are
8960 no more pending elements to output now.
8962 If ALL is 1, we output space as necessary so that
8963 we can output all the pending elements. */
8964 static void
8965 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8967 struct init_node *elt = constructor_pending_elts;
8968 tree next;
8970 retry:
8972 /* Look through the whole pending tree.
8973 If we find an element that should be output now,
8974 output it. Otherwise, set NEXT to the element
8975 that comes first among those still pending. */
8977 next = 0;
8978 while (elt)
8980 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8982 if (tree_int_cst_equal (elt->purpose,
8983 constructor_unfilled_index))
8984 output_init_element (input_location, elt->value, elt->origtype,
8985 true, TREE_TYPE (constructor_type),
8986 constructor_unfilled_index, 0, false,
8987 braced_init_obstack);
8988 else if (tree_int_cst_lt (constructor_unfilled_index,
8989 elt->purpose))
8991 /* Advance to the next smaller node. */
8992 if (elt->left)
8993 elt = elt->left;
8994 else
8996 /* We have reached the smallest node bigger than the
8997 current unfilled index. Fill the space first. */
8998 next = elt->purpose;
8999 break;
9002 else
9004 /* Advance to the next bigger node. */
9005 if (elt->right)
9006 elt = elt->right;
9007 else
9009 /* We have reached the biggest node in a subtree. Find
9010 the parent of it, which is the next bigger node. */
9011 while (elt->parent && elt->parent->right == elt)
9012 elt = elt->parent;
9013 elt = elt->parent;
9014 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9015 elt->purpose))
9017 next = elt->purpose;
9018 break;
9023 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9025 tree ctor_unfilled_bitpos, elt_bitpos;
9027 /* If the current record is complete we are done. */
9028 if (constructor_unfilled_fields == 0)
9029 break;
9031 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9032 elt_bitpos = bit_position (elt->purpose);
9033 /* We can't compare fields here because there might be empty
9034 fields in between. */
9035 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9037 constructor_unfilled_fields = elt->purpose;
9038 output_init_element (input_location, elt->value, elt->origtype,
9039 true, TREE_TYPE (elt->purpose),
9040 elt->purpose, 0, false,
9041 braced_init_obstack);
9043 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9045 /* Advance to the next smaller node. */
9046 if (elt->left)
9047 elt = elt->left;
9048 else
9050 /* We have reached the smallest node bigger than the
9051 current unfilled field. Fill the space first. */
9052 next = elt->purpose;
9053 break;
9056 else
9058 /* Advance to the next bigger node. */
9059 if (elt->right)
9060 elt = elt->right;
9061 else
9063 /* We have reached the biggest node in a subtree. Find
9064 the parent of it, which is the next bigger node. */
9065 while (elt->parent && elt->parent->right == elt)
9066 elt = elt->parent;
9067 elt = elt->parent;
9068 if (elt
9069 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9070 bit_position (elt->purpose))))
9072 next = elt->purpose;
9073 break;
9080 /* Ordinarily return, but not if we want to output all
9081 and there are elements left. */
9082 if (!(all && next != 0))
9083 return;
9085 /* If it's not incremental, just skip over the gap, so that after
9086 jumping to retry we will output the next successive element. */
9087 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9088 constructor_unfilled_fields = next;
9089 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9090 constructor_unfilled_index = next;
9092 /* ELT now points to the node in the pending tree with the next
9093 initializer to output. */
9094 goto retry;
9097 /* Add one non-braced element to the current constructor level.
9098 This adjusts the current position within the constructor's type.
9099 This may also start or terminate implicit levels
9100 to handle a partly-braced initializer.
9102 Once this has found the correct level for the new element,
9103 it calls output_init_element.
9105 IMPLICIT is true if value comes from pop_init_level (1),
9106 the new initializer has been merged with the existing one
9107 and thus no warnings should be emitted about overriding an
9108 existing initializer. */
9110 void
9111 process_init_element (location_t loc, struct c_expr value, bool implicit,
9112 struct obstack * braced_init_obstack)
9114 tree orig_value = value.value;
9115 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9116 bool strict_string = value.original_code == STRING_CST;
9117 bool was_designated = designator_depth != 0;
9119 designator_depth = 0;
9120 designator_erroneous = 0;
9122 if (!implicit && value.value && !integer_zerop (value.value))
9123 constructor_zeroinit = 0;
9125 /* Handle superfluous braces around string cst as in
9126 char x[] = {"foo"}; */
9127 if (string_flag
9128 && constructor_type
9129 && !was_designated
9130 && TREE_CODE (constructor_type) == ARRAY_TYPE
9131 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9132 && integer_zerop (constructor_unfilled_index))
9134 if (constructor_stack->replacement_value.value)
9135 error_init (loc, "excess elements in char array initializer");
9136 constructor_stack->replacement_value = value;
9137 return;
9140 if (constructor_stack->replacement_value.value != 0)
9142 error_init (loc, "excess elements in struct initializer");
9143 return;
9146 /* Ignore elements of a brace group if it is entirely superfluous
9147 and has already been diagnosed. */
9148 if (constructor_type == 0)
9149 return;
9151 if (!implicit && warn_designated_init && !was_designated
9152 && TREE_CODE (constructor_type) == RECORD_TYPE
9153 && lookup_attribute ("designated_init",
9154 TYPE_ATTRIBUTES (constructor_type)))
9155 warning_init (loc,
9156 OPT_Wdesignated_init,
9157 "positional initialization of field "
9158 "in %<struct%> declared with %<designated_init%> attribute");
9160 /* If we've exhausted any levels that didn't have braces,
9161 pop them now. */
9162 while (constructor_stack->implicit)
9164 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9165 && constructor_fields == 0)
9166 process_init_element (loc,
9167 pop_init_level (loc, 1, braced_init_obstack),
9168 true, braced_init_obstack);
9169 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9170 || VECTOR_TYPE_P (constructor_type))
9171 && constructor_max_index
9172 && tree_int_cst_lt (constructor_max_index,
9173 constructor_index))
9174 process_init_element (loc,
9175 pop_init_level (loc, 1, braced_init_obstack),
9176 true, braced_init_obstack);
9177 else
9178 break;
9181 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9182 if (constructor_range_stack)
9184 /* If value is a compound literal and we'll be just using its
9185 content, don't put it into a SAVE_EXPR. */
9186 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9187 || !require_constant_value)
9189 tree semantic_type = NULL_TREE;
9190 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9192 semantic_type = TREE_TYPE (value.value);
9193 value.value = TREE_OPERAND (value.value, 0);
9195 value.value = c_save_expr (value.value);
9196 if (semantic_type)
9197 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9198 value.value);
9202 while (1)
9204 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9206 tree fieldtype;
9207 enum tree_code fieldcode;
9209 if (constructor_fields == 0)
9211 pedwarn_init (loc, 0, "excess elements in struct initializer");
9212 break;
9215 fieldtype = TREE_TYPE (constructor_fields);
9216 if (fieldtype != error_mark_node)
9217 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9218 fieldcode = TREE_CODE (fieldtype);
9220 /* Error for non-static initialization of a flexible array member. */
9221 if (fieldcode == ARRAY_TYPE
9222 && !require_constant_value
9223 && TYPE_SIZE (fieldtype) == NULL_TREE
9224 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9226 error_init (loc, "non-static initialization of a flexible "
9227 "array member");
9228 break;
9231 /* Error for initialization of a flexible array member with
9232 a string constant if the structure is in an array. E.g.:
9233 struct S { int x; char y[]; };
9234 struct S s[] = { { 1, "foo" } };
9235 is invalid. */
9236 if (string_flag
9237 && fieldcode == ARRAY_TYPE
9238 && constructor_depth > 1
9239 && TYPE_SIZE (fieldtype) == NULL_TREE
9240 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9242 bool in_array_p = false;
9243 for (struct constructor_stack *p = constructor_stack;
9244 p && p->type; p = p->next)
9245 if (TREE_CODE (p->type) == ARRAY_TYPE)
9247 in_array_p = true;
9248 break;
9250 if (in_array_p)
9252 error_init (loc, "initialization of flexible array "
9253 "member in a nested context");
9254 break;
9258 /* Accept a string constant to initialize a subarray. */
9259 if (value.value != 0
9260 && fieldcode == ARRAY_TYPE
9261 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9262 && string_flag)
9263 value.value = orig_value;
9264 /* Otherwise, if we have come to a subaggregate,
9265 and we don't have an element of its type, push into it. */
9266 else if (value.value != 0
9267 && value.value != error_mark_node
9268 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9269 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9270 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9272 push_init_level (loc, 1, braced_init_obstack);
9273 continue;
9276 if (value.value)
9278 push_member_name (constructor_fields);
9279 output_init_element (loc, value.value, value.original_type,
9280 strict_string, fieldtype,
9281 constructor_fields, 1, implicit,
9282 braced_init_obstack);
9283 RESTORE_SPELLING_DEPTH (constructor_depth);
9285 else
9286 /* Do the bookkeeping for an element that was
9287 directly output as a constructor. */
9289 /* For a record, keep track of end position of last field. */
9290 if (DECL_SIZE (constructor_fields))
9291 constructor_bit_index
9292 = size_binop_loc (input_location, PLUS_EXPR,
9293 bit_position (constructor_fields),
9294 DECL_SIZE (constructor_fields));
9296 /* If the current field was the first one not yet written out,
9297 it isn't now, so update. */
9298 if (constructor_unfilled_fields == constructor_fields)
9300 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9301 /* Skip any nameless bit fields. */
9302 while (constructor_unfilled_fields != 0
9303 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9304 && DECL_NAME (constructor_unfilled_fields) == 0)
9305 constructor_unfilled_fields =
9306 DECL_CHAIN (constructor_unfilled_fields);
9310 constructor_fields = DECL_CHAIN (constructor_fields);
9311 /* Skip any nameless bit fields at the beginning. */
9312 while (constructor_fields != 0
9313 && DECL_C_BIT_FIELD (constructor_fields)
9314 && DECL_NAME (constructor_fields) == 0)
9315 constructor_fields = DECL_CHAIN (constructor_fields);
9317 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9319 tree fieldtype;
9320 enum tree_code fieldcode;
9322 if (constructor_fields == 0)
9324 pedwarn_init (loc, 0,
9325 "excess elements in union initializer");
9326 break;
9329 fieldtype = TREE_TYPE (constructor_fields);
9330 if (fieldtype != error_mark_node)
9331 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9332 fieldcode = TREE_CODE (fieldtype);
9334 /* Warn that traditional C rejects initialization of unions.
9335 We skip the warning if the value is zero. This is done
9336 under the assumption that the zero initializer in user
9337 code appears conditioned on e.g. __STDC__ to avoid
9338 "missing initializer" warnings and relies on default
9339 initialization to zero in the traditional C case.
9340 We also skip the warning if the initializer is designated,
9341 again on the assumption that this must be conditional on
9342 __STDC__ anyway (and we've already complained about the
9343 member-designator already). */
9344 if (!in_system_header_at (input_location) && !constructor_designated
9345 && !(value.value && (integer_zerop (value.value)
9346 || real_zerop (value.value))))
9347 warning (OPT_Wtraditional, "traditional C rejects initialization "
9348 "of unions");
9350 /* Accept a string constant to initialize a subarray. */
9351 if (value.value != 0
9352 && fieldcode == ARRAY_TYPE
9353 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9354 && string_flag)
9355 value.value = orig_value;
9356 /* Otherwise, if we have come to a subaggregate,
9357 and we don't have an element of its type, push into it. */
9358 else if (value.value != 0
9359 && value.value != error_mark_node
9360 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9361 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9362 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9364 push_init_level (loc, 1, braced_init_obstack);
9365 continue;
9368 if (value.value)
9370 push_member_name (constructor_fields);
9371 output_init_element (loc, value.value, value.original_type,
9372 strict_string, fieldtype,
9373 constructor_fields, 1, implicit,
9374 braced_init_obstack);
9375 RESTORE_SPELLING_DEPTH (constructor_depth);
9377 else
9378 /* Do the bookkeeping for an element that was
9379 directly output as a constructor. */
9381 constructor_bit_index = DECL_SIZE (constructor_fields);
9382 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9385 constructor_fields = 0;
9387 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9389 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9390 enum tree_code eltcode = TREE_CODE (elttype);
9392 /* Accept a string constant to initialize a subarray. */
9393 if (value.value != 0
9394 && eltcode == ARRAY_TYPE
9395 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9396 && string_flag)
9397 value.value = orig_value;
9398 /* Otherwise, if we have come to a subaggregate,
9399 and we don't have an element of its type, push into it. */
9400 else if (value.value != 0
9401 && value.value != error_mark_node
9402 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9403 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9404 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9406 push_init_level (loc, 1, braced_init_obstack);
9407 continue;
9410 if (constructor_max_index != 0
9411 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9412 || integer_all_onesp (constructor_max_index)))
9414 pedwarn_init (loc, 0,
9415 "excess elements in array initializer");
9416 break;
9419 /* Now output the actual element. */
9420 if (value.value)
9422 push_array_bounds (tree_to_uhwi (constructor_index));
9423 output_init_element (loc, value.value, value.original_type,
9424 strict_string, elttype,
9425 constructor_index, 1, implicit,
9426 braced_init_obstack);
9427 RESTORE_SPELLING_DEPTH (constructor_depth);
9430 constructor_index
9431 = size_binop_loc (input_location, PLUS_EXPR,
9432 constructor_index, bitsize_one_node);
9434 if (!value.value)
9435 /* If we are doing the bookkeeping for an element that was
9436 directly output as a constructor, we must update
9437 constructor_unfilled_index. */
9438 constructor_unfilled_index = constructor_index;
9440 else if (VECTOR_TYPE_P (constructor_type))
9442 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9444 /* Do a basic check of initializer size. Note that vectors
9445 always have a fixed size derived from their type. */
9446 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9448 pedwarn_init (loc, 0,
9449 "excess elements in vector initializer");
9450 break;
9453 /* Now output the actual element. */
9454 if (value.value)
9456 if (TREE_CODE (value.value) == VECTOR_CST)
9457 elttype = TYPE_MAIN_VARIANT (constructor_type);
9458 output_init_element (loc, value.value, value.original_type,
9459 strict_string, elttype,
9460 constructor_index, 1, implicit,
9461 braced_init_obstack);
9464 constructor_index
9465 = size_binop_loc (input_location,
9466 PLUS_EXPR, constructor_index, bitsize_one_node);
9468 if (!value.value)
9469 /* If we are doing the bookkeeping for an element that was
9470 directly output as a constructor, we must update
9471 constructor_unfilled_index. */
9472 constructor_unfilled_index = constructor_index;
9475 /* Handle the sole element allowed in a braced initializer
9476 for a scalar variable. */
9477 else if (constructor_type != error_mark_node
9478 && constructor_fields == 0)
9480 pedwarn_init (loc, 0,
9481 "excess elements in scalar initializer");
9482 break;
9484 else
9486 if (value.value)
9487 output_init_element (loc, value.value, value.original_type,
9488 strict_string, constructor_type,
9489 NULL_TREE, 1, implicit,
9490 braced_init_obstack);
9491 constructor_fields = 0;
9494 /* Handle range initializers either at this level or anywhere higher
9495 in the designator stack. */
9496 if (constructor_range_stack)
9498 struct constructor_range_stack *p, *range_stack;
9499 int finish = 0;
9501 range_stack = constructor_range_stack;
9502 constructor_range_stack = 0;
9503 while (constructor_stack != range_stack->stack)
9505 gcc_assert (constructor_stack->implicit);
9506 process_init_element (loc,
9507 pop_init_level (loc, 1,
9508 braced_init_obstack),
9509 true, braced_init_obstack);
9511 for (p = range_stack;
9512 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9513 p = p->prev)
9515 gcc_assert (constructor_stack->implicit);
9516 process_init_element (loc,
9517 pop_init_level (loc, 1,
9518 braced_init_obstack),
9519 true, braced_init_obstack);
9522 p->index = size_binop_loc (input_location,
9523 PLUS_EXPR, p->index, bitsize_one_node);
9524 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9525 finish = 1;
9527 while (1)
9529 constructor_index = p->index;
9530 constructor_fields = p->fields;
9531 if (finish && p->range_end && p->index == p->range_start)
9533 finish = 0;
9534 p->prev = 0;
9536 p = p->next;
9537 if (!p)
9538 break;
9539 finish_implicit_inits (loc, braced_init_obstack);
9540 push_init_level (loc, 2, braced_init_obstack);
9541 p->stack = constructor_stack;
9542 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9543 p->index = p->range_start;
9546 if (!finish)
9547 constructor_range_stack = range_stack;
9548 continue;
9551 break;
9554 constructor_range_stack = 0;
9557 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9558 (guaranteed to be 'volatile' or null) and ARGS (represented using
9559 an ASM_EXPR node). */
9560 tree
9561 build_asm_stmt (tree cv_qualifier, tree args)
9563 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9564 ASM_VOLATILE_P (args) = 1;
9565 return add_stmt (args);
9568 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9569 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9570 SIMPLE indicates whether there was anything at all after the
9571 string in the asm expression -- asm("blah") and asm("blah" : )
9572 are subtly different. We use a ASM_EXPR node to represent this. */
9573 tree
9574 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9575 tree clobbers, tree labels, bool simple)
9577 tree tail;
9578 tree args;
9579 int i;
9580 const char *constraint;
9581 const char **oconstraints;
9582 bool allows_mem, allows_reg, is_inout;
9583 int ninputs, noutputs;
9585 ninputs = list_length (inputs);
9586 noutputs = list_length (outputs);
9587 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9589 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9591 /* Remove output conversions that change the type but not the mode. */
9592 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9594 tree output = TREE_VALUE (tail);
9596 output = c_fully_fold (output, false, NULL);
9598 /* ??? Really, this should not be here. Users should be using a
9599 proper lvalue, dammit. But there's a long history of using casts
9600 in the output operands. In cases like longlong.h, this becomes a
9601 primitive form of typechecking -- if the cast can be removed, then
9602 the output operand had a type of the proper width; otherwise we'll
9603 get an error. Gross, but ... */
9604 STRIP_NOPS (output);
9606 if (!lvalue_or_else (loc, output, lv_asm))
9607 output = error_mark_node;
9609 if (output != error_mark_node
9610 && (TREE_READONLY (output)
9611 || TYPE_READONLY (TREE_TYPE (output))
9612 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9613 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9614 readonly_error (loc, output, lv_asm);
9616 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9617 oconstraints[i] = constraint;
9619 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9620 &allows_mem, &allows_reg, &is_inout))
9622 /* If the operand is going to end up in memory,
9623 mark it addressable. */
9624 if (!allows_reg && !c_mark_addressable (output))
9625 output = error_mark_node;
9626 if (!(!allows_reg && allows_mem)
9627 && output != error_mark_node
9628 && VOID_TYPE_P (TREE_TYPE (output)))
9630 error_at (loc, "invalid use of void expression");
9631 output = error_mark_node;
9634 else
9635 output = error_mark_node;
9637 TREE_VALUE (tail) = output;
9640 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9642 tree input;
9644 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9645 input = TREE_VALUE (tail);
9647 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9648 oconstraints, &allows_mem, &allows_reg))
9650 /* If the operand is going to end up in memory,
9651 mark it addressable. */
9652 if (!allows_reg && allows_mem)
9654 input = c_fully_fold (input, false, NULL);
9656 /* Strip the nops as we allow this case. FIXME, this really
9657 should be rejected or made deprecated. */
9658 STRIP_NOPS (input);
9659 if (!c_mark_addressable (input))
9660 input = error_mark_node;
9662 else
9664 struct c_expr expr;
9665 memset (&expr, 0, sizeof (expr));
9666 expr.value = input;
9667 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9668 input = c_fully_fold (expr.value, false, NULL);
9670 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9672 error_at (loc, "invalid use of void expression");
9673 input = error_mark_node;
9677 else
9678 input = error_mark_node;
9680 TREE_VALUE (tail) = input;
9683 /* ASMs with labels cannot have outputs. This should have been
9684 enforced by the parser. */
9685 gcc_assert (outputs == NULL || labels == NULL);
9687 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9689 /* asm statements without outputs, including simple ones, are treated
9690 as volatile. */
9691 ASM_INPUT_P (args) = simple;
9692 ASM_VOLATILE_P (args) = (noutputs == 0);
9694 return args;
9697 /* Generate a goto statement to LABEL. LOC is the location of the
9698 GOTO. */
9700 tree
9701 c_finish_goto_label (location_t loc, tree label)
9703 tree decl = lookup_label_for_goto (loc, label);
9704 if (!decl)
9705 return NULL_TREE;
9706 TREE_USED (decl) = 1;
9708 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9709 SET_EXPR_LOCATION (t, loc);
9710 return add_stmt (t);
9714 /* Generate a computed goto statement to EXPR. LOC is the location of
9715 the GOTO. */
9717 tree
9718 c_finish_goto_ptr (location_t loc, tree expr)
9720 tree t;
9721 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9722 expr = c_fully_fold (expr, false, NULL);
9723 expr = convert (ptr_type_node, expr);
9724 t = build1 (GOTO_EXPR, void_type_node, expr);
9725 SET_EXPR_LOCATION (t, loc);
9726 return add_stmt (t);
9729 /* Generate a C `return' statement. RETVAL is the expression for what
9730 to return, or a null pointer for `return;' with no value. LOC is
9731 the location of the return statement, or the location of the expression,
9732 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9733 is the original type of RETVAL. */
9735 tree
9736 c_finish_return (location_t loc, tree retval, tree origtype)
9738 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9739 bool no_warning = false;
9740 bool npc = false;
9741 size_t rank = 0;
9743 /* Use the expansion point to handle cases such as returning NULL
9744 in a function returning void. */
9745 source_location xloc = expansion_point_location_if_in_system_header (loc);
9747 if (TREE_THIS_VOLATILE (current_function_decl))
9748 warning_at (xloc, 0,
9749 "function declared %<noreturn%> has a %<return%> statement");
9751 if (flag_cilkplus && contains_array_notation_expr (retval))
9753 /* Array notations are allowed in a return statement if it is inside a
9754 built-in array notation reduction function. */
9755 if (!find_rank (loc, retval, retval, false, &rank))
9756 return error_mark_node;
9757 if (rank >= 1)
9759 error_at (loc, "array notation expression cannot be used as a "
9760 "return value");
9761 return error_mark_node;
9764 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9766 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9767 "allowed");
9768 return error_mark_node;
9770 if (retval)
9772 tree semantic_type = NULL_TREE;
9773 npc = null_pointer_constant_p (retval);
9774 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9776 semantic_type = TREE_TYPE (retval);
9777 retval = TREE_OPERAND (retval, 0);
9779 retval = c_fully_fold (retval, false, NULL);
9780 if (semantic_type)
9781 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9784 if (!retval)
9786 current_function_returns_null = 1;
9787 if ((warn_return_type || flag_isoc99)
9788 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9790 bool warned_here;
9791 if (flag_isoc99)
9792 warned_here = pedwarn
9793 (loc, 0,
9794 "%<return%> with no value, in function returning non-void");
9795 else
9796 warned_here = warning_at
9797 (loc, OPT_Wreturn_type,
9798 "%<return%> with no value, in function returning non-void");
9799 no_warning = true;
9800 if (warned_here)
9801 inform (DECL_SOURCE_LOCATION (current_function_decl),
9802 "declared here");
9805 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9807 current_function_returns_null = 1;
9808 bool warned_here;
9809 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9810 warned_here = pedwarn
9811 (xloc, 0,
9812 "%<return%> with a value, in function returning void");
9813 else
9814 warned_here = pedwarn
9815 (xloc, OPT_Wpedantic, "ISO C forbids "
9816 "%<return%> with expression, in function returning void");
9817 if (warned_here)
9818 inform (DECL_SOURCE_LOCATION (current_function_decl),
9819 "declared here");
9821 else
9823 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9824 retval, origtype, ic_return,
9825 npc, NULL_TREE, NULL_TREE, 0);
9826 tree res = DECL_RESULT (current_function_decl);
9827 tree inner;
9828 bool save;
9830 current_function_returns_value = 1;
9831 if (t == error_mark_node)
9832 return NULL_TREE;
9834 save = in_late_binary_op;
9835 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9836 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9837 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9838 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9839 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9840 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9841 in_late_binary_op = true;
9842 inner = t = convert (TREE_TYPE (res), t);
9843 in_late_binary_op = save;
9845 /* Strip any conversions, additions, and subtractions, and see if
9846 we are returning the address of a local variable. Warn if so. */
9847 while (1)
9849 switch (TREE_CODE (inner))
9851 CASE_CONVERT:
9852 case NON_LVALUE_EXPR:
9853 case PLUS_EXPR:
9854 case POINTER_PLUS_EXPR:
9855 inner = TREE_OPERAND (inner, 0);
9856 continue;
9858 case MINUS_EXPR:
9859 /* If the second operand of the MINUS_EXPR has a pointer
9860 type (or is converted from it), this may be valid, so
9861 don't give a warning. */
9863 tree op1 = TREE_OPERAND (inner, 1);
9865 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9866 && (CONVERT_EXPR_P (op1)
9867 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9868 op1 = TREE_OPERAND (op1, 0);
9870 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9871 break;
9873 inner = TREE_OPERAND (inner, 0);
9874 continue;
9877 case ADDR_EXPR:
9878 inner = TREE_OPERAND (inner, 0);
9880 while (REFERENCE_CLASS_P (inner)
9881 && !INDIRECT_REF_P (inner))
9882 inner = TREE_OPERAND (inner, 0);
9884 if (DECL_P (inner)
9885 && !DECL_EXTERNAL (inner)
9886 && !TREE_STATIC (inner)
9887 && DECL_CONTEXT (inner) == current_function_decl)
9889 if (TREE_CODE (inner) == LABEL_DECL)
9890 warning_at (loc, OPT_Wreturn_local_addr,
9891 "function returns address of label");
9892 else
9894 warning_at (loc, OPT_Wreturn_local_addr,
9895 "function returns address of local variable");
9896 tree zero = build_zero_cst (TREE_TYPE (res));
9897 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9900 break;
9902 default:
9903 break;
9906 break;
9909 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9910 SET_EXPR_LOCATION (retval, loc);
9912 if (warn_sequence_point)
9913 verify_sequence_points (retval);
9916 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9917 TREE_NO_WARNING (ret_stmt) |= no_warning;
9918 return add_stmt (ret_stmt);
9921 struct c_switch {
9922 /* The SWITCH_EXPR being built. */
9923 tree switch_expr;
9925 /* The original type of the testing expression, i.e. before the
9926 default conversion is applied. */
9927 tree orig_type;
9929 /* A splay-tree mapping the low element of a case range to the high
9930 element, or NULL_TREE if there is no high element. Used to
9931 determine whether or not a new case label duplicates an old case
9932 label. We need a tree, rather than simply a hash table, because
9933 of the GNU case range extension. */
9934 splay_tree cases;
9936 /* The bindings at the point of the switch. This is used for
9937 warnings crossing decls when branching to a case label. */
9938 struct c_spot_bindings *bindings;
9940 /* The next node on the stack. */
9941 struct c_switch *next;
9943 /* Remember whether the controlling expression had boolean type
9944 before integer promotions for the sake of -Wswitch-bool. */
9945 bool bool_cond_p;
9947 /* Remember whether there was a case value that is outside the
9948 range of the ORIG_TYPE. */
9949 bool outside_range_p;
9952 /* A stack of the currently active switch statements. The innermost
9953 switch statement is on the top of the stack. There is no need to
9954 mark the stack for garbage collection because it is only active
9955 during the processing of the body of a function, and we never
9956 collect at that point. */
9958 struct c_switch *c_switch_stack;
9960 /* Start a C switch statement, testing expression EXP. Return the new
9961 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9962 SWITCH_COND_LOC is the location of the switch's condition.
9963 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9965 tree
9966 c_start_case (location_t switch_loc,
9967 location_t switch_cond_loc,
9968 tree exp, bool explicit_cast_p)
9970 tree orig_type = error_mark_node;
9971 bool bool_cond_p = false;
9972 struct c_switch *cs;
9974 if (exp != error_mark_node)
9976 orig_type = TREE_TYPE (exp);
9978 if (!INTEGRAL_TYPE_P (orig_type))
9980 if (orig_type != error_mark_node)
9982 error_at (switch_cond_loc, "switch quantity not an integer");
9983 orig_type = error_mark_node;
9985 exp = integer_zero_node;
9987 else
9989 tree type = TYPE_MAIN_VARIANT (orig_type);
9990 tree e = exp;
9992 /* Warn if the condition has boolean value. */
9993 while (TREE_CODE (e) == COMPOUND_EXPR)
9994 e = TREE_OPERAND (e, 1);
9996 if ((TREE_CODE (type) == BOOLEAN_TYPE
9997 || truth_value_p (TREE_CODE (e)))
9998 /* Explicit cast to int suppresses this warning. */
9999 && !(TREE_CODE (type) == INTEGER_TYPE
10000 && explicit_cast_p))
10001 bool_cond_p = true;
10003 if (!in_system_header_at (input_location)
10004 && (type == long_integer_type_node
10005 || type == long_unsigned_type_node))
10006 warning_at (switch_cond_loc,
10007 OPT_Wtraditional, "%<long%> switch expression not "
10008 "converted to %<int%> in ISO C");
10010 exp = c_fully_fold (exp, false, NULL);
10011 exp = default_conversion (exp);
10013 if (warn_sequence_point)
10014 verify_sequence_points (exp);
10018 /* Add this new SWITCH_EXPR to the stack. */
10019 cs = XNEW (struct c_switch);
10020 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10021 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10022 cs->orig_type = orig_type;
10023 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10024 cs->bindings = c_get_switch_bindings ();
10025 cs->bool_cond_p = bool_cond_p;
10026 cs->outside_range_p = false;
10027 cs->next = c_switch_stack;
10028 c_switch_stack = cs;
10030 return add_stmt (cs->switch_expr);
10033 /* Process a case label at location LOC. */
10035 tree
10036 do_case (location_t loc, tree low_value, tree high_value)
10038 tree label = NULL_TREE;
10040 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10042 low_value = c_fully_fold (low_value, false, NULL);
10043 if (TREE_CODE (low_value) == INTEGER_CST)
10044 pedwarn (loc, OPT_Wpedantic,
10045 "case label is not an integer constant expression");
10048 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10050 high_value = c_fully_fold (high_value, false, NULL);
10051 if (TREE_CODE (high_value) == INTEGER_CST)
10052 pedwarn (input_location, OPT_Wpedantic,
10053 "case label is not an integer constant expression");
10056 if (c_switch_stack == NULL)
10058 if (low_value)
10059 error_at (loc, "case label not within a switch statement");
10060 else
10061 error_at (loc, "%<default%> label not within a switch statement");
10062 return NULL_TREE;
10065 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10066 EXPR_LOCATION (c_switch_stack->switch_expr),
10067 loc))
10068 return NULL_TREE;
10070 label = c_add_case_label (loc, c_switch_stack->cases,
10071 SWITCH_COND (c_switch_stack->switch_expr),
10072 c_switch_stack->orig_type,
10073 low_value, high_value,
10074 &c_switch_stack->outside_range_p);
10075 if (label == error_mark_node)
10076 label = NULL_TREE;
10077 return label;
10080 /* Finish the switch statement. TYPE is the original type of the
10081 controlling expression of the switch, or NULL_TREE. */
10083 void
10084 c_finish_case (tree body, tree type)
10086 struct c_switch *cs = c_switch_stack;
10087 location_t switch_location;
10089 SWITCH_BODY (cs->switch_expr) = body;
10091 /* Emit warnings as needed. */
10092 switch_location = EXPR_LOCATION (cs->switch_expr);
10093 c_do_switch_warnings (cs->cases, switch_location,
10094 type ? type : TREE_TYPE (cs->switch_expr),
10095 SWITCH_COND (cs->switch_expr),
10096 cs->bool_cond_p, cs->outside_range_p);
10098 /* Pop the stack. */
10099 c_switch_stack = cs->next;
10100 splay_tree_delete (cs->cases);
10101 c_release_switch_bindings (cs->bindings);
10102 XDELETE (cs);
10105 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10106 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10107 may be null. */
10109 void
10110 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10111 tree else_block)
10113 tree stmt;
10115 /* If the condition has array notations, then the rank of the then_block and
10116 else_block must be either 0 or be equal to the rank of the condition. If
10117 the condition does not have array notations then break them up as it is
10118 broken up in a normal expression. */
10119 if (flag_cilkplus && contains_array_notation_expr (cond))
10121 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10122 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10123 return;
10124 if (then_block
10125 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10126 return;
10127 if (else_block
10128 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10129 return;
10130 if (cond_rank != then_rank && then_rank != 0)
10132 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10133 " and the then-block");
10134 return;
10136 else if (cond_rank != else_rank && else_rank != 0)
10138 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10139 " and the else-block");
10140 return;
10144 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10145 SET_EXPR_LOCATION (stmt, if_locus);
10146 add_stmt (stmt);
10149 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10150 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10151 is false for DO loops. INCR is the FOR increment expression. BODY is
10152 the statement controlled by the loop. BLAB is the break label. CLAB is
10153 the continue label. Everything is allowed to be NULL. */
10155 void
10156 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10157 tree blab, tree clab, bool cond_is_first)
10159 tree entry = NULL, exit = NULL, t;
10161 /* In theory could forbid cilk spawn for loop increment expression,
10162 but it should work just fine. */
10164 /* If the condition is zero don't generate a loop construct. */
10165 if (cond && integer_zerop (cond))
10167 if (cond_is_first)
10169 t = build_and_jump (&blab);
10170 SET_EXPR_LOCATION (t, start_locus);
10171 add_stmt (t);
10174 else
10176 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10178 /* If we have an exit condition, then we build an IF with gotos either
10179 out of the loop, or to the top of it. If there's no exit condition,
10180 then we just build a jump back to the top. */
10181 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10183 if (cond && !integer_nonzerop (cond))
10185 /* Canonicalize the loop condition to the end. This means
10186 generating a branch to the loop condition. Reuse the
10187 continue label, if possible. */
10188 if (cond_is_first)
10190 if (incr || !clab)
10192 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10193 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10195 else
10196 t = build1 (GOTO_EXPR, void_type_node, clab);
10197 SET_EXPR_LOCATION (t, start_locus);
10198 add_stmt (t);
10201 t = build_and_jump (&blab);
10202 if (cond_is_first)
10203 exit = fold_build3_loc (start_locus,
10204 COND_EXPR, void_type_node, cond, exit, t);
10205 else
10206 exit = fold_build3_loc (input_location,
10207 COND_EXPR, void_type_node, cond, exit, t);
10209 else
10211 /* For the backward-goto's location of an unconditional loop
10212 use the beginning of the body, or, if there is none, the
10213 top of the loop. */
10214 location_t loc = EXPR_LOCATION (expr_first (body));
10215 if (loc == UNKNOWN_LOCATION)
10216 loc = start_locus;
10217 SET_EXPR_LOCATION (exit, loc);
10220 add_stmt (top);
10223 if (body)
10224 add_stmt (body);
10225 if (clab)
10226 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10227 if (incr)
10228 add_stmt (incr);
10229 if (entry)
10230 add_stmt (entry);
10231 if (exit)
10232 add_stmt (exit);
10233 if (blab)
10234 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10237 tree
10238 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10240 bool skip;
10241 tree label = *label_p;
10243 /* In switch statements break is sometimes stylistically used after
10244 a return statement. This can lead to spurious warnings about
10245 control reaching the end of a non-void function when it is
10246 inlined. Note that we are calling block_may_fallthru with
10247 language specific tree nodes; this works because
10248 block_may_fallthru returns true when given something it does not
10249 understand. */
10250 skip = !block_may_fallthru (cur_stmt_list);
10252 if (!label)
10254 if (!skip)
10255 *label_p = label = create_artificial_label (loc);
10257 else if (TREE_CODE (label) == LABEL_DECL)
10259 else switch (TREE_INT_CST_LOW (label))
10261 case 0:
10262 if (is_break)
10263 error_at (loc, "break statement not within loop or switch");
10264 else
10265 error_at (loc, "continue statement not within a loop");
10266 return NULL_TREE;
10268 case 1:
10269 gcc_assert (is_break);
10270 error_at (loc, "break statement used with OpenMP for loop");
10271 return NULL_TREE;
10273 case 2:
10274 if (is_break)
10275 error ("break statement within %<#pragma simd%> loop body");
10276 else
10277 error ("continue statement within %<#pragma simd%> loop body");
10278 return NULL_TREE;
10280 default:
10281 gcc_unreachable ();
10284 if (skip)
10285 return NULL_TREE;
10287 if (!is_break)
10288 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10290 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10293 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10295 static void
10296 emit_side_effect_warnings (location_t loc, tree expr)
10298 if (expr == error_mark_node)
10300 else if (!TREE_SIDE_EFFECTS (expr))
10302 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10303 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10305 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10307 tree r = expr;
10308 location_t cloc = loc;
10309 while (TREE_CODE (r) == COMPOUND_EXPR)
10311 if (EXPR_HAS_LOCATION (r))
10312 cloc = EXPR_LOCATION (r);
10313 r = TREE_OPERAND (r, 1);
10315 if (!TREE_SIDE_EFFECTS (r)
10316 && !VOID_TYPE_P (TREE_TYPE (r))
10317 && !CONVERT_EXPR_P (r)
10318 && !TREE_NO_WARNING (r)
10319 && !TREE_NO_WARNING (expr))
10320 warning_at (cloc, OPT_Wunused_value,
10321 "right-hand operand of comma expression has no effect");
10323 else
10324 warn_if_unused_value (expr, loc);
10327 /* Process an expression as if it were a complete statement. Emit
10328 diagnostics, but do not call ADD_STMT. LOC is the location of the
10329 statement. */
10331 tree
10332 c_process_expr_stmt (location_t loc, tree expr)
10334 tree exprv;
10336 if (!expr)
10337 return NULL_TREE;
10339 expr = c_fully_fold (expr, false, NULL);
10341 if (warn_sequence_point)
10342 verify_sequence_points (expr);
10344 if (TREE_TYPE (expr) != error_mark_node
10345 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10346 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10347 error_at (loc, "expression statement has incomplete type");
10349 /* If we're not processing a statement expression, warn about unused values.
10350 Warnings for statement expressions will be emitted later, once we figure
10351 out which is the result. */
10352 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10353 && warn_unused_value)
10354 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10356 exprv = expr;
10357 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10358 exprv = TREE_OPERAND (exprv, 1);
10359 while (CONVERT_EXPR_P (exprv))
10360 exprv = TREE_OPERAND (exprv, 0);
10361 if (DECL_P (exprv)
10362 || handled_component_p (exprv)
10363 || TREE_CODE (exprv) == ADDR_EXPR)
10364 mark_exp_read (exprv);
10366 /* If the expression is not of a type to which we cannot assign a line
10367 number, wrap the thing in a no-op NOP_EXPR. */
10368 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10370 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10371 SET_EXPR_LOCATION (expr, loc);
10374 return expr;
10377 /* Emit an expression as a statement. LOC is the location of the
10378 expression. */
10380 tree
10381 c_finish_expr_stmt (location_t loc, tree expr)
10383 if (expr)
10384 return add_stmt (c_process_expr_stmt (loc, expr));
10385 else
10386 return NULL;
10389 /* Do the opposite and emit a statement as an expression. To begin,
10390 create a new binding level and return it. */
10392 tree
10393 c_begin_stmt_expr (void)
10395 tree ret;
10397 /* We must force a BLOCK for this level so that, if it is not expanded
10398 later, there is a way to turn off the entire subtree of blocks that
10399 are contained in it. */
10400 keep_next_level ();
10401 ret = c_begin_compound_stmt (true);
10403 c_bindings_start_stmt_expr (c_switch_stack == NULL
10404 ? NULL
10405 : c_switch_stack->bindings);
10407 /* Mark the current statement list as belonging to a statement list. */
10408 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10410 return ret;
10413 /* LOC is the location of the compound statement to which this body
10414 belongs. */
10416 tree
10417 c_finish_stmt_expr (location_t loc, tree body)
10419 tree last, type, tmp, val;
10420 tree *last_p;
10422 body = c_end_compound_stmt (loc, body, true);
10424 c_bindings_end_stmt_expr (c_switch_stack == NULL
10425 ? NULL
10426 : c_switch_stack->bindings);
10428 /* Locate the last statement in BODY. See c_end_compound_stmt
10429 about always returning a BIND_EXPR. */
10430 last_p = &BIND_EXPR_BODY (body);
10431 last = BIND_EXPR_BODY (body);
10433 continue_searching:
10434 if (TREE_CODE (last) == STATEMENT_LIST)
10436 tree_stmt_iterator i;
10438 /* This can happen with degenerate cases like ({ }). No value. */
10439 if (!TREE_SIDE_EFFECTS (last))
10440 return body;
10442 /* If we're supposed to generate side effects warnings, process
10443 all of the statements except the last. */
10444 if (warn_unused_value)
10446 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10448 location_t tloc;
10449 tree t = tsi_stmt (i);
10451 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10452 emit_side_effect_warnings (tloc, t);
10455 else
10456 i = tsi_last (last);
10457 last_p = tsi_stmt_ptr (i);
10458 last = *last_p;
10461 /* If the end of the list is exception related, then the list was split
10462 by a call to push_cleanup. Continue searching. */
10463 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10464 || TREE_CODE (last) == TRY_CATCH_EXPR)
10466 last_p = &TREE_OPERAND (last, 0);
10467 last = *last_p;
10468 goto continue_searching;
10471 if (last == error_mark_node)
10472 return last;
10474 /* In the case that the BIND_EXPR is not necessary, return the
10475 expression out from inside it. */
10476 if (last == BIND_EXPR_BODY (body)
10477 && BIND_EXPR_VARS (body) == NULL)
10479 /* Even if this looks constant, do not allow it in a constant
10480 expression. */
10481 last = c_wrap_maybe_const (last, true);
10482 /* Do not warn if the return value of a statement expression is
10483 unused. */
10484 TREE_NO_WARNING (last) = 1;
10485 return last;
10488 /* Extract the type of said expression. */
10489 type = TREE_TYPE (last);
10491 /* If we're not returning a value at all, then the BIND_EXPR that
10492 we already have is a fine expression to return. */
10493 if (!type || VOID_TYPE_P (type))
10494 return body;
10496 /* Now that we've located the expression containing the value, it seems
10497 silly to make voidify_wrapper_expr repeat the process. Create a
10498 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10499 tmp = create_tmp_var_raw (type);
10501 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10502 tree_expr_nonnegative_p giving up immediately. */
10503 val = last;
10504 if (TREE_CODE (val) == NOP_EXPR
10505 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10506 val = TREE_OPERAND (val, 0);
10508 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10509 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10512 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10513 SET_EXPR_LOCATION (t, loc);
10514 return t;
10518 /* Begin and end compound statements. This is as simple as pushing
10519 and popping new statement lists from the tree. */
10521 tree
10522 c_begin_compound_stmt (bool do_scope)
10524 tree stmt = push_stmt_list ();
10525 if (do_scope)
10526 push_scope ();
10527 return stmt;
10530 /* End a compound statement. STMT is the statement. LOC is the
10531 location of the compound statement-- this is usually the location
10532 of the opening brace. */
10534 tree
10535 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10537 tree block = NULL;
10539 if (do_scope)
10541 if (c_dialect_objc ())
10542 objc_clear_super_receiver ();
10543 block = pop_scope ();
10546 stmt = pop_stmt_list (stmt);
10547 stmt = c_build_bind_expr (loc, block, stmt);
10549 /* If this compound statement is nested immediately inside a statement
10550 expression, then force a BIND_EXPR to be created. Otherwise we'll
10551 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10552 STATEMENT_LISTs merge, and thus we can lose track of what statement
10553 was really last. */
10554 if (building_stmt_list_p ()
10555 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10556 && TREE_CODE (stmt) != BIND_EXPR)
10558 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10559 TREE_SIDE_EFFECTS (stmt) = 1;
10560 SET_EXPR_LOCATION (stmt, loc);
10563 return stmt;
10566 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10567 when the current scope is exited. EH_ONLY is true when this is not
10568 meant to apply to normal control flow transfer. */
10570 void
10571 push_cleanup (tree decl, tree cleanup, bool eh_only)
10573 enum tree_code code;
10574 tree stmt, list;
10575 bool stmt_expr;
10577 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10578 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10579 add_stmt (stmt);
10580 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10581 list = push_stmt_list ();
10582 TREE_OPERAND (stmt, 0) = list;
10583 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10586 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10587 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10589 static tree
10590 build_vec_cmp (tree_code code, tree type,
10591 tree arg0, tree arg1)
10593 tree zero_vec = build_zero_cst (type);
10594 tree minus_one_vec = build_minus_one_cst (type);
10595 tree cmp_type = build_same_sized_truth_vector_type (type);
10596 tree cmp = build2 (code, cmp_type, arg0, arg1);
10597 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10600 /* Build a binary-operation expression without default conversions.
10601 CODE is the kind of expression to build.
10602 LOCATION is the operator's location.
10603 This function differs from `build' in several ways:
10604 the data type of the result is computed and recorded in it,
10605 warnings are generated if arg data types are invalid,
10606 special handling for addition and subtraction of pointers is known,
10607 and some optimization is done (operations on narrow ints
10608 are done in the narrower type when that gives the same result).
10609 Constant folding is also done before the result is returned.
10611 Note that the operands will never have enumeral types, or function
10612 or array types, because either they will have the default conversions
10613 performed or they have both just been converted to some other type in which
10614 the arithmetic is to be done. */
10616 tree
10617 build_binary_op (location_t location, enum tree_code code,
10618 tree orig_op0, tree orig_op1, int convert_p)
10620 tree type0, type1, orig_type0, orig_type1;
10621 tree eptype;
10622 enum tree_code code0, code1;
10623 tree op0, op1;
10624 tree ret = error_mark_node;
10625 const char *invalid_op_diag;
10626 bool op0_int_operands, op1_int_operands;
10627 bool int_const, int_const_or_overflow, int_operands;
10629 /* Expression code to give to the expression when it is built.
10630 Normally this is CODE, which is what the caller asked for,
10631 but in some special cases we change it. */
10632 enum tree_code resultcode = code;
10634 /* Data type in which the computation is to be performed.
10635 In the simplest cases this is the common type of the arguments. */
10636 tree result_type = NULL;
10638 /* When the computation is in excess precision, the type of the
10639 final EXCESS_PRECISION_EXPR. */
10640 tree semantic_result_type = NULL;
10642 /* Nonzero means operands have already been type-converted
10643 in whatever way is necessary.
10644 Zero means they need to be converted to RESULT_TYPE. */
10645 int converted = 0;
10647 /* Nonzero means create the expression with this type, rather than
10648 RESULT_TYPE. */
10649 tree build_type = 0;
10651 /* Nonzero means after finally constructing the expression
10652 convert it to this type. */
10653 tree final_type = 0;
10655 /* Nonzero if this is an operation like MIN or MAX which can
10656 safely be computed in short if both args are promoted shorts.
10657 Also implies COMMON.
10658 -1 indicates a bitwise operation; this makes a difference
10659 in the exact conditions for when it is safe to do the operation
10660 in a narrower mode. */
10661 int shorten = 0;
10663 /* Nonzero if this is a comparison operation;
10664 if both args are promoted shorts, compare the original shorts.
10665 Also implies COMMON. */
10666 int short_compare = 0;
10668 /* Nonzero if this is a right-shift operation, which can be computed on the
10669 original short and then promoted if the operand is a promoted short. */
10670 int short_shift = 0;
10672 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10673 int common = 0;
10675 /* True means types are compatible as far as ObjC is concerned. */
10676 bool objc_ok;
10678 /* True means this is an arithmetic operation that may need excess
10679 precision. */
10680 bool may_need_excess_precision;
10682 /* True means this is a boolean operation that converts both its
10683 operands to truth-values. */
10684 bool boolean_op = false;
10686 /* Remember whether we're doing / or %. */
10687 bool doing_div_or_mod = false;
10689 /* Remember whether we're doing << or >>. */
10690 bool doing_shift = false;
10692 /* Tree holding instrumentation expression. */
10693 tree instrument_expr = NULL;
10695 if (location == UNKNOWN_LOCATION)
10696 location = input_location;
10698 op0 = orig_op0;
10699 op1 = orig_op1;
10701 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10702 if (op0_int_operands)
10703 op0 = remove_c_maybe_const_expr (op0);
10704 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10705 if (op1_int_operands)
10706 op1 = remove_c_maybe_const_expr (op1);
10707 int_operands = (op0_int_operands && op1_int_operands);
10708 if (int_operands)
10710 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10711 && TREE_CODE (orig_op1) == INTEGER_CST);
10712 int_const = (int_const_or_overflow
10713 && !TREE_OVERFLOW (orig_op0)
10714 && !TREE_OVERFLOW (orig_op1));
10716 else
10717 int_const = int_const_or_overflow = false;
10719 /* Do not apply default conversion in mixed vector/scalar expression. */
10720 if (convert_p
10721 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10723 op0 = default_conversion (op0);
10724 op1 = default_conversion (op1);
10727 /* When Cilk Plus is enabled and there are array notations inside op0, then
10728 we check to see if there are builtin array notation functions. If
10729 so, then we take on the type of the array notation inside it. */
10730 if (flag_cilkplus && contains_array_notation_expr (op0))
10731 orig_type0 = type0 = find_correct_array_notation_type (op0);
10732 else
10733 orig_type0 = type0 = TREE_TYPE (op0);
10735 if (flag_cilkplus && contains_array_notation_expr (op1))
10736 orig_type1 = type1 = find_correct_array_notation_type (op1);
10737 else
10738 orig_type1 = type1 = TREE_TYPE (op1);
10740 /* The expression codes of the data types of the arguments tell us
10741 whether the arguments are integers, floating, pointers, etc. */
10742 code0 = TREE_CODE (type0);
10743 code1 = TREE_CODE (type1);
10745 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10746 STRIP_TYPE_NOPS (op0);
10747 STRIP_TYPE_NOPS (op1);
10749 /* If an error was already reported for one of the arguments,
10750 avoid reporting another error. */
10752 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10753 return error_mark_node;
10755 if (code0 == POINTER_TYPE
10756 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10757 return error_mark_node;
10759 if (code1 == POINTER_TYPE
10760 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10761 return error_mark_node;
10763 if ((invalid_op_diag
10764 = targetm.invalid_binary_op (code, type0, type1)))
10766 error_at (location, invalid_op_diag);
10767 return error_mark_node;
10770 switch (code)
10772 case PLUS_EXPR:
10773 case MINUS_EXPR:
10774 case MULT_EXPR:
10775 case TRUNC_DIV_EXPR:
10776 case CEIL_DIV_EXPR:
10777 case FLOOR_DIV_EXPR:
10778 case ROUND_DIV_EXPR:
10779 case EXACT_DIV_EXPR:
10780 may_need_excess_precision = true;
10781 break;
10782 default:
10783 may_need_excess_precision = false;
10784 break;
10786 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10788 op0 = TREE_OPERAND (op0, 0);
10789 type0 = TREE_TYPE (op0);
10791 else if (may_need_excess_precision
10792 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10794 type0 = eptype;
10795 op0 = convert (eptype, op0);
10797 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10799 op1 = TREE_OPERAND (op1, 0);
10800 type1 = TREE_TYPE (op1);
10802 else if (may_need_excess_precision
10803 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10805 type1 = eptype;
10806 op1 = convert (eptype, op1);
10809 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10811 /* In case when one of the operands of the binary operation is
10812 a vector and another is a scalar -- convert scalar to vector. */
10813 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10815 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10816 true);
10818 switch (convert_flag)
10820 case stv_error:
10821 return error_mark_node;
10822 case stv_firstarg:
10824 bool maybe_const = true;
10825 tree sc;
10826 sc = c_fully_fold (op0, false, &maybe_const);
10827 sc = save_expr (sc);
10828 sc = convert (TREE_TYPE (type1), sc);
10829 op0 = build_vector_from_val (type1, sc);
10830 if (!maybe_const)
10831 op0 = c_wrap_maybe_const (op0, true);
10832 orig_type0 = type0 = TREE_TYPE (op0);
10833 code0 = TREE_CODE (type0);
10834 converted = 1;
10835 break;
10837 case stv_secondarg:
10839 bool maybe_const = true;
10840 tree sc;
10841 sc = c_fully_fold (op1, false, &maybe_const);
10842 sc = save_expr (sc);
10843 sc = convert (TREE_TYPE (type0), sc);
10844 op1 = build_vector_from_val (type0, sc);
10845 if (!maybe_const)
10846 op1 = c_wrap_maybe_const (op1, true);
10847 orig_type1 = type1 = TREE_TYPE (op1);
10848 code1 = TREE_CODE (type1);
10849 converted = 1;
10850 break;
10852 default:
10853 break;
10857 switch (code)
10859 case PLUS_EXPR:
10860 /* Handle the pointer + int case. */
10861 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10863 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10864 goto return_build_binary_op;
10866 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10868 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10869 goto return_build_binary_op;
10871 else
10872 common = 1;
10873 break;
10875 case MINUS_EXPR:
10876 /* Subtraction of two similar pointers.
10877 We must subtract them as integers, then divide by object size. */
10878 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10879 && comp_target_types (location, type0, type1))
10881 ret = pointer_diff (location, op0, op1);
10882 goto return_build_binary_op;
10884 /* Handle pointer minus int. Just like pointer plus int. */
10885 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10887 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10888 goto return_build_binary_op;
10890 else
10891 common = 1;
10892 break;
10894 case MULT_EXPR:
10895 common = 1;
10896 break;
10898 case TRUNC_DIV_EXPR:
10899 case CEIL_DIV_EXPR:
10900 case FLOOR_DIV_EXPR:
10901 case ROUND_DIV_EXPR:
10902 case EXACT_DIV_EXPR:
10903 doing_div_or_mod = true;
10904 warn_for_div_by_zero (location, op1);
10906 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10907 || code0 == FIXED_POINT_TYPE
10908 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10909 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10910 || code1 == FIXED_POINT_TYPE
10911 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10913 enum tree_code tcode0 = code0, tcode1 = code1;
10915 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10916 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10917 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10918 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10920 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10921 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10922 resultcode = RDIV_EXPR;
10923 else
10924 /* Although it would be tempting to shorten always here, that
10925 loses on some targets, since the modulo instruction is
10926 undefined if the quotient can't be represented in the
10927 computation mode. We shorten only if unsigned or if
10928 dividing by something we know != -1. */
10929 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10930 || (TREE_CODE (op1) == INTEGER_CST
10931 && !integer_all_onesp (op1)));
10932 common = 1;
10934 break;
10936 case BIT_AND_EXPR:
10937 case BIT_IOR_EXPR:
10938 case BIT_XOR_EXPR:
10939 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10940 shorten = -1;
10941 /* Allow vector types which are not floating point types. */
10942 else if (code0 == VECTOR_TYPE
10943 && code1 == VECTOR_TYPE
10944 && !VECTOR_FLOAT_TYPE_P (type0)
10945 && !VECTOR_FLOAT_TYPE_P (type1))
10946 common = 1;
10947 break;
10949 case TRUNC_MOD_EXPR:
10950 case FLOOR_MOD_EXPR:
10951 doing_div_or_mod = true;
10952 warn_for_div_by_zero (location, op1);
10954 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10955 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10956 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10957 common = 1;
10958 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10960 /* Although it would be tempting to shorten always here, that loses
10961 on some targets, since the modulo instruction is undefined if the
10962 quotient can't be represented in the computation mode. We shorten
10963 only if unsigned or if dividing by something we know != -1. */
10964 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10965 || (TREE_CODE (op1) == INTEGER_CST
10966 && !integer_all_onesp (op1)));
10967 common = 1;
10969 break;
10971 case TRUTH_ANDIF_EXPR:
10972 case TRUTH_ORIF_EXPR:
10973 case TRUTH_AND_EXPR:
10974 case TRUTH_OR_EXPR:
10975 case TRUTH_XOR_EXPR:
10976 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10977 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10978 || code0 == FIXED_POINT_TYPE)
10979 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10980 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10981 || code1 == FIXED_POINT_TYPE))
10983 /* Result of these operations is always an int,
10984 but that does not mean the operands should be
10985 converted to ints! */
10986 result_type = integer_type_node;
10987 if (op0_int_operands)
10989 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10990 op0 = remove_c_maybe_const_expr (op0);
10992 else
10993 op0 = c_objc_common_truthvalue_conversion (location, op0);
10994 if (op1_int_operands)
10996 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10997 op1 = remove_c_maybe_const_expr (op1);
10999 else
11000 op1 = c_objc_common_truthvalue_conversion (location, op1);
11001 converted = 1;
11002 boolean_op = true;
11004 if (code == TRUTH_ANDIF_EXPR)
11006 int_const_or_overflow = (int_operands
11007 && TREE_CODE (orig_op0) == INTEGER_CST
11008 && (op0 == truthvalue_false_node
11009 || TREE_CODE (orig_op1) == INTEGER_CST));
11010 int_const = (int_const_or_overflow
11011 && !TREE_OVERFLOW (orig_op0)
11012 && (op0 == truthvalue_false_node
11013 || !TREE_OVERFLOW (orig_op1)));
11015 else if (code == TRUTH_ORIF_EXPR)
11017 int_const_or_overflow = (int_operands
11018 && TREE_CODE (orig_op0) == INTEGER_CST
11019 && (op0 == truthvalue_true_node
11020 || TREE_CODE (orig_op1) == INTEGER_CST));
11021 int_const = (int_const_or_overflow
11022 && !TREE_OVERFLOW (orig_op0)
11023 && (op0 == truthvalue_true_node
11024 || !TREE_OVERFLOW (orig_op1)));
11026 break;
11028 /* Shift operations: result has same type as first operand;
11029 always convert second operand to int.
11030 Also set SHORT_SHIFT if shifting rightward. */
11032 case RSHIFT_EXPR:
11033 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11034 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11035 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11036 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11038 result_type = type0;
11039 converted = 1;
11041 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11042 || code0 == VECTOR_TYPE)
11043 && code1 == INTEGER_TYPE)
11045 doing_shift = true;
11046 if (TREE_CODE (op1) == INTEGER_CST)
11048 if (tree_int_cst_sgn (op1) < 0)
11050 int_const = false;
11051 if (c_inhibit_evaluation_warnings == 0)
11052 warning_at (location, OPT_Wshift_count_negative,
11053 "right shift count is negative");
11055 else if (code0 == VECTOR_TYPE)
11057 if (compare_tree_int (op1,
11058 TYPE_PRECISION (TREE_TYPE (type0)))
11059 >= 0)
11061 int_const = false;
11062 if (c_inhibit_evaluation_warnings == 0)
11063 warning_at (location, OPT_Wshift_count_overflow,
11064 "right shift count >= width of vector element");
11067 else
11069 if (!integer_zerop (op1))
11070 short_shift = 1;
11072 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11074 int_const = false;
11075 if (c_inhibit_evaluation_warnings == 0)
11076 warning_at (location, OPT_Wshift_count_overflow,
11077 "right shift count >= width of type");
11082 /* Use the type of the value to be shifted. */
11083 result_type = type0;
11084 /* Avoid converting op1 to result_type later. */
11085 converted = 1;
11087 break;
11089 case LSHIFT_EXPR:
11090 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11091 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11092 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11093 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11095 result_type = type0;
11096 converted = 1;
11098 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11099 || code0 == VECTOR_TYPE)
11100 && code1 == INTEGER_TYPE)
11102 doing_shift = true;
11103 if (TREE_CODE (op0) == INTEGER_CST
11104 && tree_int_cst_sgn (op0) < 0)
11106 /* Don't reject a left shift of a negative value in a context
11107 where a constant expression is needed in C90. */
11108 if (flag_isoc99)
11109 int_const = false;
11110 if (c_inhibit_evaluation_warnings == 0)
11111 warning_at (location, OPT_Wshift_negative_value,
11112 "left shift of negative value");
11114 if (TREE_CODE (op1) == INTEGER_CST)
11116 if (tree_int_cst_sgn (op1) < 0)
11118 int_const = false;
11119 if (c_inhibit_evaluation_warnings == 0)
11120 warning_at (location, OPT_Wshift_count_negative,
11121 "left shift count is negative");
11123 else if (code0 == VECTOR_TYPE)
11125 if (compare_tree_int (op1,
11126 TYPE_PRECISION (TREE_TYPE (type0)))
11127 >= 0)
11129 int_const = false;
11130 if (c_inhibit_evaluation_warnings == 0)
11131 warning_at (location, OPT_Wshift_count_overflow,
11132 "left shift count >= width of vector element");
11135 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11137 int_const = false;
11138 if (c_inhibit_evaluation_warnings == 0)
11139 warning_at (location, OPT_Wshift_count_overflow,
11140 "left shift count >= width of type");
11142 else if (TREE_CODE (op0) == INTEGER_CST
11143 && maybe_warn_shift_overflow (location, op0, op1)
11144 && flag_isoc99)
11145 int_const = false;
11148 /* Use the type of the value to be shifted. */
11149 result_type = type0;
11150 /* Avoid converting op1 to result_type later. */
11151 converted = 1;
11153 break;
11155 case EQ_EXPR:
11156 case NE_EXPR:
11157 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11159 tree intt;
11160 if (!vector_types_compatible_elements_p (type0, type1))
11162 error_at (location, "comparing vectors with different "
11163 "element types");
11164 return error_mark_node;
11167 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11169 error_at (location, "comparing vectors with different "
11170 "number of elements");
11171 return error_mark_node;
11174 /* It's not precisely specified how the usual arithmetic
11175 conversions apply to the vector types. Here, we use
11176 the unsigned type if one of the operands is signed and
11177 the other one is unsigned. */
11178 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11180 if (!TYPE_UNSIGNED (type0))
11181 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11182 else
11183 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11184 warning_at (location, OPT_Wsign_compare, "comparison between "
11185 "types %qT and %qT", type0, type1);
11188 /* Always construct signed integer vector type. */
11189 intt = c_common_type_for_size (GET_MODE_BITSIZE
11190 (TYPE_MODE (TREE_TYPE (type0))), 0);
11191 result_type = build_opaque_vector_type (intt,
11192 TYPE_VECTOR_SUBPARTS (type0));
11193 converted = 1;
11194 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11195 goto return_build_binary_op;
11197 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11198 warning_at (location,
11199 OPT_Wfloat_equal,
11200 "comparing floating point with == or != is unsafe");
11201 /* Result of comparison is always int,
11202 but don't convert the args to int! */
11203 build_type = integer_type_node;
11204 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11205 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11206 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11207 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11208 short_compare = 1;
11209 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11211 if (TREE_CODE (op0) == ADDR_EXPR
11212 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11213 && !from_macro_expansion_at (location))
11215 if (code == EQ_EXPR)
11216 warning_at (location,
11217 OPT_Waddress,
11218 "the comparison will always evaluate as %<false%> "
11219 "for the address of %qD will never be NULL",
11220 TREE_OPERAND (op0, 0));
11221 else
11222 warning_at (location,
11223 OPT_Waddress,
11224 "the comparison will always evaluate as %<true%> "
11225 "for the address of %qD will never be NULL",
11226 TREE_OPERAND (op0, 0));
11228 result_type = type0;
11230 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11232 if (TREE_CODE (op1) == ADDR_EXPR
11233 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11234 && !from_macro_expansion_at (location))
11236 if (code == EQ_EXPR)
11237 warning_at (location,
11238 OPT_Waddress,
11239 "the comparison will always evaluate as %<false%> "
11240 "for the address of %qD will never be NULL",
11241 TREE_OPERAND (op1, 0));
11242 else
11243 warning_at (location,
11244 OPT_Waddress,
11245 "the comparison will always evaluate as %<true%> "
11246 "for the address of %qD will never be NULL",
11247 TREE_OPERAND (op1, 0));
11249 result_type = type1;
11251 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11253 tree tt0 = TREE_TYPE (type0);
11254 tree tt1 = TREE_TYPE (type1);
11255 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11256 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11257 addr_space_t as_common = ADDR_SPACE_GENERIC;
11259 /* Anything compares with void *. void * compares with anything.
11260 Otherwise, the targets must be compatible
11261 and both must be object or both incomplete. */
11262 if (comp_target_types (location, type0, type1))
11263 result_type = common_pointer_type (type0, type1);
11264 else if (!addr_space_superset (as0, as1, &as_common))
11266 error_at (location, "comparison of pointers to "
11267 "disjoint address spaces");
11268 return error_mark_node;
11270 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11272 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11273 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11274 "comparison of %<void *%> with function pointer");
11276 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11278 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11279 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11280 "comparison of %<void *%> with function pointer");
11282 else
11283 /* Avoid warning about the volatile ObjC EH puts on decls. */
11284 if (!objc_ok)
11285 pedwarn (location, 0,
11286 "comparison of distinct pointer types lacks a cast");
11288 if (result_type == NULL_TREE)
11290 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11291 result_type = build_pointer_type
11292 (build_qualified_type (void_type_node, qual));
11295 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11297 result_type = type0;
11298 pedwarn (location, 0, "comparison between pointer and integer");
11300 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11302 result_type = type1;
11303 pedwarn (location, 0, "comparison between pointer and integer");
11305 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11306 || truth_value_p (TREE_CODE (orig_op0)))
11307 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11308 || truth_value_p (TREE_CODE (orig_op1))))
11309 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11310 break;
11312 case LE_EXPR:
11313 case GE_EXPR:
11314 case LT_EXPR:
11315 case GT_EXPR:
11316 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11318 tree intt;
11319 if (!vector_types_compatible_elements_p (type0, type1))
11321 error_at (location, "comparing vectors with different "
11322 "element types");
11323 return error_mark_node;
11326 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11328 error_at (location, "comparing vectors with different "
11329 "number of elements");
11330 return error_mark_node;
11333 /* It's not precisely specified how the usual arithmetic
11334 conversions apply to the vector types. Here, we use
11335 the unsigned type if one of the operands is signed and
11336 the other one is unsigned. */
11337 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11339 if (!TYPE_UNSIGNED (type0))
11340 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11341 else
11342 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11343 warning_at (location, OPT_Wsign_compare, "comparison between "
11344 "types %qT and %qT", type0, type1);
11347 /* Always construct signed integer vector type. */
11348 intt = c_common_type_for_size (GET_MODE_BITSIZE
11349 (TYPE_MODE (TREE_TYPE (type0))), 0);
11350 result_type = build_opaque_vector_type (intt,
11351 TYPE_VECTOR_SUBPARTS (type0));
11352 converted = 1;
11353 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11354 goto return_build_binary_op;
11356 build_type = integer_type_node;
11357 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11358 || code0 == FIXED_POINT_TYPE)
11359 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11360 || code1 == FIXED_POINT_TYPE))
11361 short_compare = 1;
11362 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11364 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11365 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11366 addr_space_t as_common;
11368 if (comp_target_types (location, type0, type1))
11370 result_type = common_pointer_type (type0, type1);
11371 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11372 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11373 pedwarn (location, 0,
11374 "comparison of complete and incomplete pointers");
11375 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11376 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11377 "ordered comparisons of pointers to functions");
11378 else if (null_pointer_constant_p (orig_op0)
11379 || null_pointer_constant_p (orig_op1))
11380 warning_at (location, OPT_Wextra,
11381 "ordered comparison of pointer with null pointer");
11384 else if (!addr_space_superset (as0, as1, &as_common))
11386 error_at (location, "comparison of pointers to "
11387 "disjoint address spaces");
11388 return error_mark_node;
11390 else
11392 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11393 result_type = build_pointer_type
11394 (build_qualified_type (void_type_node, qual));
11395 pedwarn (location, 0,
11396 "comparison of distinct pointer types lacks a cast");
11399 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11401 result_type = type0;
11402 if (pedantic)
11403 pedwarn (location, OPT_Wpedantic,
11404 "ordered comparison of pointer with integer zero");
11405 else if (extra_warnings)
11406 warning_at (location, OPT_Wextra,
11407 "ordered comparison of pointer with integer zero");
11409 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11411 result_type = type1;
11412 if (pedantic)
11413 pedwarn (location, OPT_Wpedantic,
11414 "ordered comparison of pointer with integer zero");
11415 else if (extra_warnings)
11416 warning_at (location, OPT_Wextra,
11417 "ordered comparison of pointer with integer zero");
11419 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11421 result_type = type0;
11422 pedwarn (location, 0, "comparison between pointer and integer");
11424 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11426 result_type = type1;
11427 pedwarn (location, 0, "comparison between pointer and integer");
11429 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11430 || truth_value_p (TREE_CODE (orig_op0)))
11431 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11432 || truth_value_p (TREE_CODE (orig_op1))))
11433 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11434 break;
11436 default:
11437 gcc_unreachable ();
11440 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11441 return error_mark_node;
11443 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11444 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11445 || !vector_types_compatible_elements_p (type0, type1)))
11447 gcc_rich_location richloc (location);
11448 richloc.maybe_add_expr (orig_op0);
11449 richloc.maybe_add_expr (orig_op1);
11450 binary_op_error (&richloc, code, type0, type1);
11451 return error_mark_node;
11454 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11455 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11457 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11458 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11460 bool first_complex = (code0 == COMPLEX_TYPE);
11461 bool second_complex = (code1 == COMPLEX_TYPE);
11462 int none_complex = (!first_complex && !second_complex);
11464 if (shorten || common || short_compare)
11466 result_type = c_common_type (type0, type1);
11467 do_warn_double_promotion (result_type, type0, type1,
11468 "implicit conversion from %qT to %qT "
11469 "to match other operand of binary "
11470 "expression",
11471 location);
11472 if (result_type == error_mark_node)
11473 return error_mark_node;
11476 if (first_complex != second_complex
11477 && (code == PLUS_EXPR
11478 || code == MINUS_EXPR
11479 || code == MULT_EXPR
11480 || (code == TRUNC_DIV_EXPR && first_complex))
11481 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11482 && flag_signed_zeros)
11484 /* An operation on mixed real/complex operands must be
11485 handled specially, but the language-independent code can
11486 more easily optimize the plain complex arithmetic if
11487 -fno-signed-zeros. */
11488 tree real_type = TREE_TYPE (result_type);
11489 tree real, imag;
11490 if (type0 != orig_type0 || type1 != orig_type1)
11492 gcc_assert (may_need_excess_precision && common);
11493 semantic_result_type = c_common_type (orig_type0, orig_type1);
11495 if (first_complex)
11497 if (TREE_TYPE (op0) != result_type)
11498 op0 = convert_and_check (location, result_type, op0);
11499 if (TREE_TYPE (op1) != real_type)
11500 op1 = convert_and_check (location, real_type, op1);
11502 else
11504 if (TREE_TYPE (op0) != real_type)
11505 op0 = convert_and_check (location, real_type, op0);
11506 if (TREE_TYPE (op1) != result_type)
11507 op1 = convert_and_check (location, result_type, op1);
11509 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11510 return error_mark_node;
11511 if (first_complex)
11513 op0 = c_save_expr (op0);
11514 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11515 op0, 1);
11516 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11517 op0, 1);
11518 switch (code)
11520 case MULT_EXPR:
11521 case TRUNC_DIV_EXPR:
11522 op1 = c_save_expr (op1);
11523 imag = build2 (resultcode, real_type, imag, op1);
11524 /* Fall through. */
11525 case PLUS_EXPR:
11526 case MINUS_EXPR:
11527 real = build2 (resultcode, real_type, real, op1);
11528 break;
11529 default:
11530 gcc_unreachable();
11533 else
11535 op1 = c_save_expr (op1);
11536 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11537 op1, 1);
11538 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11539 op1, 1);
11540 switch (code)
11542 case MULT_EXPR:
11543 op0 = c_save_expr (op0);
11544 imag = build2 (resultcode, real_type, op0, imag);
11545 /* Fall through. */
11546 case PLUS_EXPR:
11547 real = build2 (resultcode, real_type, op0, real);
11548 break;
11549 case MINUS_EXPR:
11550 real = build2 (resultcode, real_type, op0, real);
11551 imag = build1 (NEGATE_EXPR, real_type, imag);
11552 break;
11553 default:
11554 gcc_unreachable();
11557 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11558 goto return_build_binary_op;
11561 /* For certain operations (which identify themselves by shorten != 0)
11562 if both args were extended from the same smaller type,
11563 do the arithmetic in that type and then extend.
11565 shorten !=0 and !=1 indicates a bitwise operation.
11566 For them, this optimization is safe only if
11567 both args are zero-extended or both are sign-extended.
11568 Otherwise, we might change the result.
11569 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11570 but calculated in (unsigned short) it would be (unsigned short)-1. */
11572 if (shorten && none_complex)
11574 final_type = result_type;
11575 result_type = shorten_binary_op (result_type, op0, op1,
11576 shorten == -1);
11579 /* Shifts can be shortened if shifting right. */
11581 if (short_shift)
11583 int unsigned_arg;
11584 tree arg0 = get_narrower (op0, &unsigned_arg);
11586 final_type = result_type;
11588 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11589 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11591 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11592 && tree_int_cst_sgn (op1) > 0
11593 /* We can shorten only if the shift count is less than the
11594 number of bits in the smaller type size. */
11595 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11596 /* We cannot drop an unsigned shift after sign-extension. */
11597 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11599 /* Do an unsigned shift if the operand was zero-extended. */
11600 result_type
11601 = c_common_signed_or_unsigned_type (unsigned_arg,
11602 TREE_TYPE (arg0));
11603 /* Convert value-to-be-shifted to that type. */
11604 if (TREE_TYPE (op0) != result_type)
11605 op0 = convert (result_type, op0);
11606 converted = 1;
11610 /* Comparison operations are shortened too but differently.
11611 They identify themselves by setting short_compare = 1. */
11613 if (short_compare)
11615 /* Don't write &op0, etc., because that would prevent op0
11616 from being kept in a register.
11617 Instead, make copies of the our local variables and
11618 pass the copies by reference, then copy them back afterward. */
11619 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11620 enum tree_code xresultcode = resultcode;
11621 tree val
11622 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11623 &xresultcode);
11625 if (val != 0)
11627 ret = val;
11628 goto return_build_binary_op;
11631 op0 = xop0, op1 = xop1;
11632 converted = 1;
11633 resultcode = xresultcode;
11635 if (c_inhibit_evaluation_warnings == 0)
11637 bool op0_maybe_const = true;
11638 bool op1_maybe_const = true;
11639 tree orig_op0_folded, orig_op1_folded;
11641 if (in_late_binary_op)
11643 orig_op0_folded = orig_op0;
11644 orig_op1_folded = orig_op1;
11646 else
11648 /* Fold for the sake of possible warnings, as in
11649 build_conditional_expr. This requires the
11650 "original" values to be folded, not just op0 and
11651 op1. */
11652 c_inhibit_evaluation_warnings++;
11653 op0 = c_fully_fold (op0, require_constant_value,
11654 &op0_maybe_const);
11655 op1 = c_fully_fold (op1, require_constant_value,
11656 &op1_maybe_const);
11657 c_inhibit_evaluation_warnings--;
11658 orig_op0_folded = c_fully_fold (orig_op0,
11659 require_constant_value,
11660 NULL);
11661 orig_op1_folded = c_fully_fold (orig_op1,
11662 require_constant_value,
11663 NULL);
11666 if (warn_sign_compare)
11667 warn_for_sign_compare (location, orig_op0_folded,
11668 orig_op1_folded, op0, op1,
11669 result_type, resultcode);
11670 if (!in_late_binary_op && !int_operands)
11672 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11673 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11674 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11675 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11681 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11682 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11683 Then the expression will be built.
11684 It will be given type FINAL_TYPE if that is nonzero;
11685 otherwise, it will be given type RESULT_TYPE. */
11687 if (!result_type)
11689 gcc_rich_location richloc (location);
11690 richloc.maybe_add_expr (orig_op0);
11691 richloc.maybe_add_expr (orig_op1);
11692 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11693 return error_mark_node;
11696 if (build_type == NULL_TREE)
11698 build_type = result_type;
11699 if ((type0 != orig_type0 || type1 != orig_type1)
11700 && !boolean_op)
11702 gcc_assert (may_need_excess_precision && common);
11703 semantic_result_type = c_common_type (orig_type0, orig_type1);
11707 if (!converted)
11709 op0 = ep_convert_and_check (location, result_type, op0,
11710 semantic_result_type);
11711 op1 = ep_convert_and_check (location, result_type, op1,
11712 semantic_result_type);
11714 /* This can happen if one operand has a vector type, and the other
11715 has a different type. */
11716 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11717 return error_mark_node;
11720 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11721 | SANITIZE_FLOAT_DIVIDE))
11722 && do_ubsan_in_current_function ()
11723 && (doing_div_or_mod || doing_shift)
11724 && !require_constant_value)
11726 /* OP0 and/or OP1 might have side-effects. */
11727 op0 = c_save_expr (op0);
11728 op1 = c_save_expr (op1);
11729 op0 = c_fully_fold (op0, false, NULL);
11730 op1 = c_fully_fold (op1, false, NULL);
11731 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11732 | SANITIZE_FLOAT_DIVIDE)))
11733 instrument_expr = ubsan_instrument_division (location, op0, op1);
11734 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11735 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11738 /* Treat expressions in initializers specially as they can't trap. */
11739 if (int_const_or_overflow)
11740 ret = (require_constant_value
11741 ? fold_build2_initializer_loc (location, resultcode, build_type,
11742 op0, op1)
11743 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11744 else
11745 ret = build2 (resultcode, build_type, op0, op1);
11746 if (final_type != 0)
11747 ret = convert (final_type, ret);
11749 return_build_binary_op:
11750 gcc_assert (ret != error_mark_node);
11751 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11752 ret = (int_operands
11753 ? note_integer_operands (ret)
11754 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11755 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11756 && !in_late_binary_op)
11757 ret = note_integer_operands (ret);
11758 if (semantic_result_type)
11759 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11760 protected_set_expr_location (ret, location);
11762 if (instrument_expr != NULL)
11763 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11764 instrument_expr, ret);
11766 return ret;
11770 /* Convert EXPR to be a truth-value, validating its type for this
11771 purpose. LOCATION is the source location for the expression. */
11773 tree
11774 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11776 bool int_const, int_operands;
11778 switch (TREE_CODE (TREE_TYPE (expr)))
11780 case ARRAY_TYPE:
11781 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11782 return error_mark_node;
11784 case RECORD_TYPE:
11785 error_at (location, "used struct type value where scalar is required");
11786 return error_mark_node;
11788 case UNION_TYPE:
11789 error_at (location, "used union type value where scalar is required");
11790 return error_mark_node;
11792 case VOID_TYPE:
11793 error_at (location, "void value not ignored as it ought to be");
11794 return error_mark_node;
11796 case POINTER_TYPE:
11797 if (reject_gcc_builtin (expr))
11798 return error_mark_node;
11799 break;
11801 case FUNCTION_TYPE:
11802 gcc_unreachable ();
11804 case VECTOR_TYPE:
11805 error_at (location, "used vector type where scalar is required");
11806 return error_mark_node;
11808 default:
11809 break;
11812 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11813 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11814 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11816 expr = remove_c_maybe_const_expr (expr);
11817 expr = build2 (NE_EXPR, integer_type_node, expr,
11818 convert (TREE_TYPE (expr), integer_zero_node));
11819 expr = note_integer_operands (expr);
11821 else
11822 /* ??? Should we also give an error for vectors rather than leaving
11823 those to give errors later? */
11824 expr = c_common_truthvalue_conversion (location, expr);
11826 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11828 if (TREE_OVERFLOW (expr))
11829 return expr;
11830 else
11831 return note_integer_operands (expr);
11833 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11834 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11835 return expr;
11839 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11840 required. */
11842 tree
11843 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11845 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11847 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11848 /* Executing a compound literal inside a function reinitializes
11849 it. */
11850 if (!TREE_STATIC (decl))
11851 *se = true;
11852 return decl;
11854 else
11855 return expr;
11858 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11859 statement. LOC is the location of the construct. */
11861 tree
11862 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11863 tree clauses)
11865 body = c_end_compound_stmt (loc, body, true);
11867 tree stmt = make_node (code);
11868 TREE_TYPE (stmt) = void_type_node;
11869 OMP_BODY (stmt) = body;
11870 OMP_CLAUSES (stmt) = clauses;
11871 SET_EXPR_LOCATION (stmt, loc);
11873 return add_stmt (stmt);
11876 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11877 statement. LOC is the location of the OACC_DATA. */
11879 tree
11880 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11882 tree stmt;
11884 block = c_end_compound_stmt (loc, block, true);
11886 stmt = make_node (OACC_DATA);
11887 TREE_TYPE (stmt) = void_type_node;
11888 OACC_DATA_CLAUSES (stmt) = clauses;
11889 OACC_DATA_BODY (stmt) = block;
11890 SET_EXPR_LOCATION (stmt, loc);
11892 return add_stmt (stmt);
11895 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11896 statement. LOC is the location of the OACC_HOST_DATA. */
11898 tree
11899 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11901 tree stmt;
11903 block = c_end_compound_stmt (loc, block, true);
11905 stmt = make_node (OACC_HOST_DATA);
11906 TREE_TYPE (stmt) = void_type_node;
11907 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11908 OACC_HOST_DATA_BODY (stmt) = block;
11909 SET_EXPR_LOCATION (stmt, loc);
11911 return add_stmt (stmt);
11914 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11916 tree
11917 c_begin_omp_parallel (void)
11919 tree block;
11921 keep_next_level ();
11922 block = c_begin_compound_stmt (true);
11924 return block;
11927 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11928 statement. LOC is the location of the OMP_PARALLEL. */
11930 tree
11931 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11933 tree stmt;
11935 block = c_end_compound_stmt (loc, block, true);
11937 stmt = make_node (OMP_PARALLEL);
11938 TREE_TYPE (stmt) = void_type_node;
11939 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11940 OMP_PARALLEL_BODY (stmt) = block;
11941 SET_EXPR_LOCATION (stmt, loc);
11943 return add_stmt (stmt);
11946 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11948 tree
11949 c_begin_omp_task (void)
11951 tree block;
11953 keep_next_level ();
11954 block = c_begin_compound_stmt (true);
11956 return block;
11959 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11960 statement. LOC is the location of the #pragma. */
11962 tree
11963 c_finish_omp_task (location_t loc, tree clauses, tree block)
11965 tree stmt;
11967 block = c_end_compound_stmt (loc, block, true);
11969 stmt = make_node (OMP_TASK);
11970 TREE_TYPE (stmt) = void_type_node;
11971 OMP_TASK_CLAUSES (stmt) = clauses;
11972 OMP_TASK_BODY (stmt) = block;
11973 SET_EXPR_LOCATION (stmt, loc);
11975 return add_stmt (stmt);
11978 /* Generate GOMP_cancel call for #pragma omp cancel. */
11980 void
11981 c_finish_omp_cancel (location_t loc, tree clauses)
11983 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11984 int mask = 0;
11985 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11986 mask = 1;
11987 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11988 mask = 2;
11989 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11990 mask = 4;
11991 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11992 mask = 8;
11993 else
11995 error_at (loc, "%<#pragma omp cancel%> must specify one of "
11996 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11997 "clauses");
11998 return;
12000 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
12001 if (ifc != NULL_TREE)
12003 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12004 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12005 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12006 build_zero_cst (type));
12008 else
12009 ifc = boolean_true_node;
12010 tree stmt = build_call_expr_loc (loc, fn, 2,
12011 build_int_cst (integer_type_node, mask),
12012 ifc);
12013 add_stmt (stmt);
12016 /* Generate GOMP_cancellation_point call for
12017 #pragma omp cancellation point. */
12019 void
12020 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12022 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12023 int mask = 0;
12024 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
12025 mask = 1;
12026 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
12027 mask = 2;
12028 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
12029 mask = 4;
12030 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
12031 mask = 8;
12032 else
12034 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12035 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12036 "clauses");
12037 return;
12039 tree stmt = build_call_expr_loc (loc, fn, 1,
12040 build_int_cst (integer_type_node, mask));
12041 add_stmt (stmt);
12044 /* Helper function for handle_omp_array_sections. Called recursively
12045 to handle multiple array-section-subscripts. C is the clause,
12046 T current expression (initially OMP_CLAUSE_DECL), which is either
12047 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12048 expression if specified, TREE_VALUE length expression if specified,
12049 TREE_CHAIN is what it has been specified after, or some decl.
12050 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12051 set to true if any of the array-section-subscript could have length
12052 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12053 first array-section-subscript which is known not to have length
12054 of one. Given say:
12055 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12056 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12057 all are or may have length of 1, array-section-subscript [:2] is the
12058 first one known not to have length 1. For array-section-subscript
12059 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12060 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12061 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12062 case though, as some lengths could be zero. */
12064 static tree
12065 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12066 bool &maybe_zero_len, unsigned int &first_non_one,
12067 enum c_omp_region_type ort)
12069 tree ret, low_bound, length, type;
12070 if (TREE_CODE (t) != TREE_LIST)
12072 if (error_operand_p (t))
12073 return error_mark_node;
12074 ret = t;
12075 if (TREE_CODE (t) == COMPONENT_REF
12076 && ort == C_ORT_OMP
12077 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12078 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12079 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12081 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12083 error_at (OMP_CLAUSE_LOCATION (c),
12084 "bit-field %qE in %qs clause",
12085 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12086 return error_mark_node;
12088 while (TREE_CODE (t) == COMPONENT_REF)
12090 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12092 error_at (OMP_CLAUSE_LOCATION (c),
12093 "%qE is a member of a union", t);
12094 return error_mark_node;
12096 t = TREE_OPERAND (t, 0);
12099 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12101 if (DECL_P (t))
12102 error_at (OMP_CLAUSE_LOCATION (c),
12103 "%qD is not a variable in %qs clause", t,
12104 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12105 else
12106 error_at (OMP_CLAUSE_LOCATION (c),
12107 "%qE is not a variable in %qs clause", t,
12108 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12109 return error_mark_node;
12111 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12112 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12114 error_at (OMP_CLAUSE_LOCATION (c),
12115 "%qD is threadprivate variable in %qs clause", t,
12116 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12117 return error_mark_node;
12119 return ret;
12122 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12123 maybe_zero_len, first_non_one, ort);
12124 if (ret == error_mark_node || ret == NULL_TREE)
12125 return ret;
12127 type = TREE_TYPE (ret);
12128 low_bound = TREE_PURPOSE (t);
12129 length = TREE_VALUE (t);
12131 if (low_bound == error_mark_node || length == error_mark_node)
12132 return error_mark_node;
12134 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12136 error_at (OMP_CLAUSE_LOCATION (c),
12137 "low bound %qE of array section does not have integral type",
12138 low_bound);
12139 return error_mark_node;
12141 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12143 error_at (OMP_CLAUSE_LOCATION (c),
12144 "length %qE of array section does not have integral type",
12145 length);
12146 return error_mark_node;
12148 if (low_bound
12149 && TREE_CODE (low_bound) == INTEGER_CST
12150 && TYPE_PRECISION (TREE_TYPE (low_bound))
12151 > TYPE_PRECISION (sizetype))
12152 low_bound = fold_convert (sizetype, low_bound);
12153 if (length
12154 && TREE_CODE (length) == INTEGER_CST
12155 && TYPE_PRECISION (TREE_TYPE (length))
12156 > TYPE_PRECISION (sizetype))
12157 length = fold_convert (sizetype, length);
12158 if (low_bound == NULL_TREE)
12159 low_bound = integer_zero_node;
12161 if (length != NULL_TREE)
12163 if (!integer_nonzerop (length))
12165 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12166 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12168 if (integer_zerop (length))
12170 error_at (OMP_CLAUSE_LOCATION (c),
12171 "zero length array section in %qs clause",
12172 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12173 return error_mark_node;
12176 else
12177 maybe_zero_len = true;
12179 if (first_non_one == types.length ()
12180 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12181 first_non_one++;
12183 if (TREE_CODE (type) == ARRAY_TYPE)
12185 if (length == NULL_TREE
12186 && (TYPE_DOMAIN (type) == NULL_TREE
12187 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12189 error_at (OMP_CLAUSE_LOCATION (c),
12190 "for unknown bound array type length expression must "
12191 "be specified");
12192 return error_mark_node;
12194 if (TREE_CODE (low_bound) == INTEGER_CST
12195 && tree_int_cst_sgn (low_bound) == -1)
12197 error_at (OMP_CLAUSE_LOCATION (c),
12198 "negative low bound in array section in %qs clause",
12199 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12200 return error_mark_node;
12202 if (length != NULL_TREE
12203 && TREE_CODE (length) == INTEGER_CST
12204 && tree_int_cst_sgn (length) == -1)
12206 error_at (OMP_CLAUSE_LOCATION (c),
12207 "negative length in array section in %qs clause",
12208 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12209 return error_mark_node;
12211 if (TYPE_DOMAIN (type)
12212 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12213 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12214 == INTEGER_CST)
12216 tree size = size_binop (PLUS_EXPR,
12217 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12218 size_one_node);
12219 if (TREE_CODE (low_bound) == INTEGER_CST)
12221 if (tree_int_cst_lt (size, low_bound))
12223 error_at (OMP_CLAUSE_LOCATION (c),
12224 "low bound %qE above array section size "
12225 "in %qs clause", low_bound,
12226 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12227 return error_mark_node;
12229 if (tree_int_cst_equal (size, low_bound))
12231 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12232 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12234 error_at (OMP_CLAUSE_LOCATION (c),
12235 "zero length array section in %qs clause",
12236 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12237 return error_mark_node;
12239 maybe_zero_len = true;
12241 else if (length == NULL_TREE
12242 && first_non_one == types.length ()
12243 && tree_int_cst_equal
12244 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12245 low_bound))
12246 first_non_one++;
12248 else if (length == NULL_TREE)
12250 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12251 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12252 maybe_zero_len = true;
12253 if (first_non_one == types.length ())
12254 first_non_one++;
12256 if (length && TREE_CODE (length) == INTEGER_CST)
12258 if (tree_int_cst_lt (size, length))
12260 error_at (OMP_CLAUSE_LOCATION (c),
12261 "length %qE above array section size "
12262 "in %qs clause", length,
12263 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12264 return error_mark_node;
12266 if (TREE_CODE (low_bound) == INTEGER_CST)
12268 tree lbpluslen
12269 = size_binop (PLUS_EXPR,
12270 fold_convert (sizetype, low_bound),
12271 fold_convert (sizetype, length));
12272 if (TREE_CODE (lbpluslen) == INTEGER_CST
12273 && tree_int_cst_lt (size, lbpluslen))
12275 error_at (OMP_CLAUSE_LOCATION (c),
12276 "high bound %qE above array section size "
12277 "in %qs clause", lbpluslen,
12278 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12279 return error_mark_node;
12284 else if (length == NULL_TREE)
12286 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12287 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12288 maybe_zero_len = true;
12289 if (first_non_one == types.length ())
12290 first_non_one++;
12293 /* For [lb:] we will need to evaluate lb more than once. */
12294 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12296 tree lb = c_save_expr (low_bound);
12297 if (lb != low_bound)
12299 TREE_PURPOSE (t) = lb;
12300 low_bound = lb;
12304 else if (TREE_CODE (type) == POINTER_TYPE)
12306 if (length == NULL_TREE)
12308 error_at (OMP_CLAUSE_LOCATION (c),
12309 "for pointer type length expression must be specified");
12310 return error_mark_node;
12312 if (length != NULL_TREE
12313 && TREE_CODE (length) == INTEGER_CST
12314 && tree_int_cst_sgn (length) == -1)
12316 error_at (OMP_CLAUSE_LOCATION (c),
12317 "negative length in array section in %qs clause",
12318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12319 return error_mark_node;
12321 /* If there is a pointer type anywhere but in the very first
12322 array-section-subscript, the array section can't be contiguous. */
12323 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12324 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12326 error_at (OMP_CLAUSE_LOCATION (c),
12327 "array section is not contiguous in %qs clause",
12328 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12329 return error_mark_node;
12332 else
12334 error_at (OMP_CLAUSE_LOCATION (c),
12335 "%qE does not have pointer or array type", ret);
12336 return error_mark_node;
12338 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12339 types.safe_push (TREE_TYPE (ret));
12340 /* We will need to evaluate lb more than once. */
12341 tree lb = c_save_expr (low_bound);
12342 if (lb != low_bound)
12344 TREE_PURPOSE (t) = lb;
12345 low_bound = lb;
12347 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12348 return ret;
12351 /* Handle array sections for clause C. */
12353 static bool
12354 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12356 bool maybe_zero_len = false;
12357 unsigned int first_non_one = 0;
12358 auto_vec<tree, 10> types;
12359 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12360 maybe_zero_len, first_non_one,
12361 ort);
12362 if (first == error_mark_node)
12363 return true;
12364 if (first == NULL_TREE)
12365 return false;
12366 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12368 tree t = OMP_CLAUSE_DECL (c);
12369 tree tem = NULL_TREE;
12370 /* Need to evaluate side effects in the length expressions
12371 if any. */
12372 while (TREE_CODE (t) == TREE_LIST)
12374 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12376 if (tem == NULL_TREE)
12377 tem = TREE_VALUE (t);
12378 else
12379 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12380 TREE_VALUE (t), tem);
12382 t = TREE_CHAIN (t);
12384 if (tem)
12385 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12386 first = c_fully_fold (first, false, NULL);
12387 OMP_CLAUSE_DECL (c) = first;
12389 else
12391 unsigned int num = types.length (), i;
12392 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12393 tree condition = NULL_TREE;
12395 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12396 maybe_zero_len = true;
12398 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12399 t = TREE_CHAIN (t))
12401 tree low_bound = TREE_PURPOSE (t);
12402 tree length = TREE_VALUE (t);
12404 i--;
12405 if (low_bound
12406 && TREE_CODE (low_bound) == INTEGER_CST
12407 && TYPE_PRECISION (TREE_TYPE (low_bound))
12408 > TYPE_PRECISION (sizetype))
12409 low_bound = fold_convert (sizetype, low_bound);
12410 if (length
12411 && TREE_CODE (length) == INTEGER_CST
12412 && TYPE_PRECISION (TREE_TYPE (length))
12413 > TYPE_PRECISION (sizetype))
12414 length = fold_convert (sizetype, length);
12415 if (low_bound == NULL_TREE)
12416 low_bound = integer_zero_node;
12417 if (!maybe_zero_len && i > first_non_one)
12419 if (integer_nonzerop (low_bound))
12420 goto do_warn_noncontiguous;
12421 if (length != NULL_TREE
12422 && TREE_CODE (length) == INTEGER_CST
12423 && TYPE_DOMAIN (types[i])
12424 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12425 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12426 == INTEGER_CST)
12428 tree size;
12429 size = size_binop (PLUS_EXPR,
12430 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12431 size_one_node);
12432 if (!tree_int_cst_equal (length, size))
12434 do_warn_noncontiguous:
12435 error_at (OMP_CLAUSE_LOCATION (c),
12436 "array section is not contiguous in %qs "
12437 "clause",
12438 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12439 return true;
12442 if (length != NULL_TREE
12443 && TREE_SIDE_EFFECTS (length))
12445 if (side_effects == NULL_TREE)
12446 side_effects = length;
12447 else
12448 side_effects = build2 (COMPOUND_EXPR,
12449 TREE_TYPE (side_effects),
12450 length, side_effects);
12453 else
12455 tree l;
12457 if (i > first_non_one
12458 && ((length && integer_nonzerop (length))
12459 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12460 continue;
12461 if (length)
12462 l = fold_convert (sizetype, length);
12463 else
12465 l = size_binop (PLUS_EXPR,
12466 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12467 size_one_node);
12468 l = size_binop (MINUS_EXPR, l,
12469 fold_convert (sizetype, low_bound));
12471 if (i > first_non_one)
12473 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12474 size_zero_node);
12475 if (condition == NULL_TREE)
12476 condition = l;
12477 else
12478 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12479 l, condition);
12481 else if (size == NULL_TREE)
12483 size = size_in_bytes (TREE_TYPE (types[i]));
12484 tree eltype = TREE_TYPE (types[num - 1]);
12485 while (TREE_CODE (eltype) == ARRAY_TYPE)
12486 eltype = TREE_TYPE (eltype);
12487 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12489 if (integer_zerop (size)
12490 || integer_zerop (size_in_bytes (eltype)))
12492 error_at (OMP_CLAUSE_LOCATION (c),
12493 "zero length array section in %qs clause",
12494 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12495 return error_mark_node;
12497 size = size_binop (EXACT_DIV_EXPR, size,
12498 size_in_bytes (eltype));
12500 size = size_binop (MULT_EXPR, size, l);
12501 if (condition)
12502 size = fold_build3 (COND_EXPR, sizetype, condition,
12503 size, size_zero_node);
12505 else
12506 size = size_binop (MULT_EXPR, size, l);
12509 if (side_effects)
12510 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12511 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12513 size = size_binop (MINUS_EXPR, size, size_one_node);
12514 size = c_fully_fold (size, false, NULL);
12515 tree index_type = build_index_type (size);
12516 tree eltype = TREE_TYPE (first);
12517 while (TREE_CODE (eltype) == ARRAY_TYPE)
12518 eltype = TREE_TYPE (eltype);
12519 tree type = build_array_type (eltype, index_type);
12520 tree ptype = build_pointer_type (eltype);
12521 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12522 t = build_fold_addr_expr (t);
12523 tree t2 = build_fold_addr_expr (first);
12524 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12525 ptrdiff_type_node, t2);
12526 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12527 ptrdiff_type_node, t2,
12528 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12529 ptrdiff_type_node, t));
12530 t2 = c_fully_fold (t2, false, NULL);
12531 if (tree_fits_shwi_p (t2))
12532 t = build2 (MEM_REF, type, t,
12533 build_int_cst (ptype, tree_to_shwi (t2)));
12534 else
12536 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12537 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12538 TREE_TYPE (t), t, t2);
12539 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12541 OMP_CLAUSE_DECL (c) = t;
12542 return false;
12544 first = c_fully_fold (first, false, NULL);
12545 OMP_CLAUSE_DECL (c) = first;
12546 if (size)
12547 size = c_fully_fold (size, false, NULL);
12548 OMP_CLAUSE_SIZE (c) = size;
12549 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12550 || (TREE_CODE (t) == COMPONENT_REF
12551 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12552 return false;
12553 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12554 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12555 switch (OMP_CLAUSE_MAP_KIND (c))
12557 case GOMP_MAP_ALLOC:
12558 case GOMP_MAP_TO:
12559 case GOMP_MAP_FROM:
12560 case GOMP_MAP_TOFROM:
12561 case GOMP_MAP_ALWAYS_TO:
12562 case GOMP_MAP_ALWAYS_FROM:
12563 case GOMP_MAP_ALWAYS_TOFROM:
12564 case GOMP_MAP_RELEASE:
12565 case GOMP_MAP_DELETE:
12566 case GOMP_MAP_FORCE_TO:
12567 case GOMP_MAP_FORCE_FROM:
12568 case GOMP_MAP_FORCE_TOFROM:
12569 case GOMP_MAP_FORCE_PRESENT:
12570 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12571 break;
12572 default:
12573 break;
12575 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12576 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12577 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12578 else if (TREE_CODE (t) == COMPONENT_REF)
12579 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12580 else
12581 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12582 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12583 && !c_mark_addressable (t))
12584 return false;
12585 OMP_CLAUSE_DECL (c2) = t;
12586 t = build_fold_addr_expr (first);
12587 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12588 tree ptr = OMP_CLAUSE_DECL (c2);
12589 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12590 ptr = build_fold_addr_expr (ptr);
12591 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12592 ptrdiff_type_node, t,
12593 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12594 ptrdiff_type_node, ptr));
12595 t = c_fully_fold (t, false, NULL);
12596 OMP_CLAUSE_SIZE (c2) = t;
12597 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12598 OMP_CLAUSE_CHAIN (c) = c2;
12600 return false;
12603 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12604 an inline call. But, remap
12605 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12606 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12608 static tree
12609 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12610 tree decl, tree placeholder)
12612 copy_body_data id;
12613 hash_map<tree, tree> decl_map;
12615 decl_map.put (omp_decl1, placeholder);
12616 decl_map.put (omp_decl2, decl);
12617 memset (&id, 0, sizeof (id));
12618 id.src_fn = DECL_CONTEXT (omp_decl1);
12619 id.dst_fn = current_function_decl;
12620 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12621 id.decl_map = &decl_map;
12623 id.copy_decl = copy_decl_no_change;
12624 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12625 id.transform_new_cfg = true;
12626 id.transform_return_to_modify = false;
12627 id.transform_lang_insert_block = NULL;
12628 id.eh_lp_nr = 0;
12629 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12630 return stmt;
12633 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12634 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12636 static tree
12637 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12639 if (*tp == (tree) data)
12640 return *tp;
12641 return NULL_TREE;
12644 /* For all elements of CLAUSES, validate them against their constraints.
12645 Remove any elements from the list that are invalid. */
12647 tree
12648 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12650 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12651 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12652 tree c, t, type, *pc;
12653 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12654 bool branch_seen = false;
12655 bool copyprivate_seen = false;
12656 bool linear_variable_step_check = false;
12657 tree *nowait_clause = NULL;
12658 bool ordered_seen = false;
12659 tree schedule_clause = NULL_TREE;
12660 bool oacc_async = false;
12662 bitmap_obstack_initialize (NULL);
12663 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12664 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12665 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12666 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12667 bitmap_initialize (&map_head, &bitmap_default_obstack);
12668 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12669 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12671 if (ort & C_ORT_ACC)
12672 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12673 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12675 oacc_async = true;
12676 break;
12679 for (pc = &clauses, c = clauses; c ; c = *pc)
12681 bool remove = false;
12682 bool need_complete = false;
12683 bool need_implicitly_determined = false;
12685 switch (OMP_CLAUSE_CODE (c))
12687 case OMP_CLAUSE_SHARED:
12688 need_implicitly_determined = true;
12689 goto check_dup_generic;
12691 case OMP_CLAUSE_PRIVATE:
12692 need_complete = true;
12693 need_implicitly_determined = true;
12694 goto check_dup_generic;
12696 case OMP_CLAUSE_REDUCTION:
12697 need_implicitly_determined = true;
12698 t = OMP_CLAUSE_DECL (c);
12699 if (TREE_CODE (t) == TREE_LIST)
12701 if (handle_omp_array_sections (c, ort))
12703 remove = true;
12704 break;
12707 t = OMP_CLAUSE_DECL (c);
12709 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12710 if (t == error_mark_node)
12712 remove = true;
12713 break;
12715 if (oacc_async)
12716 c_mark_addressable (t);
12717 type = TREE_TYPE (t);
12718 if (TREE_CODE (t) == MEM_REF)
12719 type = TREE_TYPE (type);
12720 if (TREE_CODE (type) == ARRAY_TYPE)
12722 tree oatype = type;
12723 gcc_assert (TREE_CODE (t) != MEM_REF);
12724 while (TREE_CODE (type) == ARRAY_TYPE)
12725 type = TREE_TYPE (type);
12726 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12728 error_at (OMP_CLAUSE_LOCATION (c),
12729 "%qD in %<reduction%> clause is a zero size array",
12731 remove = true;
12732 break;
12734 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12735 TYPE_SIZE_UNIT (type));
12736 if (integer_zerop (size))
12738 error_at (OMP_CLAUSE_LOCATION (c),
12739 "%qD in %<reduction%> clause is a zero size array",
12741 remove = true;
12742 break;
12744 size = size_binop (MINUS_EXPR, size, size_one_node);
12745 tree index_type = build_index_type (size);
12746 tree atype = build_array_type (type, index_type);
12747 tree ptype = build_pointer_type (type);
12748 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12749 t = build_fold_addr_expr (t);
12750 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12751 OMP_CLAUSE_DECL (c) = t;
12753 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12754 && (FLOAT_TYPE_P (type)
12755 || TREE_CODE (type) == COMPLEX_TYPE))
12757 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12758 const char *r_name = NULL;
12760 switch (r_code)
12762 case PLUS_EXPR:
12763 case MULT_EXPR:
12764 case MINUS_EXPR:
12765 break;
12766 case MIN_EXPR:
12767 if (TREE_CODE (type) == COMPLEX_TYPE)
12768 r_name = "min";
12769 break;
12770 case MAX_EXPR:
12771 if (TREE_CODE (type) == COMPLEX_TYPE)
12772 r_name = "max";
12773 break;
12774 case BIT_AND_EXPR:
12775 r_name = "&";
12776 break;
12777 case BIT_XOR_EXPR:
12778 r_name = "^";
12779 break;
12780 case BIT_IOR_EXPR:
12781 r_name = "|";
12782 break;
12783 case TRUTH_ANDIF_EXPR:
12784 if (FLOAT_TYPE_P (type))
12785 r_name = "&&";
12786 break;
12787 case TRUTH_ORIF_EXPR:
12788 if (FLOAT_TYPE_P (type))
12789 r_name = "||";
12790 break;
12791 default:
12792 gcc_unreachable ();
12794 if (r_name)
12796 error_at (OMP_CLAUSE_LOCATION (c),
12797 "%qE has invalid type for %<reduction(%s)%>",
12798 t, r_name);
12799 remove = true;
12800 break;
12803 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12805 error_at (OMP_CLAUSE_LOCATION (c),
12806 "user defined reduction not found for %qE", t);
12807 remove = true;
12808 break;
12810 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12812 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12813 type = TYPE_MAIN_VARIANT (type);
12814 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12815 VAR_DECL, NULL_TREE, type);
12816 tree decl_placeholder = NULL_TREE;
12817 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12818 DECL_ARTIFICIAL (placeholder) = 1;
12819 DECL_IGNORED_P (placeholder) = 1;
12820 if (TREE_CODE (t) == MEM_REF)
12822 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12823 VAR_DECL, NULL_TREE, type);
12824 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12825 DECL_ARTIFICIAL (decl_placeholder) = 1;
12826 DECL_IGNORED_P (decl_placeholder) = 1;
12828 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12829 c_mark_addressable (placeholder);
12830 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12831 c_mark_addressable (decl_placeholder ? decl_placeholder
12832 : OMP_CLAUSE_DECL (c));
12833 OMP_CLAUSE_REDUCTION_MERGE (c)
12834 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12835 TREE_VEC_ELT (list, 0),
12836 TREE_VEC_ELT (list, 1),
12837 decl_placeholder ? decl_placeholder
12838 : OMP_CLAUSE_DECL (c), placeholder);
12839 OMP_CLAUSE_REDUCTION_MERGE (c)
12840 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12841 void_type_node, NULL_TREE,
12842 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12843 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12844 if (TREE_VEC_LENGTH (list) == 6)
12846 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12847 c_mark_addressable (decl_placeholder ? decl_placeholder
12848 : OMP_CLAUSE_DECL (c));
12849 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12850 c_mark_addressable (placeholder);
12851 tree init = TREE_VEC_ELT (list, 5);
12852 if (init == error_mark_node)
12853 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12854 OMP_CLAUSE_REDUCTION_INIT (c)
12855 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12856 TREE_VEC_ELT (list, 3),
12857 decl_placeholder ? decl_placeholder
12858 : OMP_CLAUSE_DECL (c), placeholder);
12859 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12861 tree v = decl_placeholder ? decl_placeholder : t;
12862 OMP_CLAUSE_REDUCTION_INIT (c)
12863 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12864 OMP_CLAUSE_REDUCTION_INIT (c));
12866 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12867 c_find_omp_placeholder_r,
12868 placeholder, NULL))
12869 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12871 else
12873 tree init;
12874 tree v = decl_placeholder ? decl_placeholder : t;
12875 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12876 init = build_constructor (TREE_TYPE (v), NULL);
12877 else
12878 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12879 OMP_CLAUSE_REDUCTION_INIT (c)
12880 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12882 OMP_CLAUSE_REDUCTION_INIT (c)
12883 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12884 void_type_node, NULL_TREE,
12885 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12886 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12888 if (TREE_CODE (t) == MEM_REF)
12890 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12891 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12892 != INTEGER_CST)
12894 sorry ("variable length element type in array "
12895 "%<reduction%> clause");
12896 remove = true;
12897 break;
12899 t = TREE_OPERAND (t, 0);
12900 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12901 t = TREE_OPERAND (t, 0);
12902 if (TREE_CODE (t) == ADDR_EXPR)
12903 t = TREE_OPERAND (t, 0);
12905 goto check_dup_generic_t;
12907 case OMP_CLAUSE_COPYPRIVATE:
12908 copyprivate_seen = true;
12909 if (nowait_clause)
12911 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12912 "%<nowait%> clause must not be used together "
12913 "with %<copyprivate%>");
12914 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12915 nowait_clause = NULL;
12917 goto check_dup_generic;
12919 case OMP_CLAUSE_COPYIN:
12920 t = OMP_CLAUSE_DECL (c);
12921 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12923 error_at (OMP_CLAUSE_LOCATION (c),
12924 "%qE must be %<threadprivate%> for %<copyin%>", t);
12925 remove = true;
12926 break;
12928 goto check_dup_generic;
12930 case OMP_CLAUSE_LINEAR:
12931 if (ort != C_ORT_OMP_DECLARE_SIMD)
12932 need_implicitly_determined = true;
12933 t = OMP_CLAUSE_DECL (c);
12934 if (ort != C_ORT_OMP_DECLARE_SIMD
12935 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12937 error_at (OMP_CLAUSE_LOCATION (c),
12938 "modifier should not be specified in %<linear%> "
12939 "clause on %<simd%> or %<for%> constructs");
12940 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12942 if (ort & C_ORT_CILK)
12944 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12945 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
12946 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12948 error_at (OMP_CLAUSE_LOCATION (c),
12949 "linear clause applied to non-integral, "
12950 "non-floating, non-pointer variable with type %qT",
12951 TREE_TYPE (t));
12952 remove = true;
12953 break;
12956 else
12958 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12959 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12961 error_at (OMP_CLAUSE_LOCATION (c),
12962 "linear clause applied to non-integral non-pointer "
12963 "variable with type %qT", TREE_TYPE (t));
12964 remove = true;
12965 break;
12968 if (ort == C_ORT_OMP_DECLARE_SIMD)
12970 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12971 if (TREE_CODE (s) == PARM_DECL)
12973 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12974 /* map_head bitmap is used as uniform_head if
12975 declare_simd. */
12976 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12977 linear_variable_step_check = true;
12978 goto check_dup_generic;
12980 if (TREE_CODE (s) != INTEGER_CST)
12982 error_at (OMP_CLAUSE_LOCATION (c),
12983 "%<linear%> clause step %qE is neither constant "
12984 "nor a parameter", s);
12985 remove = true;
12986 break;
12989 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12991 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12992 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12993 OMP_CLAUSE_DECL (c), s);
12994 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12995 sizetype, fold_convert (sizetype, s),
12996 fold_convert
12997 (sizetype, OMP_CLAUSE_DECL (c)));
12998 if (s == error_mark_node)
12999 s = size_one_node;
13000 OMP_CLAUSE_LINEAR_STEP (c) = s;
13002 else
13003 OMP_CLAUSE_LINEAR_STEP (c)
13004 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13005 goto check_dup_generic;
13007 check_dup_generic:
13008 t = OMP_CLAUSE_DECL (c);
13009 check_dup_generic_t:
13010 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13012 error_at (OMP_CLAUSE_LOCATION (c),
13013 "%qE is not a variable in clause %qs", t,
13014 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13015 remove = true;
13017 else if (ort == C_ORT_ACC
13018 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13020 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13022 error ("%qD appears more than once in reduction clauses", t);
13023 remove = true;
13025 else
13026 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13028 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13029 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13030 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13032 error_at (OMP_CLAUSE_LOCATION (c),
13033 "%qE appears more than once in data clauses", t);
13034 remove = true;
13036 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13037 && bitmap_bit_p (&map_head, DECL_UID (t)))
13039 if (ort == C_ORT_ACC)
13040 error ("%qD appears more than once in data clauses", t);
13041 else
13042 error ("%qD appears both in data and map clauses", t);
13043 remove = true;
13045 else
13046 bitmap_set_bit (&generic_head, DECL_UID (t));
13047 break;
13049 case OMP_CLAUSE_FIRSTPRIVATE:
13050 t = OMP_CLAUSE_DECL (c);
13051 need_complete = true;
13052 need_implicitly_determined = true;
13053 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13055 error_at (OMP_CLAUSE_LOCATION (c),
13056 "%qE is not a variable in clause %<firstprivate%>", t);
13057 remove = true;
13059 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13060 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13062 error_at (OMP_CLAUSE_LOCATION (c),
13063 "%qE appears more than once in data clauses", t);
13064 remove = true;
13066 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13068 if (ort == C_ORT_ACC)
13069 error ("%qD appears more than once in data clauses", t);
13070 else
13071 error ("%qD appears both in data and map clauses", t);
13072 remove = true;
13074 else
13075 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13076 break;
13078 case OMP_CLAUSE_LASTPRIVATE:
13079 t = OMP_CLAUSE_DECL (c);
13080 need_complete = true;
13081 need_implicitly_determined = true;
13082 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13084 error_at (OMP_CLAUSE_LOCATION (c),
13085 "%qE is not a variable in clause %<lastprivate%>", t);
13086 remove = true;
13088 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13089 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13091 error_at (OMP_CLAUSE_LOCATION (c),
13092 "%qE appears more than once in data clauses", t);
13093 remove = true;
13095 else
13096 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13097 break;
13099 case OMP_CLAUSE_ALIGNED:
13100 t = OMP_CLAUSE_DECL (c);
13101 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13103 error_at (OMP_CLAUSE_LOCATION (c),
13104 "%qE is not a variable in %<aligned%> clause", t);
13105 remove = true;
13107 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13108 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13110 error_at (OMP_CLAUSE_LOCATION (c),
13111 "%qE in %<aligned%> clause is neither a pointer nor "
13112 "an array", t);
13113 remove = true;
13115 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13117 error_at (OMP_CLAUSE_LOCATION (c),
13118 "%qE appears more than once in %<aligned%> clauses",
13120 remove = true;
13122 else
13123 bitmap_set_bit (&aligned_head, DECL_UID (t));
13124 break;
13126 case OMP_CLAUSE_DEPEND:
13127 t = OMP_CLAUSE_DECL (c);
13128 if (t == NULL_TREE)
13130 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13131 == OMP_CLAUSE_DEPEND_SOURCE);
13132 break;
13134 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13136 gcc_assert (TREE_CODE (t) == TREE_LIST);
13137 for (; t; t = TREE_CHAIN (t))
13139 tree decl = TREE_VALUE (t);
13140 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13142 tree offset = TREE_PURPOSE (t);
13143 bool neg = wi::neg_p ((wide_int) offset);
13144 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13145 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13146 neg ? MINUS_EXPR : PLUS_EXPR,
13147 decl, offset);
13148 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13149 sizetype,
13150 fold_convert (sizetype, t2),
13151 fold_convert (sizetype, decl));
13152 if (t2 == error_mark_node)
13154 remove = true;
13155 break;
13157 TREE_PURPOSE (t) = t2;
13160 break;
13162 if (TREE_CODE (t) == TREE_LIST)
13164 if (handle_omp_array_sections (c, ort))
13165 remove = true;
13166 break;
13168 if (t == error_mark_node)
13169 remove = true;
13170 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13172 error_at (OMP_CLAUSE_LOCATION (c),
13173 "%qE is not a variable in %<depend%> clause", t);
13174 remove = true;
13176 else if (!c_mark_addressable (t))
13177 remove = true;
13178 break;
13180 case OMP_CLAUSE_MAP:
13181 case OMP_CLAUSE_TO:
13182 case OMP_CLAUSE_FROM:
13183 case OMP_CLAUSE__CACHE_:
13184 t = OMP_CLAUSE_DECL (c);
13185 if (TREE_CODE (t) == TREE_LIST)
13187 if (handle_omp_array_sections (c, ort))
13188 remove = true;
13189 else
13191 t = OMP_CLAUSE_DECL (c);
13192 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13194 error_at (OMP_CLAUSE_LOCATION (c),
13195 "array section does not have mappable type "
13196 "in %qs clause",
13197 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13198 remove = true;
13200 while (TREE_CODE (t) == ARRAY_REF)
13201 t = TREE_OPERAND (t, 0);
13202 if (TREE_CODE (t) == COMPONENT_REF
13203 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13205 while (TREE_CODE (t) == COMPONENT_REF)
13206 t = TREE_OPERAND (t, 0);
13207 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13208 break;
13209 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13211 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13212 error ("%qD appears more than once in motion"
13213 " clauses", t);
13214 else if (ort == C_ORT_ACC)
13215 error ("%qD appears more than once in data"
13216 " clauses", t);
13217 else
13218 error ("%qD appears more than once in map"
13219 " clauses", t);
13220 remove = true;
13222 else
13224 bitmap_set_bit (&map_head, DECL_UID (t));
13225 bitmap_set_bit (&map_field_head, DECL_UID (t));
13229 break;
13231 if (t == error_mark_node)
13233 remove = true;
13234 break;
13236 if (TREE_CODE (t) == COMPONENT_REF
13237 && (ort & C_ORT_OMP)
13238 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13240 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13242 error_at (OMP_CLAUSE_LOCATION (c),
13243 "bit-field %qE in %qs clause",
13244 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13245 remove = true;
13247 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13249 error_at (OMP_CLAUSE_LOCATION (c),
13250 "%qE does not have a mappable type in %qs clause",
13251 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13252 remove = true;
13254 while (TREE_CODE (t) == COMPONENT_REF)
13256 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13257 == UNION_TYPE)
13259 error_at (OMP_CLAUSE_LOCATION (c),
13260 "%qE is a member of a union", t);
13261 remove = true;
13262 break;
13264 t = TREE_OPERAND (t, 0);
13266 if (remove)
13267 break;
13268 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13270 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13271 break;
13274 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13276 error_at (OMP_CLAUSE_LOCATION (c),
13277 "%qE is not a variable in %qs clause", t,
13278 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13279 remove = true;
13281 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13283 error_at (OMP_CLAUSE_LOCATION (c),
13284 "%qD is threadprivate variable in %qs clause", t,
13285 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13286 remove = true;
13288 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13289 || (OMP_CLAUSE_MAP_KIND (c)
13290 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13291 && !c_mark_addressable (t))
13292 remove = true;
13293 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13294 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13295 || (OMP_CLAUSE_MAP_KIND (c)
13296 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13297 || (OMP_CLAUSE_MAP_KIND (c)
13298 == GOMP_MAP_FORCE_DEVICEPTR)))
13299 && t == OMP_CLAUSE_DECL (c)
13300 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13302 error_at (OMP_CLAUSE_LOCATION (c),
13303 "%qD does not have a mappable type in %qs clause", t,
13304 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13305 remove = true;
13307 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13308 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13310 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13311 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13313 error ("%qD appears more than once in data clauses", t);
13314 remove = true;
13316 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13318 if (ort == C_ORT_ACC)
13319 error ("%qD appears more than once in data clauses", t);
13320 else
13321 error ("%qD appears both in data and map clauses", t);
13322 remove = true;
13324 else
13325 bitmap_set_bit (&generic_head, DECL_UID (t));
13327 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13329 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13330 error ("%qD appears more than once in motion clauses", t);
13331 else if (ort == C_ORT_ACC)
13332 error ("%qD appears more than once in data clauses", t);
13333 else
13334 error ("%qD appears more than once in map clauses", t);
13335 remove = true;
13337 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13338 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13340 if (ort == C_ORT_ACC)
13341 error ("%qD appears more than once in data clauses", t);
13342 else
13343 error ("%qD appears both in data and map clauses", t);
13344 remove = true;
13346 else
13348 bitmap_set_bit (&map_head, DECL_UID (t));
13349 if (t != OMP_CLAUSE_DECL (c)
13350 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13351 bitmap_set_bit (&map_field_head, DECL_UID (t));
13353 break;
13355 case OMP_CLAUSE_TO_DECLARE:
13356 case OMP_CLAUSE_LINK:
13357 t = OMP_CLAUSE_DECL (c);
13358 if (TREE_CODE (t) == FUNCTION_DECL
13359 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13361 else if (!VAR_P (t))
13363 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13364 error_at (OMP_CLAUSE_LOCATION (c),
13365 "%qE is neither a variable nor a function name in "
13366 "clause %qs", t,
13367 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13368 else
13369 error_at (OMP_CLAUSE_LOCATION (c),
13370 "%qE is not a variable in clause %qs", t,
13371 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13372 remove = true;
13374 else if (DECL_THREAD_LOCAL_P (t))
13376 error_at (OMP_CLAUSE_LOCATION (c),
13377 "%qD is threadprivate variable in %qs clause", t,
13378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13379 remove = true;
13381 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13383 error_at (OMP_CLAUSE_LOCATION (c),
13384 "%qD does not have a mappable type in %qs clause", t,
13385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13386 remove = true;
13388 if (remove)
13389 break;
13390 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13392 error_at (OMP_CLAUSE_LOCATION (c),
13393 "%qE appears more than once on the same "
13394 "%<declare target%> directive", t);
13395 remove = true;
13397 else
13398 bitmap_set_bit (&generic_head, DECL_UID (t));
13399 break;
13401 case OMP_CLAUSE_UNIFORM:
13402 t = OMP_CLAUSE_DECL (c);
13403 if (TREE_CODE (t) != PARM_DECL)
13405 if (DECL_P (t))
13406 error_at (OMP_CLAUSE_LOCATION (c),
13407 "%qD is not an argument in %<uniform%> clause", t);
13408 else
13409 error_at (OMP_CLAUSE_LOCATION (c),
13410 "%qE is not an argument in %<uniform%> clause", t);
13411 remove = true;
13412 break;
13414 /* map_head bitmap is used as uniform_head if declare_simd. */
13415 bitmap_set_bit (&map_head, DECL_UID (t));
13416 goto check_dup_generic;
13418 case OMP_CLAUSE_IS_DEVICE_PTR:
13419 case OMP_CLAUSE_USE_DEVICE_PTR:
13420 t = OMP_CLAUSE_DECL (c);
13421 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13422 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13424 error_at (OMP_CLAUSE_LOCATION (c),
13425 "%qs variable is neither a pointer nor an array",
13426 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13427 remove = true;
13429 goto check_dup_generic;
13431 case OMP_CLAUSE_NOWAIT:
13432 if (copyprivate_seen)
13434 error_at (OMP_CLAUSE_LOCATION (c),
13435 "%<nowait%> clause must not be used together "
13436 "with %<copyprivate%>");
13437 remove = true;
13438 break;
13440 nowait_clause = pc;
13441 pc = &OMP_CLAUSE_CHAIN (c);
13442 continue;
13444 case OMP_CLAUSE_IF:
13445 case OMP_CLAUSE_NUM_THREADS:
13446 case OMP_CLAUSE_NUM_TEAMS:
13447 case OMP_CLAUSE_THREAD_LIMIT:
13448 case OMP_CLAUSE_DEFAULT:
13449 case OMP_CLAUSE_UNTIED:
13450 case OMP_CLAUSE_COLLAPSE:
13451 case OMP_CLAUSE_FINAL:
13452 case OMP_CLAUSE_MERGEABLE:
13453 case OMP_CLAUSE_DEVICE:
13454 case OMP_CLAUSE_DIST_SCHEDULE:
13455 case OMP_CLAUSE_PARALLEL:
13456 case OMP_CLAUSE_FOR:
13457 case OMP_CLAUSE_SECTIONS:
13458 case OMP_CLAUSE_TASKGROUP:
13459 case OMP_CLAUSE_PROC_BIND:
13460 case OMP_CLAUSE_PRIORITY:
13461 case OMP_CLAUSE_GRAINSIZE:
13462 case OMP_CLAUSE_NUM_TASKS:
13463 case OMP_CLAUSE_NOGROUP:
13464 case OMP_CLAUSE_THREADS:
13465 case OMP_CLAUSE_SIMD:
13466 case OMP_CLAUSE_HINT:
13467 case OMP_CLAUSE_DEFAULTMAP:
13468 case OMP_CLAUSE__CILK_FOR_COUNT_:
13469 case OMP_CLAUSE_NUM_GANGS:
13470 case OMP_CLAUSE_NUM_WORKERS:
13471 case OMP_CLAUSE_VECTOR_LENGTH:
13472 case OMP_CLAUSE_ASYNC:
13473 case OMP_CLAUSE_WAIT:
13474 case OMP_CLAUSE_AUTO:
13475 case OMP_CLAUSE_INDEPENDENT:
13476 case OMP_CLAUSE_SEQ:
13477 case OMP_CLAUSE_GANG:
13478 case OMP_CLAUSE_WORKER:
13479 case OMP_CLAUSE_VECTOR:
13480 case OMP_CLAUSE_TILE:
13481 pc = &OMP_CLAUSE_CHAIN (c);
13482 continue;
13484 case OMP_CLAUSE_SCHEDULE:
13485 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13487 const char *p = NULL;
13488 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13490 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13491 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13492 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13493 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13494 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13495 default: gcc_unreachable ();
13497 if (p)
13499 error_at (OMP_CLAUSE_LOCATION (c),
13500 "%<nonmonotonic%> modifier specified for %qs "
13501 "schedule kind", p);
13502 OMP_CLAUSE_SCHEDULE_KIND (c)
13503 = (enum omp_clause_schedule_kind)
13504 (OMP_CLAUSE_SCHEDULE_KIND (c)
13505 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13508 schedule_clause = c;
13509 pc = &OMP_CLAUSE_CHAIN (c);
13510 continue;
13512 case OMP_CLAUSE_ORDERED:
13513 ordered_seen = true;
13514 pc = &OMP_CLAUSE_CHAIN (c);
13515 continue;
13517 case OMP_CLAUSE_SAFELEN:
13518 safelen = c;
13519 pc = &OMP_CLAUSE_CHAIN (c);
13520 continue;
13521 case OMP_CLAUSE_SIMDLEN:
13522 simdlen = c;
13523 pc = &OMP_CLAUSE_CHAIN (c);
13524 continue;
13526 case OMP_CLAUSE_INBRANCH:
13527 case OMP_CLAUSE_NOTINBRANCH:
13528 if (branch_seen)
13530 error_at (OMP_CLAUSE_LOCATION (c),
13531 "%<inbranch%> clause is incompatible with "
13532 "%<notinbranch%>");
13533 remove = true;
13534 break;
13536 branch_seen = true;
13537 pc = &OMP_CLAUSE_CHAIN (c);
13538 continue;
13540 default:
13541 gcc_unreachable ();
13544 if (!remove)
13546 t = OMP_CLAUSE_DECL (c);
13548 if (need_complete)
13550 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13551 if (t == error_mark_node)
13552 remove = true;
13555 if (need_implicitly_determined)
13557 const char *share_name = NULL;
13559 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13560 share_name = "threadprivate";
13561 else switch (c_omp_predetermined_sharing (t))
13563 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13564 break;
13565 case OMP_CLAUSE_DEFAULT_SHARED:
13566 /* const vars may be specified in firstprivate clause. */
13567 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13568 && TREE_READONLY (t))
13569 break;
13570 share_name = "shared";
13571 break;
13572 case OMP_CLAUSE_DEFAULT_PRIVATE:
13573 share_name = "private";
13574 break;
13575 default:
13576 gcc_unreachable ();
13578 if (share_name)
13580 error_at (OMP_CLAUSE_LOCATION (c),
13581 "%qE is predetermined %qs for %qs",
13582 t, share_name,
13583 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13584 remove = true;
13589 if (remove)
13590 *pc = OMP_CLAUSE_CHAIN (c);
13591 else
13592 pc = &OMP_CLAUSE_CHAIN (c);
13595 if (simdlen
13596 && safelen
13597 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13598 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13600 error_at (OMP_CLAUSE_LOCATION (simdlen),
13601 "%<simdlen%> clause value is bigger than "
13602 "%<safelen%> clause value");
13603 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13604 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13607 if (ordered_seen
13608 && schedule_clause
13609 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13610 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13612 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13613 "%<nonmonotonic%> schedule modifier specified together "
13614 "with %<ordered%> clause");
13615 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13616 = (enum omp_clause_schedule_kind)
13617 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13618 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13621 if (linear_variable_step_check)
13622 for (pc = &clauses, c = clauses; c ; c = *pc)
13624 bool remove = false;
13625 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13626 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13627 && !bitmap_bit_p (&map_head,
13628 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13630 error_at (OMP_CLAUSE_LOCATION (c),
13631 "%<linear%> clause step is a parameter %qD not "
13632 "specified in %<uniform%> clause",
13633 OMP_CLAUSE_LINEAR_STEP (c));
13634 remove = true;
13637 if (remove)
13638 *pc = OMP_CLAUSE_CHAIN (c);
13639 else
13640 pc = &OMP_CLAUSE_CHAIN (c);
13643 bitmap_obstack_release (NULL);
13644 return clauses;
13647 /* Create a transaction node. */
13649 tree
13650 c_finish_transaction (location_t loc, tree block, int flags)
13652 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13653 if (flags & TM_STMT_ATTR_OUTER)
13654 TRANSACTION_EXPR_OUTER (stmt) = 1;
13655 if (flags & TM_STMT_ATTR_RELAXED)
13656 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13657 return add_stmt (stmt);
13660 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13661 down to the element type of an array. If ORIG_QUAL_TYPE is not
13662 NULL, then it should be used as the qualified type
13663 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13664 preserve information about the typedef name from which an array
13665 type was derived). */
13667 tree
13668 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13669 size_t orig_qual_indirect)
13671 if (type == error_mark_node)
13672 return type;
13674 if (TREE_CODE (type) == ARRAY_TYPE)
13676 tree t;
13677 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13678 type_quals, orig_qual_type,
13679 orig_qual_indirect - 1);
13681 /* See if we already have an identically qualified type. */
13682 if (orig_qual_type && orig_qual_indirect == 0)
13683 t = orig_qual_type;
13684 else
13685 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13687 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13688 && TYPE_NAME (t) == TYPE_NAME (type)
13689 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13690 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13691 TYPE_ATTRIBUTES (type)))
13692 break;
13694 if (!t)
13696 tree domain = TYPE_DOMAIN (type);
13698 t = build_variant_type_copy (type);
13699 TREE_TYPE (t) = element_type;
13701 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13702 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13703 SET_TYPE_STRUCTURAL_EQUALITY (t);
13704 else if (TYPE_CANONICAL (element_type) != element_type
13705 || (domain && TYPE_CANONICAL (domain) != domain))
13707 tree unqualified_canon
13708 = build_array_type (TYPE_CANONICAL (element_type),
13709 domain? TYPE_CANONICAL (domain)
13710 : NULL_TREE);
13711 if (TYPE_REVERSE_STORAGE_ORDER (type))
13713 unqualified_canon
13714 = build_distinct_type_copy (unqualified_canon);
13715 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13717 TYPE_CANONICAL (t)
13718 = c_build_qualified_type (unqualified_canon, type_quals);
13720 else
13721 TYPE_CANONICAL (t) = t;
13723 return t;
13726 /* A restrict-qualified pointer type must be a pointer to object or
13727 incomplete type. Note that the use of POINTER_TYPE_P also allows
13728 REFERENCE_TYPEs, which is appropriate for C++. */
13729 if ((type_quals & TYPE_QUAL_RESTRICT)
13730 && (!POINTER_TYPE_P (type)
13731 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13733 error ("invalid use of %<restrict%>");
13734 type_quals &= ~TYPE_QUAL_RESTRICT;
13737 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13738 ? orig_qual_type
13739 : build_qualified_type (type, type_quals));
13740 /* A variant type does not inherit the list of incomplete vars from the
13741 type main variant. */
13742 if (RECORD_OR_UNION_TYPE_P (var_type)
13743 && TYPE_MAIN_VARIANT (var_type) != var_type)
13744 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13745 return var_type;
13748 /* Build a VA_ARG_EXPR for the C parser. */
13750 tree
13751 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13753 if (error_operand_p (type))
13754 return error_mark_node;
13755 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13756 order because it takes the address of the expression. */
13757 else if (handled_component_p (expr)
13758 && reverse_storage_order_for_component_p (expr))
13760 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13761 return error_mark_node;
13763 else if (!COMPLETE_TYPE_P (type))
13765 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13766 "type %qT", type);
13767 return error_mark_node;
13769 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13770 warning_at (loc2, OPT_Wc___compat,
13771 "C++ requires promoted type, not enum type, in %<va_arg%>");
13772 return build_va_arg (loc2, expr, type);
13775 /* Return truthvalue of whether T1 is the same tree structure as T2.
13776 Return 1 if they are the same. Return 0 if they are different. */
13778 bool
13779 c_tree_equal (tree t1, tree t2)
13781 enum tree_code code1, code2;
13783 if (t1 == t2)
13784 return true;
13785 if (!t1 || !t2)
13786 return false;
13788 for (code1 = TREE_CODE (t1);
13789 CONVERT_EXPR_CODE_P (code1)
13790 || code1 == NON_LVALUE_EXPR;
13791 code1 = TREE_CODE (t1))
13792 t1 = TREE_OPERAND (t1, 0);
13793 for (code2 = TREE_CODE (t2);
13794 CONVERT_EXPR_CODE_P (code2)
13795 || code2 == NON_LVALUE_EXPR;
13796 code2 = TREE_CODE (t2))
13797 t2 = TREE_OPERAND (t2, 0);
13799 /* They might have become equal now. */
13800 if (t1 == t2)
13801 return true;
13803 if (code1 != code2)
13804 return false;
13806 switch (code1)
13808 case INTEGER_CST:
13809 return wi::eq_p (t1, t2);
13811 case REAL_CST:
13812 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13814 case STRING_CST:
13815 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13816 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13817 TREE_STRING_LENGTH (t1));
13819 case FIXED_CST:
13820 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13821 TREE_FIXED_CST (t2));
13823 case COMPLEX_CST:
13824 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13825 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13827 case VECTOR_CST:
13828 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13830 case CONSTRUCTOR:
13831 /* We need to do this when determining whether or not two
13832 non-type pointer to member function template arguments
13833 are the same. */
13834 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13835 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13836 return false;
13838 tree field, value;
13839 unsigned int i;
13840 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13842 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13843 if (!c_tree_equal (field, elt2->index)
13844 || !c_tree_equal (value, elt2->value))
13845 return false;
13848 return true;
13850 case TREE_LIST:
13851 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13852 return false;
13853 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13854 return false;
13855 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13857 case SAVE_EXPR:
13858 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13860 case CALL_EXPR:
13862 tree arg1, arg2;
13863 call_expr_arg_iterator iter1, iter2;
13864 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13865 return false;
13866 for (arg1 = first_call_expr_arg (t1, &iter1),
13867 arg2 = first_call_expr_arg (t2, &iter2);
13868 arg1 && arg2;
13869 arg1 = next_call_expr_arg (&iter1),
13870 arg2 = next_call_expr_arg (&iter2))
13871 if (!c_tree_equal (arg1, arg2))
13872 return false;
13873 if (arg1 || arg2)
13874 return false;
13875 return true;
13878 case TARGET_EXPR:
13880 tree o1 = TREE_OPERAND (t1, 0);
13881 tree o2 = TREE_OPERAND (t2, 0);
13883 /* Special case: if either target is an unallocated VAR_DECL,
13884 it means that it's going to be unified with whatever the
13885 TARGET_EXPR is really supposed to initialize, so treat it
13886 as being equivalent to anything. */
13887 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
13888 && !DECL_RTL_SET_P (o1))
13889 /*Nop*/;
13890 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
13891 && !DECL_RTL_SET_P (o2))
13892 /*Nop*/;
13893 else if (!c_tree_equal (o1, o2))
13894 return false;
13896 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13899 case COMPONENT_REF:
13900 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13901 return false;
13902 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13904 case PARM_DECL:
13905 case VAR_DECL:
13906 case CONST_DECL:
13907 case FIELD_DECL:
13908 case FUNCTION_DECL:
13909 case IDENTIFIER_NODE:
13910 case SSA_NAME:
13911 return false;
13913 case TREE_VEC:
13915 unsigned ix;
13916 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13917 return false;
13918 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13919 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13920 TREE_VEC_ELT (t2, ix)))
13921 return false;
13922 return true;
13925 default:
13926 break;
13929 switch (TREE_CODE_CLASS (code1))
13931 case tcc_unary:
13932 case tcc_binary:
13933 case tcc_comparison:
13934 case tcc_expression:
13935 case tcc_vl_exp:
13936 case tcc_reference:
13937 case tcc_statement:
13939 int i, n = TREE_OPERAND_LENGTH (t1);
13941 switch (code1)
13943 case PREINCREMENT_EXPR:
13944 case PREDECREMENT_EXPR:
13945 case POSTINCREMENT_EXPR:
13946 case POSTDECREMENT_EXPR:
13947 n = 1;
13948 break;
13949 case ARRAY_REF:
13950 n = 2;
13951 break;
13952 default:
13953 break;
13956 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13957 && n != TREE_OPERAND_LENGTH (t2))
13958 return false;
13960 for (i = 0; i < n; ++i)
13961 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13962 return false;
13964 return true;
13967 case tcc_type:
13968 return comptypes (t1, t2);
13969 default:
13970 gcc_unreachable ();
13972 /* We can get here with --disable-checking. */
13973 return false;
13976 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13977 spawn-helper and BODY is the newly created body for FNDECL. */
13979 void
13980 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13982 tree list = alloc_stmt_list ();
13983 tree frame = make_cilk_frame (fndecl);
13984 tree dtor = create_cilk_function_exit (frame, false, true);
13985 add_local_decl (cfun, frame);
13987 DECL_SAVED_TREE (fndecl) = list;
13988 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13989 frame);
13990 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13991 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13993 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13994 append_to_statement_list (detach_expr, &body_list);
13996 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13997 body = fold_build_cleanup_point_expr (void_type_node, body);
13999 append_to_statement_list (body, &body_list);
14000 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14001 body_list, dtor), &list);
14004 /* Returns true when the function declaration FNDECL is implicit,
14005 introduced as a result of a call to an otherwise undeclared
14006 function, and false otherwise. */
14008 bool
14009 c_decl_implicit (const_tree fndecl)
14011 return C_DECL_IMPLICIT (fndecl);