spellcheck.h: add best_match template; implement early-reject
[official-gcc.git] / gcc / c / c-typeck.c
blobf03c07bb7f15d2e78c1579ac6a281e5a3dc695e4
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 /* Likewise, prefer long double to double even if same size. */
931 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
932 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
933 return long_double_type_node;
935 /* Likewise, prefer double to float even if same size.
936 We got a couple of embedded targets with 32 bit doubles, and the
937 pdp11 might have 64 bit floats. */
938 if (TYPE_MAIN_VARIANT (t1) == double_type_node
939 || TYPE_MAIN_VARIANT (t2) == double_type_node)
940 return double_type_node;
942 /* Otherwise prefer the unsigned one. */
944 if (TYPE_UNSIGNED (t1))
945 return t1;
946 else
947 return t2;
950 /* Wrapper around c_common_type that is used by c-common.c and other
951 front end optimizations that remove promotions. ENUMERAL_TYPEs
952 are allowed here and are converted to their compatible integer types.
953 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
954 preferably a non-Boolean type as the common type. */
955 tree
956 common_type (tree t1, tree t2)
958 if (TREE_CODE (t1) == ENUMERAL_TYPE)
959 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
960 if (TREE_CODE (t2) == ENUMERAL_TYPE)
961 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
963 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
964 if (TREE_CODE (t1) == BOOLEAN_TYPE
965 && TREE_CODE (t2) == BOOLEAN_TYPE)
966 return boolean_type_node;
968 /* If either type is BOOLEAN_TYPE, then return the other. */
969 if (TREE_CODE (t1) == BOOLEAN_TYPE)
970 return t2;
971 if (TREE_CODE (t2) == BOOLEAN_TYPE)
972 return t1;
974 return c_common_type (t1, t2);
977 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
978 or various other operations. Return 2 if they are compatible
979 but a warning may be needed if you use them together. */
982 comptypes (tree type1, tree type2)
984 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
985 int val;
987 val = comptypes_internal (type1, type2, NULL, NULL);
988 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
990 return val;
993 /* Like comptypes, but if it returns non-zero because enum and int are
994 compatible, it sets *ENUM_AND_INT_P to true. */
996 static int
997 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
999 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1000 int val;
1002 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1003 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1005 return val;
1008 /* Like comptypes, but if it returns nonzero for different types, it
1009 sets *DIFFERENT_TYPES_P to true. */
1012 comptypes_check_different_types (tree type1, tree type2,
1013 bool *different_types_p)
1015 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1016 int val;
1018 val = comptypes_internal (type1, type2, NULL, different_types_p);
1019 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1021 return val;
1024 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1025 or various other operations. Return 2 if they are compatible
1026 but a warning may be needed if you use them together. If
1027 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1028 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1029 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1030 NULL, and the types are compatible but different enough not to be
1031 permitted in C11 typedef redeclarations, then this sets
1032 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1033 false, but may or may not be set if the types are incompatible.
1034 This differs from comptypes, in that we don't free the seen
1035 types. */
1037 static int
1038 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1039 bool *different_types_p)
1041 const_tree t1 = type1;
1042 const_tree t2 = type2;
1043 int attrval, val;
1045 /* Suppress errors caused by previously reported errors. */
1047 if (t1 == t2 || !t1 || !t2
1048 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1049 return 1;
1051 /* Enumerated types are compatible with integer types, but this is
1052 not transitive: two enumerated types in the same translation unit
1053 are compatible with each other only if they are the same type. */
1055 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1057 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1058 if (TREE_CODE (t2) != VOID_TYPE)
1060 if (enum_and_int_p != NULL)
1061 *enum_and_int_p = true;
1062 if (different_types_p != NULL)
1063 *different_types_p = true;
1066 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1068 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1069 if (TREE_CODE (t1) != VOID_TYPE)
1071 if (enum_and_int_p != NULL)
1072 *enum_and_int_p = true;
1073 if (different_types_p != NULL)
1074 *different_types_p = true;
1078 if (t1 == t2)
1079 return 1;
1081 /* Different classes of types can't be compatible. */
1083 if (TREE_CODE (t1) != TREE_CODE (t2))
1084 return 0;
1086 /* Qualifiers must match. C99 6.7.3p9 */
1088 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1089 return 0;
1091 /* Allow for two different type nodes which have essentially the same
1092 definition. Note that we already checked for equality of the type
1093 qualifiers (just above). */
1095 if (TREE_CODE (t1) != ARRAY_TYPE
1096 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1097 return 1;
1099 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1100 if (!(attrval = comp_type_attributes (t1, t2)))
1101 return 0;
1103 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1104 val = 0;
1106 switch (TREE_CODE (t1))
1108 case INTEGER_TYPE:
1109 case FIXED_POINT_TYPE:
1110 case REAL_TYPE:
1111 /* With these nodes, we can't determine type equivalence by
1112 looking at what is stored in the nodes themselves, because
1113 two nodes might have different TYPE_MAIN_VARIANTs but still
1114 represent the same type. For example, wchar_t and int could
1115 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1116 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1117 and are distinct types. On the other hand, int and the
1118 following typedef
1120 typedef int INT __attribute((may_alias));
1122 have identical properties, different TYPE_MAIN_VARIANTs, but
1123 represent the same type. The canonical type system keeps
1124 track of equivalence in this case, so we fall back on it. */
1125 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1127 case POINTER_TYPE:
1128 /* Do not remove mode information. */
1129 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1130 break;
1131 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1132 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1133 enum_and_int_p, different_types_p));
1134 break;
1136 case FUNCTION_TYPE:
1137 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1138 different_types_p);
1139 break;
1141 case ARRAY_TYPE:
1143 tree d1 = TYPE_DOMAIN (t1);
1144 tree d2 = TYPE_DOMAIN (t2);
1145 bool d1_variable, d2_variable;
1146 bool d1_zero, d2_zero;
1147 val = 1;
1149 /* Target types must match incl. qualifiers. */
1150 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1151 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1152 enum_and_int_p,
1153 different_types_p)))
1154 return 0;
1156 if (different_types_p != NULL
1157 && (d1 == 0) != (d2 == 0))
1158 *different_types_p = true;
1159 /* Sizes must match unless one is missing or variable. */
1160 if (d1 == 0 || d2 == 0 || d1 == d2)
1161 break;
1163 d1_zero = !TYPE_MAX_VALUE (d1);
1164 d2_zero = !TYPE_MAX_VALUE (d2);
1166 d1_variable = (!d1_zero
1167 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1168 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1169 d2_variable = (!d2_zero
1170 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1171 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1172 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1173 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1175 if (different_types_p != NULL
1176 && d1_variable != d2_variable)
1177 *different_types_p = true;
1178 if (d1_variable || d2_variable)
1179 break;
1180 if (d1_zero && d2_zero)
1181 break;
1182 if (d1_zero || d2_zero
1183 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1184 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1185 val = 0;
1187 break;
1190 case ENUMERAL_TYPE:
1191 case RECORD_TYPE:
1192 case UNION_TYPE:
1193 if (val != 1 && !same_translation_unit_p (t1, t2))
1195 tree a1 = TYPE_ATTRIBUTES (t1);
1196 tree a2 = TYPE_ATTRIBUTES (t2);
1198 if (! attribute_list_contained (a1, a2)
1199 && ! attribute_list_contained (a2, a1))
1200 break;
1202 if (attrval != 2)
1203 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1204 different_types_p);
1205 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1206 different_types_p);
1208 break;
1210 case VECTOR_TYPE:
1211 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1212 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1213 enum_and_int_p, different_types_p));
1214 break;
1216 default:
1217 break;
1219 return attrval == 2 && val == 1 ? 2 : val;
1222 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1223 their qualifiers, except for named address spaces. If the pointers point to
1224 different named addresses, then we must determine if one address space is a
1225 subset of the other. */
1227 static int
1228 comp_target_types (location_t location, tree ttl, tree ttr)
1230 int val;
1231 int val_ped;
1232 tree mvl = TREE_TYPE (ttl);
1233 tree mvr = TREE_TYPE (ttr);
1234 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1235 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1236 addr_space_t as_common;
1237 bool enum_and_int_p;
1239 /* Fail if pointers point to incompatible address spaces. */
1240 if (!addr_space_superset (asl, asr, &as_common))
1241 return 0;
1243 /* For pedantic record result of comptypes on arrays before losing
1244 qualifiers on the element type below. */
1245 val_ped = 1;
1247 if (TREE_CODE (mvl) == ARRAY_TYPE
1248 && TREE_CODE (mvr) == ARRAY_TYPE)
1249 val_ped = comptypes (mvl, mvr);
1251 /* Qualifiers on element types of array types that are
1252 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1254 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1255 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1256 : TYPE_MAIN_VARIANT (mvl));
1258 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1259 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1260 : TYPE_MAIN_VARIANT (mvr));
1262 enum_and_int_p = false;
1263 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1265 if (val == 1 && val_ped != 1)
1266 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1267 "are incompatible in ISO C");
1269 if (val == 2)
1270 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1272 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1273 warning_at (location, OPT_Wc___compat,
1274 "pointer target types incompatible in C++");
1276 return val;
1279 /* Subroutines of `comptypes'. */
1281 /* Determine whether two trees derive from the same translation unit.
1282 If the CONTEXT chain ends in a null, that tree's context is still
1283 being parsed, so if two trees have context chains ending in null,
1284 they're in the same translation unit. */
1286 same_translation_unit_p (const_tree t1, const_tree t2)
1288 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1289 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1291 case tcc_declaration:
1292 t1 = DECL_CONTEXT (t1); break;
1293 case tcc_type:
1294 t1 = TYPE_CONTEXT (t1); break;
1295 case tcc_exceptional:
1296 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1297 default: gcc_unreachable ();
1300 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1301 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1303 case tcc_declaration:
1304 t2 = DECL_CONTEXT (t2); break;
1305 case tcc_type:
1306 t2 = TYPE_CONTEXT (t2); break;
1307 case tcc_exceptional:
1308 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1309 default: gcc_unreachable ();
1312 return t1 == t2;
1315 /* Allocate the seen two types, assuming that they are compatible. */
1317 static struct tagged_tu_seen_cache *
1318 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1320 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1321 tu->next = tagged_tu_seen_base;
1322 tu->t1 = t1;
1323 tu->t2 = t2;
1325 tagged_tu_seen_base = tu;
1327 /* The C standard says that two structures in different translation
1328 units are compatible with each other only if the types of their
1329 fields are compatible (among other things). We assume that they
1330 are compatible until proven otherwise when building the cache.
1331 An example where this can occur is:
1332 struct a
1334 struct a *next;
1336 If we are comparing this against a similar struct in another TU,
1337 and did not assume they were compatible, we end up with an infinite
1338 loop. */
1339 tu->val = 1;
1340 return tu;
1343 /* Free the seen types until we get to TU_TIL. */
1345 static void
1346 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1348 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1349 while (tu != tu_til)
1351 const struct tagged_tu_seen_cache *const tu1
1352 = (const struct tagged_tu_seen_cache *) tu;
1353 tu = tu1->next;
1354 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1356 tagged_tu_seen_base = tu_til;
1359 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1360 compatible. If the two types are not the same (which has been
1361 checked earlier), this can only happen when multiple translation
1362 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1363 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1364 comptypes_internal. */
1366 static int
1367 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1368 bool *enum_and_int_p, bool *different_types_p)
1370 tree s1, s2;
1371 bool needs_warning = false;
1373 /* We have to verify that the tags of the types are the same. This
1374 is harder than it looks because this may be a typedef, so we have
1375 to go look at the original type. It may even be a typedef of a
1376 typedef...
1377 In the case of compiler-created builtin structs the TYPE_DECL
1378 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1379 while (TYPE_NAME (t1)
1380 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1381 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1382 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1384 while (TYPE_NAME (t2)
1385 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1386 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1387 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1389 /* C90 didn't have the requirement that the two tags be the same. */
1390 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1391 return 0;
1393 /* C90 didn't say what happened if one or both of the types were
1394 incomplete; we choose to follow C99 rules here, which is that they
1395 are compatible. */
1396 if (TYPE_SIZE (t1) == NULL
1397 || TYPE_SIZE (t2) == NULL)
1398 return 1;
1401 const struct tagged_tu_seen_cache * tts_i;
1402 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1403 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1404 return tts_i->val;
1407 switch (TREE_CODE (t1))
1409 case ENUMERAL_TYPE:
1411 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1412 /* Speed up the case where the type values are in the same order. */
1413 tree tv1 = TYPE_VALUES (t1);
1414 tree tv2 = TYPE_VALUES (t2);
1416 if (tv1 == tv2)
1418 return 1;
1421 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1423 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1424 break;
1425 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1427 tu->val = 0;
1428 return 0;
1432 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1434 return 1;
1436 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1438 tu->val = 0;
1439 return 0;
1442 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1444 tu->val = 0;
1445 return 0;
1448 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1450 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1451 if (s2 == NULL
1452 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1454 tu->val = 0;
1455 return 0;
1458 return 1;
1461 case UNION_TYPE:
1463 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1464 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1466 tu->val = 0;
1467 return 0;
1470 /* Speed up the common case where the fields are in the same order. */
1471 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1472 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1474 int result;
1476 if (DECL_NAME (s1) != DECL_NAME (s2))
1477 break;
1478 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1479 enum_and_int_p, different_types_p);
1481 if (result != 1 && !DECL_NAME (s1))
1482 break;
1483 if (result == 0)
1485 tu->val = 0;
1486 return 0;
1488 if (result == 2)
1489 needs_warning = true;
1491 if (TREE_CODE (s1) == FIELD_DECL
1492 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1493 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1495 tu->val = 0;
1496 return 0;
1499 if (!s1 && !s2)
1501 tu->val = needs_warning ? 2 : 1;
1502 return tu->val;
1505 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1507 bool ok = false;
1509 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1510 if (DECL_NAME (s1) == DECL_NAME (s2))
1512 int result;
1514 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1515 enum_and_int_p,
1516 different_types_p);
1518 if (result != 1 && !DECL_NAME (s1))
1519 continue;
1520 if (result == 0)
1522 tu->val = 0;
1523 return 0;
1525 if (result == 2)
1526 needs_warning = true;
1528 if (TREE_CODE (s1) == FIELD_DECL
1529 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1530 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1531 break;
1533 ok = true;
1534 break;
1536 if (!ok)
1538 tu->val = 0;
1539 return 0;
1542 tu->val = needs_warning ? 2 : 10;
1543 return tu->val;
1546 case RECORD_TYPE:
1548 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1550 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1551 s1 && s2;
1552 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1554 int result;
1555 if (TREE_CODE (s1) != TREE_CODE (s2)
1556 || DECL_NAME (s1) != DECL_NAME (s2))
1557 break;
1558 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1559 enum_and_int_p, different_types_p);
1560 if (result == 0)
1561 break;
1562 if (result == 2)
1563 needs_warning = true;
1565 if (TREE_CODE (s1) == FIELD_DECL
1566 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1567 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1568 break;
1570 if (s1 && s2)
1571 tu->val = 0;
1572 else
1573 tu->val = needs_warning ? 2 : 1;
1574 return tu->val;
1577 default:
1578 gcc_unreachable ();
1582 /* Return 1 if two function types F1 and F2 are compatible.
1583 If either type specifies no argument types,
1584 the other must specify a fixed number of self-promoting arg types.
1585 Otherwise, if one type specifies only the number of arguments,
1586 the other must specify that number of self-promoting arg types.
1587 Otherwise, the argument types must match.
1588 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1590 static int
1591 function_types_compatible_p (const_tree f1, const_tree f2,
1592 bool *enum_and_int_p, bool *different_types_p)
1594 tree args1, args2;
1595 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1596 int val = 1;
1597 int val1;
1598 tree ret1, ret2;
1600 ret1 = TREE_TYPE (f1);
1601 ret2 = TREE_TYPE (f2);
1603 /* 'volatile' qualifiers on a function's return type used to mean
1604 the function is noreturn. */
1605 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1606 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1607 if (TYPE_VOLATILE (ret1))
1608 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1609 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1610 if (TYPE_VOLATILE (ret2))
1611 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1612 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1613 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1614 if (val == 0)
1615 return 0;
1617 args1 = TYPE_ARG_TYPES (f1);
1618 args2 = TYPE_ARG_TYPES (f2);
1620 if (different_types_p != NULL
1621 && (args1 == 0) != (args2 == 0))
1622 *different_types_p = true;
1624 /* An unspecified parmlist matches any specified parmlist
1625 whose argument types don't need default promotions. */
1627 if (args1 == 0)
1629 if (!self_promoting_args_p (args2))
1630 return 0;
1631 /* If one of these types comes from a non-prototype fn definition,
1632 compare that with the other type's arglist.
1633 If they don't match, ask for a warning (but no error). */
1634 if (TYPE_ACTUAL_ARG_TYPES (f1)
1635 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1636 enum_and_int_p, different_types_p))
1637 val = 2;
1638 return val;
1640 if (args2 == 0)
1642 if (!self_promoting_args_p (args1))
1643 return 0;
1644 if (TYPE_ACTUAL_ARG_TYPES (f2)
1645 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1646 enum_and_int_p, different_types_p))
1647 val = 2;
1648 return val;
1651 /* Both types have argument lists: compare them and propagate results. */
1652 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1653 different_types_p);
1654 return val1 != 1 ? val1 : val;
1657 /* Check two lists of types for compatibility, returning 0 for
1658 incompatible, 1 for compatible, or 2 for compatible with
1659 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1660 comptypes_internal. */
1662 static int
1663 type_lists_compatible_p (const_tree args1, const_tree args2,
1664 bool *enum_and_int_p, bool *different_types_p)
1666 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1667 int val = 1;
1668 int newval = 0;
1670 while (1)
1672 tree a1, mv1, a2, mv2;
1673 if (args1 == 0 && args2 == 0)
1674 return val;
1675 /* If one list is shorter than the other,
1676 they fail to match. */
1677 if (args1 == 0 || args2 == 0)
1678 return 0;
1679 mv1 = a1 = TREE_VALUE (args1);
1680 mv2 = a2 = TREE_VALUE (args2);
1681 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1682 mv1 = (TYPE_ATOMIC (mv1)
1683 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1684 TYPE_QUAL_ATOMIC)
1685 : TYPE_MAIN_VARIANT (mv1));
1686 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1687 mv2 = (TYPE_ATOMIC (mv2)
1688 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1689 TYPE_QUAL_ATOMIC)
1690 : TYPE_MAIN_VARIANT (mv2));
1691 /* A null pointer instead of a type
1692 means there is supposed to be an argument
1693 but nothing is specified about what type it has.
1694 So match anything that self-promotes. */
1695 if (different_types_p != NULL
1696 && (a1 == 0) != (a2 == 0))
1697 *different_types_p = true;
1698 if (a1 == 0)
1700 if (c_type_promotes_to (a2) != a2)
1701 return 0;
1703 else if (a2 == 0)
1705 if (c_type_promotes_to (a1) != a1)
1706 return 0;
1708 /* If one of the lists has an error marker, ignore this arg. */
1709 else if (TREE_CODE (a1) == ERROR_MARK
1710 || TREE_CODE (a2) == ERROR_MARK)
1712 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1713 different_types_p)))
1715 if (different_types_p != NULL)
1716 *different_types_p = true;
1717 /* Allow wait (union {union wait *u; int *i} *)
1718 and wait (union wait *) to be compatible. */
1719 if (TREE_CODE (a1) == UNION_TYPE
1720 && (TYPE_NAME (a1) == 0
1721 || TYPE_TRANSPARENT_AGGR (a1))
1722 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1723 && tree_int_cst_equal (TYPE_SIZE (a1),
1724 TYPE_SIZE (a2)))
1726 tree memb;
1727 for (memb = TYPE_FIELDS (a1);
1728 memb; memb = DECL_CHAIN (memb))
1730 tree mv3 = TREE_TYPE (memb);
1731 if (mv3 && mv3 != error_mark_node
1732 && TREE_CODE (mv3) != ARRAY_TYPE)
1733 mv3 = (TYPE_ATOMIC (mv3)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1735 TYPE_QUAL_ATOMIC)
1736 : TYPE_MAIN_VARIANT (mv3));
1737 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1738 different_types_p))
1739 break;
1741 if (memb == 0)
1742 return 0;
1744 else if (TREE_CODE (a2) == UNION_TYPE
1745 && (TYPE_NAME (a2) == 0
1746 || TYPE_TRANSPARENT_AGGR (a2))
1747 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1748 && tree_int_cst_equal (TYPE_SIZE (a2),
1749 TYPE_SIZE (a1)))
1751 tree memb;
1752 for (memb = TYPE_FIELDS (a2);
1753 memb; memb = DECL_CHAIN (memb))
1755 tree mv3 = TREE_TYPE (memb);
1756 if (mv3 && mv3 != error_mark_node
1757 && TREE_CODE (mv3) != ARRAY_TYPE)
1758 mv3 = (TYPE_ATOMIC (mv3)
1759 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1760 TYPE_QUAL_ATOMIC)
1761 : TYPE_MAIN_VARIANT (mv3));
1762 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1763 different_types_p))
1764 break;
1766 if (memb == 0)
1767 return 0;
1769 else
1770 return 0;
1773 /* comptypes said ok, but record if it said to warn. */
1774 if (newval > val)
1775 val = newval;
1777 args1 = TREE_CHAIN (args1);
1778 args2 = TREE_CHAIN (args2);
1782 /* Compute the size to increment a pointer by. When a function type or void
1783 type or incomplete type is passed, size_one_node is returned.
1784 This function does not emit any diagnostics; the caller is responsible
1785 for that. */
1787 static tree
1788 c_size_in_bytes (const_tree type)
1790 enum tree_code code = TREE_CODE (type);
1792 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1793 || !COMPLETE_TYPE_P (type))
1794 return size_one_node;
1796 /* Convert in case a char is more than one unit. */
1797 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1798 size_int (TYPE_PRECISION (char_type_node)
1799 / BITS_PER_UNIT));
1802 /* Return either DECL or its known constant value (if it has one). */
1804 tree
1805 decl_constant_value (tree decl)
1807 if (/* Don't change a variable array bound or initial value to a constant
1808 in a place where a variable is invalid. Note that DECL_INITIAL
1809 isn't valid for a PARM_DECL. */
1810 current_function_decl != 0
1811 && TREE_CODE (decl) != PARM_DECL
1812 && !TREE_THIS_VOLATILE (decl)
1813 && TREE_READONLY (decl)
1814 && DECL_INITIAL (decl) != 0
1815 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1816 /* This is invalid if initial value is not constant.
1817 If it has either a function call, a memory reference,
1818 or a variable, then re-evaluating it could give different results. */
1819 && TREE_CONSTANT (DECL_INITIAL (decl))
1820 /* Check for cases where this is sub-optimal, even though valid. */
1821 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1822 return DECL_INITIAL (decl);
1823 return decl;
1826 /* Convert the array expression EXP to a pointer. */
1827 static tree
1828 array_to_pointer_conversion (location_t loc, tree exp)
1830 tree orig_exp = exp;
1831 tree type = TREE_TYPE (exp);
1832 tree adr;
1833 tree restype = TREE_TYPE (type);
1834 tree ptrtype;
1836 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1838 STRIP_TYPE_NOPS (exp);
1840 if (TREE_NO_WARNING (orig_exp))
1841 TREE_NO_WARNING (exp) = 1;
1843 ptrtype = build_pointer_type (restype);
1845 if (INDIRECT_REF_P (exp))
1846 return convert (ptrtype, TREE_OPERAND (exp, 0));
1848 /* In C++ array compound literals are temporary objects unless they are
1849 const or appear in namespace scope, so they are destroyed too soon
1850 to use them for much of anything (c++/53220). */
1851 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1853 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1854 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1855 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1856 "converting an array compound literal to a pointer "
1857 "is ill-formed in C++");
1860 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1861 return convert (ptrtype, adr);
1864 /* Convert the function expression EXP to a pointer. */
1865 static tree
1866 function_to_pointer_conversion (location_t loc, tree exp)
1868 tree orig_exp = exp;
1870 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1872 STRIP_TYPE_NOPS (exp);
1874 if (TREE_NO_WARNING (orig_exp))
1875 TREE_NO_WARNING (exp) = 1;
1877 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1880 /* Mark EXP as read, not just set, for set but not used -Wunused
1881 warning purposes. */
1883 void
1884 mark_exp_read (tree exp)
1886 switch (TREE_CODE (exp))
1888 case VAR_DECL:
1889 case PARM_DECL:
1890 DECL_READ_P (exp) = 1;
1891 break;
1892 case ARRAY_REF:
1893 case COMPONENT_REF:
1894 case MODIFY_EXPR:
1895 case REALPART_EXPR:
1896 case IMAGPART_EXPR:
1897 CASE_CONVERT:
1898 case ADDR_EXPR:
1899 mark_exp_read (TREE_OPERAND (exp, 0));
1900 break;
1901 case COMPOUND_EXPR:
1902 case C_MAYBE_CONST_EXPR:
1903 mark_exp_read (TREE_OPERAND (exp, 1));
1904 break;
1905 default:
1906 break;
1910 /* Perform the default conversion of arrays and functions to pointers.
1911 Return the result of converting EXP. For any other expression, just
1912 return EXP.
1914 LOC is the location of the expression. */
1916 struct c_expr
1917 default_function_array_conversion (location_t loc, struct c_expr exp)
1919 tree orig_exp = exp.value;
1920 tree type = TREE_TYPE (exp.value);
1921 enum tree_code code = TREE_CODE (type);
1923 switch (code)
1925 case ARRAY_TYPE:
1927 bool not_lvalue = false;
1928 bool lvalue_array_p;
1930 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1931 || CONVERT_EXPR_P (exp.value))
1932 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1934 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1935 not_lvalue = true;
1936 exp.value = TREE_OPERAND (exp.value, 0);
1939 if (TREE_NO_WARNING (orig_exp))
1940 TREE_NO_WARNING (exp.value) = 1;
1942 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1943 if (!flag_isoc99 && !lvalue_array_p)
1945 /* Before C99, non-lvalue arrays do not decay to pointers.
1946 Normally, using such an array would be invalid; but it can
1947 be used correctly inside sizeof or as a statement expression.
1948 Thus, do not give an error here; an error will result later. */
1949 return exp;
1952 exp.value = array_to_pointer_conversion (loc, exp.value);
1954 break;
1955 case FUNCTION_TYPE:
1956 exp.value = function_to_pointer_conversion (loc, exp.value);
1957 break;
1958 default:
1959 break;
1962 return exp;
1965 struct c_expr
1966 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1968 mark_exp_read (exp.value);
1969 return default_function_array_conversion (loc, exp);
1972 /* Return whether EXPR should be treated as an atomic lvalue for the
1973 purposes of load and store handling. */
1975 static bool
1976 really_atomic_lvalue (tree expr)
1978 if (error_operand_p (expr))
1979 return false;
1980 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1981 return false;
1982 if (!lvalue_p (expr))
1983 return false;
1985 /* Ignore _Atomic on register variables, since their addresses can't
1986 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1987 sequences wouldn't work. Ignore _Atomic on structures containing
1988 bit-fields, since accessing elements of atomic structures or
1989 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1990 it's undefined at translation time or execution time, and the
1991 normal atomic sequences again wouldn't work. */
1992 while (handled_component_p (expr))
1994 if (TREE_CODE (expr) == COMPONENT_REF
1995 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1996 return false;
1997 expr = TREE_OPERAND (expr, 0);
1999 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2000 return false;
2001 return true;
2004 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2005 including converting functions and arrays to pointers if CONVERT_P.
2006 If READ_P, also mark the expression as having been read. */
2008 struct c_expr
2009 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2010 bool convert_p, bool read_p)
2012 if (read_p)
2013 mark_exp_read (exp.value);
2014 if (convert_p)
2015 exp = default_function_array_conversion (loc, exp);
2016 if (really_atomic_lvalue (exp.value))
2018 vec<tree, va_gc> *params;
2019 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2020 tree expr_type = TREE_TYPE (exp.value);
2021 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2022 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2024 gcc_assert (TYPE_ATOMIC (expr_type));
2026 /* Expansion of a generic atomic load may require an addition
2027 element, so allocate enough to prevent a resize. */
2028 vec_alloc (params, 4);
2030 /* Remove the qualifiers for the rest of the expressions and
2031 create the VAL temp variable to hold the RHS. */
2032 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2033 tmp = create_tmp_var_raw (nonatomic_type);
2034 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2035 TREE_ADDRESSABLE (tmp) = 1;
2036 TREE_NO_WARNING (tmp) = 1;
2038 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2039 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2040 params->quick_push (expr_addr);
2041 params->quick_push (tmp_addr);
2042 params->quick_push (seq_cst);
2043 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2045 /* EXPR is always read. */
2046 mark_exp_read (exp.value);
2048 /* Return tmp which contains the value loaded. */
2049 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2050 NULL_TREE, NULL_TREE);
2052 return exp;
2055 /* EXP is an expression of integer type. Apply the integer promotions
2056 to it and return the promoted value. */
2058 tree
2059 perform_integral_promotions (tree exp)
2061 tree type = TREE_TYPE (exp);
2062 enum tree_code code = TREE_CODE (type);
2064 gcc_assert (INTEGRAL_TYPE_P (type));
2066 /* Normally convert enums to int,
2067 but convert wide enums to something wider. */
2068 if (code == ENUMERAL_TYPE)
2070 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2071 TYPE_PRECISION (integer_type_node)),
2072 ((TYPE_PRECISION (type)
2073 >= TYPE_PRECISION (integer_type_node))
2074 && TYPE_UNSIGNED (type)));
2076 return convert (type, exp);
2079 /* ??? This should no longer be needed now bit-fields have their
2080 proper types. */
2081 if (TREE_CODE (exp) == COMPONENT_REF
2082 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2083 /* If it's thinner than an int, promote it like a
2084 c_promoting_integer_type_p, otherwise leave it alone. */
2085 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2086 TYPE_PRECISION (integer_type_node)))
2087 return convert (integer_type_node, exp);
2089 if (c_promoting_integer_type_p (type))
2091 /* Preserve unsignedness if not really getting any wider. */
2092 if (TYPE_UNSIGNED (type)
2093 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2094 return convert (unsigned_type_node, exp);
2096 return convert (integer_type_node, exp);
2099 return exp;
2103 /* Perform default promotions for C data used in expressions.
2104 Enumeral types or short or char are converted to int.
2105 In addition, manifest constants symbols are replaced by their values. */
2107 tree
2108 default_conversion (tree exp)
2110 tree orig_exp;
2111 tree type = TREE_TYPE (exp);
2112 enum tree_code code = TREE_CODE (type);
2113 tree promoted_type;
2115 mark_exp_read (exp);
2117 /* Functions and arrays have been converted during parsing. */
2118 gcc_assert (code != FUNCTION_TYPE);
2119 if (code == ARRAY_TYPE)
2120 return exp;
2122 /* Constants can be used directly unless they're not loadable. */
2123 if (TREE_CODE (exp) == CONST_DECL)
2124 exp = DECL_INITIAL (exp);
2126 /* Strip no-op conversions. */
2127 orig_exp = exp;
2128 STRIP_TYPE_NOPS (exp);
2130 if (TREE_NO_WARNING (orig_exp))
2131 TREE_NO_WARNING (exp) = 1;
2133 if (code == VOID_TYPE)
2135 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2136 "void value not ignored as it ought to be");
2137 return error_mark_node;
2140 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2141 if (exp == error_mark_node)
2142 return error_mark_node;
2144 promoted_type = targetm.promoted_type (type);
2145 if (promoted_type)
2146 return convert (promoted_type, exp);
2148 if (INTEGRAL_TYPE_P (type))
2149 return perform_integral_promotions (exp);
2151 return exp;
2154 /* Look up COMPONENT in a structure or union TYPE.
2156 If the component name is not found, returns NULL_TREE. Otherwise,
2157 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2158 stepping down the chain to the component, which is in the last
2159 TREE_VALUE of the list. Normally the list is of length one, but if
2160 the component is embedded within (nested) anonymous structures or
2161 unions, the list steps down the chain to the component. */
2163 static tree
2164 lookup_field (tree type, tree component)
2166 tree field;
2168 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2169 to the field elements. Use a binary search on this array to quickly
2170 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2171 will always be set for structures which have many elements. */
2173 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2175 int bot, top, half;
2176 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2178 field = TYPE_FIELDS (type);
2179 bot = 0;
2180 top = TYPE_LANG_SPECIFIC (type)->s->len;
2181 while (top - bot > 1)
2183 half = (top - bot + 1) >> 1;
2184 field = field_array[bot+half];
2186 if (DECL_NAME (field) == NULL_TREE)
2188 /* Step through all anon unions in linear fashion. */
2189 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2191 field = field_array[bot++];
2192 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2194 tree anon = lookup_field (TREE_TYPE (field), component);
2196 if (anon)
2197 return tree_cons (NULL_TREE, field, anon);
2199 /* The Plan 9 compiler permits referring
2200 directly to an anonymous struct/union field
2201 using a typedef name. */
2202 if (flag_plan9_extensions
2203 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2204 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2205 == TYPE_DECL)
2206 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2207 == component))
2208 break;
2212 /* Entire record is only anon unions. */
2213 if (bot > top)
2214 return NULL_TREE;
2216 /* Restart the binary search, with new lower bound. */
2217 continue;
2220 if (DECL_NAME (field) == component)
2221 break;
2222 if (DECL_NAME (field) < component)
2223 bot += half;
2224 else
2225 top = bot + half;
2228 if (DECL_NAME (field_array[bot]) == component)
2229 field = field_array[bot];
2230 else if (DECL_NAME (field) != component)
2231 return NULL_TREE;
2233 else
2235 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2237 if (DECL_NAME (field) == NULL_TREE
2238 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2240 tree anon = lookup_field (TREE_TYPE (field), component);
2242 if (anon)
2243 return tree_cons (NULL_TREE, field, anon);
2245 /* The Plan 9 compiler permits referring directly to an
2246 anonymous struct/union field using a typedef
2247 name. */
2248 if (flag_plan9_extensions
2249 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2250 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2251 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2252 == component))
2253 break;
2256 if (DECL_NAME (field) == component)
2257 break;
2260 if (field == NULL_TREE)
2261 return NULL_TREE;
2264 return tree_cons (NULL_TREE, field, NULL_TREE);
2267 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2269 static void
2270 lookup_field_fuzzy_find_candidates (tree type, tree component,
2271 vec<tree> *candidates)
2273 tree field;
2274 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2276 if (DECL_NAME (field) == NULL_TREE
2277 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2278 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2279 candidates);
2281 if (DECL_NAME (field))
2282 candidates->safe_push (DECL_NAME (field));
2286 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2287 rather than returning a TREE_LIST for an exact match. */
2289 static tree
2290 lookup_field_fuzzy (tree type, tree component)
2292 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2294 /* First, gather a list of candidates. */
2295 auto_vec <tree> candidates;
2297 lookup_field_fuzzy_find_candidates (type, component,
2298 &candidates);
2300 return find_closest_identifier (component, &candidates);
2303 /* Support function for build_component_ref's error-handling.
2305 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2306 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2308 static bool
2309 should_suggest_deref_p (tree datum_type)
2311 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2312 allows "." for ptrs; we could be handling a failed attempt
2313 to access a property. */
2314 if (c_dialect_objc ())
2315 return false;
2317 /* Only suggest it for pointers... */
2318 if (TREE_CODE (datum_type) != POINTER_TYPE)
2319 return false;
2321 /* ...to structs/unions. */
2322 tree underlying_type = TREE_TYPE (datum_type);
2323 enum tree_code code = TREE_CODE (underlying_type);
2324 if (code == RECORD_TYPE || code == UNION_TYPE)
2325 return true;
2326 else
2327 return false;
2330 /* Make an expression to refer to the COMPONENT field of structure or
2331 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2332 location of the COMPONENT_REF. COMPONENT_LOC is the location
2333 of COMPONENT. */
2335 tree
2336 build_component_ref (location_t loc, tree datum, tree component,
2337 location_t component_loc)
2339 tree type = TREE_TYPE (datum);
2340 enum tree_code code = TREE_CODE (type);
2341 tree field = NULL;
2342 tree ref;
2343 bool datum_lvalue = lvalue_p (datum);
2345 if (!objc_is_public (datum, component))
2346 return error_mark_node;
2348 /* Detect Objective-C property syntax object.property. */
2349 if (c_dialect_objc ()
2350 && (ref = objc_maybe_build_component_ref (datum, component)))
2351 return ref;
2353 /* See if there is a field or component with name COMPONENT. */
2355 if (code == RECORD_TYPE || code == UNION_TYPE)
2357 if (!COMPLETE_TYPE_P (type))
2359 c_incomplete_type_error (loc, NULL_TREE, type);
2360 return error_mark_node;
2363 field = lookup_field (type, component);
2365 if (!field)
2367 tree guessed_id = lookup_field_fuzzy (type, component);
2368 if (guessed_id)
2370 /* Attempt to provide a fixit replacement hint, if
2371 we have a valid range for the component. */
2372 location_t reported_loc
2373 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2374 gcc_rich_location rich_loc (reported_loc);
2375 if (component_loc != UNKNOWN_LOCATION)
2376 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2377 error_at_rich_loc
2378 (&rich_loc,
2379 "%qT has no member named %qE; did you mean %qE?",
2380 type, component, guessed_id);
2382 else
2383 error_at (loc, "%qT has no member named %qE", type, component);
2384 return error_mark_node;
2387 /* Accessing elements of atomic structures or unions is undefined
2388 behavior (C11 6.5.2.3#5). */
2389 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2391 if (code == RECORD_TYPE)
2392 warning_at (loc, 0, "accessing a member %qE of an atomic "
2393 "structure %qE", component, datum);
2394 else
2395 warning_at (loc, 0, "accessing a member %qE of an atomic "
2396 "union %qE", component, datum);
2399 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2400 This might be better solved in future the way the C++ front
2401 end does it - by giving the anonymous entities each a
2402 separate name and type, and then have build_component_ref
2403 recursively call itself. We can't do that here. */
2406 tree subdatum = TREE_VALUE (field);
2407 int quals;
2408 tree subtype;
2409 bool use_datum_quals;
2411 if (TREE_TYPE (subdatum) == error_mark_node)
2412 return error_mark_node;
2414 /* If this is an rvalue, it does not have qualifiers in C
2415 standard terms and we must avoid propagating such
2416 qualifiers down to a non-lvalue array that is then
2417 converted to a pointer. */
2418 use_datum_quals = (datum_lvalue
2419 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2421 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2422 if (use_datum_quals)
2423 quals |= TYPE_QUALS (TREE_TYPE (datum));
2424 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2426 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2427 NULL_TREE);
2428 SET_EXPR_LOCATION (ref, loc);
2429 if (TREE_READONLY (subdatum)
2430 || (use_datum_quals && TREE_READONLY (datum)))
2431 TREE_READONLY (ref) = 1;
2432 if (TREE_THIS_VOLATILE (subdatum)
2433 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2434 TREE_THIS_VOLATILE (ref) = 1;
2436 if (TREE_DEPRECATED (subdatum))
2437 warn_deprecated_use (subdatum, NULL_TREE);
2439 datum = ref;
2441 field = TREE_CHAIN (field);
2443 while (field);
2445 return ref;
2447 else if (should_suggest_deref_p (type))
2449 /* Special-case the error message for "ptr.field" for the case
2450 where the user has confused "." vs "->". */
2451 rich_location richloc (line_table, loc);
2452 /* "loc" should be the "." token. */
2453 richloc.add_fixit_replace (source_range::from_location (loc), "->");
2454 error_at_rich_loc (&richloc,
2455 "%qE is a pointer; did you mean to use %<->%>?",
2456 datum);
2457 return error_mark_node;
2459 else if (code != ERROR_MARK)
2460 error_at (loc,
2461 "request for member %qE in something not a structure or union",
2462 component);
2464 return error_mark_node;
2467 /* Given an expression PTR for a pointer, return an expression
2468 for the value pointed to.
2469 ERRORSTRING is the name of the operator to appear in error messages.
2471 LOC is the location to use for the generated tree. */
2473 tree
2474 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2476 tree pointer = default_conversion (ptr);
2477 tree type = TREE_TYPE (pointer);
2478 tree ref;
2480 if (TREE_CODE (type) == POINTER_TYPE)
2482 if (CONVERT_EXPR_P (pointer)
2483 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2485 /* If a warning is issued, mark it to avoid duplicates from
2486 the backend. This only needs to be done at
2487 warn_strict_aliasing > 2. */
2488 if (warn_strict_aliasing > 2)
2489 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2490 type, TREE_OPERAND (pointer, 0)))
2491 TREE_NO_WARNING (pointer) = 1;
2494 if (TREE_CODE (pointer) == ADDR_EXPR
2495 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2496 == TREE_TYPE (type)))
2498 ref = TREE_OPERAND (pointer, 0);
2499 protected_set_expr_location (ref, loc);
2500 return ref;
2502 else
2504 tree t = TREE_TYPE (type);
2506 ref = build1 (INDIRECT_REF, t, pointer);
2508 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2510 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2512 error_at (loc, "dereferencing pointer to incomplete type "
2513 "%qT", t);
2514 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2516 return error_mark_node;
2518 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2519 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2521 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2522 so that we get the proper error message if the result is used
2523 to assign to. Also, &* is supposed to be a no-op.
2524 And ANSI C seems to specify that the type of the result
2525 should be the const type. */
2526 /* A de-reference of a pointer to const is not a const. It is valid
2527 to change it via some other pointer. */
2528 TREE_READONLY (ref) = TYPE_READONLY (t);
2529 TREE_SIDE_EFFECTS (ref)
2530 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2531 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2532 protected_set_expr_location (ref, loc);
2533 return ref;
2536 else if (TREE_CODE (pointer) != ERROR_MARK)
2537 invalid_indirection_error (loc, type, errstring);
2539 return error_mark_node;
2542 /* This handles expressions of the form "a[i]", which denotes
2543 an array reference.
2545 This is logically equivalent in C to *(a+i), but we may do it differently.
2546 If A is a variable or a member, we generate a primitive ARRAY_REF.
2547 This avoids forcing the array out of registers, and can work on
2548 arrays that are not lvalues (for example, members of structures returned
2549 by functions).
2551 For vector types, allow vector[i] but not i[vector], and create
2552 *(((type*)&vectortype) + i) for the expression.
2554 LOC is the location to use for the returned expression. */
2556 tree
2557 build_array_ref (location_t loc, tree array, tree index)
2559 tree ret;
2560 bool swapped = false;
2561 if (TREE_TYPE (array) == error_mark_node
2562 || TREE_TYPE (index) == error_mark_node)
2563 return error_mark_node;
2565 if (flag_cilkplus && contains_array_notation_expr (index))
2567 size_t rank = 0;
2568 if (!find_rank (loc, index, index, true, &rank))
2569 return error_mark_node;
2570 if (rank > 1)
2572 error_at (loc, "rank of the array's index is greater than 1");
2573 return error_mark_node;
2576 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2577 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2578 /* Allow vector[index] but not index[vector]. */
2579 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2581 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2582 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2584 error_at (loc,
2585 "subscripted value is neither array nor pointer nor vector");
2587 return error_mark_node;
2589 std::swap (array, index);
2590 swapped = true;
2593 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2595 error_at (loc, "array subscript is not an integer");
2596 return error_mark_node;
2599 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2601 error_at (loc, "subscripted value is pointer to function");
2602 return error_mark_node;
2605 /* ??? Existing practice has been to warn only when the char
2606 index is syntactically the index, not for char[array]. */
2607 if (!swapped)
2608 warn_array_subscript_with_type_char (loc, index);
2610 /* Apply default promotions *after* noticing character types. */
2611 index = default_conversion (index);
2612 if (index == error_mark_node)
2613 return error_mark_node;
2615 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2617 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2618 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2620 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2622 tree rval, type;
2624 /* An array that is indexed by a non-constant
2625 cannot be stored in a register; we must be able to do
2626 address arithmetic on its address.
2627 Likewise an array of elements of variable size. */
2628 if (TREE_CODE (index) != INTEGER_CST
2629 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2630 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2632 if (!c_mark_addressable (array))
2633 return error_mark_node;
2635 /* An array that is indexed by a constant value which is not within
2636 the array bounds cannot be stored in a register either; because we
2637 would get a crash in store_bit_field/extract_bit_field when trying
2638 to access a non-existent part of the register. */
2639 if (TREE_CODE (index) == INTEGER_CST
2640 && TYPE_DOMAIN (TREE_TYPE (array))
2641 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2643 if (!c_mark_addressable (array))
2644 return error_mark_node;
2647 if ((pedantic || warn_c90_c99_compat)
2648 && ! was_vector)
2650 tree foo = array;
2651 while (TREE_CODE (foo) == COMPONENT_REF)
2652 foo = TREE_OPERAND (foo, 0);
2653 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2654 pedwarn (loc, OPT_Wpedantic,
2655 "ISO C forbids subscripting %<register%> array");
2656 else if (!lvalue_p (foo))
2657 pedwarn_c90 (loc, OPT_Wpedantic,
2658 "ISO C90 forbids subscripting non-lvalue "
2659 "array");
2662 type = TREE_TYPE (TREE_TYPE (array));
2663 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2664 /* Array ref is const/volatile if the array elements are
2665 or if the array is. */
2666 TREE_READONLY (rval)
2667 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2668 | TREE_READONLY (array));
2669 TREE_SIDE_EFFECTS (rval)
2670 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2671 | TREE_SIDE_EFFECTS (array));
2672 TREE_THIS_VOLATILE (rval)
2673 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2674 /* This was added by rms on 16 Nov 91.
2675 It fixes vol struct foo *a; a->elts[1]
2676 in an inline function.
2677 Hope it doesn't break something else. */
2678 | TREE_THIS_VOLATILE (array));
2679 ret = require_complete_type (loc, rval);
2680 protected_set_expr_location (ret, loc);
2681 if (non_lvalue)
2682 ret = non_lvalue_loc (loc, ret);
2683 return ret;
2685 else
2687 tree ar = default_conversion (array);
2689 if (ar == error_mark_node)
2690 return ar;
2692 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2693 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2695 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2696 index, 0),
2697 RO_ARRAY_INDEXING);
2698 if (non_lvalue)
2699 ret = non_lvalue_loc (loc, ret);
2700 return ret;
2704 /* Build an external reference to identifier ID. FUN indicates
2705 whether this will be used for a function call. LOC is the source
2706 location of the identifier. This sets *TYPE to the type of the
2707 identifier, which is not the same as the type of the returned value
2708 for CONST_DECLs defined as enum constants. If the type of the
2709 identifier is not available, *TYPE is set to NULL. */
2710 tree
2711 build_external_ref (location_t loc, tree id, int fun, tree *type)
2713 tree ref;
2714 tree decl = lookup_name (id);
2716 /* In Objective-C, an instance variable (ivar) may be preferred to
2717 whatever lookup_name() found. */
2718 decl = objc_lookup_ivar (decl, id);
2720 *type = NULL;
2721 if (decl && decl != error_mark_node)
2723 ref = decl;
2724 *type = TREE_TYPE (ref);
2726 else if (fun)
2727 /* Implicit function declaration. */
2728 ref = implicitly_declare (loc, id);
2729 else if (decl == error_mark_node)
2730 /* Don't complain about something that's already been
2731 complained about. */
2732 return error_mark_node;
2733 else
2735 undeclared_variable (loc, id);
2736 return error_mark_node;
2739 if (TREE_TYPE (ref) == error_mark_node)
2740 return error_mark_node;
2742 if (TREE_DEPRECATED (ref))
2743 warn_deprecated_use (ref, NULL_TREE);
2745 /* Recursive call does not count as usage. */
2746 if (ref != current_function_decl)
2748 TREE_USED (ref) = 1;
2751 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2753 if (!in_sizeof && !in_typeof)
2754 C_DECL_USED (ref) = 1;
2755 else if (DECL_INITIAL (ref) == 0
2756 && DECL_EXTERNAL (ref)
2757 && !TREE_PUBLIC (ref))
2758 record_maybe_used_decl (ref);
2761 if (TREE_CODE (ref) == CONST_DECL)
2763 used_types_insert (TREE_TYPE (ref));
2765 if (warn_cxx_compat
2766 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2767 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2769 warning_at (loc, OPT_Wc___compat,
2770 ("enum constant defined in struct or union "
2771 "is not visible in C++"));
2772 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2775 ref = DECL_INITIAL (ref);
2776 TREE_CONSTANT (ref) = 1;
2778 else if (current_function_decl != 0
2779 && !DECL_FILE_SCOPE_P (current_function_decl)
2780 && (VAR_OR_FUNCTION_DECL_P (ref)
2781 || TREE_CODE (ref) == PARM_DECL))
2783 tree context = decl_function_context (ref);
2785 if (context != 0 && context != current_function_decl)
2786 DECL_NONLOCAL (ref) = 1;
2788 /* C99 6.7.4p3: An inline definition of a function with external
2789 linkage ... shall not contain a reference to an identifier with
2790 internal linkage. */
2791 else if (current_function_decl != 0
2792 && DECL_DECLARED_INLINE_P (current_function_decl)
2793 && DECL_EXTERNAL (current_function_decl)
2794 && VAR_OR_FUNCTION_DECL_P (ref)
2795 && (!VAR_P (ref) || TREE_STATIC (ref))
2796 && ! TREE_PUBLIC (ref)
2797 && DECL_CONTEXT (ref) != current_function_decl)
2798 record_inline_static (loc, current_function_decl, ref,
2799 csi_internal);
2801 return ref;
2804 /* Record details of decls possibly used inside sizeof or typeof. */
2805 struct maybe_used_decl
2807 /* The decl. */
2808 tree decl;
2809 /* The level seen at (in_sizeof + in_typeof). */
2810 int level;
2811 /* The next one at this level or above, or NULL. */
2812 struct maybe_used_decl *next;
2815 static struct maybe_used_decl *maybe_used_decls;
2817 /* Record that DECL, an undefined static function reference seen
2818 inside sizeof or typeof, might be used if the operand of sizeof is
2819 a VLA type or the operand of typeof is a variably modified
2820 type. */
2822 static void
2823 record_maybe_used_decl (tree decl)
2825 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2826 t->decl = decl;
2827 t->level = in_sizeof + in_typeof;
2828 t->next = maybe_used_decls;
2829 maybe_used_decls = t;
2832 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2833 USED is false, just discard them. If it is true, mark them used
2834 (if no longer inside sizeof or typeof) or move them to the next
2835 level up (if still inside sizeof or typeof). */
2837 void
2838 pop_maybe_used (bool used)
2840 struct maybe_used_decl *p = maybe_used_decls;
2841 int cur_level = in_sizeof + in_typeof;
2842 while (p && p->level > cur_level)
2844 if (used)
2846 if (cur_level == 0)
2847 C_DECL_USED (p->decl) = 1;
2848 else
2849 p->level = cur_level;
2851 p = p->next;
2853 if (!used || cur_level == 0)
2854 maybe_used_decls = p;
2857 /* Return the result of sizeof applied to EXPR. */
2859 struct c_expr
2860 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2862 struct c_expr ret;
2863 if (expr.value == error_mark_node)
2865 ret.value = error_mark_node;
2866 ret.original_code = ERROR_MARK;
2867 ret.original_type = NULL;
2868 pop_maybe_used (false);
2870 else
2872 bool expr_const_operands = true;
2874 if (TREE_CODE (expr.value) == PARM_DECL
2875 && C_ARRAY_PARAMETER (expr.value))
2877 if (warning_at (loc, OPT_Wsizeof_array_argument,
2878 "%<sizeof%> on array function parameter %qE will "
2879 "return size of %qT", expr.value,
2880 expr.original_type))
2881 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2883 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2884 &expr_const_operands);
2885 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2886 c_last_sizeof_arg = expr.value;
2887 ret.original_code = SIZEOF_EXPR;
2888 ret.original_type = NULL;
2889 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2891 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2892 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2893 folded_expr, ret.value);
2894 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2895 SET_EXPR_LOCATION (ret.value, loc);
2897 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2899 return ret;
2902 /* Return the result of sizeof applied to T, a structure for the type
2903 name passed to sizeof (rather than the type itself). LOC is the
2904 location of the original expression. */
2906 struct c_expr
2907 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2909 tree type;
2910 struct c_expr ret;
2911 tree type_expr = NULL_TREE;
2912 bool type_expr_const = true;
2913 type = groktypename (t, &type_expr, &type_expr_const);
2914 ret.value = c_sizeof (loc, type);
2915 c_last_sizeof_arg = type;
2916 ret.original_code = SIZEOF_EXPR;
2917 ret.original_type = NULL;
2918 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2919 && c_vla_type_p (type))
2921 /* If the type is a [*] array, it is a VLA but is represented as
2922 having a size of zero. In such a case we must ensure that
2923 the result of sizeof does not get folded to a constant by
2924 c_fully_fold, because if the size is evaluated the result is
2925 not constant and so constraints on zero or negative size
2926 arrays must not be applied when this sizeof call is inside
2927 another array declarator. */
2928 if (!type_expr)
2929 type_expr = integer_zero_node;
2930 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2931 type_expr, ret.value);
2932 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2934 pop_maybe_used (type != error_mark_node
2935 ? C_TYPE_VARIABLE_SIZE (type) : false);
2936 return ret;
2939 /* Build a function call to function FUNCTION with parameters PARAMS.
2940 The function call is at LOC.
2941 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2942 TREE_VALUE of each node is a parameter-expression.
2943 FUNCTION's data type may be a function type or a pointer-to-function. */
2945 tree
2946 build_function_call (location_t loc, tree function, tree params)
2948 vec<tree, va_gc> *v;
2949 tree ret;
2951 vec_alloc (v, list_length (params));
2952 for (; params; params = TREE_CHAIN (params))
2953 v->quick_push (TREE_VALUE (params));
2954 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2955 vec_free (v);
2956 return ret;
2959 /* Give a note about the location of the declaration of DECL. */
2961 static void
2962 inform_declaration (tree decl)
2964 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2965 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2968 /* Build a function call to function FUNCTION with parameters PARAMS.
2969 ORIGTYPES, if not NULL, is a vector of types; each element is
2970 either NULL or the original type of the corresponding element in
2971 PARAMS. The original type may differ from TREE_TYPE of the
2972 parameter for enums. FUNCTION's data type may be a function type
2973 or pointer-to-function. This function changes the elements of
2974 PARAMS. */
2976 tree
2977 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2978 tree function, vec<tree, va_gc> *params,
2979 vec<tree, va_gc> *origtypes)
2981 tree fntype, fundecl = 0;
2982 tree name = NULL_TREE, result;
2983 tree tem;
2984 int nargs;
2985 tree *argarray;
2988 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2989 STRIP_TYPE_NOPS (function);
2991 /* Convert anything with function type to a pointer-to-function. */
2992 if (TREE_CODE (function) == FUNCTION_DECL)
2994 name = DECL_NAME (function);
2996 if (flag_tm)
2997 tm_malloc_replacement (function);
2998 fundecl = function;
2999 /* Atomic functions have type checking/casting already done. They are
3000 often rewritten and don't match the original parameter list. */
3001 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3002 origtypes = NULL;
3004 if (flag_cilkplus
3005 && is_cilkplus_reduce_builtin (function))
3006 origtypes = NULL;
3008 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3009 function = function_to_pointer_conversion (loc, function);
3011 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3012 expressions, like those used for ObjC messenger dispatches. */
3013 if (params && !params->is_empty ())
3014 function = objc_rewrite_function_call (function, (*params)[0]);
3016 function = c_fully_fold (function, false, NULL);
3018 fntype = TREE_TYPE (function);
3020 if (TREE_CODE (fntype) == ERROR_MARK)
3021 return error_mark_node;
3023 if (!(TREE_CODE (fntype) == POINTER_TYPE
3024 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3026 if (!flag_diagnostics_show_caret)
3027 error_at (loc,
3028 "called object %qE is not a function or function pointer",
3029 function);
3030 else if (DECL_P (function))
3032 error_at (loc,
3033 "called object %qD is not a function or function pointer",
3034 function);
3035 inform_declaration (function);
3037 else
3038 error_at (loc,
3039 "called object is not a function or function pointer");
3040 return error_mark_node;
3043 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3044 current_function_returns_abnormally = 1;
3046 /* fntype now gets the type of function pointed to. */
3047 fntype = TREE_TYPE (fntype);
3049 /* Convert the parameters to the types declared in the
3050 function prototype, or apply default promotions. */
3052 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3053 origtypes, function, fundecl);
3054 if (nargs < 0)
3055 return error_mark_node;
3057 /* Check that the function is called through a compatible prototype.
3058 If it is not, warn. */
3059 if (CONVERT_EXPR_P (function)
3060 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3061 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3062 && !comptypes (fntype, TREE_TYPE (tem)))
3064 tree return_type = TREE_TYPE (fntype);
3066 /* This situation leads to run-time undefined behavior. We can't,
3067 therefore, simply error unless we can prove that all possible
3068 executions of the program must execute the code. */
3069 warning_at (loc, 0, "function called through a non-compatible type");
3071 if (VOID_TYPE_P (return_type)
3072 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3073 pedwarn (loc, 0,
3074 "function with qualified void return type called");
3077 argarray = vec_safe_address (params);
3079 /* Check that arguments to builtin functions match the expectations. */
3080 if (fundecl
3081 && DECL_BUILT_IN (fundecl)
3082 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3083 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3084 argarray))
3085 return error_mark_node;
3087 /* Check that the arguments to the function are valid. */
3088 check_function_arguments (loc, fntype, nargs, argarray);
3090 if (name != NULL_TREE
3091 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3093 if (require_constant_value)
3094 result =
3095 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3096 function, nargs, argarray);
3097 else
3098 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3099 function, nargs, argarray);
3100 if (TREE_CODE (result) == NOP_EXPR
3101 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3102 STRIP_TYPE_NOPS (result);
3104 else
3105 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3106 function, nargs, argarray);
3108 /* In this improbable scenario, a nested function returns a VM type.
3109 Create a TARGET_EXPR so that the call always has a LHS, much as
3110 what the C++ FE does for functions returning non-PODs. */
3111 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3113 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3114 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3115 NULL_TREE, NULL_TREE);
3118 if (VOID_TYPE_P (TREE_TYPE (result)))
3120 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3121 pedwarn (loc, 0,
3122 "function with qualified void return type called");
3123 return result;
3125 return require_complete_type (loc, result);
3128 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3130 tree
3131 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3132 tree function, vec<tree, va_gc> *params,
3133 vec<tree, va_gc> *origtypes)
3135 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3136 STRIP_TYPE_NOPS (function);
3138 /* Convert anything with function type to a pointer-to-function. */
3139 if (TREE_CODE (function) == FUNCTION_DECL)
3141 /* Implement type-directed function overloading for builtins.
3142 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3143 handle all the type checking. The result is a complete expression
3144 that implements this function call. */
3145 tree tem = resolve_overloaded_builtin (loc, function, params);
3146 if (tem)
3147 return tem;
3149 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3152 /* Convert the argument expressions in the vector VALUES
3153 to the types in the list TYPELIST.
3155 If TYPELIST is exhausted, or when an element has NULL as its type,
3156 perform the default conversions.
3158 ORIGTYPES is the original types of the expressions in VALUES. This
3159 holds the type of enum values which have been converted to integral
3160 types. It may be NULL.
3162 FUNCTION is a tree for the called function. It is used only for
3163 error messages, where it is formatted with %qE.
3165 This is also where warnings about wrong number of args are generated.
3167 ARG_LOC are locations of function arguments (if any).
3169 Returns the actual number of arguments processed (which may be less
3170 than the length of VALUES in some error situations), or -1 on
3171 failure. */
3173 static int
3174 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3175 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3176 tree function, tree fundecl)
3178 tree typetail, val;
3179 unsigned int parmnum;
3180 bool error_args = false;
3181 const bool type_generic = fundecl
3182 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3183 bool type_generic_remove_excess_precision = false;
3184 bool type_generic_overflow_p = false;
3185 tree selector;
3187 /* Change pointer to function to the function itself for
3188 diagnostics. */
3189 if (TREE_CODE (function) == ADDR_EXPR
3190 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3191 function = TREE_OPERAND (function, 0);
3193 /* Handle an ObjC selector specially for diagnostics. */
3194 selector = objc_message_selector ();
3196 /* For type-generic built-in functions, determine whether excess
3197 precision should be removed (classification) or not
3198 (comparison). */
3199 if (type_generic
3200 && DECL_BUILT_IN (fundecl)
3201 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3203 switch (DECL_FUNCTION_CODE (fundecl))
3205 case BUILT_IN_ISFINITE:
3206 case BUILT_IN_ISINF:
3207 case BUILT_IN_ISINF_SIGN:
3208 case BUILT_IN_ISNAN:
3209 case BUILT_IN_ISNORMAL:
3210 case BUILT_IN_FPCLASSIFY:
3211 type_generic_remove_excess_precision = true;
3212 break;
3214 case BUILT_IN_ADD_OVERFLOW_P:
3215 case BUILT_IN_SUB_OVERFLOW_P:
3216 case BUILT_IN_MUL_OVERFLOW_P:
3217 /* The last argument of these type-generic builtins
3218 should not be promoted. */
3219 type_generic_overflow_p = true;
3220 break;
3222 default:
3223 break;
3226 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3227 return vec_safe_length (values);
3229 /* Scan the given expressions and types, producing individual
3230 converted arguments. */
3232 for (typetail = typelist, parmnum = 0;
3233 values && values->iterate (parmnum, &val);
3234 ++parmnum)
3236 tree type = typetail ? TREE_VALUE (typetail) : 0;
3237 tree valtype = TREE_TYPE (val);
3238 tree rname = function;
3239 int argnum = parmnum + 1;
3240 const char *invalid_func_diag;
3241 bool excess_precision = false;
3242 bool npc;
3243 tree parmval;
3244 /* Some __atomic_* builtins have additional hidden argument at
3245 position 0. */
3246 location_t ploc
3247 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3248 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3249 : input_location;
3251 if (type == void_type_node)
3253 if (selector)
3254 error_at (loc, "too many arguments to method %qE", selector);
3255 else
3256 error_at (loc, "too many arguments to function %qE", function);
3257 inform_declaration (fundecl);
3258 return error_args ? -1 : (int) parmnum;
3261 if (selector && argnum > 2)
3263 rname = selector;
3264 argnum -= 2;
3267 npc = null_pointer_constant_p (val);
3269 /* If there is excess precision and a prototype, convert once to
3270 the required type rather than converting via the semantic
3271 type. Likewise without a prototype a float value represented
3272 as long double should be converted once to double. But for
3273 type-generic classification functions excess precision must
3274 be removed here. */
3275 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3276 && (type || !type_generic || !type_generic_remove_excess_precision))
3278 val = TREE_OPERAND (val, 0);
3279 excess_precision = true;
3281 val = c_fully_fold (val, false, NULL);
3282 STRIP_TYPE_NOPS (val);
3284 val = require_complete_type (ploc, val);
3286 if (type != 0)
3288 /* Formal parm type is specified by a function prototype. */
3290 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3292 error_at (ploc, "type of formal parameter %d is incomplete",
3293 parmnum + 1);
3294 parmval = val;
3296 else
3298 tree origtype;
3300 /* Optionally warn about conversions that
3301 differ from the default conversions. */
3302 if (warn_traditional_conversion || warn_traditional)
3304 unsigned int formal_prec = TYPE_PRECISION (type);
3306 if (INTEGRAL_TYPE_P (type)
3307 && TREE_CODE (valtype) == REAL_TYPE)
3308 warning_at (ploc, OPT_Wtraditional_conversion,
3309 "passing argument %d of %qE as integer rather "
3310 "than floating due to prototype",
3311 argnum, rname);
3312 if (INTEGRAL_TYPE_P (type)
3313 && TREE_CODE (valtype) == COMPLEX_TYPE)
3314 warning_at (ploc, OPT_Wtraditional_conversion,
3315 "passing argument %d of %qE as integer rather "
3316 "than complex due to prototype",
3317 argnum, rname);
3318 else if (TREE_CODE (type) == COMPLEX_TYPE
3319 && TREE_CODE (valtype) == REAL_TYPE)
3320 warning_at (ploc, OPT_Wtraditional_conversion,
3321 "passing argument %d of %qE as complex rather "
3322 "than floating due to prototype",
3323 argnum, rname);
3324 else if (TREE_CODE (type) == REAL_TYPE
3325 && INTEGRAL_TYPE_P (valtype))
3326 warning_at (ploc, OPT_Wtraditional_conversion,
3327 "passing argument %d of %qE as floating rather "
3328 "than integer due to prototype",
3329 argnum, rname);
3330 else if (TREE_CODE (type) == COMPLEX_TYPE
3331 && INTEGRAL_TYPE_P (valtype))
3332 warning_at (ploc, OPT_Wtraditional_conversion,
3333 "passing argument %d of %qE as complex rather "
3334 "than integer due to prototype",
3335 argnum, rname);
3336 else if (TREE_CODE (type) == REAL_TYPE
3337 && TREE_CODE (valtype) == COMPLEX_TYPE)
3338 warning_at (ploc, OPT_Wtraditional_conversion,
3339 "passing argument %d of %qE as floating rather "
3340 "than complex due to prototype",
3341 argnum, rname);
3342 /* ??? At some point, messages should be written about
3343 conversions between complex types, but that's too messy
3344 to do now. */
3345 else if (TREE_CODE (type) == REAL_TYPE
3346 && TREE_CODE (valtype) == REAL_TYPE)
3348 /* Warn if any argument is passed as `float',
3349 since without a prototype it would be `double'. */
3350 if (formal_prec == TYPE_PRECISION (float_type_node)
3351 && type != dfloat32_type_node)
3352 warning_at (ploc, 0,
3353 "passing argument %d of %qE as %<float%> "
3354 "rather than %<double%> due to prototype",
3355 argnum, rname);
3357 /* Warn if mismatch between argument and prototype
3358 for decimal float types. Warn of conversions with
3359 binary float types and of precision narrowing due to
3360 prototype. */
3361 else if (type != valtype
3362 && (type == dfloat32_type_node
3363 || type == dfloat64_type_node
3364 || type == dfloat128_type_node
3365 || valtype == dfloat32_type_node
3366 || valtype == dfloat64_type_node
3367 || valtype == dfloat128_type_node)
3368 && (formal_prec
3369 <= TYPE_PRECISION (valtype)
3370 || (type == dfloat128_type_node
3371 && (valtype
3372 != dfloat64_type_node
3373 && (valtype
3374 != dfloat32_type_node)))
3375 || (type == dfloat64_type_node
3376 && (valtype
3377 != dfloat32_type_node))))
3378 warning_at (ploc, 0,
3379 "passing argument %d of %qE as %qT "
3380 "rather than %qT due to prototype",
3381 argnum, rname, type, valtype);
3384 /* Detect integer changing in width or signedness.
3385 These warnings are only activated with
3386 -Wtraditional-conversion, not with -Wtraditional. */
3387 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3388 && INTEGRAL_TYPE_P (valtype))
3390 tree would_have_been = default_conversion (val);
3391 tree type1 = TREE_TYPE (would_have_been);
3393 if (TREE_CODE (type) == ENUMERAL_TYPE
3394 && (TYPE_MAIN_VARIANT (type)
3395 == TYPE_MAIN_VARIANT (valtype)))
3396 /* No warning if function asks for enum
3397 and the actual arg is that enum type. */
3399 else if (formal_prec != TYPE_PRECISION (type1))
3400 warning_at (ploc, OPT_Wtraditional_conversion,
3401 "passing argument %d of %qE "
3402 "with different width due to prototype",
3403 argnum, rname);
3404 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3406 /* Don't complain if the formal parameter type
3407 is an enum, because we can't tell now whether
3408 the value was an enum--even the same enum. */
3409 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3411 else if (TREE_CODE (val) == INTEGER_CST
3412 && int_fits_type_p (val, type))
3413 /* Change in signedness doesn't matter
3414 if a constant value is unaffected. */
3416 /* If the value is extended from a narrower
3417 unsigned type, it doesn't matter whether we
3418 pass it as signed or unsigned; the value
3419 certainly is the same either way. */
3420 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3421 && TYPE_UNSIGNED (valtype))
3423 else if (TYPE_UNSIGNED (type))
3424 warning_at (ploc, OPT_Wtraditional_conversion,
3425 "passing argument %d of %qE "
3426 "as unsigned due to prototype",
3427 argnum, rname);
3428 else
3429 warning_at (ploc, OPT_Wtraditional_conversion,
3430 "passing argument %d of %qE "
3431 "as signed due to prototype",
3432 argnum, rname);
3436 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3437 sake of better warnings from convert_and_check. */
3438 if (excess_precision)
3439 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3440 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3441 parmval = convert_for_assignment (loc, ploc, type,
3442 val, origtype, ic_argpass,
3443 npc, fundecl, function,
3444 parmnum + 1);
3446 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3447 && INTEGRAL_TYPE_P (type)
3448 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3449 parmval = default_conversion (parmval);
3452 else if (TREE_CODE (valtype) == REAL_TYPE
3453 && (TYPE_PRECISION (valtype)
3454 <= TYPE_PRECISION (double_type_node))
3455 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3456 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3457 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3459 if (type_generic)
3460 parmval = val;
3461 else
3463 /* Convert `float' to `double'. */
3464 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3465 warning_at (ploc, OPT_Wdouble_promotion,
3466 "implicit conversion from %qT to %qT when passing "
3467 "argument to function",
3468 valtype, double_type_node);
3469 parmval = convert (double_type_node, val);
3472 else if ((excess_precision && !type_generic)
3473 || (type_generic_overflow_p && parmnum == 2))
3474 /* A "double" argument with excess precision being passed
3475 without a prototype or in variable arguments.
3476 The last argument of __builtin_*_overflow_p should not be
3477 promoted. */
3478 parmval = convert (valtype, val);
3479 else if ((invalid_func_diag =
3480 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3482 error (invalid_func_diag);
3483 return -1;
3485 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3487 return -1;
3489 else
3490 /* Convert `short' and `char' to full-size `int'. */
3491 parmval = default_conversion (val);
3493 (*values)[parmnum] = parmval;
3494 if (parmval == error_mark_node)
3495 error_args = true;
3497 if (typetail)
3498 typetail = TREE_CHAIN (typetail);
3501 gcc_assert (parmnum == vec_safe_length (values));
3503 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3505 error_at (loc, "too few arguments to function %qE", function);
3506 inform_declaration (fundecl);
3507 return -1;
3510 return error_args ? -1 : (int) parmnum;
3513 /* This is the entry point used by the parser to build unary operators
3514 in the input. CODE, a tree_code, specifies the unary operator, and
3515 ARG is the operand. For unary plus, the C parser currently uses
3516 CONVERT_EXPR for code.
3518 LOC is the location to use for the tree generated.
3521 struct c_expr
3522 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3524 struct c_expr result;
3526 result.original_code = code;
3527 result.original_type = NULL;
3529 if (reject_gcc_builtin (arg.value))
3531 result.value = error_mark_node;
3533 else
3535 result.value = build_unary_op (loc, code, arg.value, 0);
3537 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3538 overflow_warning (loc, result.value);
3541 /* We are typically called when parsing a prefix token at LOC acting on
3542 ARG. Reflect this by updating the source range of the result to
3543 start at LOC and end at the end of ARG. */
3544 set_c_expr_source_range (&result,
3545 loc, arg.get_finish ());
3547 return result;
3550 /* This is the entry point used by the parser to build binary operators
3551 in the input. CODE, a tree_code, specifies the binary operator, and
3552 ARG1 and ARG2 are the operands. In addition to constructing the
3553 expression, we check for operands that were written with other binary
3554 operators in a way that is likely to confuse the user.
3556 LOCATION is the location of the binary operator. */
3558 struct c_expr
3559 parser_build_binary_op (location_t location, enum tree_code code,
3560 struct c_expr arg1, struct c_expr arg2)
3562 struct c_expr result;
3564 enum tree_code code1 = arg1.original_code;
3565 enum tree_code code2 = arg2.original_code;
3566 tree type1 = (arg1.original_type
3567 ? arg1.original_type
3568 : TREE_TYPE (arg1.value));
3569 tree type2 = (arg2.original_type
3570 ? arg2.original_type
3571 : TREE_TYPE (arg2.value));
3573 result.value = build_binary_op (location, code,
3574 arg1.value, arg2.value, 1);
3575 result.original_code = code;
3576 result.original_type = NULL;
3578 if (TREE_CODE (result.value) == ERROR_MARK)
3580 set_c_expr_source_range (&result,
3581 arg1.get_start (),
3582 arg2.get_finish ());
3583 return result;
3586 if (location != UNKNOWN_LOCATION)
3587 protected_set_expr_location (result.value, location);
3589 set_c_expr_source_range (&result,
3590 arg1.get_start (),
3591 arg2.get_finish ());
3593 /* Check for cases such as x+y<<z which users are likely
3594 to misinterpret. */
3595 if (warn_parentheses)
3596 warn_about_parentheses (location, code, code1, arg1.value, code2,
3597 arg2.value);
3599 if (warn_logical_op)
3600 warn_logical_operator (location, code, TREE_TYPE (result.value),
3601 code1, arg1.value, code2, arg2.value);
3603 if (warn_tautological_compare)
3605 tree lhs = arg1.value;
3606 tree rhs = arg2.value;
3607 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3609 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3610 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3611 lhs = NULL_TREE;
3612 else
3613 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3615 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3617 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3618 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3619 rhs = NULL_TREE;
3620 else
3621 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3623 if (lhs != NULL_TREE && rhs != NULL_TREE)
3624 warn_tautological_cmp (location, code, lhs, rhs);
3627 if (warn_logical_not_paren
3628 && TREE_CODE_CLASS (code) == tcc_comparison
3629 && code1 == TRUTH_NOT_EXPR
3630 && code2 != TRUTH_NOT_EXPR
3631 /* Avoid warning for !!x == y. */
3632 && (TREE_CODE (arg1.value) != NE_EXPR
3633 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3635 /* Avoid warning for !b == y where b has _Bool type. */
3636 tree t = integer_zero_node;
3637 if (TREE_CODE (arg1.value) == EQ_EXPR
3638 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3639 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3641 t = TREE_OPERAND (arg1.value, 0);
3644 if (TREE_TYPE (t) != integer_type_node)
3645 break;
3646 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3647 t = C_MAYBE_CONST_EXPR_EXPR (t);
3648 else if (CONVERT_EXPR_P (t))
3649 t = TREE_OPERAND (t, 0);
3650 else
3651 break;
3653 while (1);
3655 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3656 warn_logical_not_parentheses (location, code, arg2.value);
3659 /* Warn about comparisons against string literals, with the exception
3660 of testing for equality or inequality of a string literal with NULL. */
3661 if (code == EQ_EXPR || code == NE_EXPR)
3663 if ((code1 == STRING_CST
3664 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3665 || (code2 == STRING_CST
3666 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3667 warning_at (location, OPT_Waddress,
3668 "comparison with string literal results in unspecified behavior");
3670 else if (TREE_CODE_CLASS (code) == tcc_comparison
3671 && (code1 == STRING_CST || code2 == STRING_CST))
3672 warning_at (location, OPT_Waddress,
3673 "comparison with string literal results in unspecified behavior");
3675 if (TREE_OVERFLOW_P (result.value)
3676 && !TREE_OVERFLOW_P (arg1.value)
3677 && !TREE_OVERFLOW_P (arg2.value))
3678 overflow_warning (location, result.value);
3680 /* Warn about comparisons of different enum types. */
3681 if (warn_enum_compare
3682 && TREE_CODE_CLASS (code) == tcc_comparison
3683 && TREE_CODE (type1) == ENUMERAL_TYPE
3684 && TREE_CODE (type2) == ENUMERAL_TYPE
3685 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3686 warning_at (location, OPT_Wenum_compare,
3687 "comparison between %qT and %qT",
3688 type1, type2);
3690 return result;
3693 /* Return a tree for the difference of pointers OP0 and OP1.
3694 The resulting tree has type int. */
3696 static tree
3697 pointer_diff (location_t loc, tree op0, tree op1)
3699 tree restype = ptrdiff_type_node;
3700 tree result, inttype;
3702 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3703 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3704 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3705 tree orig_op1 = op1;
3707 /* If the operands point into different address spaces, we need to
3708 explicitly convert them to pointers into the common address space
3709 before we can subtract the numerical address values. */
3710 if (as0 != as1)
3712 addr_space_t as_common;
3713 tree common_type;
3715 /* Determine the common superset address space. This is guaranteed
3716 to exist because the caller verified that comp_target_types
3717 returned non-zero. */
3718 if (!addr_space_superset (as0, as1, &as_common))
3719 gcc_unreachable ();
3721 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3722 op0 = convert (common_type, op0);
3723 op1 = convert (common_type, op1);
3726 /* Determine integer type to perform computations in. This will usually
3727 be the same as the result type (ptrdiff_t), but may need to be a wider
3728 type if pointers for the address space are wider than ptrdiff_t. */
3729 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3730 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3731 else
3732 inttype = restype;
3734 if (TREE_CODE (target_type) == VOID_TYPE)
3735 pedwarn (loc, OPT_Wpointer_arith,
3736 "pointer of type %<void *%> used in subtraction");
3737 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3738 pedwarn (loc, OPT_Wpointer_arith,
3739 "pointer to a function used in subtraction");
3741 /* First do the subtraction as integers;
3742 then drop through to build the divide operator.
3743 Do not do default conversions on the minus operator
3744 in case restype is a short type. */
3746 op0 = build_binary_op (loc,
3747 MINUS_EXPR, convert (inttype, op0),
3748 convert (inttype, op1), 0);
3749 /* This generates an error if op1 is pointer to incomplete type. */
3750 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3751 error_at (loc, "arithmetic on pointer to an incomplete type");
3753 op1 = c_size_in_bytes (target_type);
3755 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3756 error_at (loc, "arithmetic on pointer to an empty aggregate");
3758 /* Divide by the size, in easiest possible way. */
3759 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3760 op0, convert (inttype, op1));
3762 /* Convert to final result type if necessary. */
3763 return convert (restype, result);
3766 /* Expand atomic compound assignments into an appropriate sequence as
3767 specified by the C11 standard section 6.5.16.2.
3769 _Atomic T1 E1
3770 T2 E2
3771 E1 op= E2
3773 This sequence is used for all types for which these operations are
3774 supported.
3776 In addition, built-in versions of the 'fe' prefixed routines may
3777 need to be invoked for floating point (real, complex or vector) when
3778 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3780 T1 newval;
3781 T1 old;
3782 T1 *addr
3783 T2 val
3784 fenv_t fenv
3786 addr = &E1;
3787 val = (E2);
3788 __atomic_load (addr, &old, SEQ_CST);
3789 feholdexcept (&fenv);
3790 loop:
3791 newval = old op val;
3792 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3793 SEQ_CST))
3794 goto done;
3795 feclearexcept (FE_ALL_EXCEPT);
3796 goto loop:
3797 done:
3798 feupdateenv (&fenv);
3800 The compiler will issue the __atomic_fetch_* built-in when possible,
3801 otherwise it will generate the generic form of the atomic operations.
3802 This requires temp(s) and has their address taken. The atomic processing
3803 is smart enough to figure out when the size of an object can utilize
3804 a lock-free version, and convert the built-in call to the appropriate
3805 lock-free routine. The optimizers will then dispose of any temps that
3806 are no longer required, and lock-free implementations are utilized as
3807 long as there is target support for the required size.
3809 If the operator is NOP_EXPR, then this is a simple assignment, and
3810 an __atomic_store is issued to perform the assignment rather than
3811 the above loop. */
3813 /* Build an atomic assignment at LOC, expanding into the proper
3814 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3815 the result of the operation, unless RETURN_OLD_P, in which case
3816 return the old value of LHS (this is only for postincrement and
3817 postdecrement). */
3819 static tree
3820 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3821 tree rhs, bool return_old_p)
3823 tree fndecl, func_call;
3824 vec<tree, va_gc> *params;
3825 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3826 tree old, old_addr;
3827 tree compound_stmt;
3828 tree stmt, goto_stmt;
3829 tree loop_label, loop_decl, done_label, done_decl;
3831 tree lhs_type = TREE_TYPE (lhs);
3832 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3833 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3834 tree rhs_type = TREE_TYPE (rhs);
3836 gcc_assert (TYPE_ATOMIC (lhs_type));
3838 if (return_old_p)
3839 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3841 /* Allocate enough vector items for a compare_exchange. */
3842 vec_alloc (params, 6);
3844 /* Create a compound statement to hold the sequence of statements
3845 with a loop. */
3846 compound_stmt = c_begin_compound_stmt (false);
3848 /* Fold the RHS if it hasn't already been folded. */
3849 if (modifycode != NOP_EXPR)
3850 rhs = c_fully_fold (rhs, false, NULL);
3852 /* Remove the qualifiers for the rest of the expressions and create
3853 the VAL temp variable to hold the RHS. */
3854 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3855 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3856 val = create_tmp_var_raw (nonatomic_rhs_type);
3857 TREE_ADDRESSABLE (val) = 1;
3858 TREE_NO_WARNING (val) = 1;
3859 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3860 NULL_TREE);
3861 SET_EXPR_LOCATION (rhs, loc);
3862 add_stmt (rhs);
3864 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3865 an atomic_store. */
3866 if (modifycode == NOP_EXPR)
3868 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3869 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3870 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3871 params->quick_push (lhs_addr);
3872 params->quick_push (rhs);
3873 params->quick_push (seq_cst);
3874 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3875 add_stmt (func_call);
3877 /* Finish the compound statement. */
3878 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3880 /* VAL is the value which was stored, return a COMPOUND_STMT of
3881 the statement and that value. */
3882 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3885 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3886 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3887 isn't applicable for such builtins. ??? Do we want to handle enums? */
3888 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3889 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3891 built_in_function fncode;
3892 switch (modifycode)
3894 case PLUS_EXPR:
3895 case POINTER_PLUS_EXPR:
3896 fncode = (return_old_p
3897 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3898 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3899 break;
3900 case MINUS_EXPR:
3901 fncode = (return_old_p
3902 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3903 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3904 break;
3905 case BIT_AND_EXPR:
3906 fncode = (return_old_p
3907 ? BUILT_IN_ATOMIC_FETCH_AND_N
3908 : BUILT_IN_ATOMIC_AND_FETCH_N);
3909 break;
3910 case BIT_IOR_EXPR:
3911 fncode = (return_old_p
3912 ? BUILT_IN_ATOMIC_FETCH_OR_N
3913 : BUILT_IN_ATOMIC_OR_FETCH_N);
3914 break;
3915 case BIT_XOR_EXPR:
3916 fncode = (return_old_p
3917 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3918 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3919 break;
3920 default:
3921 goto cas_loop;
3924 /* We can only use "_1" through "_16" variants of the atomic fetch
3925 built-ins. */
3926 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3927 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3928 goto cas_loop;
3930 /* If this is a pointer type, we need to multiply by the size of
3931 the pointer target type. */
3932 if (POINTER_TYPE_P (lhs_type))
3934 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3935 /* ??? This would introduce -Wdiscarded-qualifiers
3936 warning: __atomic_fetch_* expect volatile void *
3937 type as the first argument. (Assignments between
3938 atomic and non-atomic objects are OK.) */
3939 || TYPE_RESTRICT (lhs_type))
3940 goto cas_loop;
3941 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3942 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3943 convert (ptrdiff_type_node, rhs),
3944 convert (ptrdiff_type_node, sz));
3947 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3948 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3949 fndecl = builtin_decl_explicit (fncode);
3950 params->quick_push (lhs_addr);
3951 params->quick_push (rhs);
3952 params->quick_push (seq_cst);
3953 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3955 newval = create_tmp_var_raw (nonatomic_lhs_type);
3956 TREE_ADDRESSABLE (newval) = 1;
3957 TREE_NO_WARNING (newval) = 1;
3958 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
3959 NULL_TREE, NULL_TREE);
3960 SET_EXPR_LOCATION (rhs, loc);
3961 add_stmt (rhs);
3963 /* Finish the compound statement. */
3964 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3966 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
3967 the statement and that value. */
3968 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
3971 cas_loop:
3972 /* Create the variables and labels required for the op= form. */
3973 old = create_tmp_var_raw (nonatomic_lhs_type);
3974 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3975 TREE_ADDRESSABLE (old) = 1;
3976 TREE_NO_WARNING (old) = 1;
3978 newval = create_tmp_var_raw (nonatomic_lhs_type);
3979 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3980 TREE_ADDRESSABLE (newval) = 1;
3981 TREE_NO_WARNING (newval) = 1;
3983 loop_decl = create_artificial_label (loc);
3984 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3986 done_decl = create_artificial_label (loc);
3987 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3989 /* __atomic_load (addr, &old, SEQ_CST). */
3990 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3991 params->quick_push (lhs_addr);
3992 params->quick_push (old_addr);
3993 params->quick_push (seq_cst);
3994 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3995 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3996 NULL_TREE);
3997 add_stmt (old);
3998 params->truncate (0);
4000 /* Create the expressions for floating-point environment
4001 manipulation, if required. */
4002 bool need_fenv = (flag_trapping_math
4003 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4004 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4005 if (need_fenv)
4006 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4008 if (hold_call)
4009 add_stmt (hold_call);
4011 /* loop: */
4012 add_stmt (loop_label);
4014 /* newval = old + val; */
4015 rhs = build_binary_op (loc, modifycode, old, val, 1);
4016 rhs = c_fully_fold (rhs, false, NULL);
4017 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4018 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4019 NULL_TREE, 0);
4020 if (rhs != error_mark_node)
4022 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4023 NULL_TREE);
4024 SET_EXPR_LOCATION (rhs, loc);
4025 add_stmt (rhs);
4028 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4029 goto done; */
4030 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4031 params->quick_push (lhs_addr);
4032 params->quick_push (old_addr);
4033 params->quick_push (newval_addr);
4034 params->quick_push (integer_zero_node);
4035 params->quick_push (seq_cst);
4036 params->quick_push (seq_cst);
4037 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4039 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4040 SET_EXPR_LOCATION (goto_stmt, loc);
4042 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4043 SET_EXPR_LOCATION (stmt, loc);
4044 add_stmt (stmt);
4046 if (clear_call)
4047 add_stmt (clear_call);
4049 /* goto loop; */
4050 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4051 SET_EXPR_LOCATION (goto_stmt, loc);
4052 add_stmt (goto_stmt);
4054 /* done: */
4055 add_stmt (done_label);
4057 if (update_call)
4058 add_stmt (update_call);
4060 /* Finish the compound statement. */
4061 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4063 /* NEWVAL is the value that was successfully stored, return a
4064 COMPOUND_EXPR of the statement and the appropriate value. */
4065 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4066 return_old_p ? old : newval);
4069 /* Construct and perhaps optimize a tree representation
4070 for a unary operation. CODE, a tree_code, specifies the operation
4071 and XARG is the operand.
4072 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
4073 the default promotions (such as from short to int).
4074 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
4075 allows non-lvalues; this is only used to handle conversion of non-lvalue
4076 arrays to pointers in C99.
4078 LOCATION is the location of the operator. */
4080 tree
4081 build_unary_op (location_t location,
4082 enum tree_code code, tree xarg, int flag)
4084 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4085 tree arg = xarg;
4086 tree argtype = 0;
4087 enum tree_code typecode;
4088 tree val;
4089 tree ret = error_mark_node;
4090 tree eptype = NULL_TREE;
4091 int noconvert = flag;
4092 const char *invalid_op_diag;
4093 bool int_operands;
4095 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4096 if (int_operands)
4097 arg = remove_c_maybe_const_expr (arg);
4099 if (code != ADDR_EXPR)
4100 arg = require_complete_type (location, arg);
4102 typecode = TREE_CODE (TREE_TYPE (arg));
4103 if (typecode == ERROR_MARK)
4104 return error_mark_node;
4105 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4106 typecode = INTEGER_TYPE;
4108 if ((invalid_op_diag
4109 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4111 error_at (location, invalid_op_diag);
4112 return error_mark_node;
4115 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4117 eptype = TREE_TYPE (arg);
4118 arg = TREE_OPERAND (arg, 0);
4121 switch (code)
4123 case CONVERT_EXPR:
4124 /* This is used for unary plus, because a CONVERT_EXPR
4125 is enough to prevent anybody from looking inside for
4126 associativity, but won't generate any code. */
4127 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4128 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4129 || typecode == VECTOR_TYPE))
4131 error_at (location, "wrong type argument to unary plus");
4132 return error_mark_node;
4134 else if (!noconvert)
4135 arg = default_conversion (arg);
4136 arg = non_lvalue_loc (location, arg);
4137 break;
4139 case NEGATE_EXPR:
4140 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4141 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4142 || typecode == VECTOR_TYPE))
4144 error_at (location, "wrong type argument to unary minus");
4145 return error_mark_node;
4147 else if (!noconvert)
4148 arg = default_conversion (arg);
4149 break;
4151 case BIT_NOT_EXPR:
4152 /* ~ works on integer types and non float vectors. */
4153 if (typecode == INTEGER_TYPE
4154 || (typecode == VECTOR_TYPE
4155 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4157 if (!noconvert)
4158 arg = default_conversion (arg);
4160 else if (typecode == COMPLEX_TYPE)
4162 code = CONJ_EXPR;
4163 pedwarn (location, OPT_Wpedantic,
4164 "ISO C does not support %<~%> for complex conjugation");
4165 if (!noconvert)
4166 arg = default_conversion (arg);
4168 else
4170 error_at (location, "wrong type argument to bit-complement");
4171 return error_mark_node;
4173 break;
4175 case ABS_EXPR:
4176 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4178 error_at (location, "wrong type argument to abs");
4179 return error_mark_node;
4181 else if (!noconvert)
4182 arg = default_conversion (arg);
4183 break;
4185 case CONJ_EXPR:
4186 /* Conjugating a real value is a no-op, but allow it anyway. */
4187 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4188 || typecode == COMPLEX_TYPE))
4190 error_at (location, "wrong type argument to conjugation");
4191 return error_mark_node;
4193 else if (!noconvert)
4194 arg = default_conversion (arg);
4195 break;
4197 case TRUTH_NOT_EXPR:
4198 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4199 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4200 && typecode != COMPLEX_TYPE)
4202 error_at (location,
4203 "wrong type argument to unary exclamation mark");
4204 return error_mark_node;
4206 if (int_operands)
4208 arg = c_objc_common_truthvalue_conversion (location, xarg);
4209 arg = remove_c_maybe_const_expr (arg);
4211 else
4212 arg = c_objc_common_truthvalue_conversion (location, arg);
4213 ret = invert_truthvalue_loc (location, arg);
4214 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4215 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4216 location = EXPR_LOCATION (ret);
4217 goto return_build_unary_op;
4219 case REALPART_EXPR:
4220 case IMAGPART_EXPR:
4221 ret = build_real_imag_expr (location, code, arg);
4222 if (ret == error_mark_node)
4223 return error_mark_node;
4224 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4225 eptype = TREE_TYPE (eptype);
4226 goto return_build_unary_op;
4228 case PREINCREMENT_EXPR:
4229 case POSTINCREMENT_EXPR:
4230 case PREDECREMENT_EXPR:
4231 case POSTDECREMENT_EXPR:
4233 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4235 tree inner = build_unary_op (location, code,
4236 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4237 if (inner == error_mark_node)
4238 return error_mark_node;
4239 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4240 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4241 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4242 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4243 goto return_build_unary_op;
4246 /* Complain about anything that is not a true lvalue. In
4247 Objective-C, skip this check for property_refs. */
4248 if (!objc_is_property_ref (arg)
4249 && !lvalue_or_else (location,
4250 arg, ((code == PREINCREMENT_EXPR
4251 || code == POSTINCREMENT_EXPR)
4252 ? lv_increment
4253 : lv_decrement)))
4254 return error_mark_node;
4256 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4258 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4259 warning_at (location, OPT_Wc___compat,
4260 "increment of enumeration value is invalid in C++");
4261 else
4262 warning_at (location, OPT_Wc___compat,
4263 "decrement of enumeration value is invalid in C++");
4266 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4267 arg = c_fully_fold (arg, false, NULL);
4269 bool atomic_op;
4270 atomic_op = really_atomic_lvalue (arg);
4272 /* Increment or decrement the real part of the value,
4273 and don't change the imaginary part. */
4274 if (typecode == COMPLEX_TYPE)
4276 tree real, imag;
4278 pedwarn (location, OPT_Wpedantic,
4279 "ISO C does not support %<++%> and %<--%> on complex types");
4281 if (!atomic_op)
4283 arg = stabilize_reference (arg);
4284 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4285 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4286 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4287 if (real == error_mark_node || imag == error_mark_node)
4288 return error_mark_node;
4289 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4290 real, imag);
4291 goto return_build_unary_op;
4295 /* Report invalid types. */
4297 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4298 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4299 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4301 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4302 error_at (location, "wrong type argument to increment");
4303 else
4304 error_at (location, "wrong type argument to decrement");
4306 return error_mark_node;
4310 tree inc;
4312 argtype = TREE_TYPE (arg);
4314 /* Compute the increment. */
4316 if (typecode == POINTER_TYPE)
4318 /* If pointer target is an incomplete type,
4319 we just cannot know how to do the arithmetic. */
4320 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4322 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4323 error_at (location,
4324 "increment of pointer to an incomplete type %qT",
4325 TREE_TYPE (argtype));
4326 else
4327 error_at (location,
4328 "decrement of pointer to an incomplete type %qT",
4329 TREE_TYPE (argtype));
4331 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4332 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4334 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4335 pedwarn (location, OPT_Wpointer_arith,
4336 "wrong type argument to increment");
4337 else
4338 pedwarn (location, OPT_Wpointer_arith,
4339 "wrong type argument to decrement");
4342 inc = c_size_in_bytes (TREE_TYPE (argtype));
4343 inc = convert_to_ptrofftype_loc (location, inc);
4345 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4347 /* For signed fract types, we invert ++ to -- or
4348 -- to ++, and change inc from 1 to -1, because
4349 it is not possible to represent 1 in signed fract constants.
4350 For unsigned fract types, the result always overflows and
4351 we get an undefined (original) or the maximum value. */
4352 if (code == PREINCREMENT_EXPR)
4353 code = PREDECREMENT_EXPR;
4354 else if (code == PREDECREMENT_EXPR)
4355 code = PREINCREMENT_EXPR;
4356 else if (code == POSTINCREMENT_EXPR)
4357 code = POSTDECREMENT_EXPR;
4358 else /* code == POSTDECREMENT_EXPR */
4359 code = POSTINCREMENT_EXPR;
4361 inc = integer_minus_one_node;
4362 inc = convert (argtype, inc);
4364 else
4366 inc = VECTOR_TYPE_P (argtype)
4367 ? build_one_cst (argtype)
4368 : integer_one_node;
4369 inc = convert (argtype, inc);
4372 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4373 need to ask Objective-C to build the increment or decrement
4374 expression for it. */
4375 if (objc_is_property_ref (arg))
4376 return objc_build_incr_expr_for_property_ref (location, code,
4377 arg, inc);
4379 /* Report a read-only lvalue. */
4380 if (TYPE_READONLY (argtype))
4382 readonly_error (location, arg,
4383 ((code == PREINCREMENT_EXPR
4384 || code == POSTINCREMENT_EXPR)
4385 ? lv_increment : lv_decrement));
4386 return error_mark_node;
4388 else if (TREE_READONLY (arg))
4389 readonly_warning (arg,
4390 ((code == PREINCREMENT_EXPR
4391 || code == POSTINCREMENT_EXPR)
4392 ? lv_increment : lv_decrement));
4394 /* If the argument is atomic, use the special code sequences for
4395 atomic compound assignment. */
4396 if (atomic_op)
4398 arg = stabilize_reference (arg);
4399 ret = build_atomic_assign (location, arg,
4400 ((code == PREINCREMENT_EXPR
4401 || code == POSTINCREMENT_EXPR)
4402 ? PLUS_EXPR
4403 : MINUS_EXPR),
4404 (FRACT_MODE_P (TYPE_MODE (argtype))
4405 ? inc
4406 : integer_one_node),
4407 (code == POSTINCREMENT_EXPR
4408 || code == POSTDECREMENT_EXPR));
4409 goto return_build_unary_op;
4412 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4413 val = boolean_increment (code, arg);
4414 else
4415 val = build2 (code, TREE_TYPE (arg), arg, inc);
4416 TREE_SIDE_EFFECTS (val) = 1;
4417 if (TREE_CODE (val) != code)
4418 TREE_NO_WARNING (val) = 1;
4419 ret = val;
4420 goto return_build_unary_op;
4423 case ADDR_EXPR:
4424 /* Note that this operation never does default_conversion. */
4426 /* The operand of unary '&' must be an lvalue (which excludes
4427 expressions of type void), or, in C99, the result of a [] or
4428 unary '*' operator. */
4429 if (VOID_TYPE_P (TREE_TYPE (arg))
4430 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4431 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4432 pedwarn (location, 0, "taking address of expression of type %<void%>");
4434 /* Let &* cancel out to simplify resulting code. */
4435 if (INDIRECT_REF_P (arg))
4437 /* Don't let this be an lvalue. */
4438 if (lvalue_p (TREE_OPERAND (arg, 0)))
4439 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4440 ret = TREE_OPERAND (arg, 0);
4441 goto return_build_unary_op;
4444 /* Anything not already handled and not a true memory reference
4445 or a non-lvalue array is an error. */
4446 if (typecode != FUNCTION_TYPE && !flag
4447 && !lvalue_or_else (location, arg, lv_addressof))
4448 return error_mark_node;
4450 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4451 folding later. */
4452 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4454 tree inner = build_unary_op (location, code,
4455 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4456 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4457 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4458 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4459 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4460 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4461 goto return_build_unary_op;
4464 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4465 argtype = TREE_TYPE (arg);
4467 /* If the lvalue is const or volatile, merge that into the type
4468 to which the address will point. This is only needed
4469 for function types. */
4470 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4471 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4472 && TREE_CODE (argtype) == FUNCTION_TYPE)
4474 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4475 int quals = orig_quals;
4477 if (TREE_READONLY (arg))
4478 quals |= TYPE_QUAL_CONST;
4479 if (TREE_THIS_VOLATILE (arg))
4480 quals |= TYPE_QUAL_VOLATILE;
4482 argtype = c_build_qualified_type (argtype, quals);
4485 switch (TREE_CODE (arg))
4487 case COMPONENT_REF:
4488 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4490 error_at (location, "cannot take address of bit-field %qD",
4491 TREE_OPERAND (arg, 1));
4492 return error_mark_node;
4495 /* ... fall through ... */
4497 case ARRAY_REF:
4498 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4500 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4501 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4503 error_at (location, "cannot take address of scalar with "
4504 "reverse storage order");
4505 return error_mark_node;
4508 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4509 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4510 warning_at (location, OPT_Wscalar_storage_order,
4511 "address of array with reverse scalar storage "
4512 "order requested");
4515 default:
4516 break;
4519 if (!c_mark_addressable (arg))
4520 return error_mark_node;
4522 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4523 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4525 argtype = build_pointer_type (argtype);
4527 /* ??? Cope with user tricks that amount to offsetof. Delete this
4528 when we have proper support for integer constant expressions. */
4529 val = get_base_address (arg);
4530 if (val && INDIRECT_REF_P (val)
4531 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4533 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4534 goto return_build_unary_op;
4537 val = build1 (ADDR_EXPR, argtype, arg);
4539 ret = val;
4540 goto return_build_unary_op;
4542 default:
4543 gcc_unreachable ();
4546 if (argtype == 0)
4547 argtype = TREE_TYPE (arg);
4548 if (TREE_CODE (arg) == INTEGER_CST)
4549 ret = (require_constant_value
4550 ? fold_build1_initializer_loc (location, code, argtype, arg)
4551 : fold_build1_loc (location, code, argtype, arg));
4552 else
4553 ret = build1 (code, argtype, arg);
4554 return_build_unary_op:
4555 gcc_assert (ret != error_mark_node);
4556 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4557 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4558 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4559 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4560 ret = note_integer_operands (ret);
4561 if (eptype)
4562 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4563 protected_set_expr_location (ret, location);
4564 return ret;
4567 /* Return nonzero if REF is an lvalue valid for this language.
4568 Lvalues can be assigned, unless their type has TYPE_READONLY.
4569 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4571 bool
4572 lvalue_p (const_tree ref)
4574 const enum tree_code code = TREE_CODE (ref);
4576 switch (code)
4578 case REALPART_EXPR:
4579 case IMAGPART_EXPR:
4580 case COMPONENT_REF:
4581 return lvalue_p (TREE_OPERAND (ref, 0));
4583 case C_MAYBE_CONST_EXPR:
4584 return lvalue_p (TREE_OPERAND (ref, 1));
4586 case COMPOUND_LITERAL_EXPR:
4587 case STRING_CST:
4588 return 1;
4590 case INDIRECT_REF:
4591 case ARRAY_REF:
4592 case ARRAY_NOTATION_REF:
4593 case VAR_DECL:
4594 case PARM_DECL:
4595 case RESULT_DECL:
4596 case ERROR_MARK:
4597 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4598 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4600 case BIND_EXPR:
4601 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4603 default:
4604 return 0;
4608 /* Give a warning for storing in something that is read-only in GCC
4609 terms but not const in ISO C terms. */
4611 static void
4612 readonly_warning (tree arg, enum lvalue_use use)
4614 switch (use)
4616 case lv_assign:
4617 warning (0, "assignment of read-only location %qE", arg);
4618 break;
4619 case lv_increment:
4620 warning (0, "increment of read-only location %qE", arg);
4621 break;
4622 case lv_decrement:
4623 warning (0, "decrement of read-only location %qE", arg);
4624 break;
4625 default:
4626 gcc_unreachable ();
4628 return;
4632 /* Return nonzero if REF is an lvalue valid for this language;
4633 otherwise, print an error message and return zero. USE says
4634 how the lvalue is being used and so selects the error message.
4635 LOCATION is the location at which any error should be reported. */
4637 static int
4638 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4640 int win = lvalue_p (ref);
4642 if (!win)
4643 lvalue_error (loc, use);
4645 return win;
4648 /* Mark EXP saying that we need to be able to take the
4649 address of it; it should not be allocated in a register.
4650 Returns true if successful. */
4652 bool
4653 c_mark_addressable (tree exp)
4655 tree x = exp;
4657 while (1)
4658 switch (TREE_CODE (x))
4660 case COMPONENT_REF:
4661 case ADDR_EXPR:
4662 case ARRAY_REF:
4663 case REALPART_EXPR:
4664 case IMAGPART_EXPR:
4665 x = TREE_OPERAND (x, 0);
4666 break;
4668 case COMPOUND_LITERAL_EXPR:
4669 case CONSTRUCTOR:
4670 TREE_ADDRESSABLE (x) = 1;
4671 return true;
4673 case VAR_DECL:
4674 case CONST_DECL:
4675 case PARM_DECL:
4676 case RESULT_DECL:
4677 if (C_DECL_REGISTER (x)
4678 && DECL_NONLOCAL (x))
4680 if (TREE_PUBLIC (x) || is_global_var (x))
4682 error
4683 ("global register variable %qD used in nested function", x);
4684 return false;
4686 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4688 else if (C_DECL_REGISTER (x))
4690 if (TREE_PUBLIC (x) || is_global_var (x))
4691 error ("address of global register variable %qD requested", x);
4692 else
4693 error ("address of register variable %qD requested", x);
4694 return false;
4697 /* drops in */
4698 case FUNCTION_DECL:
4699 TREE_ADDRESSABLE (x) = 1;
4700 /* drops out */
4701 default:
4702 return true;
4706 /* Convert EXPR to TYPE, warning about conversion problems with
4707 constants. SEMANTIC_TYPE is the type this conversion would use
4708 without excess precision. If SEMANTIC_TYPE is NULL, this function
4709 is equivalent to convert_and_check. This function is a wrapper that
4710 handles conversions that may be different than
4711 the usual ones because of excess precision. */
4713 static tree
4714 ep_convert_and_check (location_t loc, tree type, tree expr,
4715 tree semantic_type)
4717 if (TREE_TYPE (expr) == type)
4718 return expr;
4720 if (!semantic_type)
4721 return convert_and_check (loc, type, expr);
4723 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4724 && TREE_TYPE (expr) != semantic_type)
4726 /* For integers, we need to check the real conversion, not
4727 the conversion to the excess precision type. */
4728 expr = convert_and_check (loc, semantic_type, expr);
4730 /* Result type is the excess precision type, which should be
4731 large enough, so do not check. */
4732 return convert (type, expr);
4735 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4736 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4737 if folded to an integer constant then the unselected half may
4738 contain arbitrary operations not normally permitted in constant
4739 expressions. Set the location of the expression to LOC. */
4741 tree
4742 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4743 tree op1, tree op1_original_type, tree op2,
4744 tree op2_original_type)
4746 tree type1;
4747 tree type2;
4748 enum tree_code code1;
4749 enum tree_code code2;
4750 tree result_type = NULL;
4751 tree semantic_result_type = NULL;
4752 tree orig_op1 = op1, orig_op2 = op2;
4753 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4754 bool ifexp_int_operands;
4755 tree ret;
4757 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4758 if (op1_int_operands)
4759 op1 = remove_c_maybe_const_expr (op1);
4760 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4761 if (op2_int_operands)
4762 op2 = remove_c_maybe_const_expr (op2);
4763 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4764 if (ifexp_int_operands)
4765 ifexp = remove_c_maybe_const_expr (ifexp);
4767 /* Promote both alternatives. */
4769 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4770 op1 = default_conversion (op1);
4771 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4772 op2 = default_conversion (op2);
4774 if (TREE_CODE (ifexp) == ERROR_MARK
4775 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4776 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4777 return error_mark_node;
4779 type1 = TREE_TYPE (op1);
4780 code1 = TREE_CODE (type1);
4781 type2 = TREE_TYPE (op2);
4782 code2 = TREE_CODE (type2);
4784 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4785 return error_mark_node;
4787 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4788 return error_mark_node;
4790 /* C90 does not permit non-lvalue arrays in conditional expressions.
4791 In C99 they will be pointers by now. */
4792 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4794 error_at (colon_loc, "non-lvalue array in conditional expression");
4795 return error_mark_node;
4798 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4799 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4800 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4801 || code1 == COMPLEX_TYPE)
4802 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4803 || code2 == COMPLEX_TYPE))
4805 semantic_result_type = c_common_type (type1, type2);
4806 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4808 op1 = TREE_OPERAND (op1, 0);
4809 type1 = TREE_TYPE (op1);
4810 gcc_assert (TREE_CODE (type1) == code1);
4812 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4814 op2 = TREE_OPERAND (op2, 0);
4815 type2 = TREE_TYPE (op2);
4816 gcc_assert (TREE_CODE (type2) == code2);
4820 if (warn_cxx_compat)
4822 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4823 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4825 if (TREE_CODE (t1) == ENUMERAL_TYPE
4826 && TREE_CODE (t2) == ENUMERAL_TYPE
4827 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4828 warning_at (colon_loc, OPT_Wc___compat,
4829 ("different enum types in conditional is "
4830 "invalid in C++: %qT vs %qT"),
4831 t1, t2);
4834 /* Quickly detect the usual case where op1 and op2 have the same type
4835 after promotion. */
4836 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4838 if (type1 == type2)
4839 result_type = type1;
4840 else
4841 result_type = TYPE_MAIN_VARIANT (type1);
4843 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4844 || code1 == COMPLEX_TYPE)
4845 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4846 || code2 == COMPLEX_TYPE))
4848 result_type = c_common_type (type1, type2);
4849 do_warn_double_promotion (result_type, type1, type2,
4850 "implicit conversion from %qT to %qT to "
4851 "match other result of conditional",
4852 colon_loc);
4854 /* If -Wsign-compare, warn here if type1 and type2 have
4855 different signedness. We'll promote the signed to unsigned
4856 and later code won't know it used to be different.
4857 Do this check on the original types, so that explicit casts
4858 will be considered, but default promotions won't. */
4859 if (c_inhibit_evaluation_warnings == 0)
4861 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4862 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4864 if (unsigned_op1 ^ unsigned_op2)
4866 bool ovf;
4868 /* Do not warn if the result type is signed, since the
4869 signed type will only be chosen if it can represent
4870 all the values of the unsigned type. */
4871 if (!TYPE_UNSIGNED (result_type))
4872 /* OK */;
4873 else
4875 bool op1_maybe_const = true;
4876 bool op2_maybe_const = true;
4878 /* Do not warn if the signed quantity is an
4879 unsuffixed integer literal (or some static
4880 constant expression involving such literals) and
4881 it is non-negative. This warning requires the
4882 operands to be folded for best results, so do
4883 that folding in this case even without
4884 warn_sign_compare to avoid warning options
4885 possibly affecting code generation. */
4886 c_inhibit_evaluation_warnings
4887 += (ifexp == truthvalue_false_node);
4888 op1 = c_fully_fold (op1, require_constant_value,
4889 &op1_maybe_const);
4890 c_inhibit_evaluation_warnings
4891 -= (ifexp == truthvalue_false_node);
4893 c_inhibit_evaluation_warnings
4894 += (ifexp == truthvalue_true_node);
4895 op2 = c_fully_fold (op2, require_constant_value,
4896 &op2_maybe_const);
4897 c_inhibit_evaluation_warnings
4898 -= (ifexp == truthvalue_true_node);
4900 if (warn_sign_compare)
4902 if ((unsigned_op2
4903 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4904 || (unsigned_op1
4905 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4906 /* OK */;
4907 else
4908 warning_at (colon_loc, OPT_Wsign_compare,
4909 ("signed and unsigned type in "
4910 "conditional expression"));
4912 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4913 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4914 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4915 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4920 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4922 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4923 pedwarn (colon_loc, OPT_Wpedantic,
4924 "ISO C forbids conditional expr with only one void side");
4925 result_type = void_type_node;
4927 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4929 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4930 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4931 addr_space_t as_common;
4933 if (comp_target_types (colon_loc, type1, type2))
4934 result_type = common_pointer_type (type1, type2);
4935 else if (null_pointer_constant_p (orig_op1))
4936 result_type = type2;
4937 else if (null_pointer_constant_p (orig_op2))
4938 result_type = type1;
4939 else if (!addr_space_superset (as1, as2, &as_common))
4941 error_at (colon_loc, "pointers to disjoint address spaces "
4942 "used in conditional expression");
4943 return error_mark_node;
4945 else if (VOID_TYPE_P (TREE_TYPE (type1))
4946 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4948 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4949 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4950 & ~TYPE_QUALS (TREE_TYPE (type1))))
4951 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4952 "pointer to array loses qualifier "
4953 "in conditional expression");
4955 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4956 pedwarn (colon_loc, OPT_Wpedantic,
4957 "ISO C forbids conditional expr between "
4958 "%<void *%> and function pointer");
4959 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4960 TREE_TYPE (type2)));
4962 else if (VOID_TYPE_P (TREE_TYPE (type2))
4963 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4965 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4966 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4967 & ~TYPE_QUALS (TREE_TYPE (type2))))
4968 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4969 "pointer to array loses qualifier "
4970 "in conditional expression");
4972 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4973 pedwarn (colon_loc, OPT_Wpedantic,
4974 "ISO C forbids conditional expr between "
4975 "%<void *%> and function pointer");
4976 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4977 TREE_TYPE (type1)));
4979 /* Objective-C pointer comparisons are a bit more lenient. */
4980 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4981 result_type = objc_common_type (type1, type2);
4982 else
4984 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4986 pedwarn (colon_loc, 0,
4987 "pointer type mismatch in conditional expression");
4988 result_type = build_pointer_type
4989 (build_qualified_type (void_type_node, qual));
4992 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4994 if (!null_pointer_constant_p (orig_op2))
4995 pedwarn (colon_loc, 0,
4996 "pointer/integer type mismatch in conditional expression");
4997 else
4999 op2 = null_pointer_node;
5001 result_type = type1;
5003 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5005 if (!null_pointer_constant_p (orig_op1))
5006 pedwarn (colon_loc, 0,
5007 "pointer/integer type mismatch in conditional expression");
5008 else
5010 op1 = null_pointer_node;
5012 result_type = type2;
5015 if (!result_type)
5017 if (flag_cond_mismatch)
5018 result_type = void_type_node;
5019 else
5021 error_at (colon_loc, "type mismatch in conditional expression");
5022 return error_mark_node;
5026 /* Merge const and volatile flags of the incoming types. */
5027 result_type
5028 = build_type_variant (result_type,
5029 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5030 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5032 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5033 semantic_result_type);
5034 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5035 semantic_result_type);
5037 if (ifexp_bcp && ifexp == truthvalue_true_node)
5039 op2_int_operands = true;
5040 op1 = c_fully_fold (op1, require_constant_value, NULL);
5042 if (ifexp_bcp && ifexp == truthvalue_false_node)
5044 op1_int_operands = true;
5045 op2 = c_fully_fold (op2, require_constant_value, NULL);
5047 int_const = int_operands = (ifexp_int_operands
5048 && op1_int_operands
5049 && op2_int_operands);
5050 if (int_operands)
5052 int_const = ((ifexp == truthvalue_true_node
5053 && TREE_CODE (orig_op1) == INTEGER_CST
5054 && !TREE_OVERFLOW (orig_op1))
5055 || (ifexp == truthvalue_false_node
5056 && TREE_CODE (orig_op2) == INTEGER_CST
5057 && !TREE_OVERFLOW (orig_op2)));
5060 /* Need to convert condition operand into a vector mask. */
5061 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5063 tree vectype = TREE_TYPE (ifexp);
5064 tree elem_type = TREE_TYPE (vectype);
5065 tree zero = build_int_cst (elem_type, 0);
5066 tree zero_vec = build_vector_from_val (vectype, zero);
5067 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5068 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5071 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5072 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5073 else
5075 if (int_operands)
5077 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5078 nested inside of the expression. */
5079 op1 = c_fully_fold (op1, false, NULL);
5080 op2 = c_fully_fold (op2, false, NULL);
5082 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5083 if (int_operands)
5084 ret = note_integer_operands (ret);
5086 if (semantic_result_type)
5087 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5089 protected_set_expr_location (ret, colon_loc);
5090 return ret;
5093 /* Return a compound expression that performs two expressions and
5094 returns the value of the second of them.
5096 LOC is the location of the COMPOUND_EXPR. */
5098 tree
5099 build_compound_expr (location_t loc, tree expr1, tree expr2)
5101 bool expr1_int_operands, expr2_int_operands;
5102 tree eptype = NULL_TREE;
5103 tree ret;
5105 if (flag_cilkplus
5106 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5107 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5109 error_at (loc,
5110 "spawned function call cannot be part of a comma expression");
5111 return error_mark_node;
5113 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5114 if (expr1_int_operands)
5115 expr1 = remove_c_maybe_const_expr (expr1);
5116 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5117 if (expr2_int_operands)
5118 expr2 = remove_c_maybe_const_expr (expr2);
5120 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5121 expr1 = TREE_OPERAND (expr1, 0);
5122 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5124 eptype = TREE_TYPE (expr2);
5125 expr2 = TREE_OPERAND (expr2, 0);
5128 if (!TREE_SIDE_EFFECTS (expr1))
5130 /* The left-hand operand of a comma expression is like an expression
5131 statement: with -Wunused, we should warn if it doesn't have
5132 any side-effects, unless it was explicitly cast to (void). */
5133 if (warn_unused_value)
5135 if (VOID_TYPE_P (TREE_TYPE (expr1))
5136 && CONVERT_EXPR_P (expr1))
5137 ; /* (void) a, b */
5138 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5139 && TREE_CODE (expr1) == COMPOUND_EXPR
5140 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5141 ; /* (void) a, (void) b, c */
5142 else
5143 warning_at (loc, OPT_Wunused_value,
5144 "left-hand operand of comma expression has no effect");
5147 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5148 && warn_unused_value)
5150 tree r = expr1;
5151 location_t cloc = loc;
5152 while (TREE_CODE (r) == COMPOUND_EXPR)
5154 if (EXPR_HAS_LOCATION (r))
5155 cloc = EXPR_LOCATION (r);
5156 r = TREE_OPERAND (r, 1);
5158 if (!TREE_SIDE_EFFECTS (r)
5159 && !VOID_TYPE_P (TREE_TYPE (r))
5160 && !CONVERT_EXPR_P (r))
5161 warning_at (cloc, OPT_Wunused_value,
5162 "right-hand operand of comma expression has no effect");
5165 /* With -Wunused, we should also warn if the left-hand operand does have
5166 side-effects, but computes a value which is not used. For example, in
5167 `foo() + bar(), baz()' the result of the `+' operator is not used,
5168 so we should issue a warning. */
5169 else if (warn_unused_value)
5170 warn_if_unused_value (expr1, loc);
5172 if (expr2 == error_mark_node)
5173 return error_mark_node;
5175 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5177 if (flag_isoc99
5178 && expr1_int_operands
5179 && expr2_int_operands)
5180 ret = note_integer_operands (ret);
5182 if (eptype)
5183 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5185 protected_set_expr_location (ret, loc);
5186 return ret;
5189 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5190 which we are casting. OTYPE is the type of the expression being
5191 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5192 of the cast. -Wcast-qual appeared on the command line. Named
5193 address space qualifiers are not handled here, because they result
5194 in different warnings. */
5196 static void
5197 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5199 tree in_type = type;
5200 tree in_otype = otype;
5201 int added = 0;
5202 int discarded = 0;
5203 bool is_const;
5205 /* Check that the qualifiers on IN_TYPE are a superset of the
5206 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5207 nodes is uninteresting and we stop as soon as we hit a
5208 non-POINTER_TYPE node on either type. */
5211 in_otype = TREE_TYPE (in_otype);
5212 in_type = TREE_TYPE (in_type);
5214 /* GNU C allows cv-qualified function types. 'const' means the
5215 function is very pure, 'volatile' means it can't return. We
5216 need to warn when such qualifiers are added, not when they're
5217 taken away. */
5218 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5219 && TREE_CODE (in_type) == FUNCTION_TYPE)
5220 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5221 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5222 else
5223 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5224 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5226 while (TREE_CODE (in_type) == POINTER_TYPE
5227 && TREE_CODE (in_otype) == POINTER_TYPE);
5229 if (added)
5230 warning_at (loc, OPT_Wcast_qual,
5231 "cast adds %q#v qualifier to function type", added);
5233 if (discarded)
5234 /* There are qualifiers present in IN_OTYPE that are not present
5235 in IN_TYPE. */
5236 warning_at (loc, OPT_Wcast_qual,
5237 "cast discards %qv qualifier from pointer target type",
5238 discarded);
5240 if (added || discarded)
5241 return;
5243 /* A cast from **T to const **T is unsafe, because it can cause a
5244 const value to be changed with no additional warning. We only
5245 issue this warning if T is the same on both sides, and we only
5246 issue the warning if there are the same number of pointers on
5247 both sides, as otherwise the cast is clearly unsafe anyhow. A
5248 cast is unsafe when a qualifier is added at one level and const
5249 is not present at all outer levels.
5251 To issue this warning, we check at each level whether the cast
5252 adds new qualifiers not already seen. We don't need to special
5253 case function types, as they won't have the same
5254 TYPE_MAIN_VARIANT. */
5256 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5257 return;
5258 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5259 return;
5261 in_type = type;
5262 in_otype = otype;
5263 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5266 in_type = TREE_TYPE (in_type);
5267 in_otype = TREE_TYPE (in_otype);
5268 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5269 && !is_const)
5271 warning_at (loc, OPT_Wcast_qual,
5272 "to be safe all intermediate pointers in cast from "
5273 "%qT to %qT must be %<const%> qualified",
5274 otype, type);
5275 break;
5277 if (is_const)
5278 is_const = TYPE_READONLY (in_type);
5280 while (TREE_CODE (in_type) == POINTER_TYPE);
5283 /* Build an expression representing a cast to type TYPE of expression EXPR.
5284 LOC is the location of the cast-- typically the open paren of the cast. */
5286 tree
5287 build_c_cast (location_t loc, tree type, tree expr)
5289 tree value;
5291 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5292 expr = TREE_OPERAND (expr, 0);
5294 value = expr;
5296 if (type == error_mark_node || expr == error_mark_node)
5297 return error_mark_node;
5299 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5300 only in <protocol> qualifications. But when constructing cast expressions,
5301 the protocols do matter and must be kept around. */
5302 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5303 return build1 (NOP_EXPR, type, expr);
5305 type = TYPE_MAIN_VARIANT (type);
5307 if (TREE_CODE (type) == ARRAY_TYPE)
5309 error_at (loc, "cast specifies array type");
5310 return error_mark_node;
5313 if (TREE_CODE (type) == FUNCTION_TYPE)
5315 error_at (loc, "cast specifies function type");
5316 return error_mark_node;
5319 if (!VOID_TYPE_P (type))
5321 value = require_complete_type (loc, value);
5322 if (value == error_mark_node)
5323 return error_mark_node;
5326 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5328 if (RECORD_OR_UNION_TYPE_P (type))
5329 pedwarn (loc, OPT_Wpedantic,
5330 "ISO C forbids casting nonscalar to the same type");
5332 /* Convert to remove any qualifiers from VALUE's type. */
5333 value = convert (type, value);
5335 else if (TREE_CODE (type) == UNION_TYPE)
5337 tree field;
5339 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5340 if (TREE_TYPE (field) != error_mark_node
5341 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5342 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5343 break;
5345 if (field)
5347 tree t;
5348 bool maybe_const = true;
5350 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5351 t = c_fully_fold (value, false, &maybe_const);
5352 t = build_constructor_single (type, field, t);
5353 if (!maybe_const)
5354 t = c_wrap_maybe_const (t, true);
5355 t = digest_init (loc, type, t,
5356 NULL_TREE, false, true, 0);
5357 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5358 return t;
5360 error_at (loc, "cast to union type from type not present in union");
5361 return error_mark_node;
5363 else
5365 tree otype, ovalue;
5367 if (type == void_type_node)
5369 tree t = build1 (CONVERT_EXPR, type, value);
5370 SET_EXPR_LOCATION (t, loc);
5371 return t;
5374 otype = TREE_TYPE (value);
5376 /* Optionally warn about potentially worrisome casts. */
5377 if (warn_cast_qual
5378 && TREE_CODE (type) == POINTER_TYPE
5379 && TREE_CODE (otype) == POINTER_TYPE)
5380 handle_warn_cast_qual (loc, type, otype);
5382 /* Warn about conversions between pointers to disjoint
5383 address spaces. */
5384 if (TREE_CODE (type) == POINTER_TYPE
5385 && TREE_CODE (otype) == POINTER_TYPE
5386 && !null_pointer_constant_p (value))
5388 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5389 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5390 addr_space_t as_common;
5392 if (!addr_space_superset (as_to, as_from, &as_common))
5394 if (ADDR_SPACE_GENERIC_P (as_from))
5395 warning_at (loc, 0, "cast to %s address space pointer "
5396 "from disjoint generic address space pointer",
5397 c_addr_space_name (as_to));
5399 else if (ADDR_SPACE_GENERIC_P (as_to))
5400 warning_at (loc, 0, "cast to generic address space pointer "
5401 "from disjoint %s address space pointer",
5402 c_addr_space_name (as_from));
5404 else
5405 warning_at (loc, 0, "cast to %s address space pointer "
5406 "from disjoint %s address space pointer",
5407 c_addr_space_name (as_to),
5408 c_addr_space_name (as_from));
5412 /* Warn about possible alignment problems. */
5413 if (STRICT_ALIGNMENT
5414 && TREE_CODE (type) == POINTER_TYPE
5415 && TREE_CODE (otype) == POINTER_TYPE
5416 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5417 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5418 /* Don't warn about opaque types, where the actual alignment
5419 restriction is unknown. */
5420 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5421 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5422 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5423 warning_at (loc, OPT_Wcast_align,
5424 "cast increases required alignment of target type");
5426 if (TREE_CODE (type) == INTEGER_TYPE
5427 && TREE_CODE (otype) == POINTER_TYPE
5428 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5429 /* Unlike conversion of integers to pointers, where the
5430 warning is disabled for converting constants because
5431 of cases such as SIG_*, warn about converting constant
5432 pointers to integers. In some cases it may cause unwanted
5433 sign extension, and a warning is appropriate. */
5434 warning_at (loc, OPT_Wpointer_to_int_cast,
5435 "cast from pointer to integer of different size");
5437 if (TREE_CODE (value) == CALL_EXPR
5438 && TREE_CODE (type) != TREE_CODE (otype))
5439 warning_at (loc, OPT_Wbad_function_cast,
5440 "cast from function call of type %qT "
5441 "to non-matching type %qT", otype, type);
5443 if (TREE_CODE (type) == POINTER_TYPE
5444 && TREE_CODE (otype) == INTEGER_TYPE
5445 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5446 /* Don't warn about converting any constant. */
5447 && !TREE_CONSTANT (value))
5448 warning_at (loc,
5449 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5450 "of different size");
5452 if (warn_strict_aliasing <= 2)
5453 strict_aliasing_warning (otype, type, expr);
5455 /* If pedantic, warn for conversions between function and object
5456 pointer types, except for converting a null pointer constant
5457 to function pointer type. */
5458 if (pedantic
5459 && TREE_CODE (type) == POINTER_TYPE
5460 && TREE_CODE (otype) == POINTER_TYPE
5461 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5462 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5463 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5464 "conversion of function pointer to object pointer type");
5466 if (pedantic
5467 && TREE_CODE (type) == POINTER_TYPE
5468 && TREE_CODE (otype) == POINTER_TYPE
5469 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5470 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5471 && !null_pointer_constant_p (value))
5472 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5473 "conversion of object pointer to function pointer type");
5475 ovalue = value;
5476 value = convert (type, value);
5478 /* Ignore any integer overflow caused by the cast. */
5479 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5481 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5483 if (!TREE_OVERFLOW (value))
5485 /* Avoid clobbering a shared constant. */
5486 value = copy_node (value);
5487 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5490 else if (TREE_OVERFLOW (value))
5491 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5492 value = wide_int_to_tree (TREE_TYPE (value), value);
5496 /* Don't let a cast be an lvalue. */
5497 if (lvalue_p (value))
5498 value = non_lvalue_loc (loc, value);
5500 /* Don't allow the results of casting to floating-point or complex
5501 types be confused with actual constants, or casts involving
5502 integer and pointer types other than direct integer-to-integer
5503 and integer-to-pointer be confused with integer constant
5504 expressions and null pointer constants. */
5505 if (TREE_CODE (value) == REAL_CST
5506 || TREE_CODE (value) == COMPLEX_CST
5507 || (TREE_CODE (value) == INTEGER_CST
5508 && !((TREE_CODE (expr) == INTEGER_CST
5509 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5510 || TREE_CODE (expr) == REAL_CST
5511 || TREE_CODE (expr) == COMPLEX_CST)))
5512 value = build1 (NOP_EXPR, type, value);
5514 protected_set_expr_location (value, loc);
5515 return value;
5518 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5519 location of the open paren of the cast, or the position of the cast
5520 expr. */
5521 tree
5522 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5524 tree type;
5525 tree type_expr = NULL_TREE;
5526 bool type_expr_const = true;
5527 tree ret;
5528 int saved_wsp = warn_strict_prototypes;
5530 /* This avoids warnings about unprototyped casts on
5531 integers. E.g. "#define SIG_DFL (void(*)())0". */
5532 if (TREE_CODE (expr) == INTEGER_CST)
5533 warn_strict_prototypes = 0;
5534 type = groktypename (type_name, &type_expr, &type_expr_const);
5535 warn_strict_prototypes = saved_wsp;
5537 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5538 && reject_gcc_builtin (expr))
5539 return error_mark_node;
5541 ret = build_c_cast (loc, type, expr);
5542 if (type_expr)
5544 bool inner_expr_const = true;
5545 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5546 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5547 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5548 && inner_expr_const);
5549 SET_EXPR_LOCATION (ret, loc);
5552 if (!EXPR_HAS_LOCATION (ret))
5553 protected_set_expr_location (ret, loc);
5555 /* C++ does not permits types to be defined in a cast, but it
5556 allows references to incomplete types. */
5557 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5558 warning_at (loc, OPT_Wc___compat,
5559 "defining a type in a cast is invalid in C++");
5561 return ret;
5564 /* Build an assignment expression of lvalue LHS from value RHS.
5565 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5566 may differ from TREE_TYPE (LHS) for an enum bitfield.
5567 MODIFYCODE is the code for a binary operator that we use
5568 to combine the old value of LHS with RHS to get the new value.
5569 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5570 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5571 which may differ from TREE_TYPE (RHS) for an enum value.
5573 LOCATION is the location of the MODIFYCODE operator.
5574 RHS_LOC is the location of the RHS. */
5576 tree
5577 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5578 enum tree_code modifycode,
5579 location_t rhs_loc, tree rhs, tree rhs_origtype)
5581 tree result;
5582 tree newrhs;
5583 tree rhseval = NULL_TREE;
5584 tree rhs_semantic_type = NULL_TREE;
5585 tree lhstype = TREE_TYPE (lhs);
5586 tree olhstype = lhstype;
5587 bool npc;
5588 bool is_atomic_op;
5590 /* Types that aren't fully specified cannot be used in assignments. */
5591 lhs = require_complete_type (location, lhs);
5593 /* Avoid duplicate error messages from operands that had errors. */
5594 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5595 return error_mark_node;
5597 /* Ensure an error for assigning a non-lvalue array to an array in
5598 C90. */
5599 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5601 error_at (location, "assignment to expression with array type");
5602 return error_mark_node;
5605 /* For ObjC properties, defer this check. */
5606 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5607 return error_mark_node;
5609 is_atomic_op = really_atomic_lvalue (lhs);
5611 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5613 rhs_semantic_type = TREE_TYPE (rhs);
5614 rhs = TREE_OPERAND (rhs, 0);
5617 newrhs = rhs;
5619 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5621 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5622 lhs_origtype, modifycode, rhs_loc, rhs,
5623 rhs_origtype);
5624 if (inner == error_mark_node)
5625 return error_mark_node;
5626 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5627 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5628 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5629 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5630 protected_set_expr_location (result, location);
5631 return result;
5634 /* If a binary op has been requested, combine the old LHS value with the RHS
5635 producing the value we should actually store into the LHS. */
5637 if (modifycode != NOP_EXPR)
5639 lhs = c_fully_fold (lhs, false, NULL);
5640 lhs = stabilize_reference (lhs);
5642 /* Construct the RHS for any non-atomic compound assignemnt. */
5643 if (!is_atomic_op)
5645 /* If in LHS op= RHS the RHS has side-effects, ensure they
5646 are preevaluated before the rest of the assignment expression's
5647 side-effects, because RHS could contain e.g. function calls
5648 that modify LHS. */
5649 if (TREE_SIDE_EFFECTS (rhs))
5651 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5652 rhseval = newrhs;
5654 newrhs = build_binary_op (location,
5655 modifycode, lhs, newrhs, 1);
5657 /* The original type of the right hand side is no longer
5658 meaningful. */
5659 rhs_origtype = NULL_TREE;
5663 if (c_dialect_objc ())
5665 /* Check if we are modifying an Objective-C property reference;
5666 if so, we need to generate setter calls. */
5667 result = objc_maybe_build_modify_expr (lhs, newrhs);
5668 if (result)
5669 goto return_result;
5671 /* Else, do the check that we postponed for Objective-C. */
5672 if (!lvalue_or_else (location, lhs, lv_assign))
5673 return error_mark_node;
5676 /* Give an error for storing in something that is 'const'. */
5678 if (TYPE_READONLY (lhstype)
5679 || (RECORD_OR_UNION_TYPE_P (lhstype)
5680 && C_TYPE_FIELDS_READONLY (lhstype)))
5682 readonly_error (location, lhs, lv_assign);
5683 return error_mark_node;
5685 else if (TREE_READONLY (lhs))
5686 readonly_warning (lhs, lv_assign);
5688 /* If storing into a structure or union member,
5689 it has probably been given type `int'.
5690 Compute the type that would go with
5691 the actual amount of storage the member occupies. */
5693 if (TREE_CODE (lhs) == COMPONENT_REF
5694 && (TREE_CODE (lhstype) == INTEGER_TYPE
5695 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5696 || TREE_CODE (lhstype) == REAL_TYPE
5697 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5698 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5700 /* If storing in a field that is in actuality a short or narrower than one,
5701 we must store in the field in its actual type. */
5703 if (lhstype != TREE_TYPE (lhs))
5705 lhs = copy_node (lhs);
5706 TREE_TYPE (lhs) = lhstype;
5709 /* Issue -Wc++-compat warnings about an assignment to an enum type
5710 when LHS does not have its original type. This happens for,
5711 e.g., an enum bitfield in a struct. */
5712 if (warn_cxx_compat
5713 && lhs_origtype != NULL_TREE
5714 && lhs_origtype != lhstype
5715 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5717 tree checktype = (rhs_origtype != NULL_TREE
5718 ? rhs_origtype
5719 : TREE_TYPE (rhs));
5720 if (checktype != error_mark_node
5721 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5722 || (is_atomic_op && modifycode != NOP_EXPR)))
5723 warning_at (location, OPT_Wc___compat,
5724 "enum conversion in assignment is invalid in C++");
5727 /* If the lhs is atomic, remove that qualifier. */
5728 if (is_atomic_op)
5730 lhstype = build_qualified_type (lhstype,
5731 (TYPE_QUALS (lhstype)
5732 & ~TYPE_QUAL_ATOMIC));
5733 olhstype = build_qualified_type (olhstype,
5734 (TYPE_QUALS (lhstype)
5735 & ~TYPE_QUAL_ATOMIC));
5738 /* Convert new value to destination type. Fold it first, then
5739 restore any excess precision information, for the sake of
5740 conversion warnings. */
5742 if (!(is_atomic_op && modifycode != NOP_EXPR))
5744 npc = null_pointer_constant_p (newrhs);
5745 newrhs = c_fully_fold (newrhs, false, NULL);
5746 if (rhs_semantic_type)
5747 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5748 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5749 rhs_origtype, ic_assign, npc,
5750 NULL_TREE, NULL_TREE, 0);
5751 if (TREE_CODE (newrhs) == ERROR_MARK)
5752 return error_mark_node;
5755 /* Emit ObjC write barrier, if necessary. */
5756 if (c_dialect_objc () && flag_objc_gc)
5758 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5759 if (result)
5761 protected_set_expr_location (result, location);
5762 goto return_result;
5766 /* Scan operands. */
5768 if (is_atomic_op)
5769 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5770 else
5772 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5773 TREE_SIDE_EFFECTS (result) = 1;
5774 protected_set_expr_location (result, location);
5777 /* If we got the LHS in a different type for storing in,
5778 convert the result back to the nominal type of LHS
5779 so that the value we return always has the same type
5780 as the LHS argument. */
5782 if (olhstype == TREE_TYPE (result))
5783 goto return_result;
5785 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5786 rhs_origtype, ic_assign, false, NULL_TREE,
5787 NULL_TREE, 0);
5788 protected_set_expr_location (result, location);
5790 return_result:
5791 if (rhseval)
5792 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5793 return result;
5796 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5797 This is used to implement -fplan9-extensions. */
5799 static bool
5800 find_anonymous_field_with_type (tree struct_type, tree type)
5802 tree field;
5803 bool found;
5805 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5806 found = false;
5807 for (field = TYPE_FIELDS (struct_type);
5808 field != NULL_TREE;
5809 field = TREE_CHAIN (field))
5811 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5812 ? c_build_qualified_type (TREE_TYPE (field),
5813 TYPE_QUAL_ATOMIC)
5814 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5815 if (DECL_NAME (field) == NULL
5816 && comptypes (type, fieldtype))
5818 if (found)
5819 return false;
5820 found = true;
5822 else if (DECL_NAME (field) == NULL
5823 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5824 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5826 if (found)
5827 return false;
5828 found = true;
5831 return found;
5834 /* RHS is an expression whose type is pointer to struct. If there is
5835 an anonymous field in RHS with type TYPE, then return a pointer to
5836 that field in RHS. This is used with -fplan9-extensions. This
5837 returns NULL if no conversion could be found. */
5839 static tree
5840 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5842 tree rhs_struct_type, lhs_main_type;
5843 tree field, found_field;
5844 bool found_sub_field;
5845 tree ret;
5847 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5848 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5849 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5851 gcc_assert (POINTER_TYPE_P (type));
5852 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5853 ? c_build_qualified_type (TREE_TYPE (type),
5854 TYPE_QUAL_ATOMIC)
5855 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5857 found_field = NULL_TREE;
5858 found_sub_field = false;
5859 for (field = TYPE_FIELDS (rhs_struct_type);
5860 field != NULL_TREE;
5861 field = TREE_CHAIN (field))
5863 if (DECL_NAME (field) != NULL_TREE
5864 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5865 continue;
5866 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5867 ? c_build_qualified_type (TREE_TYPE (field),
5868 TYPE_QUAL_ATOMIC)
5869 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5870 if (comptypes (lhs_main_type, fieldtype))
5872 if (found_field != NULL_TREE)
5873 return NULL_TREE;
5874 found_field = field;
5876 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5877 lhs_main_type))
5879 if (found_field != NULL_TREE)
5880 return NULL_TREE;
5881 found_field = field;
5882 found_sub_field = true;
5886 if (found_field == NULL_TREE)
5887 return NULL_TREE;
5889 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5890 build_fold_indirect_ref (rhs), found_field,
5891 NULL_TREE);
5892 ret = build_fold_addr_expr_loc (location, ret);
5894 if (found_sub_field)
5896 ret = convert_to_anonymous_field (location, type, ret);
5897 gcc_assert (ret != NULL_TREE);
5900 return ret;
5903 /* Issue an error message for a bad initializer component.
5904 GMSGID identifies the message.
5905 The component name is taken from the spelling stack. */
5907 static void
5908 error_init (location_t loc, const char *gmsgid)
5910 char *ofwhat;
5912 /* The gmsgid may be a format string with %< and %>. */
5913 error_at (loc, gmsgid);
5914 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5915 if (*ofwhat)
5916 inform (loc, "(near initialization for %qs)", ofwhat);
5919 /* Issue a pedantic warning for a bad initializer component. OPT is
5920 the option OPT_* (from options.h) controlling this warning or 0 if
5921 it is unconditionally given. GMSGID identifies the message. The
5922 component name is taken from the spelling stack. */
5924 static void
5925 pedwarn_init (location_t loc, int opt, const char *gmsgid)
5927 char *ofwhat;
5928 bool warned;
5930 /* Use the location where a macro was expanded rather than where
5931 it was defined to make sure macros defined in system headers
5932 but used incorrectly elsewhere are diagnosed. */
5933 source_location exploc = expansion_point_location_if_in_system_header (loc);
5935 /* The gmsgid may be a format string with %< and %>. */
5936 warned = pedwarn (exploc, opt, gmsgid);
5937 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5938 if (*ofwhat && warned)
5939 inform (exploc, "(near initialization for %qs)", ofwhat);
5942 /* Issue a warning for a bad initializer component.
5944 OPT is the OPT_W* value corresponding to the warning option that
5945 controls this warning. GMSGID identifies the message. The
5946 component name is taken from the spelling stack. */
5948 static void
5949 warning_init (location_t loc, int opt, const char *gmsgid)
5951 char *ofwhat;
5952 bool warned;
5954 /* Use the location where a macro was expanded rather than where
5955 it was defined to make sure macros defined in system headers
5956 but used incorrectly elsewhere are diagnosed. */
5957 source_location exploc = expansion_point_location_if_in_system_header (loc);
5959 /* The gmsgid may be a format string with %< and %>. */
5960 warned = warning_at (exploc, opt, gmsgid);
5961 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5962 if (*ofwhat && warned)
5963 inform (exploc, "(near initialization for %qs)", ofwhat);
5966 /* If TYPE is an array type and EXPR is a parenthesized string
5967 constant, warn if pedantic that EXPR is being used to initialize an
5968 object of type TYPE. */
5970 void
5971 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5973 if (pedantic
5974 && TREE_CODE (type) == ARRAY_TYPE
5975 && TREE_CODE (expr.value) == STRING_CST
5976 && expr.original_code != STRING_CST)
5977 pedwarn_init (loc, OPT_Wpedantic,
5978 "array initialized from parenthesized string constant");
5981 /* Convert value RHS to type TYPE as preparation for an assignment to
5982 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5983 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5984 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5985 constant before any folding.
5986 The real work of conversion is done by `convert'.
5987 The purpose of this function is to generate error messages
5988 for assignments that are not allowed in C.
5989 ERRTYPE says whether it is argument passing, assignment,
5990 initialization or return.
5992 In the following example, '~' denotes where EXPR_LOC and '^' where
5993 LOCATION point to:
5995 f (var); [ic_argpass]
5996 ^ ~~~
5997 x = var; [ic_assign]
5998 ^ ~~~;
5999 int x = var; [ic_init]
6001 return x; [ic_return]
6004 FUNCTION is a tree for the function being called.
6005 PARMNUM is the number of the argument, for printing in error messages. */
6007 static tree
6008 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6009 tree rhs, tree origtype, enum impl_conv errtype,
6010 bool null_pointer_constant, tree fundecl,
6011 tree function, int parmnum)
6013 enum tree_code codel = TREE_CODE (type);
6014 tree orig_rhs = rhs;
6015 tree rhstype;
6016 enum tree_code coder;
6017 tree rname = NULL_TREE;
6018 bool objc_ok = false;
6020 /* Use the expansion point location to handle cases such as user's
6021 function returning a wrong-type macro defined in a system header. */
6022 location = expansion_point_location_if_in_system_header (location);
6024 if (errtype == ic_argpass)
6026 tree selector;
6027 /* Change pointer to function to the function itself for
6028 diagnostics. */
6029 if (TREE_CODE (function) == ADDR_EXPR
6030 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6031 function = TREE_OPERAND (function, 0);
6033 /* Handle an ObjC selector specially for diagnostics. */
6034 selector = objc_message_selector ();
6035 rname = function;
6036 if (selector && parmnum > 2)
6038 rname = selector;
6039 parmnum -= 2;
6043 /* This macro is used to emit diagnostics to ensure that all format
6044 strings are complete sentences, visible to gettext and checked at
6045 compile time. */
6046 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6047 do { \
6048 switch (errtype) \
6050 case ic_argpass: \
6051 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6052 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6053 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6054 "expected %qT but argument is of type %qT", \
6055 type, rhstype); \
6056 break; \
6057 case ic_assign: \
6058 pedwarn (LOCATION, OPT, AS); \
6059 break; \
6060 case ic_init: \
6061 pedwarn_init (LOCATION, OPT, IN); \
6062 break; \
6063 case ic_return: \
6064 pedwarn (LOCATION, OPT, RE); \
6065 break; \
6066 default: \
6067 gcc_unreachable (); \
6069 } while (0)
6071 /* This macro is used to emit diagnostics to ensure that all format
6072 strings are complete sentences, visible to gettext and checked at
6073 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6074 extra parameter to enumerate qualifiers. */
6075 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6076 do { \
6077 switch (errtype) \
6079 case ic_argpass: \
6080 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6081 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6082 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6083 "expected %qT but argument is of type %qT", \
6084 type, rhstype); \
6085 break; \
6086 case ic_assign: \
6087 pedwarn (LOCATION, OPT, AS, QUALS); \
6088 break; \
6089 case ic_init: \
6090 pedwarn (LOCATION, OPT, IN, QUALS); \
6091 break; \
6092 case ic_return: \
6093 pedwarn (LOCATION, OPT, RE, QUALS); \
6094 break; \
6095 default: \
6096 gcc_unreachable (); \
6098 } while (0)
6100 /* This macro is used to emit diagnostics to ensure that all format
6101 strings are complete sentences, visible to gettext and checked at
6102 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6103 warning_at instead of pedwarn. */
6104 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6105 do { \
6106 switch (errtype) \
6108 case ic_argpass: \
6109 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6110 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6111 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6112 "expected %qT but argument is of type %qT", \
6113 type, rhstype); \
6114 break; \
6115 case ic_assign: \
6116 warning_at (LOCATION, OPT, AS, QUALS); \
6117 break; \
6118 case ic_init: \
6119 warning_at (LOCATION, OPT, IN, QUALS); \
6120 break; \
6121 case ic_return: \
6122 warning_at (LOCATION, OPT, RE, QUALS); \
6123 break; \
6124 default: \
6125 gcc_unreachable (); \
6127 } while (0)
6129 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6130 rhs = TREE_OPERAND (rhs, 0);
6132 rhstype = TREE_TYPE (rhs);
6133 coder = TREE_CODE (rhstype);
6135 if (coder == ERROR_MARK)
6136 return error_mark_node;
6138 if (c_dialect_objc ())
6140 int parmno;
6142 switch (errtype)
6144 case ic_return:
6145 parmno = 0;
6146 break;
6148 case ic_assign:
6149 parmno = -1;
6150 break;
6152 case ic_init:
6153 parmno = -2;
6154 break;
6156 default:
6157 parmno = parmnum;
6158 break;
6161 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6164 if (warn_cxx_compat)
6166 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6167 if (checktype != error_mark_node
6168 && TREE_CODE (type) == ENUMERAL_TYPE
6169 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6171 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6172 G_("enum conversion when passing argument "
6173 "%d of %qE is invalid in C++"),
6174 G_("enum conversion in assignment is "
6175 "invalid in C++"),
6176 G_("enum conversion in initialization is "
6177 "invalid in C++"),
6178 G_("enum conversion in return is "
6179 "invalid in C++"));
6183 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6184 return rhs;
6186 if (coder == VOID_TYPE)
6188 /* Except for passing an argument to an unprototyped function,
6189 this is a constraint violation. When passing an argument to
6190 an unprototyped function, it is compile-time undefined;
6191 making it a constraint in that case was rejected in
6192 DR#252. */
6193 error_at (location, "void value not ignored as it ought to be");
6194 return error_mark_node;
6196 rhs = require_complete_type (location, rhs);
6197 if (rhs == error_mark_node)
6198 return error_mark_node;
6200 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6201 return error_mark_node;
6203 /* A non-reference type can convert to a reference. This handles
6204 va_start, va_copy and possibly port built-ins. */
6205 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6207 if (!lvalue_p (rhs))
6209 error_at (location, "cannot pass rvalue to reference parameter");
6210 return error_mark_node;
6212 if (!c_mark_addressable (rhs))
6213 return error_mark_node;
6214 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6215 SET_EXPR_LOCATION (rhs, location);
6217 rhs = convert_for_assignment (location, expr_loc,
6218 build_pointer_type (TREE_TYPE (type)),
6219 rhs, origtype, errtype,
6220 null_pointer_constant, fundecl, function,
6221 parmnum);
6222 if (rhs == error_mark_node)
6223 return error_mark_node;
6225 rhs = build1 (NOP_EXPR, type, rhs);
6226 SET_EXPR_LOCATION (rhs, location);
6227 return rhs;
6229 /* Some types can interconvert without explicit casts. */
6230 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6231 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6232 return convert (type, rhs);
6233 /* Arithmetic types all interconvert, and enum is treated like int. */
6234 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6235 || codel == FIXED_POINT_TYPE
6236 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6237 || codel == BOOLEAN_TYPE)
6238 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6239 || coder == FIXED_POINT_TYPE
6240 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6241 || coder == BOOLEAN_TYPE))
6243 tree ret;
6244 bool save = in_late_binary_op;
6245 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6246 || (coder == REAL_TYPE
6247 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6248 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6249 in_late_binary_op = true;
6250 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6251 ? expr_loc : location, type, orig_rhs);
6252 in_late_binary_op = save;
6253 return ret;
6256 /* Aggregates in different TUs might need conversion. */
6257 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6258 && codel == coder
6259 && comptypes (type, rhstype))
6260 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6261 ? expr_loc : location, type, rhs);
6263 /* Conversion to a transparent union or record from its member types.
6264 This applies only to function arguments. */
6265 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6266 && TYPE_TRANSPARENT_AGGR (type))
6267 && errtype == ic_argpass)
6269 tree memb, marginal_memb = NULL_TREE;
6271 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6273 tree memb_type = TREE_TYPE (memb);
6275 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6276 TYPE_MAIN_VARIANT (rhstype)))
6277 break;
6279 if (TREE_CODE (memb_type) != POINTER_TYPE)
6280 continue;
6282 if (coder == POINTER_TYPE)
6284 tree ttl = TREE_TYPE (memb_type);
6285 tree ttr = TREE_TYPE (rhstype);
6287 /* Any non-function converts to a [const][volatile] void *
6288 and vice versa; otherwise, targets must be the same.
6289 Meanwhile, the lhs target must have all the qualifiers of
6290 the rhs. */
6291 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6292 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6293 || comp_target_types (location, memb_type, rhstype))
6295 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6296 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6297 /* If this type won't generate any warnings, use it. */
6298 if (lquals == rquals
6299 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6300 && TREE_CODE (ttl) == FUNCTION_TYPE)
6301 ? ((lquals | rquals) == rquals)
6302 : ((lquals | rquals) == lquals)))
6303 break;
6305 /* Keep looking for a better type, but remember this one. */
6306 if (!marginal_memb)
6307 marginal_memb = memb;
6311 /* Can convert integer zero to any pointer type. */
6312 if (null_pointer_constant)
6314 rhs = null_pointer_node;
6315 break;
6319 if (memb || marginal_memb)
6321 if (!memb)
6323 /* We have only a marginally acceptable member type;
6324 it needs a warning. */
6325 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6326 tree ttr = TREE_TYPE (rhstype);
6328 /* Const and volatile mean something different for function
6329 types, so the usual warnings are not appropriate. */
6330 if (TREE_CODE (ttr) == FUNCTION_TYPE
6331 && TREE_CODE (ttl) == FUNCTION_TYPE)
6333 /* Because const and volatile on functions are
6334 restrictions that say the function will not do
6335 certain things, it is okay to use a const or volatile
6336 function where an ordinary one is wanted, but not
6337 vice-versa. */
6338 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6339 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6340 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6341 OPT_Wdiscarded_qualifiers,
6342 G_("passing argument %d of %qE "
6343 "makes %q#v qualified function "
6344 "pointer from unqualified"),
6345 G_("assignment makes %q#v qualified "
6346 "function pointer from "
6347 "unqualified"),
6348 G_("initialization makes %q#v qualified "
6349 "function pointer from "
6350 "unqualified"),
6351 G_("return makes %q#v qualified function "
6352 "pointer from unqualified"),
6353 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6355 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6356 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6357 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6358 OPT_Wdiscarded_qualifiers,
6359 G_("passing argument %d of %qE discards "
6360 "%qv qualifier from pointer target type"),
6361 G_("assignment discards %qv qualifier "
6362 "from pointer target type"),
6363 G_("initialization discards %qv qualifier "
6364 "from pointer target type"),
6365 G_("return discards %qv qualifier from "
6366 "pointer target type"),
6367 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6369 memb = marginal_memb;
6372 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6373 pedwarn (location, OPT_Wpedantic,
6374 "ISO C prohibits argument conversion to union type");
6376 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6377 return build_constructor_single (type, memb, rhs);
6381 /* Conversions among pointers */
6382 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6383 && (coder == codel))
6385 tree ttl = TREE_TYPE (type);
6386 tree ttr = TREE_TYPE (rhstype);
6387 tree mvl = ttl;
6388 tree mvr = ttr;
6389 bool is_opaque_pointer;
6390 int target_cmp = 0; /* Cache comp_target_types () result. */
6391 addr_space_t asl;
6392 addr_space_t asr;
6394 if (TREE_CODE (mvl) != ARRAY_TYPE)
6395 mvl = (TYPE_ATOMIC (mvl)
6396 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6397 TYPE_QUAL_ATOMIC)
6398 : TYPE_MAIN_VARIANT (mvl));
6399 if (TREE_CODE (mvr) != ARRAY_TYPE)
6400 mvr = (TYPE_ATOMIC (mvr)
6401 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6402 TYPE_QUAL_ATOMIC)
6403 : TYPE_MAIN_VARIANT (mvr));
6404 /* Opaque pointers are treated like void pointers. */
6405 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6407 /* The Plan 9 compiler permits a pointer to a struct to be
6408 automatically converted into a pointer to an anonymous field
6409 within the struct. */
6410 if (flag_plan9_extensions
6411 && RECORD_OR_UNION_TYPE_P (mvl)
6412 && RECORD_OR_UNION_TYPE_P (mvr)
6413 && mvl != mvr)
6415 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6416 if (new_rhs != NULL_TREE)
6418 rhs = new_rhs;
6419 rhstype = TREE_TYPE (rhs);
6420 coder = TREE_CODE (rhstype);
6421 ttr = TREE_TYPE (rhstype);
6422 mvr = TYPE_MAIN_VARIANT (ttr);
6426 /* C++ does not allow the implicit conversion void* -> T*. However,
6427 for the purpose of reducing the number of false positives, we
6428 tolerate the special case of
6430 int *p = NULL;
6432 where NULL is typically defined in C to be '(void *) 0'. */
6433 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6434 warning_at (errtype == ic_argpass ? expr_loc : location,
6435 OPT_Wc___compat,
6436 "request for implicit conversion "
6437 "from %qT to %qT not permitted in C++", rhstype, type);
6439 /* See if the pointers point to incompatible address spaces. */
6440 asl = TYPE_ADDR_SPACE (ttl);
6441 asr = TYPE_ADDR_SPACE (ttr);
6442 if (!null_pointer_constant_p (rhs)
6443 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6445 switch (errtype)
6447 case ic_argpass:
6448 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6449 "non-enclosed address space", parmnum, rname);
6450 break;
6451 case ic_assign:
6452 error_at (location, "assignment from pointer to "
6453 "non-enclosed address space");
6454 break;
6455 case ic_init:
6456 error_at (location, "initialization from pointer to "
6457 "non-enclosed address space");
6458 break;
6459 case ic_return:
6460 error_at (location, "return from pointer to "
6461 "non-enclosed address space");
6462 break;
6463 default:
6464 gcc_unreachable ();
6466 return error_mark_node;
6469 /* Check if the right-hand side has a format attribute but the
6470 left-hand side doesn't. */
6471 if (warn_suggest_attribute_format
6472 && check_missing_format_attribute (type, rhstype))
6474 switch (errtype)
6476 case ic_argpass:
6477 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6478 "argument %d of %qE might be "
6479 "a candidate for a format attribute",
6480 parmnum, rname);
6481 break;
6482 case ic_assign:
6483 warning_at (location, OPT_Wsuggest_attribute_format,
6484 "assignment left-hand side might be "
6485 "a candidate for a format attribute");
6486 break;
6487 case ic_init:
6488 warning_at (location, OPT_Wsuggest_attribute_format,
6489 "initialization left-hand side might be "
6490 "a candidate for a format attribute");
6491 break;
6492 case ic_return:
6493 warning_at (location, OPT_Wsuggest_attribute_format,
6494 "return type might be "
6495 "a candidate for a format attribute");
6496 break;
6497 default:
6498 gcc_unreachable ();
6502 /* Any non-function converts to a [const][volatile] void *
6503 and vice versa; otherwise, targets must be the same.
6504 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6505 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6506 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6507 || (target_cmp = comp_target_types (location, type, rhstype))
6508 || is_opaque_pointer
6509 || ((c_common_unsigned_type (mvl)
6510 == c_common_unsigned_type (mvr))
6511 && (c_common_signed_type (mvl)
6512 == c_common_signed_type (mvr))
6513 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6515 /* Warn about loss of qualifers from pointers to arrays with
6516 qualifiers on the element type. */
6517 if (TREE_CODE (ttr) == ARRAY_TYPE)
6519 ttr = strip_array_types (ttr);
6520 ttl = strip_array_types (ttl);
6522 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6523 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6524 WARNING_FOR_QUALIFIERS (location, expr_loc,
6525 OPT_Wdiscarded_array_qualifiers,
6526 G_("passing argument %d of %qE discards "
6527 "%qv qualifier from pointer target type"),
6528 G_("assignment discards %qv qualifier "
6529 "from pointer target type"),
6530 G_("initialization discards %qv qualifier "
6531 "from pointer target type"),
6532 G_("return discards %qv qualifier from "
6533 "pointer target type"),
6534 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6536 else if (pedantic
6537 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6539 (VOID_TYPE_P (ttr)
6540 && !null_pointer_constant
6541 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6542 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6543 G_("ISO C forbids passing argument %d of "
6544 "%qE between function pointer "
6545 "and %<void *%>"),
6546 G_("ISO C forbids assignment between "
6547 "function pointer and %<void *%>"),
6548 G_("ISO C forbids initialization between "
6549 "function pointer and %<void *%>"),
6550 G_("ISO C forbids return between function "
6551 "pointer and %<void *%>"));
6552 /* Const and volatile mean something different for function types,
6553 so the usual warnings are not appropriate. */
6554 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6555 && TREE_CODE (ttl) != FUNCTION_TYPE)
6557 /* Don't warn about loss of qualifier for conversions from
6558 qualified void* to pointers to arrays with corresponding
6559 qualifier on the element type. */
6560 if (!pedantic)
6561 ttl = strip_array_types (ttl);
6563 /* Assignments between atomic and non-atomic objects are OK. */
6564 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6565 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6567 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6568 OPT_Wdiscarded_qualifiers,
6569 G_("passing argument %d of %qE discards "
6570 "%qv qualifier from pointer target type"),
6571 G_("assignment discards %qv qualifier "
6572 "from pointer target type"),
6573 G_("initialization discards %qv qualifier "
6574 "from pointer target type"),
6575 G_("return discards %qv qualifier from "
6576 "pointer target type"),
6577 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6579 /* If this is not a case of ignoring a mismatch in signedness,
6580 no warning. */
6581 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6582 || target_cmp)
6584 /* If there is a mismatch, do warn. */
6585 else if (warn_pointer_sign)
6586 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6587 G_("pointer targets in passing argument "
6588 "%d of %qE differ in signedness"),
6589 G_("pointer targets in assignment "
6590 "differ in signedness"),
6591 G_("pointer targets in initialization "
6592 "differ in signedness"),
6593 G_("pointer targets in return differ "
6594 "in signedness"));
6596 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6597 && TREE_CODE (ttr) == FUNCTION_TYPE)
6599 /* Because const and volatile on functions are restrictions
6600 that say the function will not do certain things,
6601 it is okay to use a const or volatile function
6602 where an ordinary one is wanted, but not vice-versa. */
6603 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6604 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6605 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6606 OPT_Wdiscarded_qualifiers,
6607 G_("passing argument %d of %qE makes "
6608 "%q#v qualified function pointer "
6609 "from unqualified"),
6610 G_("assignment makes %q#v qualified function "
6611 "pointer from unqualified"),
6612 G_("initialization makes %q#v qualified "
6613 "function pointer from unqualified"),
6614 G_("return makes %q#v qualified function "
6615 "pointer from unqualified"),
6616 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6619 else
6620 /* Avoid warning about the volatile ObjC EH puts on decls. */
6621 if (!objc_ok)
6622 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6623 OPT_Wincompatible_pointer_types,
6624 G_("passing argument %d of %qE from "
6625 "incompatible pointer type"),
6626 G_("assignment from incompatible pointer type"),
6627 G_("initialization from incompatible "
6628 "pointer type"),
6629 G_("return from incompatible pointer type"));
6631 return convert (type, rhs);
6633 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6635 /* ??? This should not be an error when inlining calls to
6636 unprototyped functions. */
6637 error_at (location, "invalid use of non-lvalue array");
6638 return error_mark_node;
6640 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6642 /* An explicit constant 0 can convert to a pointer,
6643 or one that results from arithmetic, even including
6644 a cast to integer type. */
6645 if (!null_pointer_constant)
6646 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6647 OPT_Wint_conversion,
6648 G_("passing argument %d of %qE makes "
6649 "pointer from integer without a cast"),
6650 G_("assignment makes pointer from integer "
6651 "without a cast"),
6652 G_("initialization makes pointer from "
6653 "integer without a cast"),
6654 G_("return makes pointer from integer "
6655 "without a cast"));
6657 return convert (type, rhs);
6659 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6661 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6662 OPT_Wint_conversion,
6663 G_("passing argument %d of %qE makes integer "
6664 "from pointer without a cast"),
6665 G_("assignment makes integer from pointer "
6666 "without a cast"),
6667 G_("initialization makes integer from pointer "
6668 "without a cast"),
6669 G_("return makes integer from pointer "
6670 "without a cast"));
6671 return convert (type, rhs);
6673 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6675 tree ret;
6676 bool save = in_late_binary_op;
6677 in_late_binary_op = true;
6678 ret = convert (type, rhs);
6679 in_late_binary_op = save;
6680 return ret;
6683 switch (errtype)
6685 case ic_argpass:
6686 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6687 rname);
6688 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6689 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6690 "expected %qT but argument is of type %qT", type, rhstype);
6691 break;
6692 case ic_assign:
6693 error_at (location, "incompatible types when assigning to type %qT from "
6694 "type %qT", type, rhstype);
6695 break;
6696 case ic_init:
6697 error_at (location,
6698 "incompatible types when initializing type %qT using type %qT",
6699 type, rhstype);
6700 break;
6701 case ic_return:
6702 error_at (location,
6703 "incompatible types when returning type %qT but %qT was "
6704 "expected", rhstype, type);
6705 break;
6706 default:
6707 gcc_unreachable ();
6710 return error_mark_node;
6713 /* If VALUE is a compound expr all of whose expressions are constant, then
6714 return its value. Otherwise, return error_mark_node.
6716 This is for handling COMPOUND_EXPRs as initializer elements
6717 which is allowed with a warning when -pedantic is specified. */
6719 static tree
6720 valid_compound_expr_initializer (tree value, tree endtype)
6722 if (TREE_CODE (value) == COMPOUND_EXPR)
6724 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6725 == error_mark_node)
6726 return error_mark_node;
6727 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6728 endtype);
6730 else if (!initializer_constant_valid_p (value, endtype))
6731 return error_mark_node;
6732 else
6733 return value;
6736 /* Perform appropriate conversions on the initial value of a variable,
6737 store it in the declaration DECL,
6738 and print any error messages that are appropriate.
6739 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6740 If the init is invalid, store an ERROR_MARK.
6742 INIT_LOC is the location of the initial value. */
6744 void
6745 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6747 tree value, type;
6748 bool npc = false;
6750 /* If variable's type was invalidly declared, just ignore it. */
6752 type = TREE_TYPE (decl);
6753 if (TREE_CODE (type) == ERROR_MARK)
6754 return;
6756 /* Digest the specified initializer into an expression. */
6758 if (init)
6759 npc = null_pointer_constant_p (init);
6760 value = digest_init (init_loc, type, init, origtype, npc,
6761 true, TREE_STATIC (decl));
6763 /* Store the expression if valid; else report error. */
6765 if (!in_system_header_at (input_location)
6766 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6767 warning (OPT_Wtraditional, "traditional C rejects automatic "
6768 "aggregate initialization");
6770 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6771 DECL_INITIAL (decl) = value;
6773 /* ANSI wants warnings about out-of-range constant initializers. */
6774 STRIP_TYPE_NOPS (value);
6775 if (TREE_STATIC (decl))
6776 constant_expression_warning (value);
6778 /* Check if we need to set array size from compound literal size. */
6779 if (TREE_CODE (type) == ARRAY_TYPE
6780 && TYPE_DOMAIN (type) == 0
6781 && value != error_mark_node)
6783 tree inside_init = init;
6785 STRIP_TYPE_NOPS (inside_init);
6786 inside_init = fold (inside_init);
6788 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6790 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6792 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6794 /* For int foo[] = (int [3]){1}; we need to set array size
6795 now since later on array initializer will be just the
6796 brace enclosed list of the compound literal. */
6797 tree etype = strip_array_types (TREE_TYPE (decl));
6798 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6799 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6800 layout_type (type);
6801 layout_decl (cldecl, 0);
6802 TREE_TYPE (decl)
6803 = c_build_qualified_type (type, TYPE_QUALS (etype));
6809 /* Methods for storing and printing names for error messages. */
6811 /* Implement a spelling stack that allows components of a name to be pushed
6812 and popped. Each element on the stack is this structure. */
6814 struct spelling
6816 int kind;
6817 union
6819 unsigned HOST_WIDE_INT i;
6820 const char *s;
6821 } u;
6824 #define SPELLING_STRING 1
6825 #define SPELLING_MEMBER 2
6826 #define SPELLING_BOUNDS 3
6828 static struct spelling *spelling; /* Next stack element (unused). */
6829 static struct spelling *spelling_base; /* Spelling stack base. */
6830 static int spelling_size; /* Size of the spelling stack. */
6832 /* Macros to save and restore the spelling stack around push_... functions.
6833 Alternative to SAVE_SPELLING_STACK. */
6835 #define SPELLING_DEPTH() (spelling - spelling_base)
6836 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6838 /* Push an element on the spelling stack with type KIND and assign VALUE
6839 to MEMBER. */
6841 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6843 int depth = SPELLING_DEPTH (); \
6845 if (depth >= spelling_size) \
6847 spelling_size += 10; \
6848 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6849 spelling_size); \
6850 RESTORE_SPELLING_DEPTH (depth); \
6853 spelling->kind = (KIND); \
6854 spelling->MEMBER = (VALUE); \
6855 spelling++; \
6858 /* Push STRING on the stack. Printed literally. */
6860 static void
6861 push_string (const char *string)
6863 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6866 /* Push a member name on the stack. Printed as '.' STRING. */
6868 static void
6869 push_member_name (tree decl)
6871 const char *const string
6872 = (DECL_NAME (decl)
6873 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6874 : _("<anonymous>"));
6875 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6878 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6880 static void
6881 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6883 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6886 /* Compute the maximum size in bytes of the printed spelling. */
6888 static int
6889 spelling_length (void)
6891 int size = 0;
6892 struct spelling *p;
6894 for (p = spelling_base; p < spelling; p++)
6896 if (p->kind == SPELLING_BOUNDS)
6897 size += 25;
6898 else
6899 size += strlen (p->u.s) + 1;
6902 return size;
6905 /* Print the spelling to BUFFER and return it. */
6907 static char *
6908 print_spelling (char *buffer)
6910 char *d = buffer;
6911 struct spelling *p;
6913 for (p = spelling_base; p < spelling; p++)
6914 if (p->kind == SPELLING_BOUNDS)
6916 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6917 d += strlen (d);
6919 else
6921 const char *s;
6922 if (p->kind == SPELLING_MEMBER)
6923 *d++ = '.';
6924 for (s = p->u.s; (*d = *s++); d++)
6927 *d++ = '\0';
6928 return buffer;
6931 /* Digest the parser output INIT as an initializer for type TYPE.
6932 Return a C expression of type TYPE to represent the initial value.
6934 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6936 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6938 If INIT is a string constant, STRICT_STRING is true if it is
6939 unparenthesized or we should not warn here for it being parenthesized.
6940 For other types of INIT, STRICT_STRING is not used.
6942 INIT_LOC is the location of the INIT.
6944 REQUIRE_CONSTANT requests an error if non-constant initializers or
6945 elements are seen. */
6947 static tree
6948 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6949 bool null_pointer_constant, bool strict_string,
6950 int require_constant)
6952 enum tree_code code = TREE_CODE (type);
6953 tree inside_init = init;
6954 tree semantic_type = NULL_TREE;
6955 bool maybe_const = true;
6957 if (type == error_mark_node
6958 || !init
6959 || error_operand_p (init))
6960 return error_mark_node;
6962 STRIP_TYPE_NOPS (inside_init);
6964 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6966 semantic_type = TREE_TYPE (inside_init);
6967 inside_init = TREE_OPERAND (inside_init, 0);
6969 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6970 inside_init = decl_constant_value_for_optimization (inside_init);
6972 /* Initialization of an array of chars from a string constant
6973 optionally enclosed in braces. */
6975 if (code == ARRAY_TYPE && inside_init
6976 && TREE_CODE (inside_init) == STRING_CST)
6978 tree typ1
6979 = (TYPE_ATOMIC (TREE_TYPE (type))
6980 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6981 TYPE_QUAL_ATOMIC)
6982 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6983 /* Note that an array could be both an array of character type
6984 and an array of wchar_t if wchar_t is signed char or unsigned
6985 char. */
6986 bool char_array = (typ1 == char_type_node
6987 || typ1 == signed_char_type_node
6988 || typ1 == unsigned_char_type_node);
6989 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6990 bool char16_array = !!comptypes (typ1, char16_type_node);
6991 bool char32_array = !!comptypes (typ1, char32_type_node);
6993 if (char_array || wchar_array || char16_array || char32_array)
6995 struct c_expr expr;
6996 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6997 expr.value = inside_init;
6998 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6999 expr.original_type = NULL;
7000 maybe_warn_string_init (init_loc, type, expr);
7002 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7003 pedwarn_init (init_loc, OPT_Wpedantic,
7004 "initialization of a flexible array member");
7006 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7007 TYPE_MAIN_VARIANT (type)))
7008 return inside_init;
7010 if (char_array)
7012 if (typ2 != char_type_node)
7014 error_init (init_loc, "char-array initialized from wide "
7015 "string");
7016 return error_mark_node;
7019 else
7021 if (typ2 == char_type_node)
7023 error_init (init_loc, "wide character array initialized "
7024 "from non-wide string");
7025 return error_mark_node;
7027 else if (!comptypes(typ1, typ2))
7029 error_init (init_loc, "wide character array initialized "
7030 "from incompatible wide string");
7031 return error_mark_node;
7035 TREE_TYPE (inside_init) = type;
7036 if (TYPE_DOMAIN (type) != 0
7037 && TYPE_SIZE (type) != 0
7038 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7040 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7042 /* Subtract the size of a single (possibly wide) character
7043 because it's ok to ignore the terminating null char
7044 that is counted in the length of the constant. */
7045 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7046 (len
7047 - (TYPE_PRECISION (typ1)
7048 / BITS_PER_UNIT))))
7049 pedwarn_init (init_loc, 0,
7050 ("initializer-string for array of chars "
7051 "is too long"));
7052 else if (warn_cxx_compat
7053 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7054 warning_at (init_loc, OPT_Wc___compat,
7055 ("initializer-string for array chars "
7056 "is too long for C++"));
7059 return inside_init;
7061 else if (INTEGRAL_TYPE_P (typ1))
7063 error_init (init_loc, "array of inappropriate type initialized "
7064 "from string constant");
7065 return error_mark_node;
7069 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7070 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7071 below and handle as a constructor. */
7072 if (code == VECTOR_TYPE
7073 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7074 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7075 && TREE_CONSTANT (inside_init))
7077 if (TREE_CODE (inside_init) == VECTOR_CST
7078 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7079 TYPE_MAIN_VARIANT (type)))
7080 return inside_init;
7082 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7084 unsigned HOST_WIDE_INT ix;
7085 tree value;
7086 bool constant_p = true;
7088 /* Iterate through elements and check if all constructor
7089 elements are *_CSTs. */
7090 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7091 if (!CONSTANT_CLASS_P (value))
7093 constant_p = false;
7094 break;
7097 if (constant_p)
7098 return build_vector_from_ctor (type,
7099 CONSTRUCTOR_ELTS (inside_init));
7103 if (warn_sequence_point)
7104 verify_sequence_points (inside_init);
7106 /* Any type can be initialized
7107 from an expression of the same type, optionally with braces. */
7109 if (inside_init && TREE_TYPE (inside_init) != 0
7110 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7111 TYPE_MAIN_VARIANT (type))
7112 || (code == ARRAY_TYPE
7113 && comptypes (TREE_TYPE (inside_init), type))
7114 || (code == VECTOR_TYPE
7115 && comptypes (TREE_TYPE (inside_init), type))
7116 || (code == POINTER_TYPE
7117 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7118 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7119 TREE_TYPE (type)))))
7121 if (code == POINTER_TYPE)
7123 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7125 if (TREE_CODE (inside_init) == STRING_CST
7126 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7127 inside_init = array_to_pointer_conversion
7128 (init_loc, inside_init);
7129 else
7131 error_init (init_loc, "invalid use of non-lvalue array");
7132 return error_mark_node;
7137 if (code == VECTOR_TYPE)
7138 /* Although the types are compatible, we may require a
7139 conversion. */
7140 inside_init = convert (type, inside_init);
7142 if (require_constant
7143 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7145 /* As an extension, allow initializing objects with static storage
7146 duration with compound literals (which are then treated just as
7147 the brace enclosed list they contain). Also allow this for
7148 vectors, as we can only assign them with compound literals. */
7149 if (flag_isoc99 && code != VECTOR_TYPE)
7150 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7151 "is not constant");
7152 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7153 inside_init = DECL_INITIAL (decl);
7156 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7157 && TREE_CODE (inside_init) != CONSTRUCTOR)
7159 error_init (init_loc, "array initialized from non-constant array "
7160 "expression");
7161 return error_mark_node;
7164 /* Compound expressions can only occur here if -Wpedantic or
7165 -pedantic-errors is specified. In the later case, we always want
7166 an error. In the former case, we simply want a warning. */
7167 if (require_constant && pedantic
7168 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7170 inside_init
7171 = valid_compound_expr_initializer (inside_init,
7172 TREE_TYPE (inside_init));
7173 if (inside_init == error_mark_node)
7174 error_init (init_loc, "initializer element is not constant");
7175 else
7176 pedwarn_init (init_loc, OPT_Wpedantic,
7177 "initializer element is not constant");
7178 if (flag_pedantic_errors)
7179 inside_init = error_mark_node;
7181 else if (require_constant
7182 && !initializer_constant_valid_p (inside_init,
7183 TREE_TYPE (inside_init)))
7185 error_init (init_loc, "initializer element is not constant");
7186 inside_init = error_mark_node;
7188 else if (require_constant && !maybe_const)
7189 pedwarn_init (init_loc, OPT_Wpedantic,
7190 "initializer element is not a constant expression");
7192 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7193 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7194 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7195 type, inside_init, origtype,
7196 ic_init, null_pointer_constant,
7197 NULL_TREE, NULL_TREE, 0);
7198 return inside_init;
7201 /* Handle scalar types, including conversions. */
7203 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7204 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7205 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7207 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7208 && (TREE_CODE (init) == STRING_CST
7209 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7210 inside_init = init = array_to_pointer_conversion (init_loc, init);
7211 if (semantic_type)
7212 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7213 inside_init);
7214 inside_init
7215 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7216 inside_init, origtype, ic_init,
7217 null_pointer_constant, NULL_TREE, NULL_TREE,
7220 /* Check to see if we have already given an error message. */
7221 if (inside_init == error_mark_node)
7223 else if (require_constant && !TREE_CONSTANT (inside_init))
7225 error_init (init_loc, "initializer element is not constant");
7226 inside_init = error_mark_node;
7228 else if (require_constant
7229 && !initializer_constant_valid_p (inside_init,
7230 TREE_TYPE (inside_init)))
7232 error_init (init_loc, "initializer element is not computable at "
7233 "load time");
7234 inside_init = error_mark_node;
7236 else if (require_constant && !maybe_const)
7237 pedwarn_init (init_loc, OPT_Wpedantic,
7238 "initializer element is not a constant expression");
7240 return inside_init;
7243 /* Come here only for records and arrays. */
7245 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7247 error_init (init_loc, "variable-sized object may not be initialized");
7248 return error_mark_node;
7251 error_init (init_loc, "invalid initializer");
7252 return error_mark_node;
7255 /* Handle initializers that use braces. */
7257 /* Type of object we are accumulating a constructor for.
7258 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7259 static tree constructor_type;
7261 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7262 left to fill. */
7263 static tree constructor_fields;
7265 /* For an ARRAY_TYPE, this is the specified index
7266 at which to store the next element we get. */
7267 static tree constructor_index;
7269 /* For an ARRAY_TYPE, this is the maximum index. */
7270 static tree constructor_max_index;
7272 /* For a RECORD_TYPE, this is the first field not yet written out. */
7273 static tree constructor_unfilled_fields;
7275 /* For an ARRAY_TYPE, this is the index of the first element
7276 not yet written out. */
7277 static tree constructor_unfilled_index;
7279 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7280 This is so we can generate gaps between fields, when appropriate. */
7281 static tree constructor_bit_index;
7283 /* If we are saving up the elements rather than allocating them,
7284 this is the list of elements so far (in reverse order,
7285 most recent first). */
7286 static vec<constructor_elt, va_gc> *constructor_elements;
7288 /* 1 if constructor should be incrementally stored into a constructor chain,
7289 0 if all the elements should be kept in AVL tree. */
7290 static int constructor_incremental;
7292 /* 1 if so far this constructor's elements are all compile-time constants. */
7293 static int constructor_constant;
7295 /* 1 if so far this constructor's elements are all valid address constants. */
7296 static int constructor_simple;
7298 /* 1 if this constructor has an element that cannot be part of a
7299 constant expression. */
7300 static int constructor_nonconst;
7302 /* 1 if this constructor is erroneous so far. */
7303 static int constructor_erroneous;
7305 /* 1 if this constructor is the universal zero initializer { 0 }. */
7306 static int constructor_zeroinit;
7308 /* Structure for managing pending initializer elements, organized as an
7309 AVL tree. */
7311 struct init_node
7313 struct init_node *left, *right;
7314 struct init_node *parent;
7315 int balance;
7316 tree purpose;
7317 tree value;
7318 tree origtype;
7321 /* Tree of pending elements at this constructor level.
7322 These are elements encountered out of order
7323 which belong at places we haven't reached yet in actually
7324 writing the output.
7325 Will never hold tree nodes across GC runs. */
7326 static struct init_node *constructor_pending_elts;
7328 /* The SPELLING_DEPTH of this constructor. */
7329 static int constructor_depth;
7331 /* DECL node for which an initializer is being read.
7332 0 means we are reading a constructor expression
7333 such as (struct foo) {...}. */
7334 static tree constructor_decl;
7336 /* Nonzero if this is an initializer for a top-level decl. */
7337 static int constructor_top_level;
7339 /* Nonzero if there were any member designators in this initializer. */
7340 static int constructor_designated;
7342 /* Nesting depth of designator list. */
7343 static int designator_depth;
7345 /* Nonzero if there were diagnosed errors in this designator list. */
7346 static int designator_erroneous;
7349 /* This stack has a level for each implicit or explicit level of
7350 structuring in the initializer, including the outermost one. It
7351 saves the values of most of the variables above. */
7353 struct constructor_range_stack;
7355 struct constructor_stack
7357 struct constructor_stack *next;
7358 tree type;
7359 tree fields;
7360 tree index;
7361 tree max_index;
7362 tree unfilled_index;
7363 tree unfilled_fields;
7364 tree bit_index;
7365 vec<constructor_elt, va_gc> *elements;
7366 struct init_node *pending_elts;
7367 int offset;
7368 int depth;
7369 /* If value nonzero, this value should replace the entire
7370 constructor at this level. */
7371 struct c_expr replacement_value;
7372 struct constructor_range_stack *range_stack;
7373 char constant;
7374 char simple;
7375 char nonconst;
7376 char implicit;
7377 char erroneous;
7378 char outer;
7379 char incremental;
7380 char designated;
7381 int designator_depth;
7384 static struct constructor_stack *constructor_stack;
7386 /* This stack represents designators from some range designator up to
7387 the last designator in the list. */
7389 struct constructor_range_stack
7391 struct constructor_range_stack *next, *prev;
7392 struct constructor_stack *stack;
7393 tree range_start;
7394 tree index;
7395 tree range_end;
7396 tree fields;
7399 static struct constructor_range_stack *constructor_range_stack;
7401 /* This stack records separate initializers that are nested.
7402 Nested initializers can't happen in ANSI C, but GNU C allows them
7403 in cases like { ... (struct foo) { ... } ... }. */
7405 struct initializer_stack
7407 struct initializer_stack *next;
7408 tree decl;
7409 struct constructor_stack *constructor_stack;
7410 struct constructor_range_stack *constructor_range_stack;
7411 vec<constructor_elt, va_gc> *elements;
7412 struct spelling *spelling;
7413 struct spelling *spelling_base;
7414 int spelling_size;
7415 char top_level;
7416 char require_constant_value;
7417 char require_constant_elements;
7420 static struct initializer_stack *initializer_stack;
7422 /* Prepare to parse and output the initializer for variable DECL. */
7424 void
7425 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7427 const char *locus;
7428 struct initializer_stack *p = XNEW (struct initializer_stack);
7430 p->decl = constructor_decl;
7431 p->require_constant_value = require_constant_value;
7432 p->require_constant_elements = require_constant_elements;
7433 p->constructor_stack = constructor_stack;
7434 p->constructor_range_stack = constructor_range_stack;
7435 p->elements = constructor_elements;
7436 p->spelling = spelling;
7437 p->spelling_base = spelling_base;
7438 p->spelling_size = spelling_size;
7439 p->top_level = constructor_top_level;
7440 p->next = initializer_stack;
7441 initializer_stack = p;
7443 constructor_decl = decl;
7444 constructor_designated = 0;
7445 constructor_top_level = top_level;
7447 if (decl != 0 && decl != error_mark_node)
7449 require_constant_value = TREE_STATIC (decl);
7450 require_constant_elements
7451 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7452 /* For a scalar, you can always use any value to initialize,
7453 even within braces. */
7454 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7455 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7457 else
7459 require_constant_value = 0;
7460 require_constant_elements = 0;
7461 locus = _("(anonymous)");
7464 constructor_stack = 0;
7465 constructor_range_stack = 0;
7467 found_missing_braces = 0;
7469 spelling_base = 0;
7470 spelling_size = 0;
7471 RESTORE_SPELLING_DEPTH (0);
7473 if (locus)
7474 push_string (locus);
7477 void
7478 finish_init (void)
7480 struct initializer_stack *p = initializer_stack;
7482 /* Free the whole constructor stack of this initializer. */
7483 while (constructor_stack)
7485 struct constructor_stack *q = constructor_stack;
7486 constructor_stack = q->next;
7487 free (q);
7490 gcc_assert (!constructor_range_stack);
7492 /* Pop back to the data of the outer initializer (if any). */
7493 free (spelling_base);
7495 constructor_decl = p->decl;
7496 require_constant_value = p->require_constant_value;
7497 require_constant_elements = p->require_constant_elements;
7498 constructor_stack = p->constructor_stack;
7499 constructor_range_stack = p->constructor_range_stack;
7500 constructor_elements = p->elements;
7501 spelling = p->spelling;
7502 spelling_base = p->spelling_base;
7503 spelling_size = p->spelling_size;
7504 constructor_top_level = p->top_level;
7505 initializer_stack = p->next;
7506 free (p);
7509 /* Call here when we see the initializer is surrounded by braces.
7510 This is instead of a call to push_init_level;
7511 it is matched by a call to pop_init_level.
7513 TYPE is the type to initialize, for a constructor expression.
7514 For an initializer for a decl, TYPE is zero. */
7516 void
7517 really_start_incremental_init (tree type)
7519 struct constructor_stack *p = XNEW (struct constructor_stack);
7521 if (type == 0)
7522 type = TREE_TYPE (constructor_decl);
7524 if (VECTOR_TYPE_P (type)
7525 && TYPE_VECTOR_OPAQUE (type))
7526 error ("opaque vector types cannot be initialized");
7528 p->type = constructor_type;
7529 p->fields = constructor_fields;
7530 p->index = constructor_index;
7531 p->max_index = constructor_max_index;
7532 p->unfilled_index = constructor_unfilled_index;
7533 p->unfilled_fields = constructor_unfilled_fields;
7534 p->bit_index = constructor_bit_index;
7535 p->elements = constructor_elements;
7536 p->constant = constructor_constant;
7537 p->simple = constructor_simple;
7538 p->nonconst = constructor_nonconst;
7539 p->erroneous = constructor_erroneous;
7540 p->pending_elts = constructor_pending_elts;
7541 p->depth = constructor_depth;
7542 p->replacement_value.value = 0;
7543 p->replacement_value.original_code = ERROR_MARK;
7544 p->replacement_value.original_type = NULL;
7545 p->implicit = 0;
7546 p->range_stack = 0;
7547 p->outer = 0;
7548 p->incremental = constructor_incremental;
7549 p->designated = constructor_designated;
7550 p->designator_depth = designator_depth;
7551 p->next = 0;
7552 constructor_stack = p;
7554 constructor_constant = 1;
7555 constructor_simple = 1;
7556 constructor_nonconst = 0;
7557 constructor_depth = SPELLING_DEPTH ();
7558 constructor_elements = NULL;
7559 constructor_pending_elts = 0;
7560 constructor_type = type;
7561 constructor_incremental = 1;
7562 constructor_designated = 0;
7563 constructor_zeroinit = 1;
7564 designator_depth = 0;
7565 designator_erroneous = 0;
7567 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7569 constructor_fields = TYPE_FIELDS (constructor_type);
7570 /* Skip any nameless bit fields at the beginning. */
7571 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7572 && DECL_NAME (constructor_fields) == 0)
7573 constructor_fields = DECL_CHAIN (constructor_fields);
7575 constructor_unfilled_fields = constructor_fields;
7576 constructor_bit_index = bitsize_zero_node;
7578 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7580 if (TYPE_DOMAIN (constructor_type))
7582 constructor_max_index
7583 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7585 /* Detect non-empty initializations of zero-length arrays. */
7586 if (constructor_max_index == NULL_TREE
7587 && TYPE_SIZE (constructor_type))
7588 constructor_max_index = integer_minus_one_node;
7590 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7591 to initialize VLAs will cause a proper error; avoid tree
7592 checking errors as well by setting a safe value. */
7593 if (constructor_max_index
7594 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7595 constructor_max_index = integer_minus_one_node;
7597 constructor_index
7598 = convert (bitsizetype,
7599 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7601 else
7603 constructor_index = bitsize_zero_node;
7604 constructor_max_index = NULL_TREE;
7607 constructor_unfilled_index = constructor_index;
7609 else if (VECTOR_TYPE_P (constructor_type))
7611 /* Vectors are like simple fixed-size arrays. */
7612 constructor_max_index =
7613 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7614 constructor_index = bitsize_zero_node;
7615 constructor_unfilled_index = constructor_index;
7617 else
7619 /* Handle the case of int x = {5}; */
7620 constructor_fields = constructor_type;
7621 constructor_unfilled_fields = constructor_type;
7625 /* Called when we see an open brace for a nested initializer. Finish
7626 off any pending levels with implicit braces. */
7627 void
7628 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7630 while (constructor_stack->implicit)
7632 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7633 && constructor_fields == 0)
7634 process_init_element (input_location,
7635 pop_init_level (loc, 1, braced_init_obstack),
7636 true, braced_init_obstack);
7637 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7638 && constructor_max_index
7639 && tree_int_cst_lt (constructor_max_index,
7640 constructor_index))
7641 process_init_element (input_location,
7642 pop_init_level (loc, 1, braced_init_obstack),
7643 true, braced_init_obstack);
7644 else
7645 break;
7649 /* Push down into a subobject, for initialization.
7650 If this is for an explicit set of braces, IMPLICIT is 0.
7651 If it is because the next element belongs at a lower level,
7652 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7654 void
7655 push_init_level (location_t loc, int implicit,
7656 struct obstack *braced_init_obstack)
7658 struct constructor_stack *p;
7659 tree value = NULL_TREE;
7661 /* Unless this is an explicit brace, we need to preserve previous
7662 content if any. */
7663 if (implicit)
7665 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7666 value = find_init_member (constructor_fields, braced_init_obstack);
7667 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7668 value = find_init_member (constructor_index, braced_init_obstack);
7671 p = XNEW (struct constructor_stack);
7672 p->type = constructor_type;
7673 p->fields = constructor_fields;
7674 p->index = constructor_index;
7675 p->max_index = constructor_max_index;
7676 p->unfilled_index = constructor_unfilled_index;
7677 p->unfilled_fields = constructor_unfilled_fields;
7678 p->bit_index = constructor_bit_index;
7679 p->elements = constructor_elements;
7680 p->constant = constructor_constant;
7681 p->simple = constructor_simple;
7682 p->nonconst = constructor_nonconst;
7683 p->erroneous = constructor_erroneous;
7684 p->pending_elts = constructor_pending_elts;
7685 p->depth = constructor_depth;
7686 p->replacement_value.value = 0;
7687 p->replacement_value.original_code = ERROR_MARK;
7688 p->replacement_value.original_type = NULL;
7689 p->implicit = implicit;
7690 p->outer = 0;
7691 p->incremental = constructor_incremental;
7692 p->designated = constructor_designated;
7693 p->designator_depth = designator_depth;
7694 p->next = constructor_stack;
7695 p->range_stack = 0;
7696 constructor_stack = p;
7698 constructor_constant = 1;
7699 constructor_simple = 1;
7700 constructor_nonconst = 0;
7701 constructor_depth = SPELLING_DEPTH ();
7702 constructor_elements = NULL;
7703 constructor_incremental = 1;
7704 constructor_designated = 0;
7705 constructor_pending_elts = 0;
7706 if (!implicit)
7708 p->range_stack = constructor_range_stack;
7709 constructor_range_stack = 0;
7710 designator_depth = 0;
7711 designator_erroneous = 0;
7714 /* Don't die if an entire brace-pair level is superfluous
7715 in the containing level. */
7716 if (constructor_type == 0)
7718 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7720 /* Don't die if there are extra init elts at the end. */
7721 if (constructor_fields == 0)
7722 constructor_type = 0;
7723 else
7725 constructor_type = TREE_TYPE (constructor_fields);
7726 push_member_name (constructor_fields);
7727 constructor_depth++;
7729 /* If upper initializer is designated, then mark this as
7730 designated too to prevent bogus warnings. */
7731 constructor_designated = p->designated;
7733 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7735 constructor_type = TREE_TYPE (constructor_type);
7736 push_array_bounds (tree_to_uhwi (constructor_index));
7737 constructor_depth++;
7740 if (constructor_type == 0)
7742 error_init (loc, "extra brace group at end of initializer");
7743 constructor_fields = 0;
7744 constructor_unfilled_fields = 0;
7745 return;
7748 if (value && TREE_CODE (value) == CONSTRUCTOR)
7750 constructor_constant = TREE_CONSTANT (value);
7751 constructor_simple = TREE_STATIC (value);
7752 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7753 constructor_elements = CONSTRUCTOR_ELTS (value);
7754 if (!vec_safe_is_empty (constructor_elements)
7755 && (TREE_CODE (constructor_type) == RECORD_TYPE
7756 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7757 set_nonincremental_init (braced_init_obstack);
7760 if (implicit == 1)
7761 found_missing_braces = 1;
7763 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7765 constructor_fields = TYPE_FIELDS (constructor_type);
7766 /* Skip any nameless bit fields at the beginning. */
7767 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7768 && DECL_NAME (constructor_fields) == 0)
7769 constructor_fields = DECL_CHAIN (constructor_fields);
7771 constructor_unfilled_fields = constructor_fields;
7772 constructor_bit_index = bitsize_zero_node;
7774 else if (VECTOR_TYPE_P (constructor_type))
7776 /* Vectors are like simple fixed-size arrays. */
7777 constructor_max_index =
7778 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7779 constructor_index = bitsize_int (0);
7780 constructor_unfilled_index = constructor_index;
7782 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7784 if (TYPE_DOMAIN (constructor_type))
7786 constructor_max_index
7787 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7789 /* Detect non-empty initializations of zero-length arrays. */
7790 if (constructor_max_index == NULL_TREE
7791 && TYPE_SIZE (constructor_type))
7792 constructor_max_index = integer_minus_one_node;
7794 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7795 to initialize VLAs will cause a proper error; avoid tree
7796 checking errors as well by setting a safe value. */
7797 if (constructor_max_index
7798 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7799 constructor_max_index = integer_minus_one_node;
7801 constructor_index
7802 = convert (bitsizetype,
7803 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7805 else
7806 constructor_index = bitsize_zero_node;
7808 constructor_unfilled_index = constructor_index;
7809 if (value && TREE_CODE (value) == STRING_CST)
7811 /* We need to split the char/wchar array into individual
7812 characters, so that we don't have to special case it
7813 everywhere. */
7814 set_nonincremental_init_from_string (value, braced_init_obstack);
7817 else
7819 if (constructor_type != error_mark_node)
7820 warning_init (input_location, 0, "braces around scalar initializer");
7821 constructor_fields = constructor_type;
7822 constructor_unfilled_fields = constructor_type;
7826 /* At the end of an implicit or explicit brace level,
7827 finish up that level of constructor. If a single expression
7828 with redundant braces initialized that level, return the
7829 c_expr structure for that expression. Otherwise, the original_code
7830 element is set to ERROR_MARK.
7831 If we were outputting the elements as they are read, return 0 as the value
7832 from inner levels (process_init_element ignores that),
7833 but return error_mark_node as the value from the outermost level
7834 (that's what we want to put in DECL_INITIAL).
7835 Otherwise, return a CONSTRUCTOR expression as the value. */
7837 struct c_expr
7838 pop_init_level (location_t loc, int implicit,
7839 struct obstack *braced_init_obstack)
7841 struct constructor_stack *p;
7842 struct c_expr ret;
7843 ret.value = 0;
7844 ret.original_code = ERROR_MARK;
7845 ret.original_type = NULL;
7847 if (implicit == 0)
7849 /* When we come to an explicit close brace,
7850 pop any inner levels that didn't have explicit braces. */
7851 while (constructor_stack->implicit)
7852 process_init_element (input_location,
7853 pop_init_level (loc, 1, braced_init_obstack),
7854 true, braced_init_obstack);
7855 gcc_assert (!constructor_range_stack);
7858 /* Now output all pending elements. */
7859 constructor_incremental = 1;
7860 output_pending_init_elements (1, braced_init_obstack);
7862 p = constructor_stack;
7864 /* Error for initializing a flexible array member, or a zero-length
7865 array member in an inappropriate context. */
7866 if (constructor_type && constructor_fields
7867 && TREE_CODE (constructor_type) == ARRAY_TYPE
7868 && TYPE_DOMAIN (constructor_type)
7869 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7871 /* Silently discard empty initializations. The parser will
7872 already have pedwarned for empty brackets. */
7873 if (integer_zerop (constructor_unfilled_index))
7874 constructor_type = NULL_TREE;
7875 else
7877 gcc_assert (!TYPE_SIZE (constructor_type));
7879 if (constructor_depth > 2)
7880 error_init (loc, "initialization of flexible array member in a nested context");
7881 else
7882 pedwarn_init (loc, OPT_Wpedantic,
7883 "initialization of a flexible array member");
7885 /* We have already issued an error message for the existence
7886 of a flexible array member not at the end of the structure.
7887 Discard the initializer so that we do not die later. */
7888 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7889 constructor_type = NULL_TREE;
7893 switch (vec_safe_length (constructor_elements))
7895 case 0:
7896 /* Initialization with { } counts as zeroinit. */
7897 constructor_zeroinit = 1;
7898 break;
7899 case 1:
7900 /* This might be zeroinit as well. */
7901 if (integer_zerop ((*constructor_elements)[0].value))
7902 constructor_zeroinit = 1;
7903 break;
7904 default:
7905 /* If the constructor has more than one element, it can't be { 0 }. */
7906 constructor_zeroinit = 0;
7907 break;
7910 /* Warn when some structs are initialized with direct aggregation. */
7911 if (!implicit && found_missing_braces && warn_missing_braces
7912 && !constructor_zeroinit)
7913 warning_init (loc, OPT_Wmissing_braces,
7914 "missing braces around initializer");
7916 /* Warn when some struct elements are implicitly initialized to zero. */
7917 if (warn_missing_field_initializers
7918 && constructor_type
7919 && TREE_CODE (constructor_type) == RECORD_TYPE
7920 && constructor_unfilled_fields)
7922 /* Do not warn for flexible array members or zero-length arrays. */
7923 while (constructor_unfilled_fields
7924 && (!DECL_SIZE (constructor_unfilled_fields)
7925 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7926 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7928 if (constructor_unfilled_fields
7929 /* Do not warn if this level of the initializer uses member
7930 designators; it is likely to be deliberate. */
7931 && !constructor_designated
7932 /* Do not warn about initializing with { 0 } or with { }. */
7933 && !constructor_zeroinit)
7935 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7936 "missing initializer for field %qD of %qT",
7937 constructor_unfilled_fields,
7938 constructor_type))
7939 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7940 "%qD declared here", constructor_unfilled_fields);
7944 /* Pad out the end of the structure. */
7945 if (p->replacement_value.value)
7946 /* If this closes a superfluous brace pair,
7947 just pass out the element between them. */
7948 ret = p->replacement_value;
7949 else if (constructor_type == 0)
7951 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
7952 && TREE_CODE (constructor_type) != ARRAY_TYPE
7953 && !VECTOR_TYPE_P (constructor_type))
7955 /* A nonincremental scalar initializer--just return
7956 the element, after verifying there is just one. */
7957 if (vec_safe_is_empty (constructor_elements))
7959 if (!constructor_erroneous)
7960 error_init (loc, "empty scalar initializer");
7961 ret.value = error_mark_node;
7963 else if (vec_safe_length (constructor_elements) != 1)
7965 error_init (loc, "extra elements in scalar initializer");
7966 ret.value = (*constructor_elements)[0].value;
7968 else
7969 ret.value = (*constructor_elements)[0].value;
7971 else
7973 if (constructor_erroneous)
7974 ret.value = error_mark_node;
7975 else
7977 ret.value = build_constructor (constructor_type,
7978 constructor_elements);
7979 if (constructor_constant)
7980 TREE_CONSTANT (ret.value) = 1;
7981 if (constructor_constant && constructor_simple)
7982 TREE_STATIC (ret.value) = 1;
7983 if (constructor_nonconst)
7984 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7988 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7990 if (constructor_nonconst)
7991 ret.original_code = C_MAYBE_CONST_EXPR;
7992 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7993 ret.original_code = ERROR_MARK;
7996 constructor_type = p->type;
7997 constructor_fields = p->fields;
7998 constructor_index = p->index;
7999 constructor_max_index = p->max_index;
8000 constructor_unfilled_index = p->unfilled_index;
8001 constructor_unfilled_fields = p->unfilled_fields;
8002 constructor_bit_index = p->bit_index;
8003 constructor_elements = p->elements;
8004 constructor_constant = p->constant;
8005 constructor_simple = p->simple;
8006 constructor_nonconst = p->nonconst;
8007 constructor_erroneous = p->erroneous;
8008 constructor_incremental = p->incremental;
8009 constructor_designated = p->designated;
8010 designator_depth = p->designator_depth;
8011 constructor_pending_elts = p->pending_elts;
8012 constructor_depth = p->depth;
8013 if (!p->implicit)
8014 constructor_range_stack = p->range_stack;
8015 RESTORE_SPELLING_DEPTH (constructor_depth);
8017 constructor_stack = p->next;
8018 free (p);
8020 if (ret.value == 0 && constructor_stack == 0)
8021 ret.value = error_mark_node;
8022 return ret;
8025 /* Common handling for both array range and field name designators.
8026 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8028 static int
8029 set_designator (location_t loc, int array,
8030 struct obstack *braced_init_obstack)
8032 tree subtype;
8033 enum tree_code subcode;
8035 /* Don't die if an entire brace-pair level is superfluous
8036 in the containing level. */
8037 if (constructor_type == 0)
8038 return 1;
8040 /* If there were errors in this designator list already, bail out
8041 silently. */
8042 if (designator_erroneous)
8043 return 1;
8045 if (!designator_depth)
8047 gcc_assert (!constructor_range_stack);
8049 /* Designator list starts at the level of closest explicit
8050 braces. */
8051 while (constructor_stack->implicit)
8052 process_init_element (input_location,
8053 pop_init_level (loc, 1, braced_init_obstack),
8054 true, braced_init_obstack);
8055 constructor_designated = 1;
8056 return 0;
8059 switch (TREE_CODE (constructor_type))
8061 case RECORD_TYPE:
8062 case UNION_TYPE:
8063 subtype = TREE_TYPE (constructor_fields);
8064 if (subtype != error_mark_node)
8065 subtype = TYPE_MAIN_VARIANT (subtype);
8066 break;
8067 case ARRAY_TYPE:
8068 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8069 break;
8070 default:
8071 gcc_unreachable ();
8074 subcode = TREE_CODE (subtype);
8075 if (array && subcode != ARRAY_TYPE)
8077 error_init (loc, "array index in non-array initializer");
8078 return 1;
8080 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8082 error_init (loc, "field name not in record or union initializer");
8083 return 1;
8086 constructor_designated = 1;
8087 finish_implicit_inits (loc, braced_init_obstack);
8088 push_init_level (loc, 2, braced_init_obstack);
8089 return 0;
8092 /* If there are range designators in designator list, push a new designator
8093 to constructor_range_stack. RANGE_END is end of such stack range or
8094 NULL_TREE if there is no range designator at this level. */
8096 static void
8097 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8099 struct constructor_range_stack *p;
8101 p = (struct constructor_range_stack *)
8102 obstack_alloc (braced_init_obstack,
8103 sizeof (struct constructor_range_stack));
8104 p->prev = constructor_range_stack;
8105 p->next = 0;
8106 p->fields = constructor_fields;
8107 p->range_start = constructor_index;
8108 p->index = constructor_index;
8109 p->stack = constructor_stack;
8110 p->range_end = range_end;
8111 if (constructor_range_stack)
8112 constructor_range_stack->next = p;
8113 constructor_range_stack = p;
8116 /* Within an array initializer, specify the next index to be initialized.
8117 FIRST is that index. If LAST is nonzero, then initialize a range
8118 of indices, running from FIRST through LAST. */
8120 void
8121 set_init_index (location_t loc, tree first, tree last,
8122 struct obstack *braced_init_obstack)
8124 if (set_designator (loc, 1, braced_init_obstack))
8125 return;
8127 designator_erroneous = 1;
8129 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8130 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8132 error_init (loc, "array index in initializer not of integer type");
8133 return;
8136 if (TREE_CODE (first) != INTEGER_CST)
8138 first = c_fully_fold (first, false, NULL);
8139 if (TREE_CODE (first) == INTEGER_CST)
8140 pedwarn_init (loc, OPT_Wpedantic,
8141 "array index in initializer is not "
8142 "an integer constant expression");
8145 if (last && TREE_CODE (last) != INTEGER_CST)
8147 last = c_fully_fold (last, false, NULL);
8148 if (TREE_CODE (last) == INTEGER_CST)
8149 pedwarn_init (loc, OPT_Wpedantic,
8150 "array index in initializer is not "
8151 "an integer constant expression");
8154 if (TREE_CODE (first) != INTEGER_CST)
8155 error_init (loc, "nonconstant array index in initializer");
8156 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8157 error_init (loc, "nonconstant array index in initializer");
8158 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8159 error_init (loc, "array index in non-array initializer");
8160 else if (tree_int_cst_sgn (first) == -1)
8161 error_init (loc, "array index in initializer exceeds array bounds");
8162 else if (constructor_max_index
8163 && tree_int_cst_lt (constructor_max_index, first))
8164 error_init (loc, "array index in initializer exceeds array bounds");
8165 else
8167 constant_expression_warning (first);
8168 if (last)
8169 constant_expression_warning (last);
8170 constructor_index = convert (bitsizetype, first);
8171 if (tree_int_cst_lt (constructor_index, first))
8173 constructor_index = copy_node (constructor_index);
8174 TREE_OVERFLOW (constructor_index) = 1;
8177 if (last)
8179 if (tree_int_cst_equal (first, last))
8180 last = 0;
8181 else if (tree_int_cst_lt (last, first))
8183 error_init (loc, "empty index range in initializer");
8184 last = 0;
8186 else
8188 last = convert (bitsizetype, last);
8189 if (constructor_max_index != 0
8190 && tree_int_cst_lt (constructor_max_index, last))
8192 error_init (loc, "array index range in initializer exceeds "
8193 "array bounds");
8194 last = 0;
8199 designator_depth++;
8200 designator_erroneous = 0;
8201 if (constructor_range_stack || last)
8202 push_range_stack (last, braced_init_obstack);
8206 /* Within a struct initializer, specify the next field to be initialized. */
8208 void
8209 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8210 struct obstack *braced_init_obstack)
8212 tree field;
8214 if (set_designator (loc, 0, braced_init_obstack))
8215 return;
8217 designator_erroneous = 1;
8219 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8221 error_init (loc, "field name not in record or union initializer");
8222 return;
8225 field = lookup_field (constructor_type, fieldname);
8227 if (field == 0)
8229 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8230 if (guessed_id)
8232 gcc_rich_location rich_loc (fieldname_loc);
8233 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8234 error_at_rich_loc
8235 (&rich_loc,
8236 "%qT has no member named %qE; did you mean %qE?",
8237 constructor_type, fieldname, guessed_id);
8239 else
8240 error_at (fieldname_loc, "%qT has no member named %qE",
8241 constructor_type, fieldname);
8243 else
8246 constructor_fields = TREE_VALUE (field);
8247 designator_depth++;
8248 designator_erroneous = 0;
8249 if (constructor_range_stack)
8250 push_range_stack (NULL_TREE, braced_init_obstack);
8251 field = TREE_CHAIN (field);
8252 if (field)
8254 if (set_designator (loc, 0, braced_init_obstack))
8255 return;
8258 while (field != NULL_TREE);
8261 /* Add a new initializer to the tree of pending initializers. PURPOSE
8262 identifies the initializer, either array index or field in a structure.
8263 VALUE is the value of that index or field. If ORIGTYPE is not
8264 NULL_TREE, it is the original type of VALUE.
8266 IMPLICIT is true if value comes from pop_init_level (1),
8267 the new initializer has been merged with the existing one
8268 and thus no warnings should be emitted about overriding an
8269 existing initializer. */
8271 static void
8272 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8273 bool implicit, struct obstack *braced_init_obstack)
8275 struct init_node *p, **q, *r;
8277 q = &constructor_pending_elts;
8278 p = 0;
8280 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8282 while (*q != 0)
8284 p = *q;
8285 if (tree_int_cst_lt (purpose, p->purpose))
8286 q = &p->left;
8287 else if (tree_int_cst_lt (p->purpose, purpose))
8288 q = &p->right;
8289 else
8291 if (!implicit)
8293 if (TREE_SIDE_EFFECTS (p->value))
8294 warning_init (loc, OPT_Woverride_init_side_effects,
8295 "initialized field with side-effects "
8296 "overwritten");
8297 else if (warn_override_init)
8298 warning_init (loc, OPT_Woverride_init,
8299 "initialized field overwritten");
8301 p->value = value;
8302 p->origtype = origtype;
8303 return;
8307 else
8309 tree bitpos;
8311 bitpos = bit_position (purpose);
8312 while (*q != NULL)
8314 p = *q;
8315 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8316 q = &p->left;
8317 else if (p->purpose != purpose)
8318 q = &p->right;
8319 else
8321 if (!implicit)
8323 if (TREE_SIDE_EFFECTS (p->value))
8324 warning_init (loc, OPT_Woverride_init_side_effects,
8325 "initialized field with side-effects "
8326 "overwritten");
8327 else if (warn_override_init)
8328 warning_init (loc, OPT_Woverride_init,
8329 "initialized field overwritten");
8331 p->value = value;
8332 p->origtype = origtype;
8333 return;
8338 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8339 sizeof (struct init_node));
8340 r->purpose = purpose;
8341 r->value = value;
8342 r->origtype = origtype;
8344 *q = r;
8345 r->parent = p;
8346 r->left = 0;
8347 r->right = 0;
8348 r->balance = 0;
8350 while (p)
8352 struct init_node *s;
8354 if (r == p->left)
8356 if (p->balance == 0)
8357 p->balance = -1;
8358 else if (p->balance < 0)
8360 if (r->balance < 0)
8362 /* L rotation. */
8363 p->left = r->right;
8364 if (p->left)
8365 p->left->parent = p;
8366 r->right = p;
8368 p->balance = 0;
8369 r->balance = 0;
8371 s = p->parent;
8372 p->parent = r;
8373 r->parent = s;
8374 if (s)
8376 if (s->left == p)
8377 s->left = r;
8378 else
8379 s->right = r;
8381 else
8382 constructor_pending_elts = r;
8384 else
8386 /* LR rotation. */
8387 struct init_node *t = r->right;
8389 r->right = t->left;
8390 if (r->right)
8391 r->right->parent = r;
8392 t->left = r;
8394 p->left = t->right;
8395 if (p->left)
8396 p->left->parent = p;
8397 t->right = p;
8399 p->balance = t->balance < 0;
8400 r->balance = -(t->balance > 0);
8401 t->balance = 0;
8403 s = p->parent;
8404 p->parent = t;
8405 r->parent = t;
8406 t->parent = s;
8407 if (s)
8409 if (s->left == p)
8410 s->left = t;
8411 else
8412 s->right = t;
8414 else
8415 constructor_pending_elts = t;
8417 break;
8419 else
8421 /* p->balance == +1; growth of left side balances the node. */
8422 p->balance = 0;
8423 break;
8426 else /* r == p->right */
8428 if (p->balance == 0)
8429 /* Growth propagation from right side. */
8430 p->balance++;
8431 else if (p->balance > 0)
8433 if (r->balance > 0)
8435 /* R rotation. */
8436 p->right = r->left;
8437 if (p->right)
8438 p->right->parent = p;
8439 r->left = p;
8441 p->balance = 0;
8442 r->balance = 0;
8444 s = p->parent;
8445 p->parent = r;
8446 r->parent = s;
8447 if (s)
8449 if (s->left == p)
8450 s->left = r;
8451 else
8452 s->right = r;
8454 else
8455 constructor_pending_elts = r;
8457 else /* r->balance == -1 */
8459 /* RL rotation */
8460 struct init_node *t = r->left;
8462 r->left = t->right;
8463 if (r->left)
8464 r->left->parent = r;
8465 t->right = r;
8467 p->right = t->left;
8468 if (p->right)
8469 p->right->parent = p;
8470 t->left = p;
8472 r->balance = (t->balance < 0);
8473 p->balance = -(t->balance > 0);
8474 t->balance = 0;
8476 s = p->parent;
8477 p->parent = t;
8478 r->parent = t;
8479 t->parent = s;
8480 if (s)
8482 if (s->left == p)
8483 s->left = t;
8484 else
8485 s->right = t;
8487 else
8488 constructor_pending_elts = t;
8490 break;
8492 else
8494 /* p->balance == -1; growth of right side balances the node. */
8495 p->balance = 0;
8496 break;
8500 r = p;
8501 p = p->parent;
8505 /* Build AVL tree from a sorted chain. */
8507 static void
8508 set_nonincremental_init (struct obstack * braced_init_obstack)
8510 unsigned HOST_WIDE_INT ix;
8511 tree index, value;
8513 if (TREE_CODE (constructor_type) != RECORD_TYPE
8514 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8515 return;
8517 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8518 add_pending_init (input_location, index, value, NULL_TREE, true,
8519 braced_init_obstack);
8520 constructor_elements = NULL;
8521 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8523 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8524 /* Skip any nameless bit fields at the beginning. */
8525 while (constructor_unfilled_fields != 0
8526 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8527 && DECL_NAME (constructor_unfilled_fields) == 0)
8528 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8531 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8533 if (TYPE_DOMAIN (constructor_type))
8534 constructor_unfilled_index
8535 = convert (bitsizetype,
8536 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8537 else
8538 constructor_unfilled_index = bitsize_zero_node;
8540 constructor_incremental = 0;
8543 /* Build AVL tree from a string constant. */
8545 static void
8546 set_nonincremental_init_from_string (tree str,
8547 struct obstack * braced_init_obstack)
8549 tree value, purpose, type;
8550 HOST_WIDE_INT val[2];
8551 const char *p, *end;
8552 int byte, wchar_bytes, charwidth, bitpos;
8554 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8556 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8557 charwidth = TYPE_PRECISION (char_type_node);
8558 type = TREE_TYPE (constructor_type);
8559 p = TREE_STRING_POINTER (str);
8560 end = p + TREE_STRING_LENGTH (str);
8562 for (purpose = bitsize_zero_node;
8563 p < end
8564 && !(constructor_max_index
8565 && tree_int_cst_lt (constructor_max_index, purpose));
8566 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8568 if (wchar_bytes == 1)
8570 val[0] = (unsigned char) *p++;
8571 val[1] = 0;
8573 else
8575 val[1] = 0;
8576 val[0] = 0;
8577 for (byte = 0; byte < wchar_bytes; byte++)
8579 if (BYTES_BIG_ENDIAN)
8580 bitpos = (wchar_bytes - byte - 1) * charwidth;
8581 else
8582 bitpos = byte * charwidth;
8583 val[bitpos % HOST_BITS_PER_WIDE_INT]
8584 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8585 << (bitpos % HOST_BITS_PER_WIDE_INT);
8589 if (!TYPE_UNSIGNED (type))
8591 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8592 if (bitpos < HOST_BITS_PER_WIDE_INT)
8594 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8596 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8597 val[1] = -1;
8600 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8602 if (val[0] < 0)
8603 val[1] = -1;
8605 else if (val[1] & (((HOST_WIDE_INT) 1)
8606 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8607 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8610 value = wide_int_to_tree (type,
8611 wide_int::from_array (val, 2,
8612 HOST_BITS_PER_WIDE_INT * 2));
8613 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8614 braced_init_obstack);
8617 constructor_incremental = 0;
8620 /* Return value of FIELD in pending initializer or zero if the field was
8621 not initialized yet. */
8623 static tree
8624 find_init_member (tree field, struct obstack * braced_init_obstack)
8626 struct init_node *p;
8628 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8630 if (constructor_incremental
8631 && tree_int_cst_lt (field, constructor_unfilled_index))
8632 set_nonincremental_init (braced_init_obstack);
8634 p = constructor_pending_elts;
8635 while (p)
8637 if (tree_int_cst_lt (field, p->purpose))
8638 p = p->left;
8639 else if (tree_int_cst_lt (p->purpose, field))
8640 p = p->right;
8641 else
8642 return p->value;
8645 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8647 tree bitpos = bit_position (field);
8649 if (constructor_incremental
8650 && (!constructor_unfilled_fields
8651 || tree_int_cst_lt (bitpos,
8652 bit_position (constructor_unfilled_fields))))
8653 set_nonincremental_init (braced_init_obstack);
8655 p = constructor_pending_elts;
8656 while (p)
8658 if (field == p->purpose)
8659 return p->value;
8660 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8661 p = p->left;
8662 else
8663 p = p->right;
8666 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8668 if (!vec_safe_is_empty (constructor_elements)
8669 && (constructor_elements->last ().index == field))
8670 return constructor_elements->last ().value;
8672 return 0;
8675 /* "Output" the next constructor element.
8676 At top level, really output it to assembler code now.
8677 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8678 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8679 TYPE is the data type that the containing data type wants here.
8680 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8681 If VALUE is a string constant, STRICT_STRING is true if it is
8682 unparenthesized or we should not warn here for it being parenthesized.
8683 For other types of VALUE, STRICT_STRING is not used.
8685 PENDING if non-nil means output pending elements that belong
8686 right after this element. (PENDING is normally 1;
8687 it is 0 while outputting pending elements, to avoid recursion.)
8689 IMPLICIT is true if value comes from pop_init_level (1),
8690 the new initializer has been merged with the existing one
8691 and thus no warnings should be emitted about overriding an
8692 existing initializer. */
8694 static void
8695 output_init_element (location_t loc, tree value, tree origtype,
8696 bool strict_string, tree type, tree field, int pending,
8697 bool implicit, struct obstack * braced_init_obstack)
8699 tree semantic_type = NULL_TREE;
8700 bool maybe_const = true;
8701 bool npc;
8703 if (type == error_mark_node || value == error_mark_node)
8705 constructor_erroneous = 1;
8706 return;
8708 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8709 && (TREE_CODE (value) == STRING_CST
8710 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8711 && !(TREE_CODE (value) == STRING_CST
8712 && TREE_CODE (type) == ARRAY_TYPE
8713 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8714 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8715 TYPE_MAIN_VARIANT (type)))
8716 value = array_to_pointer_conversion (input_location, value);
8718 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8719 && require_constant_value && pending)
8721 /* As an extension, allow initializing objects with static storage
8722 duration with compound literals (which are then treated just as
8723 the brace enclosed list they contain). */
8724 if (flag_isoc99)
8725 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8726 "constant");
8727 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8728 value = DECL_INITIAL (decl);
8731 npc = null_pointer_constant_p (value);
8732 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8734 semantic_type = TREE_TYPE (value);
8735 value = TREE_OPERAND (value, 0);
8737 value = c_fully_fold (value, require_constant_value, &maybe_const);
8739 if (value == error_mark_node)
8740 constructor_erroneous = 1;
8741 else if (!TREE_CONSTANT (value))
8742 constructor_constant = 0;
8743 else if (!initializer_constant_valid_p (value,
8744 TREE_TYPE (value),
8745 AGGREGATE_TYPE_P (constructor_type)
8746 && TYPE_REVERSE_STORAGE_ORDER
8747 (constructor_type))
8748 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8749 && DECL_C_BIT_FIELD (field)
8750 && TREE_CODE (value) != INTEGER_CST))
8751 constructor_simple = 0;
8752 if (!maybe_const)
8753 constructor_nonconst = 1;
8755 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8757 if (require_constant_value)
8759 error_init (loc, "initializer element is not constant");
8760 value = error_mark_node;
8762 else if (require_constant_elements)
8763 pedwarn (loc, OPT_Wpedantic,
8764 "initializer element is not computable at load time");
8766 else if (!maybe_const
8767 && (require_constant_value || require_constant_elements))
8768 pedwarn_init (loc, OPT_Wpedantic,
8769 "initializer element is not a constant expression");
8771 /* Issue -Wc++-compat warnings about initializing a bitfield with
8772 enum type. */
8773 if (warn_cxx_compat
8774 && field != NULL_TREE
8775 && TREE_CODE (field) == FIELD_DECL
8776 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8777 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8778 != TYPE_MAIN_VARIANT (type))
8779 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8781 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8782 if (checktype != error_mark_node
8783 && (TYPE_MAIN_VARIANT (checktype)
8784 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8785 warning_init (loc, OPT_Wc___compat,
8786 "enum conversion in initialization is invalid in C++");
8789 /* If this field is empty (and not at the end of structure),
8790 don't do anything other than checking the initializer. */
8791 if (field
8792 && (TREE_TYPE (field) == error_mark_node
8793 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8794 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8795 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8796 || DECL_CHAIN (field)))))
8797 return;
8799 if (semantic_type)
8800 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8801 value = digest_init (loc, type, value, origtype, npc, strict_string,
8802 require_constant_value);
8803 if (value == error_mark_node)
8805 constructor_erroneous = 1;
8806 return;
8808 if (require_constant_value || require_constant_elements)
8809 constant_expression_warning (value);
8811 /* If this element doesn't come next in sequence,
8812 put it on constructor_pending_elts. */
8813 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8814 && (!constructor_incremental
8815 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8817 if (constructor_incremental
8818 && tree_int_cst_lt (field, constructor_unfilled_index))
8819 set_nonincremental_init (braced_init_obstack);
8821 add_pending_init (loc, field, value, origtype, implicit,
8822 braced_init_obstack);
8823 return;
8825 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8826 && (!constructor_incremental
8827 || field != constructor_unfilled_fields))
8829 /* We do this for records but not for unions. In a union,
8830 no matter which field is specified, it can be initialized
8831 right away since it starts at the beginning of the union. */
8832 if (constructor_incremental)
8834 if (!constructor_unfilled_fields)
8835 set_nonincremental_init (braced_init_obstack);
8836 else
8838 tree bitpos, unfillpos;
8840 bitpos = bit_position (field);
8841 unfillpos = bit_position (constructor_unfilled_fields);
8843 if (tree_int_cst_lt (bitpos, unfillpos))
8844 set_nonincremental_init (braced_init_obstack);
8848 add_pending_init (loc, field, value, origtype, implicit,
8849 braced_init_obstack);
8850 return;
8852 else if (TREE_CODE (constructor_type) == UNION_TYPE
8853 && !vec_safe_is_empty (constructor_elements))
8855 if (!implicit)
8857 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8858 warning_init (loc, OPT_Woverride_init_side_effects,
8859 "initialized field with side-effects overwritten");
8860 else if (warn_override_init)
8861 warning_init (loc, OPT_Woverride_init,
8862 "initialized field overwritten");
8865 /* We can have just one union field set. */
8866 constructor_elements = NULL;
8869 /* Otherwise, output this element either to
8870 constructor_elements or to the assembler file. */
8872 constructor_elt celt = {field, value};
8873 vec_safe_push (constructor_elements, celt);
8875 /* Advance the variable that indicates sequential elements output. */
8876 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8877 constructor_unfilled_index
8878 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8879 bitsize_one_node);
8880 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8882 constructor_unfilled_fields
8883 = DECL_CHAIN (constructor_unfilled_fields);
8885 /* Skip any nameless bit fields. */
8886 while (constructor_unfilled_fields != 0
8887 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8888 && DECL_NAME (constructor_unfilled_fields) == 0)
8889 constructor_unfilled_fields =
8890 DECL_CHAIN (constructor_unfilled_fields);
8892 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8893 constructor_unfilled_fields = 0;
8895 /* Now output any pending elements which have become next. */
8896 if (pending)
8897 output_pending_init_elements (0, braced_init_obstack);
8900 /* Output any pending elements which have become next.
8901 As we output elements, constructor_unfilled_{fields,index}
8902 advances, which may cause other elements to become next;
8903 if so, they too are output.
8905 If ALL is 0, we return when there are
8906 no more pending elements to output now.
8908 If ALL is 1, we output space as necessary so that
8909 we can output all the pending elements. */
8910 static void
8911 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8913 struct init_node *elt = constructor_pending_elts;
8914 tree next;
8916 retry:
8918 /* Look through the whole pending tree.
8919 If we find an element that should be output now,
8920 output it. Otherwise, set NEXT to the element
8921 that comes first among those still pending. */
8923 next = 0;
8924 while (elt)
8926 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8928 if (tree_int_cst_equal (elt->purpose,
8929 constructor_unfilled_index))
8930 output_init_element (input_location, elt->value, elt->origtype,
8931 true, TREE_TYPE (constructor_type),
8932 constructor_unfilled_index, 0, false,
8933 braced_init_obstack);
8934 else if (tree_int_cst_lt (constructor_unfilled_index,
8935 elt->purpose))
8937 /* Advance to the next smaller node. */
8938 if (elt->left)
8939 elt = elt->left;
8940 else
8942 /* We have reached the smallest node bigger than the
8943 current unfilled index. Fill the space first. */
8944 next = elt->purpose;
8945 break;
8948 else
8950 /* Advance to the next bigger node. */
8951 if (elt->right)
8952 elt = elt->right;
8953 else
8955 /* We have reached the biggest node in a subtree. Find
8956 the parent of it, which is the next bigger node. */
8957 while (elt->parent && elt->parent->right == elt)
8958 elt = elt->parent;
8959 elt = elt->parent;
8960 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8961 elt->purpose))
8963 next = elt->purpose;
8964 break;
8969 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8971 tree ctor_unfilled_bitpos, elt_bitpos;
8973 /* If the current record is complete we are done. */
8974 if (constructor_unfilled_fields == 0)
8975 break;
8977 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8978 elt_bitpos = bit_position (elt->purpose);
8979 /* We can't compare fields here because there might be empty
8980 fields in between. */
8981 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8983 constructor_unfilled_fields = elt->purpose;
8984 output_init_element (input_location, elt->value, elt->origtype,
8985 true, TREE_TYPE (elt->purpose),
8986 elt->purpose, 0, false,
8987 braced_init_obstack);
8989 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
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 field. 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
9015 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9016 bit_position (elt->purpose))))
9018 next = elt->purpose;
9019 break;
9026 /* Ordinarily return, but not if we want to output all
9027 and there are elements left. */
9028 if (!(all && next != 0))
9029 return;
9031 /* If it's not incremental, just skip over the gap, so that after
9032 jumping to retry we will output the next successive element. */
9033 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9034 constructor_unfilled_fields = next;
9035 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9036 constructor_unfilled_index = next;
9038 /* ELT now points to the node in the pending tree with the next
9039 initializer to output. */
9040 goto retry;
9043 /* Add one non-braced element to the current constructor level.
9044 This adjusts the current position within the constructor's type.
9045 This may also start or terminate implicit levels
9046 to handle a partly-braced initializer.
9048 Once this has found the correct level for the new element,
9049 it calls output_init_element.
9051 IMPLICIT is true if value comes from pop_init_level (1),
9052 the new initializer has been merged with the existing one
9053 and thus no warnings should be emitted about overriding an
9054 existing initializer. */
9056 void
9057 process_init_element (location_t loc, struct c_expr value, bool implicit,
9058 struct obstack * braced_init_obstack)
9060 tree orig_value = value.value;
9061 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9062 bool strict_string = value.original_code == STRING_CST;
9063 bool was_designated = designator_depth != 0;
9065 designator_depth = 0;
9066 designator_erroneous = 0;
9068 if (!implicit && value.value && !integer_zerop (value.value))
9069 constructor_zeroinit = 0;
9071 /* Handle superfluous braces around string cst as in
9072 char x[] = {"foo"}; */
9073 if (string_flag
9074 && constructor_type
9075 && !was_designated
9076 && TREE_CODE (constructor_type) == ARRAY_TYPE
9077 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9078 && integer_zerop (constructor_unfilled_index))
9080 if (constructor_stack->replacement_value.value)
9081 error_init (loc, "excess elements in char array initializer");
9082 constructor_stack->replacement_value = value;
9083 return;
9086 if (constructor_stack->replacement_value.value != 0)
9088 error_init (loc, "excess elements in struct initializer");
9089 return;
9092 /* Ignore elements of a brace group if it is entirely superfluous
9093 and has already been diagnosed. */
9094 if (constructor_type == 0)
9095 return;
9097 if (!implicit && warn_designated_init && !was_designated
9098 && TREE_CODE (constructor_type) == RECORD_TYPE
9099 && lookup_attribute ("designated_init",
9100 TYPE_ATTRIBUTES (constructor_type)))
9101 warning_init (loc,
9102 OPT_Wdesignated_init,
9103 "positional initialization of field "
9104 "in %<struct%> declared with %<designated_init%> attribute");
9106 /* If we've exhausted any levels that didn't have braces,
9107 pop them now. */
9108 while (constructor_stack->implicit)
9110 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9111 && constructor_fields == 0)
9112 process_init_element (loc,
9113 pop_init_level (loc, 1, braced_init_obstack),
9114 true, braced_init_obstack);
9115 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9116 || VECTOR_TYPE_P (constructor_type))
9117 && constructor_max_index
9118 && tree_int_cst_lt (constructor_max_index,
9119 constructor_index))
9120 process_init_element (loc,
9121 pop_init_level (loc, 1, braced_init_obstack),
9122 true, braced_init_obstack);
9123 else
9124 break;
9127 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9128 if (constructor_range_stack)
9130 /* If value is a compound literal and we'll be just using its
9131 content, don't put it into a SAVE_EXPR. */
9132 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9133 || !require_constant_value)
9135 tree semantic_type = NULL_TREE;
9136 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9138 semantic_type = TREE_TYPE (value.value);
9139 value.value = TREE_OPERAND (value.value, 0);
9141 value.value = c_save_expr (value.value);
9142 if (semantic_type)
9143 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9144 value.value);
9148 while (1)
9150 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9152 tree fieldtype;
9153 enum tree_code fieldcode;
9155 if (constructor_fields == 0)
9157 pedwarn_init (loc, 0, "excess elements in struct initializer");
9158 break;
9161 fieldtype = TREE_TYPE (constructor_fields);
9162 if (fieldtype != error_mark_node)
9163 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9164 fieldcode = TREE_CODE (fieldtype);
9166 /* Error for non-static initialization of a flexible array member. */
9167 if (fieldcode == ARRAY_TYPE
9168 && !require_constant_value
9169 && TYPE_SIZE (fieldtype) == NULL_TREE
9170 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9172 error_init (loc, "non-static initialization of a flexible "
9173 "array member");
9174 break;
9177 /* Error for initialization of a flexible array member with
9178 a string constant if the structure is in an array. E.g.:
9179 struct S { int x; char y[]; };
9180 struct S s[] = { { 1, "foo" } };
9181 is invalid. */
9182 if (string_flag
9183 && fieldcode == ARRAY_TYPE
9184 && constructor_depth > 1
9185 && TYPE_SIZE (fieldtype) == NULL_TREE
9186 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9188 bool in_array_p = false;
9189 for (struct constructor_stack *p = constructor_stack;
9190 p && p->type; p = p->next)
9191 if (TREE_CODE (p->type) == ARRAY_TYPE)
9193 in_array_p = true;
9194 break;
9196 if (in_array_p)
9198 error_init (loc, "initialization of flexible array "
9199 "member in a nested context");
9200 break;
9204 /* Accept a string constant to initialize a subarray. */
9205 if (value.value != 0
9206 && fieldcode == ARRAY_TYPE
9207 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9208 && string_flag)
9209 value.value = orig_value;
9210 /* Otherwise, if we have come to a subaggregate,
9211 and we don't have an element of its type, push into it. */
9212 else if (value.value != 0
9213 && value.value != error_mark_node
9214 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9215 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9216 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9218 push_init_level (loc, 1, braced_init_obstack);
9219 continue;
9222 if (value.value)
9224 push_member_name (constructor_fields);
9225 output_init_element (loc, value.value, value.original_type,
9226 strict_string, fieldtype,
9227 constructor_fields, 1, implicit,
9228 braced_init_obstack);
9229 RESTORE_SPELLING_DEPTH (constructor_depth);
9231 else
9232 /* Do the bookkeeping for an element that was
9233 directly output as a constructor. */
9235 /* For a record, keep track of end position of last field. */
9236 if (DECL_SIZE (constructor_fields))
9237 constructor_bit_index
9238 = size_binop_loc (input_location, PLUS_EXPR,
9239 bit_position (constructor_fields),
9240 DECL_SIZE (constructor_fields));
9242 /* If the current field was the first one not yet written out,
9243 it isn't now, so update. */
9244 if (constructor_unfilled_fields == constructor_fields)
9246 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9247 /* Skip any nameless bit fields. */
9248 while (constructor_unfilled_fields != 0
9249 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9250 && DECL_NAME (constructor_unfilled_fields) == 0)
9251 constructor_unfilled_fields =
9252 DECL_CHAIN (constructor_unfilled_fields);
9256 constructor_fields = DECL_CHAIN (constructor_fields);
9257 /* Skip any nameless bit fields at the beginning. */
9258 while (constructor_fields != 0
9259 && DECL_C_BIT_FIELD (constructor_fields)
9260 && DECL_NAME (constructor_fields) == 0)
9261 constructor_fields = DECL_CHAIN (constructor_fields);
9263 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9265 tree fieldtype;
9266 enum tree_code fieldcode;
9268 if (constructor_fields == 0)
9270 pedwarn_init (loc, 0,
9271 "excess elements in union initializer");
9272 break;
9275 fieldtype = TREE_TYPE (constructor_fields);
9276 if (fieldtype != error_mark_node)
9277 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9278 fieldcode = TREE_CODE (fieldtype);
9280 /* Warn that traditional C rejects initialization of unions.
9281 We skip the warning if the value is zero. This is done
9282 under the assumption that the zero initializer in user
9283 code appears conditioned on e.g. __STDC__ to avoid
9284 "missing initializer" warnings and relies on default
9285 initialization to zero in the traditional C case.
9286 We also skip the warning if the initializer is designated,
9287 again on the assumption that this must be conditional on
9288 __STDC__ anyway (and we've already complained about the
9289 member-designator already). */
9290 if (!in_system_header_at (input_location) && !constructor_designated
9291 && !(value.value && (integer_zerop (value.value)
9292 || real_zerop (value.value))))
9293 warning (OPT_Wtraditional, "traditional C rejects initialization "
9294 "of unions");
9296 /* Accept a string constant to initialize a subarray. */
9297 if (value.value != 0
9298 && fieldcode == ARRAY_TYPE
9299 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9300 && string_flag)
9301 value.value = orig_value;
9302 /* Otherwise, if we have come to a subaggregate,
9303 and we don't have an element of its type, push into it. */
9304 else if (value.value != 0
9305 && value.value != error_mark_node
9306 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9307 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9308 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9310 push_init_level (loc, 1, braced_init_obstack);
9311 continue;
9314 if (value.value)
9316 push_member_name (constructor_fields);
9317 output_init_element (loc, value.value, value.original_type,
9318 strict_string, fieldtype,
9319 constructor_fields, 1, implicit,
9320 braced_init_obstack);
9321 RESTORE_SPELLING_DEPTH (constructor_depth);
9323 else
9324 /* Do the bookkeeping for an element that was
9325 directly output as a constructor. */
9327 constructor_bit_index = DECL_SIZE (constructor_fields);
9328 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9331 constructor_fields = 0;
9333 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9335 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9336 enum tree_code eltcode = TREE_CODE (elttype);
9338 /* Accept a string constant to initialize a subarray. */
9339 if (value.value != 0
9340 && eltcode == ARRAY_TYPE
9341 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9342 && string_flag)
9343 value.value = orig_value;
9344 /* Otherwise, if we have come to a subaggregate,
9345 and we don't have an element of its type, push into it. */
9346 else if (value.value != 0
9347 && value.value != error_mark_node
9348 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9349 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9350 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9352 push_init_level (loc, 1, braced_init_obstack);
9353 continue;
9356 if (constructor_max_index != 0
9357 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9358 || integer_all_onesp (constructor_max_index)))
9360 pedwarn_init (loc, 0,
9361 "excess elements in array initializer");
9362 break;
9365 /* Now output the actual element. */
9366 if (value.value)
9368 push_array_bounds (tree_to_uhwi (constructor_index));
9369 output_init_element (loc, value.value, value.original_type,
9370 strict_string, elttype,
9371 constructor_index, 1, implicit,
9372 braced_init_obstack);
9373 RESTORE_SPELLING_DEPTH (constructor_depth);
9376 constructor_index
9377 = size_binop_loc (input_location, PLUS_EXPR,
9378 constructor_index, bitsize_one_node);
9380 if (!value.value)
9381 /* If we are doing the bookkeeping for an element that was
9382 directly output as a constructor, we must update
9383 constructor_unfilled_index. */
9384 constructor_unfilled_index = constructor_index;
9386 else if (VECTOR_TYPE_P (constructor_type))
9388 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9390 /* Do a basic check of initializer size. Note that vectors
9391 always have a fixed size derived from their type. */
9392 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9394 pedwarn_init (loc, 0,
9395 "excess elements in vector initializer");
9396 break;
9399 /* Now output the actual element. */
9400 if (value.value)
9402 if (TREE_CODE (value.value) == VECTOR_CST)
9403 elttype = TYPE_MAIN_VARIANT (constructor_type);
9404 output_init_element (loc, value.value, value.original_type,
9405 strict_string, elttype,
9406 constructor_index, 1, implicit,
9407 braced_init_obstack);
9410 constructor_index
9411 = size_binop_loc (input_location,
9412 PLUS_EXPR, constructor_index, bitsize_one_node);
9414 if (!value.value)
9415 /* If we are doing the bookkeeping for an element that was
9416 directly output as a constructor, we must update
9417 constructor_unfilled_index. */
9418 constructor_unfilled_index = constructor_index;
9421 /* Handle the sole element allowed in a braced initializer
9422 for a scalar variable. */
9423 else if (constructor_type != error_mark_node
9424 && constructor_fields == 0)
9426 pedwarn_init (loc, 0,
9427 "excess elements in scalar initializer");
9428 break;
9430 else
9432 if (value.value)
9433 output_init_element (loc, value.value, value.original_type,
9434 strict_string, constructor_type,
9435 NULL_TREE, 1, implicit,
9436 braced_init_obstack);
9437 constructor_fields = 0;
9440 /* Handle range initializers either at this level or anywhere higher
9441 in the designator stack. */
9442 if (constructor_range_stack)
9444 struct constructor_range_stack *p, *range_stack;
9445 int finish = 0;
9447 range_stack = constructor_range_stack;
9448 constructor_range_stack = 0;
9449 while (constructor_stack != range_stack->stack)
9451 gcc_assert (constructor_stack->implicit);
9452 process_init_element (loc,
9453 pop_init_level (loc, 1,
9454 braced_init_obstack),
9455 true, braced_init_obstack);
9457 for (p = range_stack;
9458 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9459 p = p->prev)
9461 gcc_assert (constructor_stack->implicit);
9462 process_init_element (loc,
9463 pop_init_level (loc, 1,
9464 braced_init_obstack),
9465 true, braced_init_obstack);
9468 p->index = size_binop_loc (input_location,
9469 PLUS_EXPR, p->index, bitsize_one_node);
9470 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9471 finish = 1;
9473 while (1)
9475 constructor_index = p->index;
9476 constructor_fields = p->fields;
9477 if (finish && p->range_end && p->index == p->range_start)
9479 finish = 0;
9480 p->prev = 0;
9482 p = p->next;
9483 if (!p)
9484 break;
9485 finish_implicit_inits (loc, braced_init_obstack);
9486 push_init_level (loc, 2, braced_init_obstack);
9487 p->stack = constructor_stack;
9488 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9489 p->index = p->range_start;
9492 if (!finish)
9493 constructor_range_stack = range_stack;
9494 continue;
9497 break;
9500 constructor_range_stack = 0;
9503 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9504 (guaranteed to be 'volatile' or null) and ARGS (represented using
9505 an ASM_EXPR node). */
9506 tree
9507 build_asm_stmt (tree cv_qualifier, tree args)
9509 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9510 ASM_VOLATILE_P (args) = 1;
9511 return add_stmt (args);
9514 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9515 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9516 SIMPLE indicates whether there was anything at all after the
9517 string in the asm expression -- asm("blah") and asm("blah" : )
9518 are subtly different. We use a ASM_EXPR node to represent this. */
9519 tree
9520 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9521 tree clobbers, tree labels, bool simple)
9523 tree tail;
9524 tree args;
9525 int i;
9526 const char *constraint;
9527 const char **oconstraints;
9528 bool allows_mem, allows_reg, is_inout;
9529 int ninputs, noutputs;
9531 ninputs = list_length (inputs);
9532 noutputs = list_length (outputs);
9533 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9535 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9537 /* Remove output conversions that change the type but not the mode. */
9538 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9540 tree output = TREE_VALUE (tail);
9542 output = c_fully_fold (output, false, NULL);
9544 /* ??? Really, this should not be here. Users should be using a
9545 proper lvalue, dammit. But there's a long history of using casts
9546 in the output operands. In cases like longlong.h, this becomes a
9547 primitive form of typechecking -- if the cast can be removed, then
9548 the output operand had a type of the proper width; otherwise we'll
9549 get an error. Gross, but ... */
9550 STRIP_NOPS (output);
9552 if (!lvalue_or_else (loc, output, lv_asm))
9553 output = error_mark_node;
9555 if (output != error_mark_node
9556 && (TREE_READONLY (output)
9557 || TYPE_READONLY (TREE_TYPE (output))
9558 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9559 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9560 readonly_error (loc, output, lv_asm);
9562 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9563 oconstraints[i] = constraint;
9565 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9566 &allows_mem, &allows_reg, &is_inout))
9568 /* If the operand is going to end up in memory,
9569 mark it addressable. */
9570 if (!allows_reg && !c_mark_addressable (output))
9571 output = error_mark_node;
9572 if (!(!allows_reg && allows_mem)
9573 && output != error_mark_node
9574 && VOID_TYPE_P (TREE_TYPE (output)))
9576 error_at (loc, "invalid use of void expression");
9577 output = error_mark_node;
9580 else
9581 output = error_mark_node;
9583 TREE_VALUE (tail) = output;
9586 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9588 tree input;
9590 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9591 input = TREE_VALUE (tail);
9593 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9594 oconstraints, &allows_mem, &allows_reg))
9596 /* If the operand is going to end up in memory,
9597 mark it addressable. */
9598 if (!allows_reg && allows_mem)
9600 input = c_fully_fold (input, false, NULL);
9602 /* Strip the nops as we allow this case. FIXME, this really
9603 should be rejected or made deprecated. */
9604 STRIP_NOPS (input);
9605 if (!c_mark_addressable (input))
9606 input = error_mark_node;
9608 else
9610 struct c_expr expr;
9611 memset (&expr, 0, sizeof (expr));
9612 expr.value = input;
9613 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9614 input = c_fully_fold (expr.value, false, NULL);
9616 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9618 error_at (loc, "invalid use of void expression");
9619 input = error_mark_node;
9623 else
9624 input = error_mark_node;
9626 TREE_VALUE (tail) = input;
9629 /* ASMs with labels cannot have outputs. This should have been
9630 enforced by the parser. */
9631 gcc_assert (outputs == NULL || labels == NULL);
9633 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9635 /* asm statements without outputs, including simple ones, are treated
9636 as volatile. */
9637 ASM_INPUT_P (args) = simple;
9638 ASM_VOLATILE_P (args) = (noutputs == 0);
9640 return args;
9643 /* Generate a goto statement to LABEL. LOC is the location of the
9644 GOTO. */
9646 tree
9647 c_finish_goto_label (location_t loc, tree label)
9649 tree decl = lookup_label_for_goto (loc, label);
9650 if (!decl)
9651 return NULL_TREE;
9652 TREE_USED (decl) = 1;
9654 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9655 SET_EXPR_LOCATION (t, loc);
9656 return add_stmt (t);
9660 /* Generate a computed goto statement to EXPR. LOC is the location of
9661 the GOTO. */
9663 tree
9664 c_finish_goto_ptr (location_t loc, tree expr)
9666 tree t;
9667 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9668 expr = c_fully_fold (expr, false, NULL);
9669 expr = convert (ptr_type_node, expr);
9670 t = build1 (GOTO_EXPR, void_type_node, expr);
9671 SET_EXPR_LOCATION (t, loc);
9672 return add_stmt (t);
9675 /* Generate a C `return' statement. RETVAL is the expression for what
9676 to return, or a null pointer for `return;' with no value. LOC is
9677 the location of the return statement, or the location of the expression,
9678 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9679 is the original type of RETVAL. */
9681 tree
9682 c_finish_return (location_t loc, tree retval, tree origtype)
9684 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9685 bool no_warning = false;
9686 bool npc = false;
9687 size_t rank = 0;
9689 /* Use the expansion point to handle cases such as returning NULL
9690 in a function returning void. */
9691 source_location xloc = expansion_point_location_if_in_system_header (loc);
9693 if (TREE_THIS_VOLATILE (current_function_decl))
9694 warning_at (xloc, 0,
9695 "function declared %<noreturn%> has a %<return%> statement");
9697 if (flag_cilkplus && contains_array_notation_expr (retval))
9699 /* Array notations are allowed in a return statement if it is inside a
9700 built-in array notation reduction function. */
9701 if (!find_rank (loc, retval, retval, false, &rank))
9702 return error_mark_node;
9703 if (rank >= 1)
9705 error_at (loc, "array notation expression cannot be used as a "
9706 "return value");
9707 return error_mark_node;
9710 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9712 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9713 "allowed");
9714 return error_mark_node;
9716 if (retval)
9718 tree semantic_type = NULL_TREE;
9719 npc = null_pointer_constant_p (retval);
9720 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9722 semantic_type = TREE_TYPE (retval);
9723 retval = TREE_OPERAND (retval, 0);
9725 retval = c_fully_fold (retval, false, NULL);
9726 if (semantic_type)
9727 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9730 if (!retval)
9732 current_function_returns_null = 1;
9733 if ((warn_return_type || flag_isoc99)
9734 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9736 bool warned_here;
9737 if (flag_isoc99)
9738 warned_here = pedwarn
9739 (loc, 0,
9740 "%<return%> with no value, in function returning non-void");
9741 else
9742 warned_here = warning_at
9743 (loc, OPT_Wreturn_type,
9744 "%<return%> with no value, in function returning non-void");
9745 no_warning = true;
9746 if (warned_here)
9747 inform (DECL_SOURCE_LOCATION (current_function_decl),
9748 "declared here");
9751 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9753 current_function_returns_null = 1;
9754 bool warned_here;
9755 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9756 warned_here = pedwarn
9757 (xloc, 0,
9758 "%<return%> with a value, in function returning void");
9759 else
9760 warned_here = pedwarn
9761 (xloc, OPT_Wpedantic, "ISO C forbids "
9762 "%<return%> with expression, in function returning void");
9763 if (warned_here)
9764 inform (DECL_SOURCE_LOCATION (current_function_decl),
9765 "declared here");
9767 else
9769 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9770 retval, origtype, ic_return,
9771 npc, NULL_TREE, NULL_TREE, 0);
9772 tree res = DECL_RESULT (current_function_decl);
9773 tree inner;
9774 bool save;
9776 current_function_returns_value = 1;
9777 if (t == error_mark_node)
9778 return NULL_TREE;
9780 save = in_late_binary_op;
9781 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9782 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9783 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9784 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9785 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9786 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9787 in_late_binary_op = true;
9788 inner = t = convert (TREE_TYPE (res), t);
9789 in_late_binary_op = save;
9791 /* Strip any conversions, additions, and subtractions, and see if
9792 we are returning the address of a local variable. Warn if so. */
9793 while (1)
9795 switch (TREE_CODE (inner))
9797 CASE_CONVERT:
9798 case NON_LVALUE_EXPR:
9799 case PLUS_EXPR:
9800 case POINTER_PLUS_EXPR:
9801 inner = TREE_OPERAND (inner, 0);
9802 continue;
9804 case MINUS_EXPR:
9805 /* If the second operand of the MINUS_EXPR has a pointer
9806 type (or is converted from it), this may be valid, so
9807 don't give a warning. */
9809 tree op1 = TREE_OPERAND (inner, 1);
9811 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9812 && (CONVERT_EXPR_P (op1)
9813 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9814 op1 = TREE_OPERAND (op1, 0);
9816 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9817 break;
9819 inner = TREE_OPERAND (inner, 0);
9820 continue;
9823 case ADDR_EXPR:
9824 inner = TREE_OPERAND (inner, 0);
9826 while (REFERENCE_CLASS_P (inner)
9827 && !INDIRECT_REF_P (inner))
9828 inner = TREE_OPERAND (inner, 0);
9830 if (DECL_P (inner)
9831 && !DECL_EXTERNAL (inner)
9832 && !TREE_STATIC (inner)
9833 && DECL_CONTEXT (inner) == current_function_decl)
9835 if (TREE_CODE (inner) == LABEL_DECL)
9836 warning_at (loc, OPT_Wreturn_local_addr,
9837 "function returns address of label");
9838 else
9840 warning_at (loc, OPT_Wreturn_local_addr,
9841 "function returns address of local variable");
9842 tree zero = build_zero_cst (TREE_TYPE (res));
9843 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9846 break;
9848 default:
9849 break;
9852 break;
9855 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9856 SET_EXPR_LOCATION (retval, loc);
9858 if (warn_sequence_point)
9859 verify_sequence_points (retval);
9862 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9863 TREE_NO_WARNING (ret_stmt) |= no_warning;
9864 return add_stmt (ret_stmt);
9867 struct c_switch {
9868 /* The SWITCH_EXPR being built. */
9869 tree switch_expr;
9871 /* The original type of the testing expression, i.e. before the
9872 default conversion is applied. */
9873 tree orig_type;
9875 /* A splay-tree mapping the low element of a case range to the high
9876 element, or NULL_TREE if there is no high element. Used to
9877 determine whether or not a new case label duplicates an old case
9878 label. We need a tree, rather than simply a hash table, because
9879 of the GNU case range extension. */
9880 splay_tree cases;
9882 /* The bindings at the point of the switch. This is used for
9883 warnings crossing decls when branching to a case label. */
9884 struct c_spot_bindings *bindings;
9886 /* The next node on the stack. */
9887 struct c_switch *next;
9889 /* Remember whether the controlling expression had boolean type
9890 before integer promotions for the sake of -Wswitch-bool. */
9891 bool bool_cond_p;
9893 /* Remember whether there was a case value that is outside the
9894 range of the ORIG_TYPE. */
9895 bool outside_range_p;
9898 /* A stack of the currently active switch statements. The innermost
9899 switch statement is on the top of the stack. There is no need to
9900 mark the stack for garbage collection because it is only active
9901 during the processing of the body of a function, and we never
9902 collect at that point. */
9904 struct c_switch *c_switch_stack;
9906 /* Start a C switch statement, testing expression EXP. Return the new
9907 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9908 SWITCH_COND_LOC is the location of the switch's condition.
9909 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9911 tree
9912 c_start_case (location_t switch_loc,
9913 location_t switch_cond_loc,
9914 tree exp, bool explicit_cast_p)
9916 tree orig_type = error_mark_node;
9917 bool bool_cond_p = false;
9918 struct c_switch *cs;
9920 if (exp != error_mark_node)
9922 orig_type = TREE_TYPE (exp);
9924 if (!INTEGRAL_TYPE_P (orig_type))
9926 if (orig_type != error_mark_node)
9928 error_at (switch_cond_loc, "switch quantity not an integer");
9929 orig_type = error_mark_node;
9931 exp = integer_zero_node;
9933 else
9935 tree type = TYPE_MAIN_VARIANT (orig_type);
9936 tree e = exp;
9938 /* Warn if the condition has boolean value. */
9939 while (TREE_CODE (e) == COMPOUND_EXPR)
9940 e = TREE_OPERAND (e, 1);
9942 if ((TREE_CODE (type) == BOOLEAN_TYPE
9943 || truth_value_p (TREE_CODE (e)))
9944 /* Explicit cast to int suppresses this warning. */
9945 && !(TREE_CODE (type) == INTEGER_TYPE
9946 && explicit_cast_p))
9947 bool_cond_p = true;
9949 if (!in_system_header_at (input_location)
9950 && (type == long_integer_type_node
9951 || type == long_unsigned_type_node))
9952 warning_at (switch_cond_loc,
9953 OPT_Wtraditional, "%<long%> switch expression not "
9954 "converted to %<int%> in ISO C");
9956 exp = c_fully_fold (exp, false, NULL);
9957 exp = default_conversion (exp);
9959 if (warn_sequence_point)
9960 verify_sequence_points (exp);
9964 /* Add this new SWITCH_EXPR to the stack. */
9965 cs = XNEW (struct c_switch);
9966 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9967 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9968 cs->orig_type = orig_type;
9969 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9970 cs->bindings = c_get_switch_bindings ();
9971 cs->bool_cond_p = bool_cond_p;
9972 cs->outside_range_p = false;
9973 cs->next = c_switch_stack;
9974 c_switch_stack = cs;
9976 return add_stmt (cs->switch_expr);
9979 /* Process a case label at location LOC. */
9981 tree
9982 do_case (location_t loc, tree low_value, tree high_value)
9984 tree label = NULL_TREE;
9986 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9988 low_value = c_fully_fold (low_value, false, NULL);
9989 if (TREE_CODE (low_value) == INTEGER_CST)
9990 pedwarn (loc, OPT_Wpedantic,
9991 "case label is not an integer constant expression");
9994 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9996 high_value = c_fully_fold (high_value, false, NULL);
9997 if (TREE_CODE (high_value) == INTEGER_CST)
9998 pedwarn (input_location, OPT_Wpedantic,
9999 "case label is not an integer constant expression");
10002 if (c_switch_stack == NULL)
10004 if (low_value)
10005 error_at (loc, "case label not within a switch statement");
10006 else
10007 error_at (loc, "%<default%> label not within a switch statement");
10008 return NULL_TREE;
10011 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10012 EXPR_LOCATION (c_switch_stack->switch_expr),
10013 loc))
10014 return NULL_TREE;
10016 label = c_add_case_label (loc, c_switch_stack->cases,
10017 SWITCH_COND (c_switch_stack->switch_expr),
10018 c_switch_stack->orig_type,
10019 low_value, high_value,
10020 &c_switch_stack->outside_range_p);
10021 if (label == error_mark_node)
10022 label = NULL_TREE;
10023 return label;
10026 /* Finish the switch statement. TYPE is the original type of the
10027 controlling expression of the switch, or NULL_TREE. */
10029 void
10030 c_finish_case (tree body, tree type)
10032 struct c_switch *cs = c_switch_stack;
10033 location_t switch_location;
10035 SWITCH_BODY (cs->switch_expr) = body;
10037 /* Emit warnings as needed. */
10038 switch_location = EXPR_LOCATION (cs->switch_expr);
10039 c_do_switch_warnings (cs->cases, switch_location,
10040 type ? type : TREE_TYPE (cs->switch_expr),
10041 SWITCH_COND (cs->switch_expr),
10042 cs->bool_cond_p, cs->outside_range_p);
10044 /* Pop the stack. */
10045 c_switch_stack = cs->next;
10046 splay_tree_delete (cs->cases);
10047 c_release_switch_bindings (cs->bindings);
10048 XDELETE (cs);
10051 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10052 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10053 may be null. */
10055 void
10056 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10057 tree else_block)
10059 tree stmt;
10061 /* If the condition has array notations, then the rank of the then_block and
10062 else_block must be either 0 or be equal to the rank of the condition. If
10063 the condition does not have array notations then break them up as it is
10064 broken up in a normal expression. */
10065 if (flag_cilkplus && contains_array_notation_expr (cond))
10067 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10068 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10069 return;
10070 if (then_block
10071 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10072 return;
10073 if (else_block
10074 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10075 return;
10076 if (cond_rank != then_rank && then_rank != 0)
10078 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10079 " and the then-block");
10080 return;
10082 else if (cond_rank != else_rank && else_rank != 0)
10084 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10085 " and the else-block");
10086 return;
10090 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10091 SET_EXPR_LOCATION (stmt, if_locus);
10092 add_stmt (stmt);
10095 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10096 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10097 is false for DO loops. INCR is the FOR increment expression. BODY is
10098 the statement controlled by the loop. BLAB is the break label. CLAB is
10099 the continue label. Everything is allowed to be NULL. */
10101 void
10102 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10103 tree blab, tree clab, bool cond_is_first)
10105 tree entry = NULL, exit = NULL, t;
10107 /* In theory could forbid cilk spawn for loop increment expression,
10108 but it should work just fine. */
10110 /* If the condition is zero don't generate a loop construct. */
10111 if (cond && integer_zerop (cond))
10113 if (cond_is_first)
10115 t = build_and_jump (&blab);
10116 SET_EXPR_LOCATION (t, start_locus);
10117 add_stmt (t);
10120 else
10122 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10124 /* If we have an exit condition, then we build an IF with gotos either
10125 out of the loop, or to the top of it. If there's no exit condition,
10126 then we just build a jump back to the top. */
10127 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10129 if (cond && !integer_nonzerop (cond))
10131 /* Canonicalize the loop condition to the end. This means
10132 generating a branch to the loop condition. Reuse the
10133 continue label, if possible. */
10134 if (cond_is_first)
10136 if (incr || !clab)
10138 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10139 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10141 else
10142 t = build1 (GOTO_EXPR, void_type_node, clab);
10143 SET_EXPR_LOCATION (t, start_locus);
10144 add_stmt (t);
10147 t = build_and_jump (&blab);
10148 if (cond_is_first)
10149 exit = fold_build3_loc (start_locus,
10150 COND_EXPR, void_type_node, cond, exit, t);
10151 else
10152 exit = fold_build3_loc (input_location,
10153 COND_EXPR, void_type_node, cond, exit, t);
10155 else
10157 /* For the backward-goto's location of an unconditional loop
10158 use the beginning of the body, or, if there is none, the
10159 top of the loop. */
10160 location_t loc = EXPR_LOCATION (expr_first (body));
10161 if (loc == UNKNOWN_LOCATION)
10162 loc = start_locus;
10163 SET_EXPR_LOCATION (exit, loc);
10166 add_stmt (top);
10169 if (body)
10170 add_stmt (body);
10171 if (clab)
10172 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10173 if (incr)
10174 add_stmt (incr);
10175 if (entry)
10176 add_stmt (entry);
10177 if (exit)
10178 add_stmt (exit);
10179 if (blab)
10180 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10183 tree
10184 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10186 bool skip;
10187 tree label = *label_p;
10189 /* In switch statements break is sometimes stylistically used after
10190 a return statement. This can lead to spurious warnings about
10191 control reaching the end of a non-void function when it is
10192 inlined. Note that we are calling block_may_fallthru with
10193 language specific tree nodes; this works because
10194 block_may_fallthru returns true when given something it does not
10195 understand. */
10196 skip = !block_may_fallthru (cur_stmt_list);
10198 if (!label)
10200 if (!skip)
10201 *label_p = label = create_artificial_label (loc);
10203 else if (TREE_CODE (label) == LABEL_DECL)
10205 else switch (TREE_INT_CST_LOW (label))
10207 case 0:
10208 if (is_break)
10209 error_at (loc, "break statement not within loop or switch");
10210 else
10211 error_at (loc, "continue statement not within a loop");
10212 return NULL_TREE;
10214 case 1:
10215 gcc_assert (is_break);
10216 error_at (loc, "break statement used with OpenMP for loop");
10217 return NULL_TREE;
10219 case 2:
10220 if (is_break)
10221 error ("break statement within %<#pragma simd%> loop body");
10222 else
10223 error ("continue statement within %<#pragma simd%> loop body");
10224 return NULL_TREE;
10226 default:
10227 gcc_unreachable ();
10230 if (skip)
10231 return NULL_TREE;
10233 if (!is_break)
10234 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10236 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10239 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10241 static void
10242 emit_side_effect_warnings (location_t loc, tree expr)
10244 if (expr == error_mark_node)
10246 else if (!TREE_SIDE_EFFECTS (expr))
10248 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10249 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10251 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10253 tree r = expr;
10254 location_t cloc = loc;
10255 while (TREE_CODE (r) == COMPOUND_EXPR)
10257 if (EXPR_HAS_LOCATION (r))
10258 cloc = EXPR_LOCATION (r);
10259 r = TREE_OPERAND (r, 1);
10261 if (!TREE_SIDE_EFFECTS (r)
10262 && !VOID_TYPE_P (TREE_TYPE (r))
10263 && !CONVERT_EXPR_P (r)
10264 && !TREE_NO_WARNING (r)
10265 && !TREE_NO_WARNING (expr))
10266 warning_at (cloc, OPT_Wunused_value,
10267 "right-hand operand of comma expression has no effect");
10269 else
10270 warn_if_unused_value (expr, loc);
10273 /* Process an expression as if it were a complete statement. Emit
10274 diagnostics, but do not call ADD_STMT. LOC is the location of the
10275 statement. */
10277 tree
10278 c_process_expr_stmt (location_t loc, tree expr)
10280 tree exprv;
10282 if (!expr)
10283 return NULL_TREE;
10285 expr = c_fully_fold (expr, false, NULL);
10287 if (warn_sequence_point)
10288 verify_sequence_points (expr);
10290 if (TREE_TYPE (expr) != error_mark_node
10291 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10292 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10293 error_at (loc, "expression statement has incomplete type");
10295 /* If we're not processing a statement expression, warn about unused values.
10296 Warnings for statement expressions will be emitted later, once we figure
10297 out which is the result. */
10298 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10299 && warn_unused_value)
10300 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10302 exprv = expr;
10303 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10304 exprv = TREE_OPERAND (exprv, 1);
10305 while (CONVERT_EXPR_P (exprv))
10306 exprv = TREE_OPERAND (exprv, 0);
10307 if (DECL_P (exprv)
10308 || handled_component_p (exprv)
10309 || TREE_CODE (exprv) == ADDR_EXPR)
10310 mark_exp_read (exprv);
10312 /* If the expression is not of a type to which we cannot assign a line
10313 number, wrap the thing in a no-op NOP_EXPR. */
10314 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10316 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10317 SET_EXPR_LOCATION (expr, loc);
10320 return expr;
10323 /* Emit an expression as a statement. LOC is the location of the
10324 expression. */
10326 tree
10327 c_finish_expr_stmt (location_t loc, tree expr)
10329 if (expr)
10330 return add_stmt (c_process_expr_stmt (loc, expr));
10331 else
10332 return NULL;
10335 /* Do the opposite and emit a statement as an expression. To begin,
10336 create a new binding level and return it. */
10338 tree
10339 c_begin_stmt_expr (void)
10341 tree ret;
10343 /* We must force a BLOCK for this level so that, if it is not expanded
10344 later, there is a way to turn off the entire subtree of blocks that
10345 are contained in it. */
10346 keep_next_level ();
10347 ret = c_begin_compound_stmt (true);
10349 c_bindings_start_stmt_expr (c_switch_stack == NULL
10350 ? NULL
10351 : c_switch_stack->bindings);
10353 /* Mark the current statement list as belonging to a statement list. */
10354 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10356 return ret;
10359 /* LOC is the location of the compound statement to which this body
10360 belongs. */
10362 tree
10363 c_finish_stmt_expr (location_t loc, tree body)
10365 tree last, type, tmp, val;
10366 tree *last_p;
10368 body = c_end_compound_stmt (loc, body, true);
10370 c_bindings_end_stmt_expr (c_switch_stack == NULL
10371 ? NULL
10372 : c_switch_stack->bindings);
10374 /* Locate the last statement in BODY. See c_end_compound_stmt
10375 about always returning a BIND_EXPR. */
10376 last_p = &BIND_EXPR_BODY (body);
10377 last = BIND_EXPR_BODY (body);
10379 continue_searching:
10380 if (TREE_CODE (last) == STATEMENT_LIST)
10382 tree_stmt_iterator i;
10384 /* This can happen with degenerate cases like ({ }). No value. */
10385 if (!TREE_SIDE_EFFECTS (last))
10386 return body;
10388 /* If we're supposed to generate side effects warnings, process
10389 all of the statements except the last. */
10390 if (warn_unused_value)
10392 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10394 location_t tloc;
10395 tree t = tsi_stmt (i);
10397 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10398 emit_side_effect_warnings (tloc, t);
10401 else
10402 i = tsi_last (last);
10403 last_p = tsi_stmt_ptr (i);
10404 last = *last_p;
10407 /* If the end of the list is exception related, then the list was split
10408 by a call to push_cleanup. Continue searching. */
10409 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10410 || TREE_CODE (last) == TRY_CATCH_EXPR)
10412 last_p = &TREE_OPERAND (last, 0);
10413 last = *last_p;
10414 goto continue_searching;
10417 if (last == error_mark_node)
10418 return last;
10420 /* In the case that the BIND_EXPR is not necessary, return the
10421 expression out from inside it. */
10422 if (last == BIND_EXPR_BODY (body)
10423 && BIND_EXPR_VARS (body) == NULL)
10425 /* Even if this looks constant, do not allow it in a constant
10426 expression. */
10427 last = c_wrap_maybe_const (last, true);
10428 /* Do not warn if the return value of a statement expression is
10429 unused. */
10430 TREE_NO_WARNING (last) = 1;
10431 return last;
10434 /* Extract the type of said expression. */
10435 type = TREE_TYPE (last);
10437 /* If we're not returning a value at all, then the BIND_EXPR that
10438 we already have is a fine expression to return. */
10439 if (!type || VOID_TYPE_P (type))
10440 return body;
10442 /* Now that we've located the expression containing the value, it seems
10443 silly to make voidify_wrapper_expr repeat the process. Create a
10444 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10445 tmp = create_tmp_var_raw (type);
10447 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10448 tree_expr_nonnegative_p giving up immediately. */
10449 val = last;
10450 if (TREE_CODE (val) == NOP_EXPR
10451 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10452 val = TREE_OPERAND (val, 0);
10454 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10455 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10458 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10459 SET_EXPR_LOCATION (t, loc);
10460 return t;
10464 /* Begin and end compound statements. This is as simple as pushing
10465 and popping new statement lists from the tree. */
10467 tree
10468 c_begin_compound_stmt (bool do_scope)
10470 tree stmt = push_stmt_list ();
10471 if (do_scope)
10472 push_scope ();
10473 return stmt;
10476 /* End a compound statement. STMT is the statement. LOC is the
10477 location of the compound statement-- this is usually the location
10478 of the opening brace. */
10480 tree
10481 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10483 tree block = NULL;
10485 if (do_scope)
10487 if (c_dialect_objc ())
10488 objc_clear_super_receiver ();
10489 block = pop_scope ();
10492 stmt = pop_stmt_list (stmt);
10493 stmt = c_build_bind_expr (loc, block, stmt);
10495 /* If this compound statement is nested immediately inside a statement
10496 expression, then force a BIND_EXPR to be created. Otherwise we'll
10497 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10498 STATEMENT_LISTs merge, and thus we can lose track of what statement
10499 was really last. */
10500 if (building_stmt_list_p ()
10501 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10502 && TREE_CODE (stmt) != BIND_EXPR)
10504 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10505 TREE_SIDE_EFFECTS (stmt) = 1;
10506 SET_EXPR_LOCATION (stmt, loc);
10509 return stmt;
10512 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10513 when the current scope is exited. EH_ONLY is true when this is not
10514 meant to apply to normal control flow transfer. */
10516 void
10517 push_cleanup (tree decl, tree cleanup, bool eh_only)
10519 enum tree_code code;
10520 tree stmt, list;
10521 bool stmt_expr;
10523 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10524 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10525 add_stmt (stmt);
10526 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10527 list = push_stmt_list ();
10528 TREE_OPERAND (stmt, 0) = list;
10529 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10532 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10533 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10535 static tree
10536 build_vec_cmp (tree_code code, tree type,
10537 tree arg0, tree arg1)
10539 tree zero_vec = build_zero_cst (type);
10540 tree minus_one_vec = build_minus_one_cst (type);
10541 tree cmp_type = build_same_sized_truth_vector_type (type);
10542 tree cmp = build2 (code, cmp_type, arg0, arg1);
10543 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10546 /* Build a binary-operation expression without default conversions.
10547 CODE is the kind of expression to build.
10548 LOCATION is the operator's location.
10549 This function differs from `build' in several ways:
10550 the data type of the result is computed and recorded in it,
10551 warnings are generated if arg data types are invalid,
10552 special handling for addition and subtraction of pointers is known,
10553 and some optimization is done (operations on narrow ints
10554 are done in the narrower type when that gives the same result).
10555 Constant folding is also done before the result is returned.
10557 Note that the operands will never have enumeral types, or function
10558 or array types, because either they will have the default conversions
10559 performed or they have both just been converted to some other type in which
10560 the arithmetic is to be done. */
10562 tree
10563 build_binary_op (location_t location, enum tree_code code,
10564 tree orig_op0, tree orig_op1, int convert_p)
10566 tree type0, type1, orig_type0, orig_type1;
10567 tree eptype;
10568 enum tree_code code0, code1;
10569 tree op0, op1;
10570 tree ret = error_mark_node;
10571 const char *invalid_op_diag;
10572 bool op0_int_operands, op1_int_operands;
10573 bool int_const, int_const_or_overflow, int_operands;
10575 /* Expression code to give to the expression when it is built.
10576 Normally this is CODE, which is what the caller asked for,
10577 but in some special cases we change it. */
10578 enum tree_code resultcode = code;
10580 /* Data type in which the computation is to be performed.
10581 In the simplest cases this is the common type of the arguments. */
10582 tree result_type = NULL;
10584 /* When the computation is in excess precision, the type of the
10585 final EXCESS_PRECISION_EXPR. */
10586 tree semantic_result_type = NULL;
10588 /* Nonzero means operands have already been type-converted
10589 in whatever way is necessary.
10590 Zero means they need to be converted to RESULT_TYPE. */
10591 int converted = 0;
10593 /* Nonzero means create the expression with this type, rather than
10594 RESULT_TYPE. */
10595 tree build_type = 0;
10597 /* Nonzero means after finally constructing the expression
10598 convert it to this type. */
10599 tree final_type = 0;
10601 /* Nonzero if this is an operation like MIN or MAX which can
10602 safely be computed in short if both args are promoted shorts.
10603 Also implies COMMON.
10604 -1 indicates a bitwise operation; this makes a difference
10605 in the exact conditions for when it is safe to do the operation
10606 in a narrower mode. */
10607 int shorten = 0;
10609 /* Nonzero if this is a comparison operation;
10610 if both args are promoted shorts, compare the original shorts.
10611 Also implies COMMON. */
10612 int short_compare = 0;
10614 /* Nonzero if this is a right-shift operation, which can be computed on the
10615 original short and then promoted if the operand is a promoted short. */
10616 int short_shift = 0;
10618 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10619 int common = 0;
10621 /* True means types are compatible as far as ObjC is concerned. */
10622 bool objc_ok;
10624 /* True means this is an arithmetic operation that may need excess
10625 precision. */
10626 bool may_need_excess_precision;
10628 /* True means this is a boolean operation that converts both its
10629 operands to truth-values. */
10630 bool boolean_op = false;
10632 /* Remember whether we're doing / or %. */
10633 bool doing_div_or_mod = false;
10635 /* Remember whether we're doing << or >>. */
10636 bool doing_shift = false;
10638 /* Tree holding instrumentation expression. */
10639 tree instrument_expr = NULL;
10641 if (location == UNKNOWN_LOCATION)
10642 location = input_location;
10644 op0 = orig_op0;
10645 op1 = orig_op1;
10647 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10648 if (op0_int_operands)
10649 op0 = remove_c_maybe_const_expr (op0);
10650 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10651 if (op1_int_operands)
10652 op1 = remove_c_maybe_const_expr (op1);
10653 int_operands = (op0_int_operands && op1_int_operands);
10654 if (int_operands)
10656 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10657 && TREE_CODE (orig_op1) == INTEGER_CST);
10658 int_const = (int_const_or_overflow
10659 && !TREE_OVERFLOW (orig_op0)
10660 && !TREE_OVERFLOW (orig_op1));
10662 else
10663 int_const = int_const_or_overflow = false;
10665 /* Do not apply default conversion in mixed vector/scalar expression. */
10666 if (convert_p
10667 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10669 op0 = default_conversion (op0);
10670 op1 = default_conversion (op1);
10673 /* When Cilk Plus is enabled and there are array notations inside op0, then
10674 we check to see if there are builtin array notation functions. If
10675 so, then we take on the type of the array notation inside it. */
10676 if (flag_cilkplus && contains_array_notation_expr (op0))
10677 orig_type0 = type0 = find_correct_array_notation_type (op0);
10678 else
10679 orig_type0 = type0 = TREE_TYPE (op0);
10681 if (flag_cilkplus && contains_array_notation_expr (op1))
10682 orig_type1 = type1 = find_correct_array_notation_type (op1);
10683 else
10684 orig_type1 = type1 = TREE_TYPE (op1);
10686 /* The expression codes of the data types of the arguments tell us
10687 whether the arguments are integers, floating, pointers, etc. */
10688 code0 = TREE_CODE (type0);
10689 code1 = TREE_CODE (type1);
10691 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10692 STRIP_TYPE_NOPS (op0);
10693 STRIP_TYPE_NOPS (op1);
10695 /* If an error was already reported for one of the arguments,
10696 avoid reporting another error. */
10698 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10699 return error_mark_node;
10701 if (code0 == POINTER_TYPE
10702 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10703 return error_mark_node;
10705 if (code1 == POINTER_TYPE
10706 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10707 return error_mark_node;
10709 if ((invalid_op_diag
10710 = targetm.invalid_binary_op (code, type0, type1)))
10712 error_at (location, invalid_op_diag);
10713 return error_mark_node;
10716 switch (code)
10718 case PLUS_EXPR:
10719 case MINUS_EXPR:
10720 case MULT_EXPR:
10721 case TRUNC_DIV_EXPR:
10722 case CEIL_DIV_EXPR:
10723 case FLOOR_DIV_EXPR:
10724 case ROUND_DIV_EXPR:
10725 case EXACT_DIV_EXPR:
10726 may_need_excess_precision = true;
10727 break;
10728 default:
10729 may_need_excess_precision = false;
10730 break;
10732 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10734 op0 = TREE_OPERAND (op0, 0);
10735 type0 = TREE_TYPE (op0);
10737 else if (may_need_excess_precision
10738 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10740 type0 = eptype;
10741 op0 = convert (eptype, op0);
10743 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10745 op1 = TREE_OPERAND (op1, 0);
10746 type1 = TREE_TYPE (op1);
10748 else if (may_need_excess_precision
10749 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10751 type1 = eptype;
10752 op1 = convert (eptype, op1);
10755 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10757 /* In case when one of the operands of the binary operation is
10758 a vector and another is a scalar -- convert scalar to vector. */
10759 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10761 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10762 true);
10764 switch (convert_flag)
10766 case stv_error:
10767 return error_mark_node;
10768 case stv_firstarg:
10770 bool maybe_const = true;
10771 tree sc;
10772 sc = c_fully_fold (op0, false, &maybe_const);
10773 sc = save_expr (sc);
10774 sc = convert (TREE_TYPE (type1), sc);
10775 op0 = build_vector_from_val (type1, sc);
10776 if (!maybe_const)
10777 op0 = c_wrap_maybe_const (op0, true);
10778 orig_type0 = type0 = TREE_TYPE (op0);
10779 code0 = TREE_CODE (type0);
10780 converted = 1;
10781 break;
10783 case stv_secondarg:
10785 bool maybe_const = true;
10786 tree sc;
10787 sc = c_fully_fold (op1, false, &maybe_const);
10788 sc = save_expr (sc);
10789 sc = convert (TREE_TYPE (type0), sc);
10790 op1 = build_vector_from_val (type0, sc);
10791 if (!maybe_const)
10792 op1 = c_wrap_maybe_const (op1, true);
10793 orig_type1 = type1 = TREE_TYPE (op1);
10794 code1 = TREE_CODE (type1);
10795 converted = 1;
10796 break;
10798 default:
10799 break;
10803 switch (code)
10805 case PLUS_EXPR:
10806 /* Handle the pointer + int case. */
10807 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10809 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10810 goto return_build_binary_op;
10812 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10814 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10815 goto return_build_binary_op;
10817 else
10818 common = 1;
10819 break;
10821 case MINUS_EXPR:
10822 /* Subtraction of two similar pointers.
10823 We must subtract them as integers, then divide by object size. */
10824 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10825 && comp_target_types (location, type0, type1))
10827 ret = pointer_diff (location, op0, op1);
10828 goto return_build_binary_op;
10830 /* Handle pointer minus int. Just like pointer plus int. */
10831 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10833 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10834 goto return_build_binary_op;
10836 else
10837 common = 1;
10838 break;
10840 case MULT_EXPR:
10841 common = 1;
10842 break;
10844 case TRUNC_DIV_EXPR:
10845 case CEIL_DIV_EXPR:
10846 case FLOOR_DIV_EXPR:
10847 case ROUND_DIV_EXPR:
10848 case EXACT_DIV_EXPR:
10849 doing_div_or_mod = true;
10850 warn_for_div_by_zero (location, op1);
10852 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10853 || code0 == FIXED_POINT_TYPE
10854 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10855 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10856 || code1 == FIXED_POINT_TYPE
10857 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10859 enum tree_code tcode0 = code0, tcode1 = code1;
10861 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10862 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10863 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10864 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10866 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10867 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10868 resultcode = RDIV_EXPR;
10869 else
10870 /* Although it would be tempting to shorten always here, that
10871 loses on some targets, since the modulo instruction is
10872 undefined if the quotient can't be represented in the
10873 computation mode. We shorten only if unsigned or if
10874 dividing by something we know != -1. */
10875 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10876 || (TREE_CODE (op1) == INTEGER_CST
10877 && !integer_all_onesp (op1)));
10878 common = 1;
10880 break;
10882 case BIT_AND_EXPR:
10883 case BIT_IOR_EXPR:
10884 case BIT_XOR_EXPR:
10885 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10886 shorten = -1;
10887 /* Allow vector types which are not floating point types. */
10888 else if (code0 == VECTOR_TYPE
10889 && code1 == VECTOR_TYPE
10890 && !VECTOR_FLOAT_TYPE_P (type0)
10891 && !VECTOR_FLOAT_TYPE_P (type1))
10892 common = 1;
10893 break;
10895 case TRUNC_MOD_EXPR:
10896 case FLOOR_MOD_EXPR:
10897 doing_div_or_mod = true;
10898 warn_for_div_by_zero (location, op1);
10900 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10901 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10902 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10903 common = 1;
10904 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10906 /* Although it would be tempting to shorten always here, that loses
10907 on some targets, since the modulo instruction is undefined if the
10908 quotient can't be represented in the computation mode. We shorten
10909 only if unsigned or if dividing by something we know != -1. */
10910 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10911 || (TREE_CODE (op1) == INTEGER_CST
10912 && !integer_all_onesp (op1)));
10913 common = 1;
10915 break;
10917 case TRUTH_ANDIF_EXPR:
10918 case TRUTH_ORIF_EXPR:
10919 case TRUTH_AND_EXPR:
10920 case TRUTH_OR_EXPR:
10921 case TRUTH_XOR_EXPR:
10922 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10923 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10924 || code0 == FIXED_POINT_TYPE)
10925 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10926 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10927 || code1 == FIXED_POINT_TYPE))
10929 /* Result of these operations is always an int,
10930 but that does not mean the operands should be
10931 converted to ints! */
10932 result_type = integer_type_node;
10933 if (op0_int_operands)
10935 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10936 op0 = remove_c_maybe_const_expr (op0);
10938 else
10939 op0 = c_objc_common_truthvalue_conversion (location, op0);
10940 if (op1_int_operands)
10942 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10943 op1 = remove_c_maybe_const_expr (op1);
10945 else
10946 op1 = c_objc_common_truthvalue_conversion (location, op1);
10947 converted = 1;
10948 boolean_op = true;
10950 if (code == TRUTH_ANDIF_EXPR)
10952 int_const_or_overflow = (int_operands
10953 && TREE_CODE (orig_op0) == INTEGER_CST
10954 && (op0 == truthvalue_false_node
10955 || TREE_CODE (orig_op1) == INTEGER_CST));
10956 int_const = (int_const_or_overflow
10957 && !TREE_OVERFLOW (orig_op0)
10958 && (op0 == truthvalue_false_node
10959 || !TREE_OVERFLOW (orig_op1)));
10961 else if (code == TRUTH_ORIF_EXPR)
10963 int_const_or_overflow = (int_operands
10964 && TREE_CODE (orig_op0) == INTEGER_CST
10965 && (op0 == truthvalue_true_node
10966 || TREE_CODE (orig_op1) == INTEGER_CST));
10967 int_const = (int_const_or_overflow
10968 && !TREE_OVERFLOW (orig_op0)
10969 && (op0 == truthvalue_true_node
10970 || !TREE_OVERFLOW (orig_op1)));
10972 break;
10974 /* Shift operations: result has same type as first operand;
10975 always convert second operand to int.
10976 Also set SHORT_SHIFT if shifting rightward. */
10978 case RSHIFT_EXPR:
10979 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10980 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10982 result_type = type0;
10983 converted = 1;
10985 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10986 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10987 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10988 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10990 result_type = type0;
10991 converted = 1;
10993 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10994 && code1 == INTEGER_TYPE)
10996 doing_shift = true;
10997 if (TREE_CODE (op1) == INTEGER_CST)
10999 if (tree_int_cst_sgn (op1) < 0)
11001 int_const = false;
11002 if (c_inhibit_evaluation_warnings == 0)
11003 warning_at (location, OPT_Wshift_count_negative,
11004 "right shift count is negative");
11006 else
11008 if (!integer_zerop (op1))
11009 short_shift = 1;
11011 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11013 int_const = false;
11014 if (c_inhibit_evaluation_warnings == 0)
11015 warning_at (location, OPT_Wshift_count_overflow,
11016 "right shift count >= width of type");
11021 /* Use the type of the value to be shifted. */
11022 result_type = type0;
11023 /* Avoid converting op1 to result_type later. */
11024 converted = 1;
11026 break;
11028 case LSHIFT_EXPR:
11029 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
11030 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
11032 result_type = type0;
11033 converted = 1;
11035 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11036 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11037 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11038 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11040 result_type = type0;
11041 converted = 1;
11043 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
11044 && code1 == INTEGER_TYPE)
11046 doing_shift = true;
11047 if (TREE_CODE (op0) == INTEGER_CST
11048 && tree_int_cst_sgn (op0) < 0)
11050 /* Don't reject a left shift of a negative value in a context
11051 where a constant expression is needed in C90. */
11052 if (flag_isoc99)
11053 int_const = false;
11054 if (c_inhibit_evaluation_warnings == 0)
11055 warning_at (location, OPT_Wshift_negative_value,
11056 "left shift of negative value");
11058 if (TREE_CODE (op1) == INTEGER_CST)
11060 if (tree_int_cst_sgn (op1) < 0)
11062 int_const = false;
11063 if (c_inhibit_evaluation_warnings == 0)
11064 warning_at (location, OPT_Wshift_count_negative,
11065 "left shift count is negative");
11067 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11069 int_const = false;
11070 if (c_inhibit_evaluation_warnings == 0)
11071 warning_at (location, OPT_Wshift_count_overflow,
11072 "left shift count >= width of type");
11074 else if (TREE_CODE (op0) == INTEGER_CST
11075 && maybe_warn_shift_overflow (location, op0, op1)
11076 && flag_isoc99)
11077 int_const = false;
11080 /* Use the type of the value to be shifted. */
11081 result_type = type0;
11082 /* Avoid converting op1 to result_type later. */
11083 converted = 1;
11085 break;
11087 case EQ_EXPR:
11088 case NE_EXPR:
11089 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11091 tree intt;
11092 if (!vector_types_compatible_elements_p (type0, type1))
11094 error_at (location, "comparing vectors with different "
11095 "element types");
11096 return error_mark_node;
11099 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11101 error_at (location, "comparing vectors with different "
11102 "number of elements");
11103 return error_mark_node;
11106 /* It's not precisely specified how the usual arithmetic
11107 conversions apply to the vector types. Here, we use
11108 the unsigned type if one of the operands is signed and
11109 the other one is unsigned. */
11110 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11112 if (!TYPE_UNSIGNED (type0))
11113 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11114 else
11115 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11116 warning_at (location, OPT_Wsign_compare, "comparison between "
11117 "types %qT and %qT", type0, type1);
11120 /* Always construct signed integer vector type. */
11121 intt = c_common_type_for_size (GET_MODE_BITSIZE
11122 (TYPE_MODE (TREE_TYPE (type0))), 0);
11123 result_type = build_opaque_vector_type (intt,
11124 TYPE_VECTOR_SUBPARTS (type0));
11125 converted = 1;
11126 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11127 goto return_build_binary_op;
11129 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11130 warning_at (location,
11131 OPT_Wfloat_equal,
11132 "comparing floating point with == or != is unsafe");
11133 /* Result of comparison is always int,
11134 but don't convert the args to int! */
11135 build_type = integer_type_node;
11136 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11137 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11138 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11139 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11140 short_compare = 1;
11141 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11143 if (TREE_CODE (op0) == ADDR_EXPR
11144 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11145 && !from_macro_expansion_at (location))
11147 if (code == EQ_EXPR)
11148 warning_at (location,
11149 OPT_Waddress,
11150 "the comparison will always evaluate as %<false%> "
11151 "for the address of %qD will never be NULL",
11152 TREE_OPERAND (op0, 0));
11153 else
11154 warning_at (location,
11155 OPT_Waddress,
11156 "the comparison will always evaluate as %<true%> "
11157 "for the address of %qD will never be NULL",
11158 TREE_OPERAND (op0, 0));
11160 result_type = type0;
11162 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11164 if (TREE_CODE (op1) == ADDR_EXPR
11165 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11166 && !from_macro_expansion_at (location))
11168 if (code == EQ_EXPR)
11169 warning_at (location,
11170 OPT_Waddress,
11171 "the comparison will always evaluate as %<false%> "
11172 "for the address of %qD will never be NULL",
11173 TREE_OPERAND (op1, 0));
11174 else
11175 warning_at (location,
11176 OPT_Waddress,
11177 "the comparison will always evaluate as %<true%> "
11178 "for the address of %qD will never be NULL",
11179 TREE_OPERAND (op1, 0));
11181 result_type = type1;
11183 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11185 tree tt0 = TREE_TYPE (type0);
11186 tree tt1 = TREE_TYPE (type1);
11187 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11188 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11189 addr_space_t as_common = ADDR_SPACE_GENERIC;
11191 /* Anything compares with void *. void * compares with anything.
11192 Otherwise, the targets must be compatible
11193 and both must be object or both incomplete. */
11194 if (comp_target_types (location, type0, type1))
11195 result_type = common_pointer_type (type0, type1);
11196 else if (!addr_space_superset (as0, as1, &as_common))
11198 error_at (location, "comparison of pointers to "
11199 "disjoint address spaces");
11200 return error_mark_node;
11202 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11204 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11205 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11206 "comparison of %<void *%> with function pointer");
11208 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11210 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11211 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11212 "comparison of %<void *%> with function pointer");
11214 else
11215 /* Avoid warning about the volatile ObjC EH puts on decls. */
11216 if (!objc_ok)
11217 pedwarn (location, 0,
11218 "comparison of distinct pointer types lacks a cast");
11220 if (result_type == NULL_TREE)
11222 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11223 result_type = build_pointer_type
11224 (build_qualified_type (void_type_node, qual));
11227 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11229 result_type = type0;
11230 pedwarn (location, 0, "comparison between pointer and integer");
11232 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11234 result_type = type1;
11235 pedwarn (location, 0, "comparison between pointer and integer");
11237 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11238 || truth_value_p (TREE_CODE (orig_op0)))
11239 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11240 || truth_value_p (TREE_CODE (orig_op1))))
11241 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11242 break;
11244 case LE_EXPR:
11245 case GE_EXPR:
11246 case LT_EXPR:
11247 case GT_EXPR:
11248 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11250 tree intt;
11251 if (!vector_types_compatible_elements_p (type0, type1))
11253 error_at (location, "comparing vectors with different "
11254 "element types");
11255 return error_mark_node;
11258 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11260 error_at (location, "comparing vectors with different "
11261 "number of elements");
11262 return error_mark_node;
11265 /* It's not precisely specified how the usual arithmetic
11266 conversions apply to the vector types. Here, we use
11267 the unsigned type if one of the operands is signed and
11268 the other one is unsigned. */
11269 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11271 if (!TYPE_UNSIGNED (type0))
11272 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11273 else
11274 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11275 warning_at (location, OPT_Wsign_compare, "comparison between "
11276 "types %qT and %qT", type0, type1);
11279 /* Always construct signed integer vector type. */
11280 intt = c_common_type_for_size (GET_MODE_BITSIZE
11281 (TYPE_MODE (TREE_TYPE (type0))), 0);
11282 result_type = build_opaque_vector_type (intt,
11283 TYPE_VECTOR_SUBPARTS (type0));
11284 converted = 1;
11285 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11286 goto return_build_binary_op;
11288 build_type = integer_type_node;
11289 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11290 || code0 == FIXED_POINT_TYPE)
11291 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11292 || code1 == FIXED_POINT_TYPE))
11293 short_compare = 1;
11294 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11296 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11297 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11298 addr_space_t as_common;
11300 if (comp_target_types (location, type0, type1))
11302 result_type = common_pointer_type (type0, type1);
11303 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11304 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11305 pedwarn (location, 0,
11306 "comparison of complete and incomplete pointers");
11307 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11308 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11309 "ordered comparisons of pointers to functions");
11310 else if (null_pointer_constant_p (orig_op0)
11311 || null_pointer_constant_p (orig_op1))
11312 warning_at (location, OPT_Wextra,
11313 "ordered comparison of pointer with null pointer");
11316 else if (!addr_space_superset (as0, as1, &as_common))
11318 error_at (location, "comparison of pointers to "
11319 "disjoint address spaces");
11320 return error_mark_node;
11322 else
11324 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11325 result_type = build_pointer_type
11326 (build_qualified_type (void_type_node, qual));
11327 pedwarn (location, 0,
11328 "comparison of distinct pointer types lacks a cast");
11331 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11333 result_type = type0;
11334 if (pedantic)
11335 pedwarn (location, OPT_Wpedantic,
11336 "ordered comparison of pointer with integer zero");
11337 else if (extra_warnings)
11338 warning_at (location, OPT_Wextra,
11339 "ordered comparison of pointer with integer zero");
11341 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11343 result_type = type1;
11344 if (pedantic)
11345 pedwarn (location, OPT_Wpedantic,
11346 "ordered comparison of pointer with integer zero");
11347 else if (extra_warnings)
11348 warning_at (location, OPT_Wextra,
11349 "ordered comparison of pointer with integer zero");
11351 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11353 result_type = type0;
11354 pedwarn (location, 0, "comparison between pointer and integer");
11356 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11358 result_type = type1;
11359 pedwarn (location, 0, "comparison between pointer and integer");
11361 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11362 || truth_value_p (TREE_CODE (orig_op0)))
11363 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11364 || truth_value_p (TREE_CODE (orig_op1))))
11365 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11366 break;
11368 default:
11369 gcc_unreachable ();
11372 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11373 return error_mark_node;
11375 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11376 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11377 || !vector_types_compatible_elements_p (type0, type1)))
11379 gcc_rich_location richloc (location);
11380 richloc.maybe_add_expr (orig_op0);
11381 richloc.maybe_add_expr (orig_op1);
11382 binary_op_error (&richloc, code, type0, type1);
11383 return error_mark_node;
11386 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11387 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11389 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11390 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11392 bool first_complex = (code0 == COMPLEX_TYPE);
11393 bool second_complex = (code1 == COMPLEX_TYPE);
11394 int none_complex = (!first_complex && !second_complex);
11396 if (shorten || common || short_compare)
11398 result_type = c_common_type (type0, type1);
11399 do_warn_double_promotion (result_type, type0, type1,
11400 "implicit conversion from %qT to %qT "
11401 "to match other operand of binary "
11402 "expression",
11403 location);
11404 if (result_type == error_mark_node)
11405 return error_mark_node;
11408 if (first_complex != second_complex
11409 && (code == PLUS_EXPR
11410 || code == MINUS_EXPR
11411 || code == MULT_EXPR
11412 || (code == TRUNC_DIV_EXPR && first_complex))
11413 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11414 && flag_signed_zeros)
11416 /* An operation on mixed real/complex operands must be
11417 handled specially, but the language-independent code can
11418 more easily optimize the plain complex arithmetic if
11419 -fno-signed-zeros. */
11420 tree real_type = TREE_TYPE (result_type);
11421 tree real, imag;
11422 if (type0 != orig_type0 || type1 != orig_type1)
11424 gcc_assert (may_need_excess_precision && common);
11425 semantic_result_type = c_common_type (orig_type0, orig_type1);
11427 if (first_complex)
11429 if (TREE_TYPE (op0) != result_type)
11430 op0 = convert_and_check (location, result_type, op0);
11431 if (TREE_TYPE (op1) != real_type)
11432 op1 = convert_and_check (location, real_type, op1);
11434 else
11436 if (TREE_TYPE (op0) != real_type)
11437 op0 = convert_and_check (location, real_type, op0);
11438 if (TREE_TYPE (op1) != result_type)
11439 op1 = convert_and_check (location, result_type, op1);
11441 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11442 return error_mark_node;
11443 if (first_complex)
11445 op0 = c_save_expr (op0);
11446 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11447 op0, 1);
11448 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11449 op0, 1);
11450 switch (code)
11452 case MULT_EXPR:
11453 case TRUNC_DIV_EXPR:
11454 op1 = c_save_expr (op1);
11455 imag = build2 (resultcode, real_type, imag, op1);
11456 /* Fall through. */
11457 case PLUS_EXPR:
11458 case MINUS_EXPR:
11459 real = build2 (resultcode, real_type, real, op1);
11460 break;
11461 default:
11462 gcc_unreachable();
11465 else
11467 op1 = c_save_expr (op1);
11468 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11469 op1, 1);
11470 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11471 op1, 1);
11472 switch (code)
11474 case MULT_EXPR:
11475 op0 = c_save_expr (op0);
11476 imag = build2 (resultcode, real_type, op0, imag);
11477 /* Fall through. */
11478 case PLUS_EXPR:
11479 real = build2 (resultcode, real_type, op0, real);
11480 break;
11481 case MINUS_EXPR:
11482 real = build2 (resultcode, real_type, op0, real);
11483 imag = build1 (NEGATE_EXPR, real_type, imag);
11484 break;
11485 default:
11486 gcc_unreachable();
11489 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11490 goto return_build_binary_op;
11493 /* For certain operations (which identify themselves by shorten != 0)
11494 if both args were extended from the same smaller type,
11495 do the arithmetic in that type and then extend.
11497 shorten !=0 and !=1 indicates a bitwise operation.
11498 For them, this optimization is safe only if
11499 both args are zero-extended or both are sign-extended.
11500 Otherwise, we might change the result.
11501 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11502 but calculated in (unsigned short) it would be (unsigned short)-1. */
11504 if (shorten && none_complex)
11506 final_type = result_type;
11507 result_type = shorten_binary_op (result_type, op0, op1,
11508 shorten == -1);
11511 /* Shifts can be shortened if shifting right. */
11513 if (short_shift)
11515 int unsigned_arg;
11516 tree arg0 = get_narrower (op0, &unsigned_arg);
11518 final_type = result_type;
11520 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11521 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11523 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11524 && tree_int_cst_sgn (op1) > 0
11525 /* We can shorten only if the shift count is less than the
11526 number of bits in the smaller type size. */
11527 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11528 /* We cannot drop an unsigned shift after sign-extension. */
11529 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11531 /* Do an unsigned shift if the operand was zero-extended. */
11532 result_type
11533 = c_common_signed_or_unsigned_type (unsigned_arg,
11534 TREE_TYPE (arg0));
11535 /* Convert value-to-be-shifted to that type. */
11536 if (TREE_TYPE (op0) != result_type)
11537 op0 = convert (result_type, op0);
11538 converted = 1;
11542 /* Comparison operations are shortened too but differently.
11543 They identify themselves by setting short_compare = 1. */
11545 if (short_compare)
11547 /* Don't write &op0, etc., because that would prevent op0
11548 from being kept in a register.
11549 Instead, make copies of the our local variables and
11550 pass the copies by reference, then copy them back afterward. */
11551 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11552 enum tree_code xresultcode = resultcode;
11553 tree val
11554 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11555 &xresultcode);
11557 if (val != 0)
11559 ret = val;
11560 goto return_build_binary_op;
11563 op0 = xop0, op1 = xop1;
11564 converted = 1;
11565 resultcode = xresultcode;
11567 if (c_inhibit_evaluation_warnings == 0)
11569 bool op0_maybe_const = true;
11570 bool op1_maybe_const = true;
11571 tree orig_op0_folded, orig_op1_folded;
11573 if (in_late_binary_op)
11575 orig_op0_folded = orig_op0;
11576 orig_op1_folded = orig_op1;
11578 else
11580 /* Fold for the sake of possible warnings, as in
11581 build_conditional_expr. This requires the
11582 "original" values to be folded, not just op0 and
11583 op1. */
11584 c_inhibit_evaluation_warnings++;
11585 op0 = c_fully_fold (op0, require_constant_value,
11586 &op0_maybe_const);
11587 op1 = c_fully_fold (op1, require_constant_value,
11588 &op1_maybe_const);
11589 c_inhibit_evaluation_warnings--;
11590 orig_op0_folded = c_fully_fold (orig_op0,
11591 require_constant_value,
11592 NULL);
11593 orig_op1_folded = c_fully_fold (orig_op1,
11594 require_constant_value,
11595 NULL);
11598 if (warn_sign_compare)
11599 warn_for_sign_compare (location, orig_op0_folded,
11600 orig_op1_folded, op0, op1,
11601 result_type, resultcode);
11602 if (!in_late_binary_op && !int_operands)
11604 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11605 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11606 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11607 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11613 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11614 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11615 Then the expression will be built.
11616 It will be given type FINAL_TYPE if that is nonzero;
11617 otherwise, it will be given type RESULT_TYPE. */
11619 if (!result_type)
11621 gcc_rich_location richloc (location);
11622 richloc.maybe_add_expr (orig_op0);
11623 richloc.maybe_add_expr (orig_op1);
11624 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11625 return error_mark_node;
11628 if (build_type == NULL_TREE)
11630 build_type = result_type;
11631 if ((type0 != orig_type0 || type1 != orig_type1)
11632 && !boolean_op)
11634 gcc_assert (may_need_excess_precision && common);
11635 semantic_result_type = c_common_type (orig_type0, orig_type1);
11639 if (!converted)
11641 op0 = ep_convert_and_check (location, result_type, op0,
11642 semantic_result_type);
11643 op1 = ep_convert_and_check (location, result_type, op1,
11644 semantic_result_type);
11646 /* This can happen if one operand has a vector type, and the other
11647 has a different type. */
11648 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11649 return error_mark_node;
11652 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11653 | SANITIZE_FLOAT_DIVIDE))
11654 && do_ubsan_in_current_function ()
11655 && (doing_div_or_mod || doing_shift)
11656 && !require_constant_value)
11658 /* OP0 and/or OP1 might have side-effects. */
11659 op0 = c_save_expr (op0);
11660 op1 = c_save_expr (op1);
11661 op0 = c_fully_fold (op0, false, NULL);
11662 op1 = c_fully_fold (op1, false, NULL);
11663 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11664 | SANITIZE_FLOAT_DIVIDE)))
11665 instrument_expr = ubsan_instrument_division (location, op0, op1);
11666 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11667 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11670 /* Treat expressions in initializers specially as they can't trap. */
11671 if (int_const_or_overflow)
11672 ret = (require_constant_value
11673 ? fold_build2_initializer_loc (location, resultcode, build_type,
11674 op0, op1)
11675 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11676 else
11677 ret = build2 (resultcode, build_type, op0, op1);
11678 if (final_type != 0)
11679 ret = convert (final_type, ret);
11681 return_build_binary_op:
11682 gcc_assert (ret != error_mark_node);
11683 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11684 ret = (int_operands
11685 ? note_integer_operands (ret)
11686 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11687 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11688 && !in_late_binary_op)
11689 ret = note_integer_operands (ret);
11690 if (semantic_result_type)
11691 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11692 protected_set_expr_location (ret, location);
11694 if (instrument_expr != NULL)
11695 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11696 instrument_expr, ret);
11698 return ret;
11702 /* Convert EXPR to be a truth-value, validating its type for this
11703 purpose. LOCATION is the source location for the expression. */
11705 tree
11706 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11708 bool int_const, int_operands;
11710 switch (TREE_CODE (TREE_TYPE (expr)))
11712 case ARRAY_TYPE:
11713 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11714 return error_mark_node;
11716 case RECORD_TYPE:
11717 error_at (location, "used struct type value where scalar is required");
11718 return error_mark_node;
11720 case UNION_TYPE:
11721 error_at (location, "used union type value where scalar is required");
11722 return error_mark_node;
11724 case VOID_TYPE:
11725 error_at (location, "void value not ignored as it ought to be");
11726 return error_mark_node;
11728 case POINTER_TYPE:
11729 if (reject_gcc_builtin (expr))
11730 return error_mark_node;
11731 break;
11733 case FUNCTION_TYPE:
11734 gcc_unreachable ();
11736 case VECTOR_TYPE:
11737 error_at (location, "used vector type where scalar is required");
11738 return error_mark_node;
11740 default:
11741 break;
11744 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11745 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11746 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11748 expr = remove_c_maybe_const_expr (expr);
11749 expr = build2 (NE_EXPR, integer_type_node, expr,
11750 convert (TREE_TYPE (expr), integer_zero_node));
11751 expr = note_integer_operands (expr);
11753 else
11754 /* ??? Should we also give an error for vectors rather than leaving
11755 those to give errors later? */
11756 expr = c_common_truthvalue_conversion (location, expr);
11758 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11760 if (TREE_OVERFLOW (expr))
11761 return expr;
11762 else
11763 return note_integer_operands (expr);
11765 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11766 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11767 return expr;
11771 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11772 required. */
11774 tree
11775 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11777 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11779 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11780 /* Executing a compound literal inside a function reinitializes
11781 it. */
11782 if (!TREE_STATIC (decl))
11783 *se = true;
11784 return decl;
11786 else
11787 return expr;
11790 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11791 statement. LOC is the location of the construct. */
11793 tree
11794 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11795 tree clauses)
11797 body = c_end_compound_stmt (loc, body, true);
11799 tree stmt = make_node (code);
11800 TREE_TYPE (stmt) = void_type_node;
11801 OMP_BODY (stmt) = body;
11802 OMP_CLAUSES (stmt) = clauses;
11803 SET_EXPR_LOCATION (stmt, loc);
11805 return add_stmt (stmt);
11808 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11809 statement. LOC is the location of the OACC_DATA. */
11811 tree
11812 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11814 tree stmt;
11816 block = c_end_compound_stmt (loc, block, true);
11818 stmt = make_node (OACC_DATA);
11819 TREE_TYPE (stmt) = void_type_node;
11820 OACC_DATA_CLAUSES (stmt) = clauses;
11821 OACC_DATA_BODY (stmt) = block;
11822 SET_EXPR_LOCATION (stmt, loc);
11824 return add_stmt (stmt);
11827 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11828 statement. LOC is the location of the OACC_HOST_DATA. */
11830 tree
11831 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11833 tree stmt;
11835 block = c_end_compound_stmt (loc, block, true);
11837 stmt = make_node (OACC_HOST_DATA);
11838 TREE_TYPE (stmt) = void_type_node;
11839 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11840 OACC_HOST_DATA_BODY (stmt) = block;
11841 SET_EXPR_LOCATION (stmt, loc);
11843 return add_stmt (stmt);
11846 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11848 tree
11849 c_begin_omp_parallel (void)
11851 tree block;
11853 keep_next_level ();
11854 block = c_begin_compound_stmt (true);
11856 return block;
11859 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11860 statement. LOC is the location of the OMP_PARALLEL. */
11862 tree
11863 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11865 tree stmt;
11867 block = c_end_compound_stmt (loc, block, true);
11869 stmt = make_node (OMP_PARALLEL);
11870 TREE_TYPE (stmt) = void_type_node;
11871 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11872 OMP_PARALLEL_BODY (stmt) = block;
11873 SET_EXPR_LOCATION (stmt, loc);
11875 return add_stmt (stmt);
11878 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11880 tree
11881 c_begin_omp_task (void)
11883 tree block;
11885 keep_next_level ();
11886 block = c_begin_compound_stmt (true);
11888 return block;
11891 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11892 statement. LOC is the location of the #pragma. */
11894 tree
11895 c_finish_omp_task (location_t loc, tree clauses, tree block)
11897 tree stmt;
11899 block = c_end_compound_stmt (loc, block, true);
11901 stmt = make_node (OMP_TASK);
11902 TREE_TYPE (stmt) = void_type_node;
11903 OMP_TASK_CLAUSES (stmt) = clauses;
11904 OMP_TASK_BODY (stmt) = block;
11905 SET_EXPR_LOCATION (stmt, loc);
11907 return add_stmt (stmt);
11910 /* Generate GOMP_cancel call for #pragma omp cancel. */
11912 void
11913 c_finish_omp_cancel (location_t loc, tree clauses)
11915 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11916 int mask = 0;
11917 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11918 mask = 1;
11919 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11920 mask = 2;
11921 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11922 mask = 4;
11923 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11924 mask = 8;
11925 else
11927 error_at (loc, "%<#pragma omp cancel must specify one of "
11928 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11929 "clauses");
11930 return;
11932 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11933 if (ifc != NULL_TREE)
11935 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11936 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11937 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11938 build_zero_cst (type));
11940 else
11941 ifc = boolean_true_node;
11942 tree stmt = build_call_expr_loc (loc, fn, 2,
11943 build_int_cst (integer_type_node, mask),
11944 ifc);
11945 add_stmt (stmt);
11948 /* Generate GOMP_cancellation_point call for
11949 #pragma omp cancellation point. */
11951 void
11952 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11954 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11955 int mask = 0;
11956 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11957 mask = 1;
11958 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11959 mask = 2;
11960 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11961 mask = 4;
11962 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11963 mask = 8;
11964 else
11966 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11967 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11968 "clauses");
11969 return;
11971 tree stmt = build_call_expr_loc (loc, fn, 1,
11972 build_int_cst (integer_type_node, mask));
11973 add_stmt (stmt);
11976 /* Helper function for handle_omp_array_sections. Called recursively
11977 to handle multiple array-section-subscripts. C is the clause,
11978 T current expression (initially OMP_CLAUSE_DECL), which is either
11979 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11980 expression if specified, TREE_VALUE length expression if specified,
11981 TREE_CHAIN is what it has been specified after, or some decl.
11982 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11983 set to true if any of the array-section-subscript could have length
11984 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11985 first array-section-subscript which is known not to have length
11986 of one. Given say:
11987 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11988 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11989 all are or may have length of 1, array-section-subscript [:2] is the
11990 first one known not to have length 1. For array-section-subscript
11991 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11992 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11993 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11994 case though, as some lengths could be zero. */
11996 static tree
11997 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11998 bool &maybe_zero_len, unsigned int &first_non_one,
11999 enum c_omp_region_type ort)
12001 tree ret, low_bound, length, type;
12002 if (TREE_CODE (t) != TREE_LIST)
12004 if (error_operand_p (t))
12005 return error_mark_node;
12006 ret = t;
12007 if (TREE_CODE (t) == COMPONENT_REF
12008 && ort == C_ORT_OMP
12009 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12010 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12011 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12013 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12015 error_at (OMP_CLAUSE_LOCATION (c),
12016 "bit-field %qE in %qs clause",
12017 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12018 return error_mark_node;
12020 while (TREE_CODE (t) == COMPONENT_REF)
12022 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12024 error_at (OMP_CLAUSE_LOCATION (c),
12025 "%qE is a member of a union", t);
12026 return error_mark_node;
12028 t = TREE_OPERAND (t, 0);
12031 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12033 if (DECL_P (t))
12034 error_at (OMP_CLAUSE_LOCATION (c),
12035 "%qD is not a variable in %qs clause", t,
12036 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12037 else
12038 error_at (OMP_CLAUSE_LOCATION (c),
12039 "%qE is not a variable in %qs clause", t,
12040 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12041 return error_mark_node;
12043 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12044 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12046 error_at (OMP_CLAUSE_LOCATION (c),
12047 "%qD is threadprivate variable in %qs clause", t,
12048 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12049 return error_mark_node;
12051 return ret;
12054 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12055 maybe_zero_len, first_non_one, ort);
12056 if (ret == error_mark_node || ret == NULL_TREE)
12057 return ret;
12059 type = TREE_TYPE (ret);
12060 low_bound = TREE_PURPOSE (t);
12061 length = TREE_VALUE (t);
12063 if (low_bound == error_mark_node || length == error_mark_node)
12064 return error_mark_node;
12066 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12068 error_at (OMP_CLAUSE_LOCATION (c),
12069 "low bound %qE of array section does not have integral type",
12070 low_bound);
12071 return error_mark_node;
12073 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12075 error_at (OMP_CLAUSE_LOCATION (c),
12076 "length %qE of array section does not have integral type",
12077 length);
12078 return error_mark_node;
12080 if (low_bound
12081 && TREE_CODE (low_bound) == INTEGER_CST
12082 && TYPE_PRECISION (TREE_TYPE (low_bound))
12083 > TYPE_PRECISION (sizetype))
12084 low_bound = fold_convert (sizetype, low_bound);
12085 if (length
12086 && TREE_CODE (length) == INTEGER_CST
12087 && TYPE_PRECISION (TREE_TYPE (length))
12088 > TYPE_PRECISION (sizetype))
12089 length = fold_convert (sizetype, length);
12090 if (low_bound == NULL_TREE)
12091 low_bound = integer_zero_node;
12093 if (length != NULL_TREE)
12095 if (!integer_nonzerop (length))
12097 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12098 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12100 if (integer_zerop (length))
12102 error_at (OMP_CLAUSE_LOCATION (c),
12103 "zero length array section in %qs clause",
12104 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12105 return error_mark_node;
12108 else
12109 maybe_zero_len = true;
12111 if (first_non_one == types.length ()
12112 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12113 first_non_one++;
12115 if (TREE_CODE (type) == ARRAY_TYPE)
12117 if (length == NULL_TREE
12118 && (TYPE_DOMAIN (type) == NULL_TREE
12119 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12121 error_at (OMP_CLAUSE_LOCATION (c),
12122 "for unknown bound array type length expression must "
12123 "be specified");
12124 return error_mark_node;
12126 if (TREE_CODE (low_bound) == INTEGER_CST
12127 && tree_int_cst_sgn (low_bound) == -1)
12129 error_at (OMP_CLAUSE_LOCATION (c),
12130 "negative low bound in array section in %qs clause",
12131 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12132 return error_mark_node;
12134 if (length != NULL_TREE
12135 && TREE_CODE (length) == INTEGER_CST
12136 && tree_int_cst_sgn (length) == -1)
12138 error_at (OMP_CLAUSE_LOCATION (c),
12139 "negative length in array section in %qs clause",
12140 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12141 return error_mark_node;
12143 if (TYPE_DOMAIN (type)
12144 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12145 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12146 == INTEGER_CST)
12148 tree size = size_binop (PLUS_EXPR,
12149 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12150 size_one_node);
12151 if (TREE_CODE (low_bound) == INTEGER_CST)
12153 if (tree_int_cst_lt (size, low_bound))
12155 error_at (OMP_CLAUSE_LOCATION (c),
12156 "low bound %qE above array section size "
12157 "in %qs clause", low_bound,
12158 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12159 return error_mark_node;
12161 if (tree_int_cst_equal (size, low_bound))
12163 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12164 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12166 error_at (OMP_CLAUSE_LOCATION (c),
12167 "zero length array section in %qs clause",
12168 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12169 return error_mark_node;
12171 maybe_zero_len = true;
12173 else if (length == NULL_TREE
12174 && first_non_one == types.length ()
12175 && tree_int_cst_equal
12176 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12177 low_bound))
12178 first_non_one++;
12180 else if (length == NULL_TREE)
12182 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12183 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12184 maybe_zero_len = true;
12185 if (first_non_one == types.length ())
12186 first_non_one++;
12188 if (length && TREE_CODE (length) == INTEGER_CST)
12190 if (tree_int_cst_lt (size, length))
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "length %qE above array section size "
12194 "in %qs clause", length,
12195 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12196 return error_mark_node;
12198 if (TREE_CODE (low_bound) == INTEGER_CST)
12200 tree lbpluslen
12201 = size_binop (PLUS_EXPR,
12202 fold_convert (sizetype, low_bound),
12203 fold_convert (sizetype, length));
12204 if (TREE_CODE (lbpluslen) == INTEGER_CST
12205 && tree_int_cst_lt (size, lbpluslen))
12207 error_at (OMP_CLAUSE_LOCATION (c),
12208 "high bound %qE above array section size "
12209 "in %qs clause", lbpluslen,
12210 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12211 return error_mark_node;
12216 else if (length == NULL_TREE)
12218 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12219 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12220 maybe_zero_len = true;
12221 if (first_non_one == types.length ())
12222 first_non_one++;
12225 /* For [lb:] we will need to evaluate lb more than once. */
12226 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12228 tree lb = c_save_expr (low_bound);
12229 if (lb != low_bound)
12231 TREE_PURPOSE (t) = lb;
12232 low_bound = lb;
12236 else if (TREE_CODE (type) == POINTER_TYPE)
12238 if (length == NULL_TREE)
12240 error_at (OMP_CLAUSE_LOCATION (c),
12241 "for pointer type length expression must be specified");
12242 return error_mark_node;
12244 if (length != NULL_TREE
12245 && TREE_CODE (length) == INTEGER_CST
12246 && tree_int_cst_sgn (length) == -1)
12248 error_at (OMP_CLAUSE_LOCATION (c),
12249 "negative length in array section in %qs clause",
12250 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12251 return error_mark_node;
12253 /* If there is a pointer type anywhere but in the very first
12254 array-section-subscript, the array section can't be contiguous. */
12255 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12256 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12258 error_at (OMP_CLAUSE_LOCATION (c),
12259 "array section is not contiguous in %qs clause",
12260 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12261 return error_mark_node;
12264 else
12266 error_at (OMP_CLAUSE_LOCATION (c),
12267 "%qE does not have pointer or array type", ret);
12268 return error_mark_node;
12270 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12271 types.safe_push (TREE_TYPE (ret));
12272 /* We will need to evaluate lb more than once. */
12273 tree lb = c_save_expr (low_bound);
12274 if (lb != low_bound)
12276 TREE_PURPOSE (t) = lb;
12277 low_bound = lb;
12279 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12280 return ret;
12283 /* Handle array sections for clause C. */
12285 static bool
12286 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12288 bool maybe_zero_len = false;
12289 unsigned int first_non_one = 0;
12290 auto_vec<tree, 10> types;
12291 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12292 maybe_zero_len, first_non_one,
12293 ort);
12294 if (first == error_mark_node)
12295 return true;
12296 if (first == NULL_TREE)
12297 return false;
12298 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12300 tree t = OMP_CLAUSE_DECL (c);
12301 tree tem = NULL_TREE;
12302 /* Need to evaluate side effects in the length expressions
12303 if any. */
12304 while (TREE_CODE (t) == TREE_LIST)
12306 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12308 if (tem == NULL_TREE)
12309 tem = TREE_VALUE (t);
12310 else
12311 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12312 TREE_VALUE (t), tem);
12314 t = TREE_CHAIN (t);
12316 if (tem)
12317 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12318 first = c_fully_fold (first, false, NULL);
12319 OMP_CLAUSE_DECL (c) = first;
12321 else
12323 unsigned int num = types.length (), i;
12324 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12325 tree condition = NULL_TREE;
12327 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12328 maybe_zero_len = true;
12330 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12331 t = TREE_CHAIN (t))
12333 tree low_bound = TREE_PURPOSE (t);
12334 tree length = TREE_VALUE (t);
12336 i--;
12337 if (low_bound
12338 && TREE_CODE (low_bound) == INTEGER_CST
12339 && TYPE_PRECISION (TREE_TYPE (low_bound))
12340 > TYPE_PRECISION (sizetype))
12341 low_bound = fold_convert (sizetype, low_bound);
12342 if (length
12343 && TREE_CODE (length) == INTEGER_CST
12344 && TYPE_PRECISION (TREE_TYPE (length))
12345 > TYPE_PRECISION (sizetype))
12346 length = fold_convert (sizetype, length);
12347 if (low_bound == NULL_TREE)
12348 low_bound = integer_zero_node;
12349 if (!maybe_zero_len && i > first_non_one)
12351 if (integer_nonzerop (low_bound))
12352 goto do_warn_noncontiguous;
12353 if (length != NULL_TREE
12354 && TREE_CODE (length) == INTEGER_CST
12355 && TYPE_DOMAIN (types[i])
12356 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12357 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12358 == INTEGER_CST)
12360 tree size;
12361 size = size_binop (PLUS_EXPR,
12362 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12363 size_one_node);
12364 if (!tree_int_cst_equal (length, size))
12366 do_warn_noncontiguous:
12367 error_at (OMP_CLAUSE_LOCATION (c),
12368 "array section is not contiguous in %qs "
12369 "clause",
12370 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12371 return true;
12374 if (length != NULL_TREE
12375 && TREE_SIDE_EFFECTS (length))
12377 if (side_effects == NULL_TREE)
12378 side_effects = length;
12379 else
12380 side_effects = build2 (COMPOUND_EXPR,
12381 TREE_TYPE (side_effects),
12382 length, side_effects);
12385 else
12387 tree l;
12389 if (i > first_non_one
12390 && ((length && integer_nonzerop (length))
12391 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12392 continue;
12393 if (length)
12394 l = fold_convert (sizetype, length);
12395 else
12397 l = size_binop (PLUS_EXPR,
12398 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12399 size_one_node);
12400 l = size_binop (MINUS_EXPR, l,
12401 fold_convert (sizetype, low_bound));
12403 if (i > first_non_one)
12405 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12406 size_zero_node);
12407 if (condition == NULL_TREE)
12408 condition = l;
12409 else
12410 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12411 l, condition);
12413 else if (size == NULL_TREE)
12415 size = size_in_bytes (TREE_TYPE (types[i]));
12416 tree eltype = TREE_TYPE (types[num - 1]);
12417 while (TREE_CODE (eltype) == ARRAY_TYPE)
12418 eltype = TREE_TYPE (eltype);
12419 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12421 if (integer_zerop (size)
12422 || integer_zerop (size_in_bytes (eltype)))
12424 error_at (OMP_CLAUSE_LOCATION (c),
12425 "zero length array section in %qs clause",
12426 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12427 return error_mark_node;
12429 size = size_binop (EXACT_DIV_EXPR, size,
12430 size_in_bytes (eltype));
12432 size = size_binop (MULT_EXPR, size, l);
12433 if (condition)
12434 size = fold_build3 (COND_EXPR, sizetype, condition,
12435 size, size_zero_node);
12437 else
12438 size = size_binop (MULT_EXPR, size, l);
12441 if (side_effects)
12442 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12443 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12445 size = size_binop (MINUS_EXPR, size, size_one_node);
12446 size = c_fully_fold (size, false, NULL);
12447 tree index_type = build_index_type (size);
12448 tree eltype = TREE_TYPE (first);
12449 while (TREE_CODE (eltype) == ARRAY_TYPE)
12450 eltype = TREE_TYPE (eltype);
12451 tree type = build_array_type (eltype, index_type);
12452 tree ptype = build_pointer_type (eltype);
12453 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12454 t = build_fold_addr_expr (t);
12455 tree t2 = build_fold_addr_expr (first);
12456 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12457 ptrdiff_type_node, t2);
12458 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12459 ptrdiff_type_node, t2,
12460 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12461 ptrdiff_type_node, t));
12462 t2 = c_fully_fold (t2, false, NULL);
12463 if (tree_fits_shwi_p (t2))
12464 t = build2 (MEM_REF, type, t,
12465 build_int_cst (ptype, tree_to_shwi (t2)));
12466 else
12468 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12469 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12470 TREE_TYPE (t), t, t2);
12471 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12473 OMP_CLAUSE_DECL (c) = t;
12474 return false;
12476 first = c_fully_fold (first, false, NULL);
12477 OMP_CLAUSE_DECL (c) = first;
12478 if (size)
12479 size = c_fully_fold (size, false, NULL);
12480 OMP_CLAUSE_SIZE (c) = size;
12481 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12482 || (TREE_CODE (t) == COMPONENT_REF
12483 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12484 return false;
12485 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12486 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12487 switch (OMP_CLAUSE_MAP_KIND (c))
12489 case GOMP_MAP_ALLOC:
12490 case GOMP_MAP_TO:
12491 case GOMP_MAP_FROM:
12492 case GOMP_MAP_TOFROM:
12493 case GOMP_MAP_ALWAYS_TO:
12494 case GOMP_MAP_ALWAYS_FROM:
12495 case GOMP_MAP_ALWAYS_TOFROM:
12496 case GOMP_MAP_RELEASE:
12497 case GOMP_MAP_DELETE:
12498 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12499 break;
12500 default:
12501 break;
12503 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12504 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12505 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12506 else if (TREE_CODE (t) == COMPONENT_REF)
12507 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12508 else
12509 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12510 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12511 && !c_mark_addressable (t))
12512 return false;
12513 OMP_CLAUSE_DECL (c2) = t;
12514 t = build_fold_addr_expr (first);
12515 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12516 tree ptr = OMP_CLAUSE_DECL (c2);
12517 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12518 ptr = build_fold_addr_expr (ptr);
12519 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12520 ptrdiff_type_node, t,
12521 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12522 ptrdiff_type_node, ptr));
12523 t = c_fully_fold (t, false, NULL);
12524 OMP_CLAUSE_SIZE (c2) = t;
12525 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12526 OMP_CLAUSE_CHAIN (c) = c2;
12528 return false;
12531 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12532 an inline call. But, remap
12533 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12534 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12536 static tree
12537 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12538 tree decl, tree placeholder)
12540 copy_body_data id;
12541 hash_map<tree, tree> decl_map;
12543 decl_map.put (omp_decl1, placeholder);
12544 decl_map.put (omp_decl2, decl);
12545 memset (&id, 0, sizeof (id));
12546 id.src_fn = DECL_CONTEXT (omp_decl1);
12547 id.dst_fn = current_function_decl;
12548 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12549 id.decl_map = &decl_map;
12551 id.copy_decl = copy_decl_no_change;
12552 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12553 id.transform_new_cfg = true;
12554 id.transform_return_to_modify = false;
12555 id.transform_lang_insert_block = NULL;
12556 id.eh_lp_nr = 0;
12557 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12558 return stmt;
12561 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12562 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12564 static tree
12565 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12567 if (*tp == (tree) data)
12568 return *tp;
12569 return NULL_TREE;
12572 /* For all elements of CLAUSES, validate them against their constraints.
12573 Remove any elements from the list that are invalid. */
12575 tree
12576 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12578 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12579 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12580 tree c, t, type, *pc;
12581 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12582 bool branch_seen = false;
12583 bool copyprivate_seen = false;
12584 bool linear_variable_step_check = false;
12585 tree *nowait_clause = NULL;
12586 bool ordered_seen = false;
12587 tree schedule_clause = NULL_TREE;
12588 bool oacc_async = false;
12590 bitmap_obstack_initialize (NULL);
12591 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12592 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12593 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12594 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12595 bitmap_initialize (&map_head, &bitmap_default_obstack);
12596 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12597 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12599 if (ort & C_ORT_ACC)
12600 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12601 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12603 oacc_async = true;
12604 break;
12607 for (pc = &clauses, c = clauses; c ; c = *pc)
12609 bool remove = false;
12610 bool need_complete = false;
12611 bool need_implicitly_determined = false;
12613 switch (OMP_CLAUSE_CODE (c))
12615 case OMP_CLAUSE_SHARED:
12616 need_implicitly_determined = true;
12617 goto check_dup_generic;
12619 case OMP_CLAUSE_PRIVATE:
12620 need_complete = true;
12621 need_implicitly_determined = true;
12622 goto check_dup_generic;
12624 case OMP_CLAUSE_REDUCTION:
12625 need_implicitly_determined = true;
12626 t = OMP_CLAUSE_DECL (c);
12627 if (TREE_CODE (t) == TREE_LIST)
12629 if (handle_omp_array_sections (c, ort))
12631 remove = true;
12632 break;
12635 t = OMP_CLAUSE_DECL (c);
12637 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12638 if (t == error_mark_node)
12640 remove = true;
12641 break;
12643 if (oacc_async)
12644 c_mark_addressable (t);
12645 type = TREE_TYPE (t);
12646 if (TREE_CODE (t) == MEM_REF)
12647 type = TREE_TYPE (type);
12648 if (TREE_CODE (type) == ARRAY_TYPE)
12650 tree oatype = type;
12651 gcc_assert (TREE_CODE (t) != MEM_REF);
12652 while (TREE_CODE (type) == ARRAY_TYPE)
12653 type = TREE_TYPE (type);
12654 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12656 error_at (OMP_CLAUSE_LOCATION (c),
12657 "%qD in %<reduction%> clause is a zero size array",
12659 remove = true;
12660 break;
12662 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12663 TYPE_SIZE_UNIT (type));
12664 if (integer_zerop (size))
12666 error_at (OMP_CLAUSE_LOCATION (c),
12667 "%qD in %<reduction%> clause is a zero size array",
12669 remove = true;
12670 break;
12672 size = size_binop (MINUS_EXPR, size, size_one_node);
12673 tree index_type = build_index_type (size);
12674 tree atype = build_array_type (type, index_type);
12675 tree ptype = build_pointer_type (type);
12676 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12677 t = build_fold_addr_expr (t);
12678 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12679 OMP_CLAUSE_DECL (c) = t;
12681 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12682 && (FLOAT_TYPE_P (type)
12683 || TREE_CODE (type) == COMPLEX_TYPE))
12685 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12686 const char *r_name = NULL;
12688 switch (r_code)
12690 case PLUS_EXPR:
12691 case MULT_EXPR:
12692 case MINUS_EXPR:
12693 break;
12694 case MIN_EXPR:
12695 if (TREE_CODE (type) == COMPLEX_TYPE)
12696 r_name = "min";
12697 break;
12698 case MAX_EXPR:
12699 if (TREE_CODE (type) == COMPLEX_TYPE)
12700 r_name = "max";
12701 break;
12702 case BIT_AND_EXPR:
12703 r_name = "&";
12704 break;
12705 case BIT_XOR_EXPR:
12706 r_name = "^";
12707 break;
12708 case BIT_IOR_EXPR:
12709 r_name = "|";
12710 break;
12711 case TRUTH_ANDIF_EXPR:
12712 if (FLOAT_TYPE_P (type))
12713 r_name = "&&";
12714 break;
12715 case TRUTH_ORIF_EXPR:
12716 if (FLOAT_TYPE_P (type))
12717 r_name = "||";
12718 break;
12719 default:
12720 gcc_unreachable ();
12722 if (r_name)
12724 error_at (OMP_CLAUSE_LOCATION (c),
12725 "%qE has invalid type for %<reduction(%s)%>",
12726 t, r_name);
12727 remove = true;
12728 break;
12731 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12733 error_at (OMP_CLAUSE_LOCATION (c),
12734 "user defined reduction not found for %qE", t);
12735 remove = true;
12736 break;
12738 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12740 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12741 type = TYPE_MAIN_VARIANT (type);
12742 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12743 VAR_DECL, NULL_TREE, type);
12744 tree decl_placeholder = NULL_TREE;
12745 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12746 DECL_ARTIFICIAL (placeholder) = 1;
12747 DECL_IGNORED_P (placeholder) = 1;
12748 if (TREE_CODE (t) == MEM_REF)
12750 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12751 VAR_DECL, NULL_TREE, type);
12752 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12753 DECL_ARTIFICIAL (decl_placeholder) = 1;
12754 DECL_IGNORED_P (decl_placeholder) = 1;
12756 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12757 c_mark_addressable (placeholder);
12758 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12759 c_mark_addressable (decl_placeholder ? decl_placeholder
12760 : OMP_CLAUSE_DECL (c));
12761 OMP_CLAUSE_REDUCTION_MERGE (c)
12762 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12763 TREE_VEC_ELT (list, 0),
12764 TREE_VEC_ELT (list, 1),
12765 decl_placeholder ? decl_placeholder
12766 : OMP_CLAUSE_DECL (c), placeholder);
12767 OMP_CLAUSE_REDUCTION_MERGE (c)
12768 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12769 void_type_node, NULL_TREE,
12770 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12771 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12772 if (TREE_VEC_LENGTH (list) == 6)
12774 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12775 c_mark_addressable (decl_placeholder ? decl_placeholder
12776 : OMP_CLAUSE_DECL (c));
12777 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12778 c_mark_addressable (placeholder);
12779 tree init = TREE_VEC_ELT (list, 5);
12780 if (init == error_mark_node)
12781 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12782 OMP_CLAUSE_REDUCTION_INIT (c)
12783 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12784 TREE_VEC_ELT (list, 3),
12785 decl_placeholder ? decl_placeholder
12786 : OMP_CLAUSE_DECL (c), placeholder);
12787 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12789 tree v = decl_placeholder ? decl_placeholder : t;
12790 OMP_CLAUSE_REDUCTION_INIT (c)
12791 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12792 OMP_CLAUSE_REDUCTION_INIT (c));
12794 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12795 c_find_omp_placeholder_r,
12796 placeholder, NULL))
12797 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12799 else
12801 tree init;
12802 tree v = decl_placeholder ? decl_placeholder : t;
12803 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12804 init = build_constructor (TREE_TYPE (v), NULL);
12805 else
12806 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12807 OMP_CLAUSE_REDUCTION_INIT (c)
12808 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12810 OMP_CLAUSE_REDUCTION_INIT (c)
12811 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12812 void_type_node, NULL_TREE,
12813 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12814 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12816 if (TREE_CODE (t) == MEM_REF)
12818 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12819 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12820 != INTEGER_CST)
12822 sorry ("variable length element type in array "
12823 "%<reduction%> clause");
12824 remove = true;
12825 break;
12827 t = TREE_OPERAND (t, 0);
12828 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12829 t = TREE_OPERAND (t, 0);
12830 if (TREE_CODE (t) == ADDR_EXPR)
12831 t = TREE_OPERAND (t, 0);
12833 goto check_dup_generic_t;
12835 case OMP_CLAUSE_COPYPRIVATE:
12836 copyprivate_seen = true;
12837 if (nowait_clause)
12839 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12840 "%<nowait%> clause must not be used together "
12841 "with %<copyprivate%>");
12842 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12843 nowait_clause = NULL;
12845 goto check_dup_generic;
12847 case OMP_CLAUSE_COPYIN:
12848 t = OMP_CLAUSE_DECL (c);
12849 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12851 error_at (OMP_CLAUSE_LOCATION (c),
12852 "%qE must be %<threadprivate%> for %<copyin%>", t);
12853 remove = true;
12854 break;
12856 goto check_dup_generic;
12858 case OMP_CLAUSE_LINEAR:
12859 if (ort != C_ORT_OMP_DECLARE_SIMD)
12860 need_implicitly_determined = true;
12861 t = OMP_CLAUSE_DECL (c);
12862 if (ort != C_ORT_OMP_DECLARE_SIMD
12863 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12865 error_at (OMP_CLAUSE_LOCATION (c),
12866 "modifier should not be specified in %<linear%> "
12867 "clause on %<simd%> or %<for%> constructs");
12868 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12870 if (ort & C_ORT_CILK)
12872 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12873 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
12874 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12876 error_at (OMP_CLAUSE_LOCATION (c),
12877 "linear clause applied to non-integral, "
12878 "non-floating, non-pointer variable with type %qT",
12879 TREE_TYPE (t));
12880 remove = true;
12881 break;
12884 else
12886 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12887 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12889 error_at (OMP_CLAUSE_LOCATION (c),
12890 "linear clause applied to non-integral non-pointer "
12891 "variable with type %qT", TREE_TYPE (t));
12892 remove = true;
12893 break;
12896 if (ort == C_ORT_OMP_DECLARE_SIMD)
12898 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12899 if (TREE_CODE (s) == PARM_DECL)
12901 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12902 /* map_head bitmap is used as uniform_head if
12903 declare_simd. */
12904 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12905 linear_variable_step_check = true;
12906 goto check_dup_generic;
12908 if (TREE_CODE (s) != INTEGER_CST)
12910 error_at (OMP_CLAUSE_LOCATION (c),
12911 "%<linear%> clause step %qE is neither constant "
12912 "nor a parameter", s);
12913 remove = true;
12914 break;
12917 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12919 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12920 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12921 OMP_CLAUSE_DECL (c), s);
12922 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12923 sizetype, fold_convert (sizetype, s),
12924 fold_convert
12925 (sizetype, OMP_CLAUSE_DECL (c)));
12926 if (s == error_mark_node)
12927 s = size_one_node;
12928 OMP_CLAUSE_LINEAR_STEP (c) = s;
12930 else
12931 OMP_CLAUSE_LINEAR_STEP (c)
12932 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12933 goto check_dup_generic;
12935 check_dup_generic:
12936 t = OMP_CLAUSE_DECL (c);
12937 check_dup_generic_t:
12938 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12940 error_at (OMP_CLAUSE_LOCATION (c),
12941 "%qE is not a variable in clause %qs", t,
12942 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12943 remove = true;
12945 else if (ort == C_ORT_ACC
12946 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12948 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
12950 error ("%qD appears more than once in reduction clauses", t);
12951 remove = true;
12953 else
12954 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
12956 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12957 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12958 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12960 error_at (OMP_CLAUSE_LOCATION (c),
12961 "%qE appears more than once in data clauses", t);
12962 remove = true;
12964 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12965 && bitmap_bit_p (&map_head, DECL_UID (t)))
12967 if (ort == C_ORT_ACC)
12968 error ("%qD appears more than once in data clauses", t);
12969 else
12970 error ("%qD appears both in data and map clauses", t);
12971 remove = true;
12973 else
12974 bitmap_set_bit (&generic_head, DECL_UID (t));
12975 break;
12977 case OMP_CLAUSE_FIRSTPRIVATE:
12978 t = OMP_CLAUSE_DECL (c);
12979 need_complete = true;
12980 need_implicitly_determined = true;
12981 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12983 error_at (OMP_CLAUSE_LOCATION (c),
12984 "%qE is not a variable in clause %<firstprivate%>", t);
12985 remove = true;
12987 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12988 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12990 error_at (OMP_CLAUSE_LOCATION (c),
12991 "%qE appears more than once in data clauses", t);
12992 remove = true;
12994 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12996 if (ort == C_ORT_ACC)
12997 error ("%qD appears more than once in data clauses", t);
12998 else
12999 error ("%qD appears both in data and map clauses", t);
13000 remove = true;
13002 else
13003 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13004 break;
13006 case OMP_CLAUSE_LASTPRIVATE:
13007 t = OMP_CLAUSE_DECL (c);
13008 need_complete = true;
13009 need_implicitly_determined = true;
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 %<lastprivate%>", t);
13014 remove = true;
13016 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13017 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13019 error_at (OMP_CLAUSE_LOCATION (c),
13020 "%qE appears more than once in data clauses", t);
13021 remove = true;
13023 else
13024 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13025 break;
13027 case OMP_CLAUSE_ALIGNED:
13028 t = OMP_CLAUSE_DECL (c);
13029 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13031 error_at (OMP_CLAUSE_LOCATION (c),
13032 "%qE is not a variable in %<aligned%> clause", t);
13033 remove = true;
13035 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13036 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13038 error_at (OMP_CLAUSE_LOCATION (c),
13039 "%qE in %<aligned%> clause is neither a pointer nor "
13040 "an array", t);
13041 remove = true;
13043 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13045 error_at (OMP_CLAUSE_LOCATION (c),
13046 "%qE appears more than once in %<aligned%> clauses",
13048 remove = true;
13050 else
13051 bitmap_set_bit (&aligned_head, DECL_UID (t));
13052 break;
13054 case OMP_CLAUSE_DEPEND:
13055 t = OMP_CLAUSE_DECL (c);
13056 if (t == NULL_TREE)
13058 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13059 == OMP_CLAUSE_DEPEND_SOURCE);
13060 break;
13062 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13064 gcc_assert (TREE_CODE (t) == TREE_LIST);
13065 for (; t; t = TREE_CHAIN (t))
13067 tree decl = TREE_VALUE (t);
13068 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13070 tree offset = TREE_PURPOSE (t);
13071 bool neg = wi::neg_p ((wide_int) offset);
13072 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13073 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13074 neg ? MINUS_EXPR : PLUS_EXPR,
13075 decl, offset);
13076 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13077 sizetype,
13078 fold_convert (sizetype, t2),
13079 fold_convert (sizetype, decl));
13080 if (t2 == error_mark_node)
13082 remove = true;
13083 break;
13085 TREE_PURPOSE (t) = t2;
13088 break;
13090 if (TREE_CODE (t) == TREE_LIST)
13092 if (handle_omp_array_sections (c, ort))
13093 remove = true;
13094 break;
13096 if (t == error_mark_node)
13097 remove = true;
13098 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13100 error_at (OMP_CLAUSE_LOCATION (c),
13101 "%qE is not a variable in %<depend%> clause", t);
13102 remove = true;
13104 else if (!c_mark_addressable (t))
13105 remove = true;
13106 break;
13108 case OMP_CLAUSE_MAP:
13109 case OMP_CLAUSE_TO:
13110 case OMP_CLAUSE_FROM:
13111 case OMP_CLAUSE__CACHE_:
13112 t = OMP_CLAUSE_DECL (c);
13113 if (TREE_CODE (t) == TREE_LIST)
13115 if (handle_omp_array_sections (c, ort))
13116 remove = true;
13117 else
13119 t = OMP_CLAUSE_DECL (c);
13120 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13122 error_at (OMP_CLAUSE_LOCATION (c),
13123 "array section does not have mappable type "
13124 "in %qs clause",
13125 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13126 remove = true;
13128 while (TREE_CODE (t) == ARRAY_REF)
13129 t = TREE_OPERAND (t, 0);
13130 if (TREE_CODE (t) == COMPONENT_REF
13131 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13133 while (TREE_CODE (t) == COMPONENT_REF)
13134 t = TREE_OPERAND (t, 0);
13135 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13136 break;
13137 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13139 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13140 error ("%qD appears more than once in motion"
13141 " clauses", t);
13142 else if (ort == C_ORT_ACC)
13143 error ("%qD appears more than once in data"
13144 " clauses", t);
13145 else
13146 error ("%qD appears more than once in map"
13147 " clauses", t);
13148 remove = true;
13150 else
13152 bitmap_set_bit (&map_head, DECL_UID (t));
13153 bitmap_set_bit (&map_field_head, DECL_UID (t));
13157 break;
13159 if (t == error_mark_node)
13161 remove = true;
13162 break;
13164 if (TREE_CODE (t) == COMPONENT_REF
13165 && (ort & C_ORT_OMP)
13166 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13168 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13170 error_at (OMP_CLAUSE_LOCATION (c),
13171 "bit-field %qE in %qs clause",
13172 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13173 remove = true;
13175 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13177 error_at (OMP_CLAUSE_LOCATION (c),
13178 "%qE does not have a mappable type in %qs clause",
13179 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13180 remove = true;
13182 while (TREE_CODE (t) == COMPONENT_REF)
13184 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13185 == UNION_TYPE)
13187 error_at (OMP_CLAUSE_LOCATION (c),
13188 "%qE is a member of a union", t);
13189 remove = true;
13190 break;
13192 t = TREE_OPERAND (t, 0);
13194 if (remove)
13195 break;
13196 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13198 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13199 break;
13202 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13204 error_at (OMP_CLAUSE_LOCATION (c),
13205 "%qE is not a variable in %qs clause", t,
13206 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13207 remove = true;
13209 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13211 error_at (OMP_CLAUSE_LOCATION (c),
13212 "%qD is threadprivate variable in %qs clause", t,
13213 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13214 remove = true;
13216 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13217 || (OMP_CLAUSE_MAP_KIND (c)
13218 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13219 && !c_mark_addressable (t))
13220 remove = true;
13221 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13222 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13223 || (OMP_CLAUSE_MAP_KIND (c)
13224 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13225 || (OMP_CLAUSE_MAP_KIND (c)
13226 == GOMP_MAP_FORCE_DEVICEPTR)))
13227 && t == OMP_CLAUSE_DECL (c)
13228 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13230 error_at (OMP_CLAUSE_LOCATION (c),
13231 "%qD does not have a mappable type in %qs clause", t,
13232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13233 remove = true;
13235 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13236 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13238 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13239 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13241 error ("%qD appears more than once in data clauses", t);
13242 remove = true;
13244 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13246 if (ort == C_ORT_ACC)
13247 error ("%qD appears more than once in data clauses", t);
13248 else
13249 error ("%qD appears both in data and map clauses", t);
13250 remove = true;
13252 else
13253 bitmap_set_bit (&generic_head, DECL_UID (t));
13255 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13257 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13258 error ("%qD appears more than once in motion clauses", t);
13259 else if (ort == C_ORT_ACC)
13260 error ("%qD appears more than once in data clauses", t);
13261 else
13262 error ("%qD appears more than once in map clauses", t);
13263 remove = true;
13265 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13266 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13268 if (ort == C_ORT_ACC)
13269 error ("%qD appears more than once in data clauses", t);
13270 else
13271 error ("%qD appears both in data and map clauses", t);
13272 remove = true;
13274 else
13276 bitmap_set_bit (&map_head, DECL_UID (t));
13277 if (t != OMP_CLAUSE_DECL (c)
13278 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13279 bitmap_set_bit (&map_field_head, DECL_UID (t));
13281 break;
13283 case OMP_CLAUSE_TO_DECLARE:
13284 case OMP_CLAUSE_LINK:
13285 t = OMP_CLAUSE_DECL (c);
13286 if (TREE_CODE (t) == FUNCTION_DECL
13287 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13289 else if (!VAR_P (t))
13291 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13292 error_at (OMP_CLAUSE_LOCATION (c),
13293 "%qE is neither a variable nor a function name in "
13294 "clause %qs", t,
13295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13296 else
13297 error_at (OMP_CLAUSE_LOCATION (c),
13298 "%qE is not a variable in clause %qs", t,
13299 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13300 remove = true;
13302 else if (DECL_THREAD_LOCAL_P (t))
13304 error_at (OMP_CLAUSE_LOCATION (c),
13305 "%qD is threadprivate variable in %qs clause", t,
13306 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13307 remove = true;
13309 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13311 error_at (OMP_CLAUSE_LOCATION (c),
13312 "%qD does not have a mappable type in %qs clause", t,
13313 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13314 remove = true;
13316 if (remove)
13317 break;
13318 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13320 error_at (OMP_CLAUSE_LOCATION (c),
13321 "%qE appears more than once on the same "
13322 "%<declare target%> directive", t);
13323 remove = true;
13325 else
13326 bitmap_set_bit (&generic_head, DECL_UID (t));
13327 break;
13329 case OMP_CLAUSE_UNIFORM:
13330 t = OMP_CLAUSE_DECL (c);
13331 if (TREE_CODE (t) != PARM_DECL)
13333 if (DECL_P (t))
13334 error_at (OMP_CLAUSE_LOCATION (c),
13335 "%qD is not an argument in %<uniform%> clause", t);
13336 else
13337 error_at (OMP_CLAUSE_LOCATION (c),
13338 "%qE is not an argument in %<uniform%> clause", t);
13339 remove = true;
13340 break;
13342 /* map_head bitmap is used as uniform_head if declare_simd. */
13343 bitmap_set_bit (&map_head, DECL_UID (t));
13344 goto check_dup_generic;
13346 case OMP_CLAUSE_IS_DEVICE_PTR:
13347 case OMP_CLAUSE_USE_DEVICE_PTR:
13348 t = OMP_CLAUSE_DECL (c);
13349 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13350 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13352 error_at (OMP_CLAUSE_LOCATION (c),
13353 "%qs variable is neither a pointer nor an array",
13354 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13355 remove = true;
13357 goto check_dup_generic;
13359 case OMP_CLAUSE_NOWAIT:
13360 if (copyprivate_seen)
13362 error_at (OMP_CLAUSE_LOCATION (c),
13363 "%<nowait%> clause must not be used together "
13364 "with %<copyprivate%>");
13365 remove = true;
13366 break;
13368 nowait_clause = pc;
13369 pc = &OMP_CLAUSE_CHAIN (c);
13370 continue;
13372 case OMP_CLAUSE_IF:
13373 case OMP_CLAUSE_NUM_THREADS:
13374 case OMP_CLAUSE_NUM_TEAMS:
13375 case OMP_CLAUSE_THREAD_LIMIT:
13376 case OMP_CLAUSE_DEFAULT:
13377 case OMP_CLAUSE_UNTIED:
13378 case OMP_CLAUSE_COLLAPSE:
13379 case OMP_CLAUSE_FINAL:
13380 case OMP_CLAUSE_MERGEABLE:
13381 case OMP_CLAUSE_DEVICE:
13382 case OMP_CLAUSE_DIST_SCHEDULE:
13383 case OMP_CLAUSE_PARALLEL:
13384 case OMP_CLAUSE_FOR:
13385 case OMP_CLAUSE_SECTIONS:
13386 case OMP_CLAUSE_TASKGROUP:
13387 case OMP_CLAUSE_PROC_BIND:
13388 case OMP_CLAUSE_PRIORITY:
13389 case OMP_CLAUSE_GRAINSIZE:
13390 case OMP_CLAUSE_NUM_TASKS:
13391 case OMP_CLAUSE_NOGROUP:
13392 case OMP_CLAUSE_THREADS:
13393 case OMP_CLAUSE_SIMD:
13394 case OMP_CLAUSE_HINT:
13395 case OMP_CLAUSE_DEFAULTMAP:
13396 case OMP_CLAUSE__CILK_FOR_COUNT_:
13397 case OMP_CLAUSE_NUM_GANGS:
13398 case OMP_CLAUSE_NUM_WORKERS:
13399 case OMP_CLAUSE_VECTOR_LENGTH:
13400 case OMP_CLAUSE_ASYNC:
13401 case OMP_CLAUSE_WAIT:
13402 case OMP_CLAUSE_AUTO:
13403 case OMP_CLAUSE_INDEPENDENT:
13404 case OMP_CLAUSE_SEQ:
13405 case OMP_CLAUSE_GANG:
13406 case OMP_CLAUSE_WORKER:
13407 case OMP_CLAUSE_VECTOR:
13408 case OMP_CLAUSE_TILE:
13409 pc = &OMP_CLAUSE_CHAIN (c);
13410 continue;
13412 case OMP_CLAUSE_SCHEDULE:
13413 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13415 const char *p = NULL;
13416 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13418 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13419 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13420 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13421 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13422 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13423 default: gcc_unreachable ();
13425 if (p)
13427 error_at (OMP_CLAUSE_LOCATION (c),
13428 "%<nonmonotonic%> modifier specified for %qs "
13429 "schedule kind", p);
13430 OMP_CLAUSE_SCHEDULE_KIND (c)
13431 = (enum omp_clause_schedule_kind)
13432 (OMP_CLAUSE_SCHEDULE_KIND (c)
13433 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13436 schedule_clause = c;
13437 pc = &OMP_CLAUSE_CHAIN (c);
13438 continue;
13440 case OMP_CLAUSE_ORDERED:
13441 ordered_seen = true;
13442 pc = &OMP_CLAUSE_CHAIN (c);
13443 continue;
13445 case OMP_CLAUSE_SAFELEN:
13446 safelen = c;
13447 pc = &OMP_CLAUSE_CHAIN (c);
13448 continue;
13449 case OMP_CLAUSE_SIMDLEN:
13450 simdlen = c;
13451 pc = &OMP_CLAUSE_CHAIN (c);
13452 continue;
13454 case OMP_CLAUSE_INBRANCH:
13455 case OMP_CLAUSE_NOTINBRANCH:
13456 if (branch_seen)
13458 error_at (OMP_CLAUSE_LOCATION (c),
13459 "%<inbranch%> clause is incompatible with "
13460 "%<notinbranch%>");
13461 remove = true;
13462 break;
13464 branch_seen = true;
13465 pc = &OMP_CLAUSE_CHAIN (c);
13466 continue;
13468 default:
13469 gcc_unreachable ();
13472 if (!remove)
13474 t = OMP_CLAUSE_DECL (c);
13476 if (need_complete)
13478 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13479 if (t == error_mark_node)
13480 remove = true;
13483 if (need_implicitly_determined)
13485 const char *share_name = NULL;
13487 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13488 share_name = "threadprivate";
13489 else switch (c_omp_predetermined_sharing (t))
13491 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13492 break;
13493 case OMP_CLAUSE_DEFAULT_SHARED:
13494 /* const vars may be specified in firstprivate clause. */
13495 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13496 && TREE_READONLY (t))
13497 break;
13498 share_name = "shared";
13499 break;
13500 case OMP_CLAUSE_DEFAULT_PRIVATE:
13501 share_name = "private";
13502 break;
13503 default:
13504 gcc_unreachable ();
13506 if (share_name)
13508 error_at (OMP_CLAUSE_LOCATION (c),
13509 "%qE is predetermined %qs for %qs",
13510 t, share_name,
13511 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13512 remove = true;
13517 if (remove)
13518 *pc = OMP_CLAUSE_CHAIN (c);
13519 else
13520 pc = &OMP_CLAUSE_CHAIN (c);
13523 if (simdlen
13524 && safelen
13525 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13526 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13528 error_at (OMP_CLAUSE_LOCATION (simdlen),
13529 "%<simdlen%> clause value is bigger than "
13530 "%<safelen%> clause value");
13531 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13532 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13535 if (ordered_seen
13536 && schedule_clause
13537 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13538 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13540 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13541 "%<nonmonotonic%> schedule modifier specified together "
13542 "with %<ordered%> clause");
13543 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13544 = (enum omp_clause_schedule_kind)
13545 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13546 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13549 if (linear_variable_step_check)
13550 for (pc = &clauses, c = clauses; c ; c = *pc)
13552 bool remove = false;
13553 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13554 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13555 && !bitmap_bit_p (&map_head,
13556 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13558 error_at (OMP_CLAUSE_LOCATION (c),
13559 "%<linear%> clause step is a parameter %qD not "
13560 "specified in %<uniform%> clause",
13561 OMP_CLAUSE_LINEAR_STEP (c));
13562 remove = true;
13565 if (remove)
13566 *pc = OMP_CLAUSE_CHAIN (c);
13567 else
13568 pc = &OMP_CLAUSE_CHAIN (c);
13571 bitmap_obstack_release (NULL);
13572 return clauses;
13575 /* Create a transaction node. */
13577 tree
13578 c_finish_transaction (location_t loc, tree block, int flags)
13580 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13581 if (flags & TM_STMT_ATTR_OUTER)
13582 TRANSACTION_EXPR_OUTER (stmt) = 1;
13583 if (flags & TM_STMT_ATTR_RELAXED)
13584 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13585 return add_stmt (stmt);
13588 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13589 down to the element type of an array. If ORIG_QUAL_TYPE is not
13590 NULL, then it should be used as the qualified type
13591 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13592 preserve information about the typedef name from which an array
13593 type was derived). */
13595 tree
13596 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13597 size_t orig_qual_indirect)
13599 if (type == error_mark_node)
13600 return type;
13602 if (TREE_CODE (type) == ARRAY_TYPE)
13604 tree t;
13605 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13606 type_quals, orig_qual_type,
13607 orig_qual_indirect - 1);
13609 /* See if we already have an identically qualified type. */
13610 if (orig_qual_type && orig_qual_indirect == 0)
13611 t = orig_qual_type;
13612 else
13613 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13615 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13616 && TYPE_NAME (t) == TYPE_NAME (type)
13617 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13618 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13619 TYPE_ATTRIBUTES (type)))
13620 break;
13622 if (!t)
13624 tree domain = TYPE_DOMAIN (type);
13626 t = build_variant_type_copy (type);
13627 TREE_TYPE (t) = element_type;
13629 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13630 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13631 SET_TYPE_STRUCTURAL_EQUALITY (t);
13632 else if (TYPE_CANONICAL (element_type) != element_type
13633 || (domain && TYPE_CANONICAL (domain) != domain))
13635 tree unqualified_canon
13636 = build_array_type (TYPE_CANONICAL (element_type),
13637 domain? TYPE_CANONICAL (domain)
13638 : NULL_TREE);
13639 if (TYPE_REVERSE_STORAGE_ORDER (type))
13641 unqualified_canon
13642 = build_distinct_type_copy (unqualified_canon);
13643 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13645 TYPE_CANONICAL (t)
13646 = c_build_qualified_type (unqualified_canon, type_quals);
13648 else
13649 TYPE_CANONICAL (t) = t;
13651 return t;
13654 /* A restrict-qualified pointer type must be a pointer to object or
13655 incomplete type. Note that the use of POINTER_TYPE_P also allows
13656 REFERENCE_TYPEs, which is appropriate for C++. */
13657 if ((type_quals & TYPE_QUAL_RESTRICT)
13658 && (!POINTER_TYPE_P (type)
13659 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13661 error ("invalid use of %<restrict%>");
13662 type_quals &= ~TYPE_QUAL_RESTRICT;
13665 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13666 ? orig_qual_type
13667 : build_qualified_type (type, type_quals));
13668 /* A variant type does not inherit the list of incomplete vars from the
13669 type main variant. */
13670 if (RECORD_OR_UNION_TYPE_P (var_type))
13671 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13672 return var_type;
13675 /* Build a VA_ARG_EXPR for the C parser. */
13677 tree
13678 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13680 if (error_operand_p (type))
13681 return error_mark_node;
13682 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13683 order because it takes the address of the expression. */
13684 else if (handled_component_p (expr)
13685 && reverse_storage_order_for_component_p (expr))
13687 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13688 return error_mark_node;
13690 else if (!COMPLETE_TYPE_P (type))
13692 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13693 "type %qT", type);
13694 return error_mark_node;
13696 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13697 warning_at (loc2, OPT_Wc___compat,
13698 "C++ requires promoted type, not enum type, in %<va_arg%>");
13699 return build_va_arg (loc2, expr, type);
13702 /* Return truthvalue of whether T1 is the same tree structure as T2.
13703 Return 1 if they are the same. Return 0 if they are different. */
13705 bool
13706 c_tree_equal (tree t1, tree t2)
13708 enum tree_code code1, code2;
13710 if (t1 == t2)
13711 return true;
13712 if (!t1 || !t2)
13713 return false;
13715 for (code1 = TREE_CODE (t1);
13716 CONVERT_EXPR_CODE_P (code1)
13717 || code1 == NON_LVALUE_EXPR;
13718 code1 = TREE_CODE (t1))
13719 t1 = TREE_OPERAND (t1, 0);
13720 for (code2 = TREE_CODE (t2);
13721 CONVERT_EXPR_CODE_P (code2)
13722 || code2 == NON_LVALUE_EXPR;
13723 code2 = TREE_CODE (t2))
13724 t2 = TREE_OPERAND (t2, 0);
13726 /* They might have become equal now. */
13727 if (t1 == t2)
13728 return true;
13730 if (code1 != code2)
13731 return false;
13733 switch (code1)
13735 case INTEGER_CST:
13736 return wi::eq_p (t1, t2);
13738 case REAL_CST:
13739 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13741 case STRING_CST:
13742 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13743 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13744 TREE_STRING_LENGTH (t1));
13746 case FIXED_CST:
13747 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13748 TREE_FIXED_CST (t2));
13750 case COMPLEX_CST:
13751 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13752 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13754 case VECTOR_CST:
13755 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13757 case CONSTRUCTOR:
13758 /* We need to do this when determining whether or not two
13759 non-type pointer to member function template arguments
13760 are the same. */
13761 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13762 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13763 return false;
13765 tree field, value;
13766 unsigned int i;
13767 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13769 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13770 if (!c_tree_equal (field, elt2->index)
13771 || !c_tree_equal (value, elt2->value))
13772 return false;
13775 return true;
13777 case TREE_LIST:
13778 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13779 return false;
13780 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13781 return false;
13782 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13784 case SAVE_EXPR:
13785 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13787 case CALL_EXPR:
13789 tree arg1, arg2;
13790 call_expr_arg_iterator iter1, iter2;
13791 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13792 return false;
13793 for (arg1 = first_call_expr_arg (t1, &iter1),
13794 arg2 = first_call_expr_arg (t2, &iter2);
13795 arg1 && arg2;
13796 arg1 = next_call_expr_arg (&iter1),
13797 arg2 = next_call_expr_arg (&iter2))
13798 if (!c_tree_equal (arg1, arg2))
13799 return false;
13800 if (arg1 || arg2)
13801 return false;
13802 return true;
13805 case TARGET_EXPR:
13807 tree o1 = TREE_OPERAND (t1, 0);
13808 tree o2 = TREE_OPERAND (t2, 0);
13810 /* Special case: if either target is an unallocated VAR_DECL,
13811 it means that it's going to be unified with whatever the
13812 TARGET_EXPR is really supposed to initialize, so treat it
13813 as being equivalent to anything. */
13814 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
13815 && !DECL_RTL_SET_P (o1))
13816 /*Nop*/;
13817 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
13818 && !DECL_RTL_SET_P (o2))
13819 /*Nop*/;
13820 else if (!c_tree_equal (o1, o2))
13821 return false;
13823 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13826 case COMPONENT_REF:
13827 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13828 return false;
13829 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13831 case PARM_DECL:
13832 case VAR_DECL:
13833 case CONST_DECL:
13834 case FIELD_DECL:
13835 case FUNCTION_DECL:
13836 case IDENTIFIER_NODE:
13837 case SSA_NAME:
13838 return false;
13840 case TREE_VEC:
13842 unsigned ix;
13843 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13844 return false;
13845 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13846 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13847 TREE_VEC_ELT (t2, ix)))
13848 return false;
13849 return true;
13852 default:
13853 break;
13856 switch (TREE_CODE_CLASS (code1))
13858 case tcc_unary:
13859 case tcc_binary:
13860 case tcc_comparison:
13861 case tcc_expression:
13862 case tcc_vl_exp:
13863 case tcc_reference:
13864 case tcc_statement:
13866 int i, n = TREE_OPERAND_LENGTH (t1);
13868 switch (code1)
13870 case PREINCREMENT_EXPR:
13871 case PREDECREMENT_EXPR:
13872 case POSTINCREMENT_EXPR:
13873 case POSTDECREMENT_EXPR:
13874 n = 1;
13875 break;
13876 case ARRAY_REF:
13877 n = 2;
13878 break;
13879 default:
13880 break;
13883 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13884 && n != TREE_OPERAND_LENGTH (t2))
13885 return false;
13887 for (i = 0; i < n; ++i)
13888 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13889 return false;
13891 return true;
13894 case tcc_type:
13895 return comptypes (t1, t2);
13896 default:
13897 gcc_unreachable ();
13899 /* We can get here with --disable-checking. */
13900 return false;
13903 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13904 spawn-helper and BODY is the newly created body for FNDECL. */
13906 void
13907 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13909 tree list = alloc_stmt_list ();
13910 tree frame = make_cilk_frame (fndecl);
13911 tree dtor = create_cilk_function_exit (frame, false, true);
13912 add_local_decl (cfun, frame);
13914 DECL_SAVED_TREE (fndecl) = list;
13915 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13916 frame);
13917 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13918 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13920 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13921 append_to_statement_list (detach_expr, &body_list);
13923 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13924 body = fold_build_cleanup_point_expr (void_type_node, body);
13926 append_to_statement_list (body, &body_list);
13927 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13928 body_list, dtor), &list);
13931 /* Returns true when the function declaration FNDECL is implicit,
13932 introduced as a result of a call to an otherwise undeclared
13933 function, and false otherwise. */
13935 bool
13936 c_decl_implicit (const_tree fndecl)
13938 return C_DECL_IMPLICIT (fndecl);