PR c/71552 - Confusing error for incorrect struct initialization
[official-gcc.git] / gcc / c / c-typeck.c
blob818ad944f3fc2fd5e8573100cb1f67134c04c596
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 if (result_type == error_mark_node)
4850 return error_mark_node;
4851 do_warn_double_promotion (result_type, type1, type2,
4852 "implicit conversion from %qT to %qT to "
4853 "match other result of conditional",
4854 colon_loc);
4856 /* If -Wsign-compare, warn here if type1 and type2 have
4857 different signedness. We'll promote the signed to unsigned
4858 and later code won't know it used to be different.
4859 Do this check on the original types, so that explicit casts
4860 will be considered, but default promotions won't. */
4861 if (c_inhibit_evaluation_warnings == 0)
4863 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4864 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4866 if (unsigned_op1 ^ unsigned_op2)
4868 bool ovf;
4870 /* Do not warn if the result type is signed, since the
4871 signed type will only be chosen if it can represent
4872 all the values of the unsigned type. */
4873 if (!TYPE_UNSIGNED (result_type))
4874 /* OK */;
4875 else
4877 bool op1_maybe_const = true;
4878 bool op2_maybe_const = true;
4880 /* Do not warn if the signed quantity is an
4881 unsuffixed integer literal (or some static
4882 constant expression involving such literals) and
4883 it is non-negative. This warning requires the
4884 operands to be folded for best results, so do
4885 that folding in this case even without
4886 warn_sign_compare to avoid warning options
4887 possibly affecting code generation. */
4888 c_inhibit_evaluation_warnings
4889 += (ifexp == truthvalue_false_node);
4890 op1 = c_fully_fold (op1, require_constant_value,
4891 &op1_maybe_const);
4892 c_inhibit_evaluation_warnings
4893 -= (ifexp == truthvalue_false_node);
4895 c_inhibit_evaluation_warnings
4896 += (ifexp == truthvalue_true_node);
4897 op2 = c_fully_fold (op2, require_constant_value,
4898 &op2_maybe_const);
4899 c_inhibit_evaluation_warnings
4900 -= (ifexp == truthvalue_true_node);
4902 if (warn_sign_compare)
4904 if ((unsigned_op2
4905 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4906 || (unsigned_op1
4907 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4908 /* OK */;
4909 else
4910 warning_at (colon_loc, OPT_Wsign_compare,
4911 ("signed and unsigned type in "
4912 "conditional expression"));
4914 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4915 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4916 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4917 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4922 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4924 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4925 pedwarn (colon_loc, OPT_Wpedantic,
4926 "ISO C forbids conditional expr with only one void side");
4927 result_type = void_type_node;
4929 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4931 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4932 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4933 addr_space_t as_common;
4935 if (comp_target_types (colon_loc, type1, type2))
4936 result_type = common_pointer_type (type1, type2);
4937 else if (null_pointer_constant_p (orig_op1))
4938 result_type = type2;
4939 else if (null_pointer_constant_p (orig_op2))
4940 result_type = type1;
4941 else if (!addr_space_superset (as1, as2, &as_common))
4943 error_at (colon_loc, "pointers to disjoint address spaces "
4944 "used in conditional expression");
4945 return error_mark_node;
4947 else if (VOID_TYPE_P (TREE_TYPE (type1))
4948 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4950 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4951 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4952 & ~TYPE_QUALS (TREE_TYPE (type1))))
4953 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4954 "pointer to array loses qualifier "
4955 "in conditional expression");
4957 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4958 pedwarn (colon_loc, OPT_Wpedantic,
4959 "ISO C forbids conditional expr between "
4960 "%<void *%> and function pointer");
4961 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4962 TREE_TYPE (type2)));
4964 else if (VOID_TYPE_P (TREE_TYPE (type2))
4965 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4967 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4968 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4969 & ~TYPE_QUALS (TREE_TYPE (type2))))
4970 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4971 "pointer to array loses qualifier "
4972 "in conditional expression");
4974 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4975 pedwarn (colon_loc, OPT_Wpedantic,
4976 "ISO C forbids conditional expr between "
4977 "%<void *%> and function pointer");
4978 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4979 TREE_TYPE (type1)));
4981 /* Objective-C pointer comparisons are a bit more lenient. */
4982 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4983 result_type = objc_common_type (type1, type2);
4984 else
4986 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4988 pedwarn (colon_loc, 0,
4989 "pointer type mismatch in conditional expression");
4990 result_type = build_pointer_type
4991 (build_qualified_type (void_type_node, qual));
4994 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4996 if (!null_pointer_constant_p (orig_op2))
4997 pedwarn (colon_loc, 0,
4998 "pointer/integer type mismatch in conditional expression");
4999 else
5001 op2 = null_pointer_node;
5003 result_type = type1;
5005 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5007 if (!null_pointer_constant_p (orig_op1))
5008 pedwarn (colon_loc, 0,
5009 "pointer/integer type mismatch in conditional expression");
5010 else
5012 op1 = null_pointer_node;
5014 result_type = type2;
5017 if (!result_type)
5019 if (flag_cond_mismatch)
5020 result_type = void_type_node;
5021 else
5023 error_at (colon_loc, "type mismatch in conditional expression");
5024 return error_mark_node;
5028 /* Merge const and volatile flags of the incoming types. */
5029 result_type
5030 = build_type_variant (result_type,
5031 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5032 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5034 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5035 semantic_result_type);
5036 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5037 semantic_result_type);
5039 if (ifexp_bcp && ifexp == truthvalue_true_node)
5041 op2_int_operands = true;
5042 op1 = c_fully_fold (op1, require_constant_value, NULL);
5044 if (ifexp_bcp && ifexp == truthvalue_false_node)
5046 op1_int_operands = true;
5047 op2 = c_fully_fold (op2, require_constant_value, NULL);
5049 int_const = int_operands = (ifexp_int_operands
5050 && op1_int_operands
5051 && op2_int_operands);
5052 if (int_operands)
5054 int_const = ((ifexp == truthvalue_true_node
5055 && TREE_CODE (orig_op1) == INTEGER_CST
5056 && !TREE_OVERFLOW (orig_op1))
5057 || (ifexp == truthvalue_false_node
5058 && TREE_CODE (orig_op2) == INTEGER_CST
5059 && !TREE_OVERFLOW (orig_op2)));
5062 /* Need to convert condition operand into a vector mask. */
5063 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5065 tree vectype = TREE_TYPE (ifexp);
5066 tree elem_type = TREE_TYPE (vectype);
5067 tree zero = build_int_cst (elem_type, 0);
5068 tree zero_vec = build_vector_from_val (vectype, zero);
5069 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5070 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5073 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5074 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5075 else
5077 if (int_operands)
5079 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5080 nested inside of the expression. */
5081 op1 = c_fully_fold (op1, false, NULL);
5082 op2 = c_fully_fold (op2, false, NULL);
5084 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5085 if (int_operands)
5086 ret = note_integer_operands (ret);
5088 if (semantic_result_type)
5089 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5091 protected_set_expr_location (ret, colon_loc);
5092 return ret;
5095 /* Return a compound expression that performs two expressions and
5096 returns the value of the second of them.
5098 LOC is the location of the COMPOUND_EXPR. */
5100 tree
5101 build_compound_expr (location_t loc, tree expr1, tree expr2)
5103 bool expr1_int_operands, expr2_int_operands;
5104 tree eptype = NULL_TREE;
5105 tree ret;
5107 if (flag_cilkplus
5108 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5109 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5111 error_at (loc,
5112 "spawned function call cannot be part of a comma expression");
5113 return error_mark_node;
5115 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5116 if (expr1_int_operands)
5117 expr1 = remove_c_maybe_const_expr (expr1);
5118 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5119 if (expr2_int_operands)
5120 expr2 = remove_c_maybe_const_expr (expr2);
5122 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5123 expr1 = TREE_OPERAND (expr1, 0);
5124 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5126 eptype = TREE_TYPE (expr2);
5127 expr2 = TREE_OPERAND (expr2, 0);
5130 if (!TREE_SIDE_EFFECTS (expr1))
5132 /* The left-hand operand of a comma expression is like an expression
5133 statement: with -Wunused, we should warn if it doesn't have
5134 any side-effects, unless it was explicitly cast to (void). */
5135 if (warn_unused_value)
5137 if (VOID_TYPE_P (TREE_TYPE (expr1))
5138 && CONVERT_EXPR_P (expr1))
5139 ; /* (void) a, b */
5140 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5141 && TREE_CODE (expr1) == COMPOUND_EXPR
5142 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5143 ; /* (void) a, (void) b, c */
5144 else
5145 warning_at (loc, OPT_Wunused_value,
5146 "left-hand operand of comma expression has no effect");
5149 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5150 && warn_unused_value)
5152 tree r = expr1;
5153 location_t cloc = loc;
5154 while (TREE_CODE (r) == COMPOUND_EXPR)
5156 if (EXPR_HAS_LOCATION (r))
5157 cloc = EXPR_LOCATION (r);
5158 r = TREE_OPERAND (r, 1);
5160 if (!TREE_SIDE_EFFECTS (r)
5161 && !VOID_TYPE_P (TREE_TYPE (r))
5162 && !CONVERT_EXPR_P (r))
5163 warning_at (cloc, OPT_Wunused_value,
5164 "right-hand operand of comma expression has no effect");
5167 /* With -Wunused, we should also warn if the left-hand operand does have
5168 side-effects, but computes a value which is not used. For example, in
5169 `foo() + bar(), baz()' the result of the `+' operator is not used,
5170 so we should issue a warning. */
5171 else if (warn_unused_value)
5172 warn_if_unused_value (expr1, loc);
5174 if (expr2 == error_mark_node)
5175 return error_mark_node;
5177 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5179 if (flag_isoc99
5180 && expr1_int_operands
5181 && expr2_int_operands)
5182 ret = note_integer_operands (ret);
5184 if (eptype)
5185 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5187 protected_set_expr_location (ret, loc);
5188 return ret;
5191 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5192 which we are casting. OTYPE is the type of the expression being
5193 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5194 of the cast. -Wcast-qual appeared on the command line. Named
5195 address space qualifiers are not handled here, because they result
5196 in different warnings. */
5198 static void
5199 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5201 tree in_type = type;
5202 tree in_otype = otype;
5203 int added = 0;
5204 int discarded = 0;
5205 bool is_const;
5207 /* Check that the qualifiers on IN_TYPE are a superset of the
5208 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5209 nodes is uninteresting and we stop as soon as we hit a
5210 non-POINTER_TYPE node on either type. */
5213 in_otype = TREE_TYPE (in_otype);
5214 in_type = TREE_TYPE (in_type);
5216 /* GNU C allows cv-qualified function types. 'const' means the
5217 function is very pure, 'volatile' means it can't return. We
5218 need to warn when such qualifiers are added, not when they're
5219 taken away. */
5220 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5221 && TREE_CODE (in_type) == FUNCTION_TYPE)
5222 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5223 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5224 else
5225 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5226 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5228 while (TREE_CODE (in_type) == POINTER_TYPE
5229 && TREE_CODE (in_otype) == POINTER_TYPE);
5231 if (added)
5232 warning_at (loc, OPT_Wcast_qual,
5233 "cast adds %q#v qualifier to function type", added);
5235 if (discarded)
5236 /* There are qualifiers present in IN_OTYPE that are not present
5237 in IN_TYPE. */
5238 warning_at (loc, OPT_Wcast_qual,
5239 "cast discards %qv qualifier from pointer target type",
5240 discarded);
5242 if (added || discarded)
5243 return;
5245 /* A cast from **T to const **T is unsafe, because it can cause a
5246 const value to be changed with no additional warning. We only
5247 issue this warning if T is the same on both sides, and we only
5248 issue the warning if there are the same number of pointers on
5249 both sides, as otherwise the cast is clearly unsafe anyhow. A
5250 cast is unsafe when a qualifier is added at one level and const
5251 is not present at all outer levels.
5253 To issue this warning, we check at each level whether the cast
5254 adds new qualifiers not already seen. We don't need to special
5255 case function types, as they won't have the same
5256 TYPE_MAIN_VARIANT. */
5258 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5259 return;
5260 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5261 return;
5263 in_type = type;
5264 in_otype = otype;
5265 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5268 in_type = TREE_TYPE (in_type);
5269 in_otype = TREE_TYPE (in_otype);
5270 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5271 && !is_const)
5273 warning_at (loc, OPT_Wcast_qual,
5274 "to be safe all intermediate pointers in cast from "
5275 "%qT to %qT must be %<const%> qualified",
5276 otype, type);
5277 break;
5279 if (is_const)
5280 is_const = TYPE_READONLY (in_type);
5282 while (TREE_CODE (in_type) == POINTER_TYPE);
5285 /* Build an expression representing a cast to type TYPE of expression EXPR.
5286 LOC is the location of the cast-- typically the open paren of the cast. */
5288 tree
5289 build_c_cast (location_t loc, tree type, tree expr)
5291 tree value;
5293 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5294 expr = TREE_OPERAND (expr, 0);
5296 value = expr;
5298 if (type == error_mark_node || expr == error_mark_node)
5299 return error_mark_node;
5301 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5302 only in <protocol> qualifications. But when constructing cast expressions,
5303 the protocols do matter and must be kept around. */
5304 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5305 return build1 (NOP_EXPR, type, expr);
5307 type = TYPE_MAIN_VARIANT (type);
5309 if (TREE_CODE (type) == ARRAY_TYPE)
5311 error_at (loc, "cast specifies array type");
5312 return error_mark_node;
5315 if (TREE_CODE (type) == FUNCTION_TYPE)
5317 error_at (loc, "cast specifies function type");
5318 return error_mark_node;
5321 if (!VOID_TYPE_P (type))
5323 value = require_complete_type (loc, value);
5324 if (value == error_mark_node)
5325 return error_mark_node;
5328 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5330 if (RECORD_OR_UNION_TYPE_P (type))
5331 pedwarn (loc, OPT_Wpedantic,
5332 "ISO C forbids casting nonscalar to the same type");
5334 /* Convert to remove any qualifiers from VALUE's type. */
5335 value = convert (type, value);
5337 else if (TREE_CODE (type) == UNION_TYPE)
5339 tree field;
5341 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5342 if (TREE_TYPE (field) != error_mark_node
5343 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5344 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5345 break;
5347 if (field)
5349 tree t;
5350 bool maybe_const = true;
5352 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5353 t = c_fully_fold (value, false, &maybe_const);
5354 t = build_constructor_single (type, field, t);
5355 if (!maybe_const)
5356 t = c_wrap_maybe_const (t, true);
5357 t = digest_init (loc, type, t,
5358 NULL_TREE, false, true, 0);
5359 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5360 return t;
5362 error_at (loc, "cast to union type from type not present in union");
5363 return error_mark_node;
5365 else
5367 tree otype, ovalue;
5369 if (type == void_type_node)
5371 tree t = build1 (CONVERT_EXPR, type, value);
5372 SET_EXPR_LOCATION (t, loc);
5373 return t;
5376 otype = TREE_TYPE (value);
5378 /* Optionally warn about potentially worrisome casts. */
5379 if (warn_cast_qual
5380 && TREE_CODE (type) == POINTER_TYPE
5381 && TREE_CODE (otype) == POINTER_TYPE)
5382 handle_warn_cast_qual (loc, type, otype);
5384 /* Warn about conversions between pointers to disjoint
5385 address spaces. */
5386 if (TREE_CODE (type) == POINTER_TYPE
5387 && TREE_CODE (otype) == POINTER_TYPE
5388 && !null_pointer_constant_p (value))
5390 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5391 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5392 addr_space_t as_common;
5394 if (!addr_space_superset (as_to, as_from, &as_common))
5396 if (ADDR_SPACE_GENERIC_P (as_from))
5397 warning_at (loc, 0, "cast to %s address space pointer "
5398 "from disjoint generic address space pointer",
5399 c_addr_space_name (as_to));
5401 else if (ADDR_SPACE_GENERIC_P (as_to))
5402 warning_at (loc, 0, "cast to generic address space pointer "
5403 "from disjoint %s address space pointer",
5404 c_addr_space_name (as_from));
5406 else
5407 warning_at (loc, 0, "cast to %s address space pointer "
5408 "from disjoint %s address space pointer",
5409 c_addr_space_name (as_to),
5410 c_addr_space_name (as_from));
5414 /* Warn about possible alignment problems. */
5415 if (STRICT_ALIGNMENT
5416 && TREE_CODE (type) == POINTER_TYPE
5417 && TREE_CODE (otype) == POINTER_TYPE
5418 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5419 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5420 /* Don't warn about opaque types, where the actual alignment
5421 restriction is unknown. */
5422 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5423 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5424 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5425 warning_at (loc, OPT_Wcast_align,
5426 "cast increases required alignment of target type");
5428 if (TREE_CODE (type) == INTEGER_TYPE
5429 && TREE_CODE (otype) == POINTER_TYPE
5430 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5431 /* Unlike conversion of integers to pointers, where the
5432 warning is disabled for converting constants because
5433 of cases such as SIG_*, warn about converting constant
5434 pointers to integers. In some cases it may cause unwanted
5435 sign extension, and a warning is appropriate. */
5436 warning_at (loc, OPT_Wpointer_to_int_cast,
5437 "cast from pointer to integer of different size");
5439 if (TREE_CODE (value) == CALL_EXPR
5440 && TREE_CODE (type) != TREE_CODE (otype))
5441 warning_at (loc, OPT_Wbad_function_cast,
5442 "cast from function call of type %qT "
5443 "to non-matching type %qT", otype, type);
5445 if (TREE_CODE (type) == POINTER_TYPE
5446 && TREE_CODE (otype) == INTEGER_TYPE
5447 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5448 /* Don't warn about converting any constant. */
5449 && !TREE_CONSTANT (value))
5450 warning_at (loc,
5451 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5452 "of different size");
5454 if (warn_strict_aliasing <= 2)
5455 strict_aliasing_warning (otype, type, expr);
5457 /* If pedantic, warn for conversions between function and object
5458 pointer types, except for converting a null pointer constant
5459 to function pointer type. */
5460 if (pedantic
5461 && TREE_CODE (type) == POINTER_TYPE
5462 && TREE_CODE (otype) == POINTER_TYPE
5463 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5464 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5465 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5466 "conversion of function pointer to object pointer type");
5468 if (pedantic
5469 && TREE_CODE (type) == POINTER_TYPE
5470 && TREE_CODE (otype) == POINTER_TYPE
5471 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5472 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5473 && !null_pointer_constant_p (value))
5474 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5475 "conversion of object pointer to function pointer type");
5477 ovalue = value;
5478 value = convert (type, value);
5480 /* Ignore any integer overflow caused by the cast. */
5481 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5483 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5485 if (!TREE_OVERFLOW (value))
5487 /* Avoid clobbering a shared constant. */
5488 value = copy_node (value);
5489 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5492 else if (TREE_OVERFLOW (value))
5493 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5494 value = wide_int_to_tree (TREE_TYPE (value), value);
5498 /* Don't let a cast be an lvalue. */
5499 if (lvalue_p (value))
5500 value = non_lvalue_loc (loc, value);
5502 /* Don't allow the results of casting to floating-point or complex
5503 types be confused with actual constants, or casts involving
5504 integer and pointer types other than direct integer-to-integer
5505 and integer-to-pointer be confused with integer constant
5506 expressions and null pointer constants. */
5507 if (TREE_CODE (value) == REAL_CST
5508 || TREE_CODE (value) == COMPLEX_CST
5509 || (TREE_CODE (value) == INTEGER_CST
5510 && !((TREE_CODE (expr) == INTEGER_CST
5511 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5512 || TREE_CODE (expr) == REAL_CST
5513 || TREE_CODE (expr) == COMPLEX_CST)))
5514 value = build1 (NOP_EXPR, type, value);
5516 protected_set_expr_location (value, loc);
5517 return value;
5520 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5521 location of the open paren of the cast, or the position of the cast
5522 expr. */
5523 tree
5524 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5526 tree type;
5527 tree type_expr = NULL_TREE;
5528 bool type_expr_const = true;
5529 tree ret;
5530 int saved_wsp = warn_strict_prototypes;
5532 /* This avoids warnings about unprototyped casts on
5533 integers. E.g. "#define SIG_DFL (void(*)())0". */
5534 if (TREE_CODE (expr) == INTEGER_CST)
5535 warn_strict_prototypes = 0;
5536 type = groktypename (type_name, &type_expr, &type_expr_const);
5537 warn_strict_prototypes = saved_wsp;
5539 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5540 && reject_gcc_builtin (expr))
5541 return error_mark_node;
5543 ret = build_c_cast (loc, type, expr);
5544 if (type_expr)
5546 bool inner_expr_const = true;
5547 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5548 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5549 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5550 && inner_expr_const);
5551 SET_EXPR_LOCATION (ret, loc);
5554 if (!EXPR_HAS_LOCATION (ret))
5555 protected_set_expr_location (ret, loc);
5557 /* C++ does not permits types to be defined in a cast, but it
5558 allows references to incomplete types. */
5559 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5560 warning_at (loc, OPT_Wc___compat,
5561 "defining a type in a cast is invalid in C++");
5563 return ret;
5566 /* Build an assignment expression of lvalue LHS from value RHS.
5567 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5568 may differ from TREE_TYPE (LHS) for an enum bitfield.
5569 MODIFYCODE is the code for a binary operator that we use
5570 to combine the old value of LHS with RHS to get the new value.
5571 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5572 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5573 which may differ from TREE_TYPE (RHS) for an enum value.
5575 LOCATION is the location of the MODIFYCODE operator.
5576 RHS_LOC is the location of the RHS. */
5578 tree
5579 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5580 enum tree_code modifycode,
5581 location_t rhs_loc, tree rhs, tree rhs_origtype)
5583 tree result;
5584 tree newrhs;
5585 tree rhseval = NULL_TREE;
5586 tree rhs_semantic_type = NULL_TREE;
5587 tree lhstype = TREE_TYPE (lhs);
5588 tree olhstype = lhstype;
5589 bool npc;
5590 bool is_atomic_op;
5592 /* Types that aren't fully specified cannot be used in assignments. */
5593 lhs = require_complete_type (location, lhs);
5595 /* Avoid duplicate error messages from operands that had errors. */
5596 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5597 return error_mark_node;
5599 /* Ensure an error for assigning a non-lvalue array to an array in
5600 C90. */
5601 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5603 error_at (location, "assignment to expression with array type");
5604 return error_mark_node;
5607 /* For ObjC properties, defer this check. */
5608 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5609 return error_mark_node;
5611 is_atomic_op = really_atomic_lvalue (lhs);
5613 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5615 rhs_semantic_type = TREE_TYPE (rhs);
5616 rhs = TREE_OPERAND (rhs, 0);
5619 newrhs = rhs;
5621 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5623 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5624 lhs_origtype, modifycode, rhs_loc, rhs,
5625 rhs_origtype);
5626 if (inner == error_mark_node)
5627 return error_mark_node;
5628 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5629 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5630 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5631 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5632 protected_set_expr_location (result, location);
5633 return result;
5636 /* If a binary op has been requested, combine the old LHS value with the RHS
5637 producing the value we should actually store into the LHS. */
5639 if (modifycode != NOP_EXPR)
5641 lhs = c_fully_fold (lhs, false, NULL);
5642 lhs = stabilize_reference (lhs);
5644 /* Construct the RHS for any non-atomic compound assignemnt. */
5645 if (!is_atomic_op)
5647 /* If in LHS op= RHS the RHS has side-effects, ensure they
5648 are preevaluated before the rest of the assignment expression's
5649 side-effects, because RHS could contain e.g. function calls
5650 that modify LHS. */
5651 if (TREE_SIDE_EFFECTS (rhs))
5653 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5654 rhseval = newrhs;
5656 newrhs = build_binary_op (location,
5657 modifycode, lhs, newrhs, 1);
5659 /* The original type of the right hand side is no longer
5660 meaningful. */
5661 rhs_origtype = NULL_TREE;
5665 if (c_dialect_objc ())
5667 /* Check if we are modifying an Objective-C property reference;
5668 if so, we need to generate setter calls. */
5669 result = objc_maybe_build_modify_expr (lhs, newrhs);
5670 if (result)
5671 goto return_result;
5673 /* Else, do the check that we postponed for Objective-C. */
5674 if (!lvalue_or_else (location, lhs, lv_assign))
5675 return error_mark_node;
5678 /* Give an error for storing in something that is 'const'. */
5680 if (TYPE_READONLY (lhstype)
5681 || (RECORD_OR_UNION_TYPE_P (lhstype)
5682 && C_TYPE_FIELDS_READONLY (lhstype)))
5684 readonly_error (location, lhs, lv_assign);
5685 return error_mark_node;
5687 else if (TREE_READONLY (lhs))
5688 readonly_warning (lhs, lv_assign);
5690 /* If storing into a structure or union member,
5691 it has probably been given type `int'.
5692 Compute the type that would go with
5693 the actual amount of storage the member occupies. */
5695 if (TREE_CODE (lhs) == COMPONENT_REF
5696 && (TREE_CODE (lhstype) == INTEGER_TYPE
5697 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5698 || TREE_CODE (lhstype) == REAL_TYPE
5699 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5700 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5702 /* If storing in a field that is in actuality a short or narrower than one,
5703 we must store in the field in its actual type. */
5705 if (lhstype != TREE_TYPE (lhs))
5707 lhs = copy_node (lhs);
5708 TREE_TYPE (lhs) = lhstype;
5711 /* Issue -Wc++-compat warnings about an assignment to an enum type
5712 when LHS does not have its original type. This happens for,
5713 e.g., an enum bitfield in a struct. */
5714 if (warn_cxx_compat
5715 && lhs_origtype != NULL_TREE
5716 && lhs_origtype != lhstype
5717 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5719 tree checktype = (rhs_origtype != NULL_TREE
5720 ? rhs_origtype
5721 : TREE_TYPE (rhs));
5722 if (checktype != error_mark_node
5723 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5724 || (is_atomic_op && modifycode != NOP_EXPR)))
5725 warning_at (location, OPT_Wc___compat,
5726 "enum conversion in assignment is invalid in C++");
5729 /* If the lhs is atomic, remove that qualifier. */
5730 if (is_atomic_op)
5732 lhstype = build_qualified_type (lhstype,
5733 (TYPE_QUALS (lhstype)
5734 & ~TYPE_QUAL_ATOMIC));
5735 olhstype = build_qualified_type (olhstype,
5736 (TYPE_QUALS (lhstype)
5737 & ~TYPE_QUAL_ATOMIC));
5740 /* Convert new value to destination type. Fold it first, then
5741 restore any excess precision information, for the sake of
5742 conversion warnings. */
5744 if (!(is_atomic_op && modifycode != NOP_EXPR))
5746 npc = null_pointer_constant_p (newrhs);
5747 newrhs = c_fully_fold (newrhs, false, NULL);
5748 if (rhs_semantic_type)
5749 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5750 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5751 rhs_origtype, ic_assign, npc,
5752 NULL_TREE, NULL_TREE, 0);
5753 if (TREE_CODE (newrhs) == ERROR_MARK)
5754 return error_mark_node;
5757 /* Emit ObjC write barrier, if necessary. */
5758 if (c_dialect_objc () && flag_objc_gc)
5760 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5761 if (result)
5763 protected_set_expr_location (result, location);
5764 goto return_result;
5768 /* Scan operands. */
5770 if (is_atomic_op)
5771 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5772 else
5774 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5775 TREE_SIDE_EFFECTS (result) = 1;
5776 protected_set_expr_location (result, location);
5779 /* If we got the LHS in a different type for storing in,
5780 convert the result back to the nominal type of LHS
5781 so that the value we return always has the same type
5782 as the LHS argument. */
5784 if (olhstype == TREE_TYPE (result))
5785 goto return_result;
5787 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5788 rhs_origtype, ic_assign, false, NULL_TREE,
5789 NULL_TREE, 0);
5790 protected_set_expr_location (result, location);
5792 return_result:
5793 if (rhseval)
5794 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5795 return result;
5798 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5799 This is used to implement -fplan9-extensions. */
5801 static bool
5802 find_anonymous_field_with_type (tree struct_type, tree type)
5804 tree field;
5805 bool found;
5807 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5808 found = false;
5809 for (field = TYPE_FIELDS (struct_type);
5810 field != NULL_TREE;
5811 field = TREE_CHAIN (field))
5813 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5814 ? c_build_qualified_type (TREE_TYPE (field),
5815 TYPE_QUAL_ATOMIC)
5816 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5817 if (DECL_NAME (field) == NULL
5818 && comptypes (type, fieldtype))
5820 if (found)
5821 return false;
5822 found = true;
5824 else if (DECL_NAME (field) == NULL
5825 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5826 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5828 if (found)
5829 return false;
5830 found = true;
5833 return found;
5836 /* RHS is an expression whose type is pointer to struct. If there is
5837 an anonymous field in RHS with type TYPE, then return a pointer to
5838 that field in RHS. This is used with -fplan9-extensions. This
5839 returns NULL if no conversion could be found. */
5841 static tree
5842 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5844 tree rhs_struct_type, lhs_main_type;
5845 tree field, found_field;
5846 bool found_sub_field;
5847 tree ret;
5849 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5850 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5851 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5853 gcc_assert (POINTER_TYPE_P (type));
5854 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5855 ? c_build_qualified_type (TREE_TYPE (type),
5856 TYPE_QUAL_ATOMIC)
5857 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5859 found_field = NULL_TREE;
5860 found_sub_field = false;
5861 for (field = TYPE_FIELDS (rhs_struct_type);
5862 field != NULL_TREE;
5863 field = TREE_CHAIN (field))
5865 if (DECL_NAME (field) != NULL_TREE
5866 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5867 continue;
5868 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5869 ? c_build_qualified_type (TREE_TYPE (field),
5870 TYPE_QUAL_ATOMIC)
5871 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5872 if (comptypes (lhs_main_type, fieldtype))
5874 if (found_field != NULL_TREE)
5875 return NULL_TREE;
5876 found_field = field;
5878 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5879 lhs_main_type))
5881 if (found_field != NULL_TREE)
5882 return NULL_TREE;
5883 found_field = field;
5884 found_sub_field = true;
5888 if (found_field == NULL_TREE)
5889 return NULL_TREE;
5891 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5892 build_fold_indirect_ref (rhs), found_field,
5893 NULL_TREE);
5894 ret = build_fold_addr_expr_loc (location, ret);
5896 if (found_sub_field)
5898 ret = convert_to_anonymous_field (location, type, ret);
5899 gcc_assert (ret != NULL_TREE);
5902 return ret;
5905 /* Issue an error message for a bad initializer component.
5906 GMSGID identifies the message.
5907 The component name is taken from the spelling stack. */
5909 static void
5910 error_init (location_t loc, const char *gmsgid)
5912 char *ofwhat;
5914 /* The gmsgid may be a format string with %< and %>. */
5915 error_at (loc, gmsgid);
5916 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5917 if (*ofwhat)
5918 inform (loc, "(near initialization for %qs)", ofwhat);
5921 /* Issue a pedantic warning for a bad initializer component. OPT is
5922 the option OPT_* (from options.h) controlling this warning or 0 if
5923 it is unconditionally given. GMSGID identifies the message. The
5924 component name is taken from the spelling stack. */
5926 static void
5927 pedwarn_init (location_t loc, int opt, const char *gmsgid)
5929 char *ofwhat;
5930 bool warned;
5932 /* Use the location where a macro was expanded rather than where
5933 it was defined to make sure macros defined in system headers
5934 but used incorrectly elsewhere are diagnosed. */
5935 source_location exploc = expansion_point_location_if_in_system_header (loc);
5937 /* The gmsgid may be a format string with %< and %>. */
5938 warned = pedwarn (exploc, opt, gmsgid);
5939 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5940 if (*ofwhat && warned)
5941 inform (exploc, "(near initialization for %qs)", ofwhat);
5944 /* Issue a warning for a bad initializer component.
5946 OPT is the OPT_W* value corresponding to the warning option that
5947 controls this warning. GMSGID identifies the message. The
5948 component name is taken from the spelling stack. */
5950 static void
5951 warning_init (location_t loc, int opt, const char *gmsgid)
5953 char *ofwhat;
5954 bool warned;
5956 /* Use the location where a macro was expanded rather than where
5957 it was defined to make sure macros defined in system headers
5958 but used incorrectly elsewhere are diagnosed. */
5959 source_location exploc = expansion_point_location_if_in_system_header (loc);
5961 /* The gmsgid may be a format string with %< and %>. */
5962 warned = warning_at (exploc, opt, gmsgid);
5963 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5964 if (*ofwhat && warned)
5965 inform (exploc, "(near initialization for %qs)", ofwhat);
5968 /* If TYPE is an array type and EXPR is a parenthesized string
5969 constant, warn if pedantic that EXPR is being used to initialize an
5970 object of type TYPE. */
5972 void
5973 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5975 if (pedantic
5976 && TREE_CODE (type) == ARRAY_TYPE
5977 && TREE_CODE (expr.value) == STRING_CST
5978 && expr.original_code != STRING_CST)
5979 pedwarn_init (loc, OPT_Wpedantic,
5980 "array initialized from parenthesized string constant");
5983 /* Convert value RHS to type TYPE as preparation for an assignment to
5984 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5985 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5986 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5987 constant before any folding.
5988 The real work of conversion is done by `convert'.
5989 The purpose of this function is to generate error messages
5990 for assignments that are not allowed in C.
5991 ERRTYPE says whether it is argument passing, assignment,
5992 initialization or return.
5994 In the following example, '~' denotes where EXPR_LOC and '^' where
5995 LOCATION point to:
5997 f (var); [ic_argpass]
5998 ^ ~~~
5999 x = var; [ic_assign]
6000 ^ ~~~;
6001 int x = var; [ic_init]
6003 return x; [ic_return]
6006 FUNCTION is a tree for the function being called.
6007 PARMNUM is the number of the argument, for printing in error messages. */
6009 static tree
6010 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6011 tree rhs, tree origtype, enum impl_conv errtype,
6012 bool null_pointer_constant, tree fundecl,
6013 tree function, int parmnum)
6015 enum tree_code codel = TREE_CODE (type);
6016 tree orig_rhs = rhs;
6017 tree rhstype;
6018 enum tree_code coder;
6019 tree rname = NULL_TREE;
6020 bool objc_ok = false;
6022 /* Use the expansion point location to handle cases such as user's
6023 function returning a wrong-type macro defined in a system header. */
6024 location = expansion_point_location_if_in_system_header (location);
6026 if (errtype == ic_argpass)
6028 tree selector;
6029 /* Change pointer to function to the function itself for
6030 diagnostics. */
6031 if (TREE_CODE (function) == ADDR_EXPR
6032 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6033 function = TREE_OPERAND (function, 0);
6035 /* Handle an ObjC selector specially for diagnostics. */
6036 selector = objc_message_selector ();
6037 rname = function;
6038 if (selector && parmnum > 2)
6040 rname = selector;
6041 parmnum -= 2;
6045 /* This macro is used to emit diagnostics to ensure that all format
6046 strings are complete sentences, visible to gettext and checked at
6047 compile time. */
6048 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6049 do { \
6050 switch (errtype) \
6052 case ic_argpass: \
6053 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6054 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6055 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6056 "expected %qT but argument is of type %qT", \
6057 type, rhstype); \
6058 break; \
6059 case ic_assign: \
6060 pedwarn (LOCATION, OPT, AS); \
6061 break; \
6062 case ic_init: \
6063 pedwarn_init (LOCATION, OPT, IN); \
6064 break; \
6065 case ic_return: \
6066 pedwarn (LOCATION, OPT, RE); \
6067 break; \
6068 default: \
6069 gcc_unreachable (); \
6071 } while (0)
6073 /* This macro is used to emit diagnostics to ensure that all format
6074 strings are complete sentences, visible to gettext and checked at
6075 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6076 extra parameter to enumerate qualifiers. */
6077 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6078 do { \
6079 switch (errtype) \
6081 case ic_argpass: \
6082 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6083 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6084 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6085 "expected %qT but argument is of type %qT", \
6086 type, rhstype); \
6087 break; \
6088 case ic_assign: \
6089 pedwarn (LOCATION, OPT, AS, QUALS); \
6090 break; \
6091 case ic_init: \
6092 pedwarn (LOCATION, OPT, IN, QUALS); \
6093 break; \
6094 case ic_return: \
6095 pedwarn (LOCATION, OPT, RE, QUALS); \
6096 break; \
6097 default: \
6098 gcc_unreachable (); \
6100 } while (0)
6102 /* This macro is used to emit diagnostics to ensure that all format
6103 strings are complete sentences, visible to gettext and checked at
6104 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6105 warning_at instead of pedwarn. */
6106 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6107 do { \
6108 switch (errtype) \
6110 case ic_argpass: \
6111 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6112 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6113 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6114 "expected %qT but argument is of type %qT", \
6115 type, rhstype); \
6116 break; \
6117 case ic_assign: \
6118 warning_at (LOCATION, OPT, AS, QUALS); \
6119 break; \
6120 case ic_init: \
6121 warning_at (LOCATION, OPT, IN, QUALS); \
6122 break; \
6123 case ic_return: \
6124 warning_at (LOCATION, OPT, RE, QUALS); \
6125 break; \
6126 default: \
6127 gcc_unreachable (); \
6129 } while (0)
6131 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6132 rhs = TREE_OPERAND (rhs, 0);
6134 rhstype = TREE_TYPE (rhs);
6135 coder = TREE_CODE (rhstype);
6137 if (coder == ERROR_MARK)
6138 return error_mark_node;
6140 if (c_dialect_objc ())
6142 int parmno;
6144 switch (errtype)
6146 case ic_return:
6147 parmno = 0;
6148 break;
6150 case ic_assign:
6151 parmno = -1;
6152 break;
6154 case ic_init:
6155 parmno = -2;
6156 break;
6158 default:
6159 parmno = parmnum;
6160 break;
6163 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6166 if (warn_cxx_compat)
6168 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6169 if (checktype != error_mark_node
6170 && TREE_CODE (type) == ENUMERAL_TYPE
6171 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6173 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6174 G_("enum conversion when passing argument "
6175 "%d of %qE is invalid in C++"),
6176 G_("enum conversion in assignment is "
6177 "invalid in C++"),
6178 G_("enum conversion in initialization is "
6179 "invalid in C++"),
6180 G_("enum conversion in return is "
6181 "invalid in C++"));
6185 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6186 return rhs;
6188 if (coder == VOID_TYPE)
6190 /* Except for passing an argument to an unprototyped function,
6191 this is a constraint violation. When passing an argument to
6192 an unprototyped function, it is compile-time undefined;
6193 making it a constraint in that case was rejected in
6194 DR#252. */
6195 error_at (location, "void value not ignored as it ought to be");
6196 return error_mark_node;
6198 rhs = require_complete_type (location, rhs);
6199 if (rhs == error_mark_node)
6200 return error_mark_node;
6202 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6203 return error_mark_node;
6205 /* A non-reference type can convert to a reference. This handles
6206 va_start, va_copy and possibly port built-ins. */
6207 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6209 if (!lvalue_p (rhs))
6211 error_at (location, "cannot pass rvalue to reference parameter");
6212 return error_mark_node;
6214 if (!c_mark_addressable (rhs))
6215 return error_mark_node;
6216 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6217 SET_EXPR_LOCATION (rhs, location);
6219 rhs = convert_for_assignment (location, expr_loc,
6220 build_pointer_type (TREE_TYPE (type)),
6221 rhs, origtype, errtype,
6222 null_pointer_constant, fundecl, function,
6223 parmnum);
6224 if (rhs == error_mark_node)
6225 return error_mark_node;
6227 rhs = build1 (NOP_EXPR, type, rhs);
6228 SET_EXPR_LOCATION (rhs, location);
6229 return rhs;
6231 /* Some types can interconvert without explicit casts. */
6232 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6233 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6234 return convert (type, rhs);
6235 /* Arithmetic types all interconvert, and enum is treated like int. */
6236 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6237 || codel == FIXED_POINT_TYPE
6238 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6239 || codel == BOOLEAN_TYPE)
6240 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6241 || coder == FIXED_POINT_TYPE
6242 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6243 || coder == BOOLEAN_TYPE))
6245 tree ret;
6246 bool save = in_late_binary_op;
6247 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6248 || (coder == REAL_TYPE
6249 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6250 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6251 in_late_binary_op = true;
6252 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6253 ? expr_loc : location, type, orig_rhs);
6254 in_late_binary_op = save;
6255 return ret;
6258 /* Aggregates in different TUs might need conversion. */
6259 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6260 && codel == coder
6261 && comptypes (type, rhstype))
6262 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6263 ? expr_loc : location, type, rhs);
6265 /* Conversion to a transparent union or record from its member types.
6266 This applies only to function arguments. */
6267 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6268 && TYPE_TRANSPARENT_AGGR (type))
6269 && errtype == ic_argpass)
6271 tree memb, marginal_memb = NULL_TREE;
6273 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6275 tree memb_type = TREE_TYPE (memb);
6277 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6278 TYPE_MAIN_VARIANT (rhstype)))
6279 break;
6281 if (TREE_CODE (memb_type) != POINTER_TYPE)
6282 continue;
6284 if (coder == POINTER_TYPE)
6286 tree ttl = TREE_TYPE (memb_type);
6287 tree ttr = TREE_TYPE (rhstype);
6289 /* Any non-function converts to a [const][volatile] void *
6290 and vice versa; otherwise, targets must be the same.
6291 Meanwhile, the lhs target must have all the qualifiers of
6292 the rhs. */
6293 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6294 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6295 || comp_target_types (location, memb_type, rhstype))
6297 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6298 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6299 /* If this type won't generate any warnings, use it. */
6300 if (lquals == rquals
6301 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6302 && TREE_CODE (ttl) == FUNCTION_TYPE)
6303 ? ((lquals | rquals) == rquals)
6304 : ((lquals | rquals) == lquals)))
6305 break;
6307 /* Keep looking for a better type, but remember this one. */
6308 if (!marginal_memb)
6309 marginal_memb = memb;
6313 /* Can convert integer zero to any pointer type. */
6314 if (null_pointer_constant)
6316 rhs = null_pointer_node;
6317 break;
6321 if (memb || marginal_memb)
6323 if (!memb)
6325 /* We have only a marginally acceptable member type;
6326 it needs a warning. */
6327 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6328 tree ttr = TREE_TYPE (rhstype);
6330 /* Const and volatile mean something different for function
6331 types, so the usual warnings are not appropriate. */
6332 if (TREE_CODE (ttr) == FUNCTION_TYPE
6333 && TREE_CODE (ttl) == FUNCTION_TYPE)
6335 /* Because const and volatile on functions are
6336 restrictions that say the function will not do
6337 certain things, it is okay to use a const or volatile
6338 function where an ordinary one is wanted, but not
6339 vice-versa. */
6340 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6341 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6342 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6343 OPT_Wdiscarded_qualifiers,
6344 G_("passing argument %d of %qE "
6345 "makes %q#v qualified function "
6346 "pointer from unqualified"),
6347 G_("assignment makes %q#v qualified "
6348 "function pointer from "
6349 "unqualified"),
6350 G_("initialization makes %q#v qualified "
6351 "function pointer from "
6352 "unqualified"),
6353 G_("return makes %q#v qualified function "
6354 "pointer from unqualified"),
6355 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6357 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6358 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6359 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6360 OPT_Wdiscarded_qualifiers,
6361 G_("passing argument %d of %qE discards "
6362 "%qv qualifier from pointer target type"),
6363 G_("assignment discards %qv qualifier "
6364 "from pointer target type"),
6365 G_("initialization discards %qv qualifier "
6366 "from pointer target type"),
6367 G_("return discards %qv qualifier from "
6368 "pointer target type"),
6369 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6371 memb = marginal_memb;
6374 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6375 pedwarn (location, OPT_Wpedantic,
6376 "ISO C prohibits argument conversion to union type");
6378 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6379 return build_constructor_single (type, memb, rhs);
6383 /* Conversions among pointers */
6384 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6385 && (coder == codel))
6387 tree ttl = TREE_TYPE (type);
6388 tree ttr = TREE_TYPE (rhstype);
6389 tree mvl = ttl;
6390 tree mvr = ttr;
6391 bool is_opaque_pointer;
6392 int target_cmp = 0; /* Cache comp_target_types () result. */
6393 addr_space_t asl;
6394 addr_space_t asr;
6396 if (TREE_CODE (mvl) != ARRAY_TYPE)
6397 mvl = (TYPE_ATOMIC (mvl)
6398 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6399 TYPE_QUAL_ATOMIC)
6400 : TYPE_MAIN_VARIANT (mvl));
6401 if (TREE_CODE (mvr) != ARRAY_TYPE)
6402 mvr = (TYPE_ATOMIC (mvr)
6403 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6404 TYPE_QUAL_ATOMIC)
6405 : TYPE_MAIN_VARIANT (mvr));
6406 /* Opaque pointers are treated like void pointers. */
6407 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6409 /* The Plan 9 compiler permits a pointer to a struct to be
6410 automatically converted into a pointer to an anonymous field
6411 within the struct. */
6412 if (flag_plan9_extensions
6413 && RECORD_OR_UNION_TYPE_P (mvl)
6414 && RECORD_OR_UNION_TYPE_P (mvr)
6415 && mvl != mvr)
6417 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6418 if (new_rhs != NULL_TREE)
6420 rhs = new_rhs;
6421 rhstype = TREE_TYPE (rhs);
6422 coder = TREE_CODE (rhstype);
6423 ttr = TREE_TYPE (rhstype);
6424 mvr = TYPE_MAIN_VARIANT (ttr);
6428 /* C++ does not allow the implicit conversion void* -> T*. However,
6429 for the purpose of reducing the number of false positives, we
6430 tolerate the special case of
6432 int *p = NULL;
6434 where NULL is typically defined in C to be '(void *) 0'. */
6435 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6436 warning_at (errtype == ic_argpass ? expr_loc : location,
6437 OPT_Wc___compat,
6438 "request for implicit conversion "
6439 "from %qT to %qT not permitted in C++", rhstype, type);
6441 /* See if the pointers point to incompatible address spaces. */
6442 asl = TYPE_ADDR_SPACE (ttl);
6443 asr = TYPE_ADDR_SPACE (ttr);
6444 if (!null_pointer_constant_p (rhs)
6445 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6447 switch (errtype)
6449 case ic_argpass:
6450 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6451 "non-enclosed address space", parmnum, rname);
6452 break;
6453 case ic_assign:
6454 error_at (location, "assignment from pointer to "
6455 "non-enclosed address space");
6456 break;
6457 case ic_init:
6458 error_at (location, "initialization from pointer to "
6459 "non-enclosed address space");
6460 break;
6461 case ic_return:
6462 error_at (location, "return from pointer to "
6463 "non-enclosed address space");
6464 break;
6465 default:
6466 gcc_unreachable ();
6468 return error_mark_node;
6471 /* Check if the right-hand side has a format attribute but the
6472 left-hand side doesn't. */
6473 if (warn_suggest_attribute_format
6474 && check_missing_format_attribute (type, rhstype))
6476 switch (errtype)
6478 case ic_argpass:
6479 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6480 "argument %d of %qE might be "
6481 "a candidate for a format attribute",
6482 parmnum, rname);
6483 break;
6484 case ic_assign:
6485 warning_at (location, OPT_Wsuggest_attribute_format,
6486 "assignment left-hand side might be "
6487 "a candidate for a format attribute");
6488 break;
6489 case ic_init:
6490 warning_at (location, OPT_Wsuggest_attribute_format,
6491 "initialization left-hand side might be "
6492 "a candidate for a format attribute");
6493 break;
6494 case ic_return:
6495 warning_at (location, OPT_Wsuggest_attribute_format,
6496 "return type might be "
6497 "a candidate for a format attribute");
6498 break;
6499 default:
6500 gcc_unreachable ();
6504 /* Any non-function converts to a [const][volatile] void *
6505 and vice versa; otherwise, targets must be the same.
6506 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6507 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6508 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6509 || (target_cmp = comp_target_types (location, type, rhstype))
6510 || is_opaque_pointer
6511 || ((c_common_unsigned_type (mvl)
6512 == c_common_unsigned_type (mvr))
6513 && (c_common_signed_type (mvl)
6514 == c_common_signed_type (mvr))
6515 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6517 /* Warn about loss of qualifers from pointers to arrays with
6518 qualifiers on the element type. */
6519 if (TREE_CODE (ttr) == ARRAY_TYPE)
6521 ttr = strip_array_types (ttr);
6522 ttl = strip_array_types (ttl);
6524 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6525 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6526 WARNING_FOR_QUALIFIERS (location, expr_loc,
6527 OPT_Wdiscarded_array_qualifiers,
6528 G_("passing argument %d of %qE discards "
6529 "%qv qualifier from pointer target type"),
6530 G_("assignment discards %qv qualifier "
6531 "from pointer target type"),
6532 G_("initialization discards %qv qualifier "
6533 "from pointer target type"),
6534 G_("return discards %qv qualifier from "
6535 "pointer target type"),
6536 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6538 else if (pedantic
6539 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6541 (VOID_TYPE_P (ttr)
6542 && !null_pointer_constant
6543 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6544 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6545 G_("ISO C forbids passing argument %d of "
6546 "%qE between function pointer "
6547 "and %<void *%>"),
6548 G_("ISO C forbids assignment between "
6549 "function pointer and %<void *%>"),
6550 G_("ISO C forbids initialization between "
6551 "function pointer and %<void *%>"),
6552 G_("ISO C forbids return between function "
6553 "pointer and %<void *%>"));
6554 /* Const and volatile mean something different for function types,
6555 so the usual warnings are not appropriate. */
6556 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6557 && TREE_CODE (ttl) != FUNCTION_TYPE)
6559 /* Don't warn about loss of qualifier for conversions from
6560 qualified void* to pointers to arrays with corresponding
6561 qualifier on the element type. */
6562 if (!pedantic)
6563 ttl = strip_array_types (ttl);
6565 /* Assignments between atomic and non-atomic objects are OK. */
6566 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6567 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6569 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6570 OPT_Wdiscarded_qualifiers,
6571 G_("passing argument %d of %qE discards "
6572 "%qv qualifier from pointer target type"),
6573 G_("assignment discards %qv qualifier "
6574 "from pointer target type"),
6575 G_("initialization discards %qv qualifier "
6576 "from pointer target type"),
6577 G_("return discards %qv qualifier from "
6578 "pointer target type"),
6579 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6581 /* If this is not a case of ignoring a mismatch in signedness,
6582 no warning. */
6583 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6584 || target_cmp)
6586 /* If there is a mismatch, do warn. */
6587 else if (warn_pointer_sign)
6588 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6589 G_("pointer targets in passing argument "
6590 "%d of %qE differ in signedness"),
6591 G_("pointer targets in assignment "
6592 "differ in signedness"),
6593 G_("pointer targets in initialization "
6594 "differ in signedness"),
6595 G_("pointer targets in return differ "
6596 "in signedness"));
6598 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6599 && TREE_CODE (ttr) == FUNCTION_TYPE)
6601 /* Because const and volatile on functions are restrictions
6602 that say the function will not do certain things,
6603 it is okay to use a const or volatile function
6604 where an ordinary one is wanted, but not vice-versa. */
6605 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6606 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6607 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6608 OPT_Wdiscarded_qualifiers,
6609 G_("passing argument %d of %qE makes "
6610 "%q#v qualified function pointer "
6611 "from unqualified"),
6612 G_("assignment makes %q#v qualified function "
6613 "pointer from unqualified"),
6614 G_("initialization makes %q#v qualified "
6615 "function pointer from unqualified"),
6616 G_("return makes %q#v qualified function "
6617 "pointer from unqualified"),
6618 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6621 else
6622 /* Avoid warning about the volatile ObjC EH puts on decls. */
6623 if (!objc_ok)
6624 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6625 OPT_Wincompatible_pointer_types,
6626 G_("passing argument %d of %qE from "
6627 "incompatible pointer type"),
6628 G_("assignment from incompatible pointer type"),
6629 G_("initialization from incompatible "
6630 "pointer type"),
6631 G_("return from incompatible pointer type"));
6633 return convert (type, rhs);
6635 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6637 /* ??? This should not be an error when inlining calls to
6638 unprototyped functions. */
6639 error_at (location, "invalid use of non-lvalue array");
6640 return error_mark_node;
6642 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6644 /* An explicit constant 0 can convert to a pointer,
6645 or one that results from arithmetic, even including
6646 a cast to integer type. */
6647 if (!null_pointer_constant)
6648 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6649 OPT_Wint_conversion,
6650 G_("passing argument %d of %qE makes "
6651 "pointer from integer without a cast"),
6652 G_("assignment makes pointer from integer "
6653 "without a cast"),
6654 G_("initialization makes pointer from "
6655 "integer without a cast"),
6656 G_("return makes pointer from integer "
6657 "without a cast"));
6659 return convert (type, rhs);
6661 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6663 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6664 OPT_Wint_conversion,
6665 G_("passing argument %d of %qE makes integer "
6666 "from pointer without a cast"),
6667 G_("assignment makes integer from pointer "
6668 "without a cast"),
6669 G_("initialization makes integer from pointer "
6670 "without a cast"),
6671 G_("return makes integer from pointer "
6672 "without a cast"));
6673 return convert (type, rhs);
6675 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6677 tree ret;
6678 bool save = in_late_binary_op;
6679 in_late_binary_op = true;
6680 ret = convert (type, rhs);
6681 in_late_binary_op = save;
6682 return ret;
6685 switch (errtype)
6687 case ic_argpass:
6688 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6689 rname);
6690 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6691 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6692 "expected %qT but argument is of type %qT", type, rhstype);
6693 break;
6694 case ic_assign:
6695 error_at (location, "incompatible types when assigning to type %qT from "
6696 "type %qT", type, rhstype);
6697 break;
6698 case ic_init:
6699 error_at (location,
6700 "incompatible types when initializing type %qT using type %qT",
6701 type, rhstype);
6702 break;
6703 case ic_return:
6704 error_at (location,
6705 "incompatible types when returning type %qT but %qT was "
6706 "expected", rhstype, type);
6707 break;
6708 default:
6709 gcc_unreachable ();
6712 return error_mark_node;
6715 /* If VALUE is a compound expr all of whose expressions are constant, then
6716 return its value. Otherwise, return error_mark_node.
6718 This is for handling COMPOUND_EXPRs as initializer elements
6719 which is allowed with a warning when -pedantic is specified. */
6721 static tree
6722 valid_compound_expr_initializer (tree value, tree endtype)
6724 if (TREE_CODE (value) == COMPOUND_EXPR)
6726 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6727 == error_mark_node)
6728 return error_mark_node;
6729 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6730 endtype);
6732 else if (!initializer_constant_valid_p (value, endtype))
6733 return error_mark_node;
6734 else
6735 return value;
6738 /* Perform appropriate conversions on the initial value of a variable,
6739 store it in the declaration DECL,
6740 and print any error messages that are appropriate.
6741 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6742 If the init is invalid, store an ERROR_MARK.
6744 INIT_LOC is the location of the initial value. */
6746 void
6747 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6749 tree value, type;
6750 bool npc = false;
6752 /* If variable's type was invalidly declared, just ignore it. */
6754 type = TREE_TYPE (decl);
6755 if (TREE_CODE (type) == ERROR_MARK)
6756 return;
6758 /* Digest the specified initializer into an expression. */
6760 if (init)
6761 npc = null_pointer_constant_p (init);
6762 value = digest_init (init_loc, type, init, origtype, npc,
6763 true, TREE_STATIC (decl));
6765 /* Store the expression if valid; else report error. */
6767 if (!in_system_header_at (input_location)
6768 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6769 warning (OPT_Wtraditional, "traditional C rejects automatic "
6770 "aggregate initialization");
6772 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6773 DECL_INITIAL (decl) = value;
6775 /* ANSI wants warnings about out-of-range constant initializers. */
6776 STRIP_TYPE_NOPS (value);
6777 if (TREE_STATIC (decl))
6778 constant_expression_warning (value);
6780 /* Check if we need to set array size from compound literal size. */
6781 if (TREE_CODE (type) == ARRAY_TYPE
6782 && TYPE_DOMAIN (type) == 0
6783 && value != error_mark_node)
6785 tree inside_init = init;
6787 STRIP_TYPE_NOPS (inside_init);
6788 inside_init = fold (inside_init);
6790 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6792 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6794 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6796 /* For int foo[] = (int [3]){1}; we need to set array size
6797 now since later on array initializer will be just the
6798 brace enclosed list of the compound literal. */
6799 tree etype = strip_array_types (TREE_TYPE (decl));
6800 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6801 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6802 layout_type (type);
6803 layout_decl (cldecl, 0);
6804 TREE_TYPE (decl)
6805 = c_build_qualified_type (type, TYPE_QUALS (etype));
6811 /* Methods for storing and printing names for error messages. */
6813 /* Implement a spelling stack that allows components of a name to be pushed
6814 and popped. Each element on the stack is this structure. */
6816 struct spelling
6818 int kind;
6819 union
6821 unsigned HOST_WIDE_INT i;
6822 const char *s;
6823 } u;
6826 #define SPELLING_STRING 1
6827 #define SPELLING_MEMBER 2
6828 #define SPELLING_BOUNDS 3
6830 static struct spelling *spelling; /* Next stack element (unused). */
6831 static struct spelling *spelling_base; /* Spelling stack base. */
6832 static int spelling_size; /* Size of the spelling stack. */
6834 /* Macros to save and restore the spelling stack around push_... functions.
6835 Alternative to SAVE_SPELLING_STACK. */
6837 #define SPELLING_DEPTH() (spelling - spelling_base)
6838 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6840 /* Push an element on the spelling stack with type KIND and assign VALUE
6841 to MEMBER. */
6843 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6845 int depth = SPELLING_DEPTH (); \
6847 if (depth >= spelling_size) \
6849 spelling_size += 10; \
6850 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6851 spelling_size); \
6852 RESTORE_SPELLING_DEPTH (depth); \
6855 spelling->kind = (KIND); \
6856 spelling->MEMBER = (VALUE); \
6857 spelling++; \
6860 /* Push STRING on the stack. Printed literally. */
6862 static void
6863 push_string (const char *string)
6865 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6868 /* Push a member name on the stack. Printed as '.' STRING. */
6870 static void
6871 push_member_name (tree decl)
6873 const char *const string
6874 = (DECL_NAME (decl)
6875 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6876 : _("<anonymous>"));
6877 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6880 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6882 static void
6883 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6885 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6888 /* Compute the maximum size in bytes of the printed spelling. */
6890 static int
6891 spelling_length (void)
6893 int size = 0;
6894 struct spelling *p;
6896 for (p = spelling_base; p < spelling; p++)
6898 if (p->kind == SPELLING_BOUNDS)
6899 size += 25;
6900 else
6901 size += strlen (p->u.s) + 1;
6904 return size;
6907 /* Print the spelling to BUFFER and return it. */
6909 static char *
6910 print_spelling (char *buffer)
6912 char *d = buffer;
6913 struct spelling *p;
6915 for (p = spelling_base; p < spelling; p++)
6916 if (p->kind == SPELLING_BOUNDS)
6918 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6919 d += strlen (d);
6921 else
6923 const char *s;
6924 if (p->kind == SPELLING_MEMBER)
6925 *d++ = '.';
6926 for (s = p->u.s; (*d = *s++); d++)
6929 *d++ = '\0';
6930 return buffer;
6933 /* Digest the parser output INIT as an initializer for type TYPE.
6934 Return a C expression of type TYPE to represent the initial value.
6936 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6938 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6940 If INIT is a string constant, STRICT_STRING is true if it is
6941 unparenthesized or we should not warn here for it being parenthesized.
6942 For other types of INIT, STRICT_STRING is not used.
6944 INIT_LOC is the location of the INIT.
6946 REQUIRE_CONSTANT requests an error if non-constant initializers or
6947 elements are seen. */
6949 static tree
6950 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6951 bool null_pointer_constant, bool strict_string,
6952 int require_constant)
6954 enum tree_code code = TREE_CODE (type);
6955 tree inside_init = init;
6956 tree semantic_type = NULL_TREE;
6957 bool maybe_const = true;
6959 if (type == error_mark_node
6960 || !init
6961 || error_operand_p (init))
6962 return error_mark_node;
6964 STRIP_TYPE_NOPS (inside_init);
6966 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6968 semantic_type = TREE_TYPE (inside_init);
6969 inside_init = TREE_OPERAND (inside_init, 0);
6971 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6972 inside_init = decl_constant_value_for_optimization (inside_init);
6974 /* Initialization of an array of chars from a string constant
6975 optionally enclosed in braces. */
6977 if (code == ARRAY_TYPE && inside_init
6978 && TREE_CODE (inside_init) == STRING_CST)
6980 tree typ1
6981 = (TYPE_ATOMIC (TREE_TYPE (type))
6982 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6983 TYPE_QUAL_ATOMIC)
6984 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6985 /* Note that an array could be both an array of character type
6986 and an array of wchar_t if wchar_t is signed char or unsigned
6987 char. */
6988 bool char_array = (typ1 == char_type_node
6989 || typ1 == signed_char_type_node
6990 || typ1 == unsigned_char_type_node);
6991 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6992 bool char16_array = !!comptypes (typ1, char16_type_node);
6993 bool char32_array = !!comptypes (typ1, char32_type_node);
6995 if (char_array || wchar_array || char16_array || char32_array)
6997 struct c_expr expr;
6998 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6999 expr.value = inside_init;
7000 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7001 expr.original_type = NULL;
7002 maybe_warn_string_init (init_loc, type, expr);
7004 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7005 pedwarn_init (init_loc, OPT_Wpedantic,
7006 "initialization of a flexible array member");
7008 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7009 TYPE_MAIN_VARIANT (type)))
7010 return inside_init;
7012 if (char_array)
7014 if (typ2 != char_type_node)
7016 error_init (init_loc, "char-array initialized from wide "
7017 "string");
7018 return error_mark_node;
7021 else
7023 if (typ2 == char_type_node)
7025 error_init (init_loc, "wide character array initialized "
7026 "from non-wide string");
7027 return error_mark_node;
7029 else if (!comptypes(typ1, typ2))
7031 error_init (init_loc, "wide character array initialized "
7032 "from incompatible wide string");
7033 return error_mark_node;
7037 TREE_TYPE (inside_init) = type;
7038 if (TYPE_DOMAIN (type) != 0
7039 && TYPE_SIZE (type) != 0
7040 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7042 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7044 /* Subtract the size of a single (possibly wide) character
7045 because it's ok to ignore the terminating null char
7046 that is counted in the length of the constant. */
7047 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7048 (len
7049 - (TYPE_PRECISION (typ1)
7050 / BITS_PER_UNIT))))
7051 pedwarn_init (init_loc, 0,
7052 ("initializer-string for array of chars "
7053 "is too long"));
7054 else if (warn_cxx_compat
7055 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7056 warning_at (init_loc, OPT_Wc___compat,
7057 ("initializer-string for array chars "
7058 "is too long for C++"));
7061 return inside_init;
7063 else if (INTEGRAL_TYPE_P (typ1))
7065 error_init (init_loc, "array of inappropriate type initialized "
7066 "from string constant");
7067 return error_mark_node;
7071 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7072 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7073 below and handle as a constructor. */
7074 if (code == VECTOR_TYPE
7075 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7076 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7077 && TREE_CONSTANT (inside_init))
7079 if (TREE_CODE (inside_init) == VECTOR_CST
7080 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7081 TYPE_MAIN_VARIANT (type)))
7082 return inside_init;
7084 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7086 unsigned HOST_WIDE_INT ix;
7087 tree value;
7088 bool constant_p = true;
7090 /* Iterate through elements and check if all constructor
7091 elements are *_CSTs. */
7092 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7093 if (!CONSTANT_CLASS_P (value))
7095 constant_p = false;
7096 break;
7099 if (constant_p)
7100 return build_vector_from_ctor (type,
7101 CONSTRUCTOR_ELTS (inside_init));
7105 if (warn_sequence_point)
7106 verify_sequence_points (inside_init);
7108 /* Any type can be initialized
7109 from an expression of the same type, optionally with braces. */
7111 if (inside_init && TREE_TYPE (inside_init) != 0
7112 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7113 TYPE_MAIN_VARIANT (type))
7114 || (code == ARRAY_TYPE
7115 && comptypes (TREE_TYPE (inside_init), type))
7116 || (code == VECTOR_TYPE
7117 && comptypes (TREE_TYPE (inside_init), type))
7118 || (code == POINTER_TYPE
7119 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7120 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7121 TREE_TYPE (type)))))
7123 if (code == POINTER_TYPE)
7125 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7127 if (TREE_CODE (inside_init) == STRING_CST
7128 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7129 inside_init = array_to_pointer_conversion
7130 (init_loc, inside_init);
7131 else
7133 error_init (init_loc, "invalid use of non-lvalue array");
7134 return error_mark_node;
7139 if (code == VECTOR_TYPE)
7140 /* Although the types are compatible, we may require a
7141 conversion. */
7142 inside_init = convert (type, inside_init);
7144 if (require_constant
7145 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7147 /* As an extension, allow initializing objects with static storage
7148 duration with compound literals (which are then treated just as
7149 the brace enclosed list they contain). Also allow this for
7150 vectors, as we can only assign them with compound literals. */
7151 if (flag_isoc99 && code != VECTOR_TYPE)
7152 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7153 "is not constant");
7154 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7155 inside_init = DECL_INITIAL (decl);
7158 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7159 && TREE_CODE (inside_init) != CONSTRUCTOR)
7161 error_init (init_loc, "array initialized from non-constant array "
7162 "expression");
7163 return error_mark_node;
7166 /* Compound expressions can only occur here if -Wpedantic or
7167 -pedantic-errors is specified. In the later case, we always want
7168 an error. In the former case, we simply want a warning. */
7169 if (require_constant && pedantic
7170 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7172 inside_init
7173 = valid_compound_expr_initializer (inside_init,
7174 TREE_TYPE (inside_init));
7175 if (inside_init == error_mark_node)
7176 error_init (init_loc, "initializer element is not constant");
7177 else
7178 pedwarn_init (init_loc, OPT_Wpedantic,
7179 "initializer element is not constant");
7180 if (flag_pedantic_errors)
7181 inside_init = error_mark_node;
7183 else if (require_constant
7184 && !initializer_constant_valid_p (inside_init,
7185 TREE_TYPE (inside_init)))
7187 error_init (init_loc, "initializer element is not constant");
7188 inside_init = error_mark_node;
7190 else if (require_constant && !maybe_const)
7191 pedwarn_init (init_loc, OPT_Wpedantic,
7192 "initializer element is not a constant expression");
7194 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7195 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7196 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7197 type, inside_init, origtype,
7198 ic_init, null_pointer_constant,
7199 NULL_TREE, NULL_TREE, 0);
7200 return inside_init;
7203 /* Handle scalar types, including conversions. */
7205 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7206 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7207 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7209 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7210 && (TREE_CODE (init) == STRING_CST
7211 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7212 inside_init = init = array_to_pointer_conversion (init_loc, init);
7213 if (semantic_type)
7214 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7215 inside_init);
7216 inside_init
7217 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7218 inside_init, origtype, ic_init,
7219 null_pointer_constant, NULL_TREE, NULL_TREE,
7222 /* Check to see if we have already given an error message. */
7223 if (inside_init == error_mark_node)
7225 else if (require_constant && !TREE_CONSTANT (inside_init))
7227 error_init (init_loc, "initializer element is not constant");
7228 inside_init = error_mark_node;
7230 else if (require_constant
7231 && !initializer_constant_valid_p (inside_init,
7232 TREE_TYPE (inside_init)))
7234 error_init (init_loc, "initializer element is not computable at "
7235 "load time");
7236 inside_init = error_mark_node;
7238 else if (require_constant && !maybe_const)
7239 pedwarn_init (init_loc, OPT_Wpedantic,
7240 "initializer element is not a constant expression");
7242 return inside_init;
7245 /* Come here only for records and arrays. */
7247 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7249 error_init (init_loc, "variable-sized object may not be initialized");
7250 return error_mark_node;
7253 error_init (init_loc, "invalid initializer");
7254 return error_mark_node;
7257 /* Handle initializers that use braces. */
7259 /* Type of object we are accumulating a constructor for.
7260 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7261 static tree constructor_type;
7263 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7264 left to fill. */
7265 static tree constructor_fields;
7267 /* For an ARRAY_TYPE, this is the specified index
7268 at which to store the next element we get. */
7269 static tree constructor_index;
7271 /* For an ARRAY_TYPE, this is the maximum index. */
7272 static tree constructor_max_index;
7274 /* For a RECORD_TYPE, this is the first field not yet written out. */
7275 static tree constructor_unfilled_fields;
7277 /* For an ARRAY_TYPE, this is the index of the first element
7278 not yet written out. */
7279 static tree constructor_unfilled_index;
7281 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7282 This is so we can generate gaps between fields, when appropriate. */
7283 static tree constructor_bit_index;
7285 /* If we are saving up the elements rather than allocating them,
7286 this is the list of elements so far (in reverse order,
7287 most recent first). */
7288 static vec<constructor_elt, va_gc> *constructor_elements;
7290 /* 1 if constructor should be incrementally stored into a constructor chain,
7291 0 if all the elements should be kept in AVL tree. */
7292 static int constructor_incremental;
7294 /* 1 if so far this constructor's elements are all compile-time constants. */
7295 static int constructor_constant;
7297 /* 1 if so far this constructor's elements are all valid address constants. */
7298 static int constructor_simple;
7300 /* 1 if this constructor has an element that cannot be part of a
7301 constant expression. */
7302 static int constructor_nonconst;
7304 /* 1 if this constructor is erroneous so far. */
7305 static int constructor_erroneous;
7307 /* 1 if this constructor is the universal zero initializer { 0 }. */
7308 static int constructor_zeroinit;
7310 /* Structure for managing pending initializer elements, organized as an
7311 AVL tree. */
7313 struct init_node
7315 struct init_node *left, *right;
7316 struct init_node *parent;
7317 int balance;
7318 tree purpose;
7319 tree value;
7320 tree origtype;
7323 /* Tree of pending elements at this constructor level.
7324 These are elements encountered out of order
7325 which belong at places we haven't reached yet in actually
7326 writing the output.
7327 Will never hold tree nodes across GC runs. */
7328 static struct init_node *constructor_pending_elts;
7330 /* The SPELLING_DEPTH of this constructor. */
7331 static int constructor_depth;
7333 /* DECL node for which an initializer is being read.
7334 0 means we are reading a constructor expression
7335 such as (struct foo) {...}. */
7336 static tree constructor_decl;
7338 /* Nonzero if this is an initializer for a top-level decl. */
7339 static int constructor_top_level;
7341 /* Nonzero if there were any member designators in this initializer. */
7342 static int constructor_designated;
7344 /* Nesting depth of designator list. */
7345 static int designator_depth;
7347 /* Nonzero if there were diagnosed errors in this designator list. */
7348 static int designator_erroneous;
7351 /* This stack has a level for each implicit or explicit level of
7352 structuring in the initializer, including the outermost one. It
7353 saves the values of most of the variables above. */
7355 struct constructor_range_stack;
7357 struct constructor_stack
7359 struct constructor_stack *next;
7360 tree type;
7361 tree fields;
7362 tree index;
7363 tree max_index;
7364 tree unfilled_index;
7365 tree unfilled_fields;
7366 tree bit_index;
7367 vec<constructor_elt, va_gc> *elements;
7368 struct init_node *pending_elts;
7369 int offset;
7370 int depth;
7371 /* If value nonzero, this value should replace the entire
7372 constructor at this level. */
7373 struct c_expr replacement_value;
7374 struct constructor_range_stack *range_stack;
7375 char constant;
7376 char simple;
7377 char nonconst;
7378 char implicit;
7379 char erroneous;
7380 char outer;
7381 char incremental;
7382 char designated;
7383 int designator_depth;
7386 static struct constructor_stack *constructor_stack;
7388 /* This stack represents designators from some range designator up to
7389 the last designator in the list. */
7391 struct constructor_range_stack
7393 struct constructor_range_stack *next, *prev;
7394 struct constructor_stack *stack;
7395 tree range_start;
7396 tree index;
7397 tree range_end;
7398 tree fields;
7401 static struct constructor_range_stack *constructor_range_stack;
7403 /* This stack records separate initializers that are nested.
7404 Nested initializers can't happen in ANSI C, but GNU C allows them
7405 in cases like { ... (struct foo) { ... } ... }. */
7407 struct initializer_stack
7409 struct initializer_stack *next;
7410 tree decl;
7411 struct constructor_stack *constructor_stack;
7412 struct constructor_range_stack *constructor_range_stack;
7413 vec<constructor_elt, va_gc> *elements;
7414 struct spelling *spelling;
7415 struct spelling *spelling_base;
7416 int spelling_size;
7417 char top_level;
7418 char require_constant_value;
7419 char require_constant_elements;
7422 static struct initializer_stack *initializer_stack;
7424 /* Prepare to parse and output the initializer for variable DECL. */
7426 void
7427 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7429 const char *locus;
7430 struct initializer_stack *p = XNEW (struct initializer_stack);
7432 p->decl = constructor_decl;
7433 p->require_constant_value = require_constant_value;
7434 p->require_constant_elements = require_constant_elements;
7435 p->constructor_stack = constructor_stack;
7436 p->constructor_range_stack = constructor_range_stack;
7437 p->elements = constructor_elements;
7438 p->spelling = spelling;
7439 p->spelling_base = spelling_base;
7440 p->spelling_size = spelling_size;
7441 p->top_level = constructor_top_level;
7442 p->next = initializer_stack;
7443 initializer_stack = p;
7445 constructor_decl = decl;
7446 constructor_designated = 0;
7447 constructor_top_level = top_level;
7449 if (decl != 0 && decl != error_mark_node)
7451 require_constant_value = TREE_STATIC (decl);
7452 require_constant_elements
7453 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7454 /* For a scalar, you can always use any value to initialize,
7455 even within braces. */
7456 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7457 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7459 else
7461 require_constant_value = 0;
7462 require_constant_elements = 0;
7463 locus = _("(anonymous)");
7466 constructor_stack = 0;
7467 constructor_range_stack = 0;
7469 found_missing_braces = 0;
7471 spelling_base = 0;
7472 spelling_size = 0;
7473 RESTORE_SPELLING_DEPTH (0);
7475 if (locus)
7476 push_string (locus);
7479 void
7480 finish_init (void)
7482 struct initializer_stack *p = initializer_stack;
7484 /* Free the whole constructor stack of this initializer. */
7485 while (constructor_stack)
7487 struct constructor_stack *q = constructor_stack;
7488 constructor_stack = q->next;
7489 free (q);
7492 gcc_assert (!constructor_range_stack);
7494 /* Pop back to the data of the outer initializer (if any). */
7495 free (spelling_base);
7497 constructor_decl = p->decl;
7498 require_constant_value = p->require_constant_value;
7499 require_constant_elements = p->require_constant_elements;
7500 constructor_stack = p->constructor_stack;
7501 constructor_range_stack = p->constructor_range_stack;
7502 constructor_elements = p->elements;
7503 spelling = p->spelling;
7504 spelling_base = p->spelling_base;
7505 spelling_size = p->spelling_size;
7506 constructor_top_level = p->top_level;
7507 initializer_stack = p->next;
7508 free (p);
7511 /* Call here when we see the initializer is surrounded by braces.
7512 This is instead of a call to push_init_level;
7513 it is matched by a call to pop_init_level.
7515 TYPE is the type to initialize, for a constructor expression.
7516 For an initializer for a decl, TYPE is zero. */
7518 void
7519 really_start_incremental_init (tree type)
7521 struct constructor_stack *p = XNEW (struct constructor_stack);
7523 if (type == 0)
7524 type = TREE_TYPE (constructor_decl);
7526 if (VECTOR_TYPE_P (type)
7527 && TYPE_VECTOR_OPAQUE (type))
7528 error ("opaque vector types cannot be initialized");
7530 p->type = constructor_type;
7531 p->fields = constructor_fields;
7532 p->index = constructor_index;
7533 p->max_index = constructor_max_index;
7534 p->unfilled_index = constructor_unfilled_index;
7535 p->unfilled_fields = constructor_unfilled_fields;
7536 p->bit_index = constructor_bit_index;
7537 p->elements = constructor_elements;
7538 p->constant = constructor_constant;
7539 p->simple = constructor_simple;
7540 p->nonconst = constructor_nonconst;
7541 p->erroneous = constructor_erroneous;
7542 p->pending_elts = constructor_pending_elts;
7543 p->depth = constructor_depth;
7544 p->replacement_value.value = 0;
7545 p->replacement_value.original_code = ERROR_MARK;
7546 p->replacement_value.original_type = NULL;
7547 p->implicit = 0;
7548 p->range_stack = 0;
7549 p->outer = 0;
7550 p->incremental = constructor_incremental;
7551 p->designated = constructor_designated;
7552 p->designator_depth = designator_depth;
7553 p->next = 0;
7554 constructor_stack = p;
7556 constructor_constant = 1;
7557 constructor_simple = 1;
7558 constructor_nonconst = 0;
7559 constructor_depth = SPELLING_DEPTH ();
7560 constructor_elements = NULL;
7561 constructor_pending_elts = 0;
7562 constructor_type = type;
7563 constructor_incremental = 1;
7564 constructor_designated = 0;
7565 constructor_zeroinit = 1;
7566 designator_depth = 0;
7567 designator_erroneous = 0;
7569 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7571 constructor_fields = TYPE_FIELDS (constructor_type);
7572 /* Skip any nameless bit fields at the beginning. */
7573 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7574 && DECL_NAME (constructor_fields) == 0)
7575 constructor_fields = DECL_CHAIN (constructor_fields);
7577 constructor_unfilled_fields = constructor_fields;
7578 constructor_bit_index = bitsize_zero_node;
7580 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7582 if (TYPE_DOMAIN (constructor_type))
7584 constructor_max_index
7585 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7587 /* Detect non-empty initializations of zero-length arrays. */
7588 if (constructor_max_index == NULL_TREE
7589 && TYPE_SIZE (constructor_type))
7590 constructor_max_index = integer_minus_one_node;
7592 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7593 to initialize VLAs will cause a proper error; avoid tree
7594 checking errors as well by setting a safe value. */
7595 if (constructor_max_index
7596 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7597 constructor_max_index = integer_minus_one_node;
7599 constructor_index
7600 = convert (bitsizetype,
7601 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7603 else
7605 constructor_index = bitsize_zero_node;
7606 constructor_max_index = NULL_TREE;
7609 constructor_unfilled_index = constructor_index;
7611 else if (VECTOR_TYPE_P (constructor_type))
7613 /* Vectors are like simple fixed-size arrays. */
7614 constructor_max_index =
7615 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7616 constructor_index = bitsize_zero_node;
7617 constructor_unfilled_index = constructor_index;
7619 else
7621 /* Handle the case of int x = {5}; */
7622 constructor_fields = constructor_type;
7623 constructor_unfilled_fields = constructor_type;
7627 /* Called when we see an open brace for a nested initializer. Finish
7628 off any pending levels with implicit braces. */
7629 void
7630 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7632 while (constructor_stack->implicit)
7634 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7635 && constructor_fields == 0)
7636 process_init_element (input_location,
7637 pop_init_level (loc, 1, braced_init_obstack),
7638 true, braced_init_obstack);
7639 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7640 && constructor_max_index
7641 && tree_int_cst_lt (constructor_max_index,
7642 constructor_index))
7643 process_init_element (input_location,
7644 pop_init_level (loc, 1, braced_init_obstack),
7645 true, braced_init_obstack);
7646 else
7647 break;
7651 /* Push down into a subobject, for initialization.
7652 If this is for an explicit set of braces, IMPLICIT is 0.
7653 If it is because the next element belongs at a lower level,
7654 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7656 void
7657 push_init_level (location_t loc, int implicit,
7658 struct obstack *braced_init_obstack)
7660 struct constructor_stack *p;
7661 tree value = NULL_TREE;
7663 /* Unless this is an explicit brace, we need to preserve previous
7664 content if any. */
7665 if (implicit)
7667 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7668 value = find_init_member (constructor_fields, braced_init_obstack);
7669 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7670 value = find_init_member (constructor_index, braced_init_obstack);
7673 p = XNEW (struct constructor_stack);
7674 p->type = constructor_type;
7675 p->fields = constructor_fields;
7676 p->index = constructor_index;
7677 p->max_index = constructor_max_index;
7678 p->unfilled_index = constructor_unfilled_index;
7679 p->unfilled_fields = constructor_unfilled_fields;
7680 p->bit_index = constructor_bit_index;
7681 p->elements = constructor_elements;
7682 p->constant = constructor_constant;
7683 p->simple = constructor_simple;
7684 p->nonconst = constructor_nonconst;
7685 p->erroneous = constructor_erroneous;
7686 p->pending_elts = constructor_pending_elts;
7687 p->depth = constructor_depth;
7688 p->replacement_value.value = 0;
7689 p->replacement_value.original_code = ERROR_MARK;
7690 p->replacement_value.original_type = NULL;
7691 p->implicit = implicit;
7692 p->outer = 0;
7693 p->incremental = constructor_incremental;
7694 p->designated = constructor_designated;
7695 p->designator_depth = designator_depth;
7696 p->next = constructor_stack;
7697 p->range_stack = 0;
7698 constructor_stack = p;
7700 constructor_constant = 1;
7701 constructor_simple = 1;
7702 constructor_nonconst = 0;
7703 constructor_depth = SPELLING_DEPTH ();
7704 constructor_elements = NULL;
7705 constructor_incremental = 1;
7706 constructor_designated = 0;
7707 constructor_pending_elts = 0;
7708 if (!implicit)
7710 p->range_stack = constructor_range_stack;
7711 constructor_range_stack = 0;
7712 designator_depth = 0;
7713 designator_erroneous = 0;
7716 /* Don't die if an entire brace-pair level is superfluous
7717 in the containing level. */
7718 if (constructor_type == 0)
7720 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7722 /* Don't die if there are extra init elts at the end. */
7723 if (constructor_fields == 0)
7724 constructor_type = 0;
7725 else
7727 constructor_type = TREE_TYPE (constructor_fields);
7728 push_member_name (constructor_fields);
7729 constructor_depth++;
7731 /* If upper initializer is designated, then mark this as
7732 designated too to prevent bogus warnings. */
7733 constructor_designated = p->designated;
7735 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7737 constructor_type = TREE_TYPE (constructor_type);
7738 push_array_bounds (tree_to_uhwi (constructor_index));
7739 constructor_depth++;
7742 if (constructor_type == 0)
7744 error_init (loc, "extra brace group at end of initializer");
7745 constructor_fields = 0;
7746 constructor_unfilled_fields = 0;
7747 return;
7750 if (value && TREE_CODE (value) == CONSTRUCTOR)
7752 constructor_constant = TREE_CONSTANT (value);
7753 constructor_simple = TREE_STATIC (value);
7754 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7755 constructor_elements = CONSTRUCTOR_ELTS (value);
7756 if (!vec_safe_is_empty (constructor_elements)
7757 && (TREE_CODE (constructor_type) == RECORD_TYPE
7758 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7759 set_nonincremental_init (braced_init_obstack);
7762 if (implicit == 1)
7763 found_missing_braces = 1;
7765 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7767 constructor_fields = TYPE_FIELDS (constructor_type);
7768 /* Skip any nameless bit fields at the beginning. */
7769 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7770 && DECL_NAME (constructor_fields) == 0)
7771 constructor_fields = DECL_CHAIN (constructor_fields);
7773 constructor_unfilled_fields = constructor_fields;
7774 constructor_bit_index = bitsize_zero_node;
7776 else if (VECTOR_TYPE_P (constructor_type))
7778 /* Vectors are like simple fixed-size arrays. */
7779 constructor_max_index =
7780 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7781 constructor_index = bitsize_int (0);
7782 constructor_unfilled_index = constructor_index;
7784 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7786 if (TYPE_DOMAIN (constructor_type))
7788 constructor_max_index
7789 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7791 /* Detect non-empty initializations of zero-length arrays. */
7792 if (constructor_max_index == NULL_TREE
7793 && TYPE_SIZE (constructor_type))
7794 constructor_max_index = integer_minus_one_node;
7796 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7797 to initialize VLAs will cause a proper error; avoid tree
7798 checking errors as well by setting a safe value. */
7799 if (constructor_max_index
7800 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7801 constructor_max_index = integer_minus_one_node;
7803 constructor_index
7804 = convert (bitsizetype,
7805 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7807 else
7808 constructor_index = bitsize_zero_node;
7810 constructor_unfilled_index = constructor_index;
7811 if (value && TREE_CODE (value) == STRING_CST)
7813 /* We need to split the char/wchar array into individual
7814 characters, so that we don't have to special case it
7815 everywhere. */
7816 set_nonincremental_init_from_string (value, braced_init_obstack);
7819 else
7821 if (constructor_type != error_mark_node)
7822 warning_init (input_location, 0, "braces around scalar initializer");
7823 constructor_fields = constructor_type;
7824 constructor_unfilled_fields = constructor_type;
7828 /* At the end of an implicit or explicit brace level,
7829 finish up that level of constructor. If a single expression
7830 with redundant braces initialized that level, return the
7831 c_expr structure for that expression. Otherwise, the original_code
7832 element is set to ERROR_MARK.
7833 If we were outputting the elements as they are read, return 0 as the value
7834 from inner levels (process_init_element ignores that),
7835 but return error_mark_node as the value from the outermost level
7836 (that's what we want to put in DECL_INITIAL).
7837 Otherwise, return a CONSTRUCTOR expression as the value. */
7839 struct c_expr
7840 pop_init_level (location_t loc, int implicit,
7841 struct obstack *braced_init_obstack)
7843 struct constructor_stack *p;
7844 struct c_expr ret;
7845 ret.value = 0;
7846 ret.original_code = ERROR_MARK;
7847 ret.original_type = NULL;
7849 if (implicit == 0)
7851 /* When we come to an explicit close brace,
7852 pop any inner levels that didn't have explicit braces. */
7853 while (constructor_stack->implicit)
7854 process_init_element (input_location,
7855 pop_init_level (loc, 1, braced_init_obstack),
7856 true, braced_init_obstack);
7857 gcc_assert (!constructor_range_stack);
7860 /* Now output all pending elements. */
7861 constructor_incremental = 1;
7862 output_pending_init_elements (1, braced_init_obstack);
7864 p = constructor_stack;
7866 /* Error for initializing a flexible array member, or a zero-length
7867 array member in an inappropriate context. */
7868 if (constructor_type && constructor_fields
7869 && TREE_CODE (constructor_type) == ARRAY_TYPE
7870 && TYPE_DOMAIN (constructor_type)
7871 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7873 /* Silently discard empty initializations. The parser will
7874 already have pedwarned for empty brackets. */
7875 if (integer_zerop (constructor_unfilled_index))
7876 constructor_type = NULL_TREE;
7877 else
7879 gcc_assert (!TYPE_SIZE (constructor_type));
7881 if (constructor_depth > 2)
7882 error_init (loc, "initialization of flexible array member in a nested context");
7883 else
7884 pedwarn_init (loc, OPT_Wpedantic,
7885 "initialization of a flexible array member");
7887 /* We have already issued an error message for the existence
7888 of a flexible array member not at the end of the structure.
7889 Discard the initializer so that we do not die later. */
7890 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7891 constructor_type = NULL_TREE;
7895 switch (vec_safe_length (constructor_elements))
7897 case 0:
7898 /* Initialization with { } counts as zeroinit. */
7899 constructor_zeroinit = 1;
7900 break;
7901 case 1:
7902 /* This might be zeroinit as well. */
7903 if (integer_zerop ((*constructor_elements)[0].value))
7904 constructor_zeroinit = 1;
7905 break;
7906 default:
7907 /* If the constructor has more than one element, it can't be { 0 }. */
7908 constructor_zeroinit = 0;
7909 break;
7912 /* Warn when some structs are initialized with direct aggregation. */
7913 if (!implicit && found_missing_braces && warn_missing_braces
7914 && !constructor_zeroinit)
7915 warning_init (loc, OPT_Wmissing_braces,
7916 "missing braces around initializer");
7918 /* Warn when some struct elements are implicitly initialized to zero. */
7919 if (warn_missing_field_initializers
7920 && constructor_type
7921 && TREE_CODE (constructor_type) == RECORD_TYPE
7922 && constructor_unfilled_fields)
7924 /* Do not warn for flexible array members or zero-length arrays. */
7925 while (constructor_unfilled_fields
7926 && (!DECL_SIZE (constructor_unfilled_fields)
7927 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7928 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7930 if (constructor_unfilled_fields
7931 /* Do not warn if this level of the initializer uses member
7932 designators; it is likely to be deliberate. */
7933 && !constructor_designated
7934 /* Do not warn about initializing with { 0 } or with { }. */
7935 && !constructor_zeroinit)
7937 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7938 "missing initializer for field %qD of %qT",
7939 constructor_unfilled_fields,
7940 constructor_type))
7941 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7942 "%qD declared here", constructor_unfilled_fields);
7946 /* Pad out the end of the structure. */
7947 if (p->replacement_value.value)
7948 /* If this closes a superfluous brace pair,
7949 just pass out the element between them. */
7950 ret = p->replacement_value;
7951 else if (constructor_type == 0)
7953 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
7954 && TREE_CODE (constructor_type) != ARRAY_TYPE
7955 && !VECTOR_TYPE_P (constructor_type))
7957 /* A nonincremental scalar initializer--just return
7958 the element, after verifying there is just one. */
7959 if (vec_safe_is_empty (constructor_elements))
7961 if (!constructor_erroneous)
7962 error_init (loc, "empty scalar initializer");
7963 ret.value = error_mark_node;
7965 else if (vec_safe_length (constructor_elements) != 1)
7967 error_init (loc, "extra elements in scalar initializer");
7968 ret.value = (*constructor_elements)[0].value;
7970 else
7971 ret.value = (*constructor_elements)[0].value;
7973 else
7975 if (constructor_erroneous)
7976 ret.value = error_mark_node;
7977 else
7979 ret.value = build_constructor (constructor_type,
7980 constructor_elements);
7981 if (constructor_constant)
7982 TREE_CONSTANT (ret.value) = 1;
7983 if (constructor_constant && constructor_simple)
7984 TREE_STATIC (ret.value) = 1;
7985 if (constructor_nonconst)
7986 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7990 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7992 if (constructor_nonconst)
7993 ret.original_code = C_MAYBE_CONST_EXPR;
7994 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7995 ret.original_code = ERROR_MARK;
7998 constructor_type = p->type;
7999 constructor_fields = p->fields;
8000 constructor_index = p->index;
8001 constructor_max_index = p->max_index;
8002 constructor_unfilled_index = p->unfilled_index;
8003 constructor_unfilled_fields = p->unfilled_fields;
8004 constructor_bit_index = p->bit_index;
8005 constructor_elements = p->elements;
8006 constructor_constant = p->constant;
8007 constructor_simple = p->simple;
8008 constructor_nonconst = p->nonconst;
8009 constructor_erroneous = p->erroneous;
8010 constructor_incremental = p->incremental;
8011 constructor_designated = p->designated;
8012 designator_depth = p->designator_depth;
8013 constructor_pending_elts = p->pending_elts;
8014 constructor_depth = p->depth;
8015 if (!p->implicit)
8016 constructor_range_stack = p->range_stack;
8017 RESTORE_SPELLING_DEPTH (constructor_depth);
8019 constructor_stack = p->next;
8020 free (p);
8022 if (ret.value == 0 && constructor_stack == 0)
8023 ret.value = error_mark_node;
8024 return ret;
8027 /* Common handling for both array range and field name designators.
8028 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8030 static int
8031 set_designator (location_t loc, int array,
8032 struct obstack *braced_init_obstack)
8034 tree subtype;
8035 enum tree_code subcode;
8037 /* Don't die if an entire brace-pair level is superfluous
8038 in the containing level. */
8039 if (constructor_type == 0)
8040 return 1;
8042 /* If there were errors in this designator list already, bail out
8043 silently. */
8044 if (designator_erroneous)
8045 return 1;
8047 if (!designator_depth)
8049 gcc_assert (!constructor_range_stack);
8051 /* Designator list starts at the level of closest explicit
8052 braces. */
8053 while (constructor_stack->implicit)
8054 process_init_element (input_location,
8055 pop_init_level (loc, 1, braced_init_obstack),
8056 true, braced_init_obstack);
8057 constructor_designated = 1;
8058 return 0;
8061 switch (TREE_CODE (constructor_type))
8063 case RECORD_TYPE:
8064 case UNION_TYPE:
8065 subtype = TREE_TYPE (constructor_fields);
8066 if (subtype != error_mark_node)
8067 subtype = TYPE_MAIN_VARIANT (subtype);
8068 break;
8069 case ARRAY_TYPE:
8070 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8071 break;
8072 default:
8073 gcc_unreachable ();
8076 subcode = TREE_CODE (subtype);
8077 if (array && subcode != ARRAY_TYPE)
8079 error_init (loc, "array index in non-array initializer");
8080 return 1;
8082 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8084 error_init (loc, "field name not in record or union initializer");
8085 return 1;
8088 constructor_designated = 1;
8089 finish_implicit_inits (loc, braced_init_obstack);
8090 push_init_level (loc, 2, braced_init_obstack);
8091 return 0;
8094 /* If there are range designators in designator list, push a new designator
8095 to constructor_range_stack. RANGE_END is end of such stack range or
8096 NULL_TREE if there is no range designator at this level. */
8098 static void
8099 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8101 struct constructor_range_stack *p;
8103 p = (struct constructor_range_stack *)
8104 obstack_alloc (braced_init_obstack,
8105 sizeof (struct constructor_range_stack));
8106 p->prev = constructor_range_stack;
8107 p->next = 0;
8108 p->fields = constructor_fields;
8109 p->range_start = constructor_index;
8110 p->index = constructor_index;
8111 p->stack = constructor_stack;
8112 p->range_end = range_end;
8113 if (constructor_range_stack)
8114 constructor_range_stack->next = p;
8115 constructor_range_stack = p;
8118 /* Within an array initializer, specify the next index to be initialized.
8119 FIRST is that index. If LAST is nonzero, then initialize a range
8120 of indices, running from FIRST through LAST. */
8122 void
8123 set_init_index (location_t loc, tree first, tree last,
8124 struct obstack *braced_init_obstack)
8126 if (set_designator (loc, 1, braced_init_obstack))
8127 return;
8129 designator_erroneous = 1;
8131 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8132 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8134 error_init (loc, "array index in initializer not of integer type");
8135 return;
8138 if (TREE_CODE (first) != INTEGER_CST)
8140 first = c_fully_fold (first, false, NULL);
8141 if (TREE_CODE (first) == INTEGER_CST)
8142 pedwarn_init (loc, OPT_Wpedantic,
8143 "array index in initializer is not "
8144 "an integer constant expression");
8147 if (last && TREE_CODE (last) != INTEGER_CST)
8149 last = c_fully_fold (last, false, NULL);
8150 if (TREE_CODE (last) == INTEGER_CST)
8151 pedwarn_init (loc, OPT_Wpedantic,
8152 "array index in initializer is not "
8153 "an integer constant expression");
8156 if (TREE_CODE (first) != INTEGER_CST)
8157 error_init (loc, "nonconstant array index in initializer");
8158 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8159 error_init (loc, "nonconstant array index in initializer");
8160 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8161 error_init (loc, "array index in non-array initializer");
8162 else if (tree_int_cst_sgn (first) == -1)
8163 error_init (loc, "array index in initializer exceeds array bounds");
8164 else if (constructor_max_index
8165 && tree_int_cst_lt (constructor_max_index, first))
8166 error_init (loc, "array index in initializer exceeds array bounds");
8167 else
8169 constant_expression_warning (first);
8170 if (last)
8171 constant_expression_warning (last);
8172 constructor_index = convert (bitsizetype, first);
8173 if (tree_int_cst_lt (constructor_index, first))
8175 constructor_index = copy_node (constructor_index);
8176 TREE_OVERFLOW (constructor_index) = 1;
8179 if (last)
8181 if (tree_int_cst_equal (first, last))
8182 last = 0;
8183 else if (tree_int_cst_lt (last, first))
8185 error_init (loc, "empty index range in initializer");
8186 last = 0;
8188 else
8190 last = convert (bitsizetype, last);
8191 if (constructor_max_index != 0
8192 && tree_int_cst_lt (constructor_max_index, last))
8194 error_init (loc, "array index range in initializer exceeds "
8195 "array bounds");
8196 last = 0;
8201 designator_depth++;
8202 designator_erroneous = 0;
8203 if (constructor_range_stack || last)
8204 push_range_stack (last, braced_init_obstack);
8208 /* Within a struct initializer, specify the next field to be initialized. */
8210 void
8211 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8212 struct obstack *braced_init_obstack)
8214 tree field;
8216 if (set_designator (loc, 0, braced_init_obstack))
8217 return;
8219 designator_erroneous = 1;
8221 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8223 error_init (loc, "field name not in record or union initializer");
8224 return;
8227 field = lookup_field (constructor_type, fieldname);
8229 if (field == 0)
8231 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8232 if (guessed_id)
8234 gcc_rich_location rich_loc (fieldname_loc);
8235 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8236 error_at_rich_loc
8237 (&rich_loc,
8238 "%qT has no member named %qE; did you mean %qE?",
8239 constructor_type, fieldname, guessed_id);
8241 else
8242 error_at (fieldname_loc, "%qT has no member named %qE",
8243 constructor_type, fieldname);
8245 else
8248 constructor_fields = TREE_VALUE (field);
8249 designator_depth++;
8250 designator_erroneous = 0;
8251 if (constructor_range_stack)
8252 push_range_stack (NULL_TREE, braced_init_obstack);
8253 field = TREE_CHAIN (field);
8254 if (field)
8256 if (set_designator (loc, 0, braced_init_obstack))
8257 return;
8260 while (field != NULL_TREE);
8263 /* Add a new initializer to the tree of pending initializers. PURPOSE
8264 identifies the initializer, either array index or field in a structure.
8265 VALUE is the value of that index or field. If ORIGTYPE is not
8266 NULL_TREE, it is the original type of VALUE.
8268 IMPLICIT is true if value comes from pop_init_level (1),
8269 the new initializer has been merged with the existing one
8270 and thus no warnings should be emitted about overriding an
8271 existing initializer. */
8273 static void
8274 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8275 bool implicit, struct obstack *braced_init_obstack)
8277 struct init_node *p, **q, *r;
8279 q = &constructor_pending_elts;
8280 p = 0;
8282 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8284 while (*q != 0)
8286 p = *q;
8287 if (tree_int_cst_lt (purpose, p->purpose))
8288 q = &p->left;
8289 else if (tree_int_cst_lt (p->purpose, purpose))
8290 q = &p->right;
8291 else
8293 if (!implicit)
8295 if (TREE_SIDE_EFFECTS (p->value))
8296 warning_init (loc, OPT_Woverride_init_side_effects,
8297 "initialized field with side-effects "
8298 "overwritten");
8299 else if (warn_override_init)
8300 warning_init (loc, OPT_Woverride_init,
8301 "initialized field overwritten");
8303 p->value = value;
8304 p->origtype = origtype;
8305 return;
8309 else
8311 tree bitpos;
8313 bitpos = bit_position (purpose);
8314 while (*q != NULL)
8316 p = *q;
8317 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8318 q = &p->left;
8319 else if (p->purpose != purpose)
8320 q = &p->right;
8321 else
8323 if (!implicit)
8325 if (TREE_SIDE_EFFECTS (p->value))
8326 warning_init (loc, OPT_Woverride_init_side_effects,
8327 "initialized field with side-effects "
8328 "overwritten");
8329 else if (warn_override_init)
8330 warning_init (loc, OPT_Woverride_init,
8331 "initialized field overwritten");
8333 p->value = value;
8334 p->origtype = origtype;
8335 return;
8340 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8341 sizeof (struct init_node));
8342 r->purpose = purpose;
8343 r->value = value;
8344 r->origtype = origtype;
8346 *q = r;
8347 r->parent = p;
8348 r->left = 0;
8349 r->right = 0;
8350 r->balance = 0;
8352 while (p)
8354 struct init_node *s;
8356 if (r == p->left)
8358 if (p->balance == 0)
8359 p->balance = -1;
8360 else if (p->balance < 0)
8362 if (r->balance < 0)
8364 /* L rotation. */
8365 p->left = r->right;
8366 if (p->left)
8367 p->left->parent = p;
8368 r->right = p;
8370 p->balance = 0;
8371 r->balance = 0;
8373 s = p->parent;
8374 p->parent = r;
8375 r->parent = s;
8376 if (s)
8378 if (s->left == p)
8379 s->left = r;
8380 else
8381 s->right = r;
8383 else
8384 constructor_pending_elts = r;
8386 else
8388 /* LR rotation. */
8389 struct init_node *t = r->right;
8391 r->right = t->left;
8392 if (r->right)
8393 r->right->parent = r;
8394 t->left = r;
8396 p->left = t->right;
8397 if (p->left)
8398 p->left->parent = p;
8399 t->right = p;
8401 p->balance = t->balance < 0;
8402 r->balance = -(t->balance > 0);
8403 t->balance = 0;
8405 s = p->parent;
8406 p->parent = t;
8407 r->parent = t;
8408 t->parent = s;
8409 if (s)
8411 if (s->left == p)
8412 s->left = t;
8413 else
8414 s->right = t;
8416 else
8417 constructor_pending_elts = t;
8419 break;
8421 else
8423 /* p->balance == +1; growth of left side balances the node. */
8424 p->balance = 0;
8425 break;
8428 else /* r == p->right */
8430 if (p->balance == 0)
8431 /* Growth propagation from right side. */
8432 p->balance++;
8433 else if (p->balance > 0)
8435 if (r->balance > 0)
8437 /* R rotation. */
8438 p->right = r->left;
8439 if (p->right)
8440 p->right->parent = p;
8441 r->left = p;
8443 p->balance = 0;
8444 r->balance = 0;
8446 s = p->parent;
8447 p->parent = r;
8448 r->parent = s;
8449 if (s)
8451 if (s->left == p)
8452 s->left = r;
8453 else
8454 s->right = r;
8456 else
8457 constructor_pending_elts = r;
8459 else /* r->balance == -1 */
8461 /* RL rotation */
8462 struct init_node *t = r->left;
8464 r->left = t->right;
8465 if (r->left)
8466 r->left->parent = r;
8467 t->right = r;
8469 p->right = t->left;
8470 if (p->right)
8471 p->right->parent = p;
8472 t->left = p;
8474 r->balance = (t->balance < 0);
8475 p->balance = -(t->balance > 0);
8476 t->balance = 0;
8478 s = p->parent;
8479 p->parent = t;
8480 r->parent = t;
8481 t->parent = s;
8482 if (s)
8484 if (s->left == p)
8485 s->left = t;
8486 else
8487 s->right = t;
8489 else
8490 constructor_pending_elts = t;
8492 break;
8494 else
8496 /* p->balance == -1; growth of right side balances the node. */
8497 p->balance = 0;
8498 break;
8502 r = p;
8503 p = p->parent;
8507 /* Build AVL tree from a sorted chain. */
8509 static void
8510 set_nonincremental_init (struct obstack * braced_init_obstack)
8512 unsigned HOST_WIDE_INT ix;
8513 tree index, value;
8515 if (TREE_CODE (constructor_type) != RECORD_TYPE
8516 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8517 return;
8519 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8520 add_pending_init (input_location, index, value, NULL_TREE, true,
8521 braced_init_obstack);
8522 constructor_elements = NULL;
8523 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8525 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8526 /* Skip any nameless bit fields at the beginning. */
8527 while (constructor_unfilled_fields != 0
8528 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8529 && DECL_NAME (constructor_unfilled_fields) == 0)
8530 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8533 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8535 if (TYPE_DOMAIN (constructor_type))
8536 constructor_unfilled_index
8537 = convert (bitsizetype,
8538 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8539 else
8540 constructor_unfilled_index = bitsize_zero_node;
8542 constructor_incremental = 0;
8545 /* Build AVL tree from a string constant. */
8547 static void
8548 set_nonincremental_init_from_string (tree str,
8549 struct obstack * braced_init_obstack)
8551 tree value, purpose, type;
8552 HOST_WIDE_INT val[2];
8553 const char *p, *end;
8554 int byte, wchar_bytes, charwidth, bitpos;
8556 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8558 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8559 charwidth = TYPE_PRECISION (char_type_node);
8560 type = TREE_TYPE (constructor_type);
8561 p = TREE_STRING_POINTER (str);
8562 end = p + TREE_STRING_LENGTH (str);
8564 for (purpose = bitsize_zero_node;
8565 p < end
8566 && !(constructor_max_index
8567 && tree_int_cst_lt (constructor_max_index, purpose));
8568 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8570 if (wchar_bytes == 1)
8572 val[0] = (unsigned char) *p++;
8573 val[1] = 0;
8575 else
8577 val[1] = 0;
8578 val[0] = 0;
8579 for (byte = 0; byte < wchar_bytes; byte++)
8581 if (BYTES_BIG_ENDIAN)
8582 bitpos = (wchar_bytes - byte - 1) * charwidth;
8583 else
8584 bitpos = byte * charwidth;
8585 val[bitpos % HOST_BITS_PER_WIDE_INT]
8586 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8587 << (bitpos % HOST_BITS_PER_WIDE_INT);
8591 if (!TYPE_UNSIGNED (type))
8593 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8594 if (bitpos < HOST_BITS_PER_WIDE_INT)
8596 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8598 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8599 val[1] = -1;
8602 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8604 if (val[0] < 0)
8605 val[1] = -1;
8607 else if (val[1] & (((HOST_WIDE_INT) 1)
8608 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8609 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8612 value = wide_int_to_tree (type,
8613 wide_int::from_array (val, 2,
8614 HOST_BITS_PER_WIDE_INT * 2));
8615 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8616 braced_init_obstack);
8619 constructor_incremental = 0;
8622 /* Return value of FIELD in pending initializer or zero if the field was
8623 not initialized yet. */
8625 static tree
8626 find_init_member (tree field, struct obstack * braced_init_obstack)
8628 struct init_node *p;
8630 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8632 if (constructor_incremental
8633 && tree_int_cst_lt (field, constructor_unfilled_index))
8634 set_nonincremental_init (braced_init_obstack);
8636 p = constructor_pending_elts;
8637 while (p)
8639 if (tree_int_cst_lt (field, p->purpose))
8640 p = p->left;
8641 else if (tree_int_cst_lt (p->purpose, field))
8642 p = p->right;
8643 else
8644 return p->value;
8647 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8649 tree bitpos = bit_position (field);
8651 if (constructor_incremental
8652 && (!constructor_unfilled_fields
8653 || tree_int_cst_lt (bitpos,
8654 bit_position (constructor_unfilled_fields))))
8655 set_nonincremental_init (braced_init_obstack);
8657 p = constructor_pending_elts;
8658 while (p)
8660 if (field == p->purpose)
8661 return p->value;
8662 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8663 p = p->left;
8664 else
8665 p = p->right;
8668 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8670 if (!vec_safe_is_empty (constructor_elements)
8671 && (constructor_elements->last ().index == field))
8672 return constructor_elements->last ().value;
8674 return 0;
8677 /* "Output" the next constructor element.
8678 At top level, really output it to assembler code now.
8679 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8680 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8681 TYPE is the data type that the containing data type wants here.
8682 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8683 If VALUE is a string constant, STRICT_STRING is true if it is
8684 unparenthesized or we should not warn here for it being parenthesized.
8685 For other types of VALUE, STRICT_STRING is not used.
8687 PENDING if non-nil means output pending elements that belong
8688 right after this element. (PENDING is normally 1;
8689 it is 0 while outputting pending elements, to avoid recursion.)
8691 IMPLICIT is true if value comes from pop_init_level (1),
8692 the new initializer has been merged with the existing one
8693 and thus no warnings should be emitted about overriding an
8694 existing initializer. */
8696 static void
8697 output_init_element (location_t loc, tree value, tree origtype,
8698 bool strict_string, tree type, tree field, int pending,
8699 bool implicit, struct obstack * braced_init_obstack)
8701 tree semantic_type = NULL_TREE;
8702 bool maybe_const = true;
8703 bool npc;
8705 if (type == error_mark_node || value == error_mark_node)
8707 constructor_erroneous = 1;
8708 return;
8710 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8711 && (TREE_CODE (value) == STRING_CST
8712 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8713 && !(TREE_CODE (value) == STRING_CST
8714 && TREE_CODE (type) == ARRAY_TYPE
8715 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8716 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8717 TYPE_MAIN_VARIANT (type)))
8718 value = array_to_pointer_conversion (input_location, value);
8720 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8721 && require_constant_value && pending)
8723 /* As an extension, allow initializing objects with static storage
8724 duration with compound literals (which are then treated just as
8725 the brace enclosed list they contain). */
8726 if (flag_isoc99)
8727 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8728 "constant");
8729 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8730 value = DECL_INITIAL (decl);
8733 npc = null_pointer_constant_p (value);
8734 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8736 semantic_type = TREE_TYPE (value);
8737 value = TREE_OPERAND (value, 0);
8739 value = c_fully_fold (value, require_constant_value, &maybe_const);
8741 if (value == error_mark_node)
8742 constructor_erroneous = 1;
8743 else if (!TREE_CONSTANT (value))
8744 constructor_constant = 0;
8745 else if (!initializer_constant_valid_p (value,
8746 TREE_TYPE (value),
8747 AGGREGATE_TYPE_P (constructor_type)
8748 && TYPE_REVERSE_STORAGE_ORDER
8749 (constructor_type))
8750 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8751 && DECL_C_BIT_FIELD (field)
8752 && TREE_CODE (value) != INTEGER_CST))
8753 constructor_simple = 0;
8754 if (!maybe_const)
8755 constructor_nonconst = 1;
8757 /* Digest the initializer and issue any errors about incompatible
8758 types before issuing errors about non-constant initializers. */
8759 tree new_value = value;
8760 if (semantic_type)
8761 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8762 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8763 require_constant_value);
8764 if (new_value == error_mark_node)
8766 constructor_erroneous = 1;
8767 return;
8769 if (require_constant_value || require_constant_elements)
8770 constant_expression_warning (new_value);
8772 /* Proceed to check the constness of the original initializer. */
8773 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8775 if (require_constant_value)
8777 error_init (loc, "initializer element is not constant");
8778 value = error_mark_node;
8780 else if (require_constant_elements)
8781 pedwarn (loc, OPT_Wpedantic,
8782 "initializer element is not computable at load time");
8784 else if (!maybe_const
8785 && (require_constant_value || require_constant_elements))
8786 pedwarn_init (loc, OPT_Wpedantic,
8787 "initializer element is not a constant expression");
8789 /* Issue -Wc++-compat warnings about initializing a bitfield with
8790 enum type. */
8791 if (warn_cxx_compat
8792 && field != NULL_TREE
8793 && TREE_CODE (field) == FIELD_DECL
8794 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8795 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8796 != TYPE_MAIN_VARIANT (type))
8797 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8799 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8800 if (checktype != error_mark_node
8801 && (TYPE_MAIN_VARIANT (checktype)
8802 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8803 warning_init (loc, OPT_Wc___compat,
8804 "enum conversion in initialization is invalid in C++");
8807 /* If this field is empty (and not at the end of structure),
8808 don't do anything other than checking the initializer. */
8809 if (field
8810 && (TREE_TYPE (field) == error_mark_node
8811 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8812 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8813 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8814 || DECL_CHAIN (field)))))
8815 return;
8817 /* Finally, set VALUE to the initializer value digested above. */
8818 value = new_value;
8820 /* If this element doesn't come next in sequence,
8821 put it on constructor_pending_elts. */
8822 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8823 && (!constructor_incremental
8824 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8826 if (constructor_incremental
8827 && tree_int_cst_lt (field, constructor_unfilled_index))
8828 set_nonincremental_init (braced_init_obstack);
8830 add_pending_init (loc, field, value, origtype, implicit,
8831 braced_init_obstack);
8832 return;
8834 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8835 && (!constructor_incremental
8836 || field != constructor_unfilled_fields))
8838 /* We do this for records but not for unions. In a union,
8839 no matter which field is specified, it can be initialized
8840 right away since it starts at the beginning of the union. */
8841 if (constructor_incremental)
8843 if (!constructor_unfilled_fields)
8844 set_nonincremental_init (braced_init_obstack);
8845 else
8847 tree bitpos, unfillpos;
8849 bitpos = bit_position (field);
8850 unfillpos = bit_position (constructor_unfilled_fields);
8852 if (tree_int_cst_lt (bitpos, unfillpos))
8853 set_nonincremental_init (braced_init_obstack);
8857 add_pending_init (loc, field, value, origtype, implicit,
8858 braced_init_obstack);
8859 return;
8861 else if (TREE_CODE (constructor_type) == UNION_TYPE
8862 && !vec_safe_is_empty (constructor_elements))
8864 if (!implicit)
8866 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8867 warning_init (loc, OPT_Woverride_init_side_effects,
8868 "initialized field with side-effects overwritten");
8869 else if (warn_override_init)
8870 warning_init (loc, OPT_Woverride_init,
8871 "initialized field overwritten");
8874 /* We can have just one union field set. */
8875 constructor_elements = NULL;
8878 /* Otherwise, output this element either to
8879 constructor_elements or to the assembler file. */
8881 constructor_elt celt = {field, value};
8882 vec_safe_push (constructor_elements, celt);
8884 /* Advance the variable that indicates sequential elements output. */
8885 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8886 constructor_unfilled_index
8887 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8888 bitsize_one_node);
8889 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8891 constructor_unfilled_fields
8892 = DECL_CHAIN (constructor_unfilled_fields);
8894 /* Skip any nameless bit fields. */
8895 while (constructor_unfilled_fields != 0
8896 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8897 && DECL_NAME (constructor_unfilled_fields) == 0)
8898 constructor_unfilled_fields =
8899 DECL_CHAIN (constructor_unfilled_fields);
8901 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8902 constructor_unfilled_fields = 0;
8904 /* Now output any pending elements which have become next. */
8905 if (pending)
8906 output_pending_init_elements (0, braced_init_obstack);
8909 /* Output any pending elements which have become next.
8910 As we output elements, constructor_unfilled_{fields,index}
8911 advances, which may cause other elements to become next;
8912 if so, they too are output.
8914 If ALL is 0, we return when there are
8915 no more pending elements to output now.
8917 If ALL is 1, we output space as necessary so that
8918 we can output all the pending elements. */
8919 static void
8920 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8922 struct init_node *elt = constructor_pending_elts;
8923 tree next;
8925 retry:
8927 /* Look through the whole pending tree.
8928 If we find an element that should be output now,
8929 output it. Otherwise, set NEXT to the element
8930 that comes first among those still pending. */
8932 next = 0;
8933 while (elt)
8935 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8937 if (tree_int_cst_equal (elt->purpose,
8938 constructor_unfilled_index))
8939 output_init_element (input_location, elt->value, elt->origtype,
8940 true, TREE_TYPE (constructor_type),
8941 constructor_unfilled_index, 0, false,
8942 braced_init_obstack);
8943 else if (tree_int_cst_lt (constructor_unfilled_index,
8944 elt->purpose))
8946 /* Advance to the next smaller node. */
8947 if (elt->left)
8948 elt = elt->left;
8949 else
8951 /* We have reached the smallest node bigger than the
8952 current unfilled index. Fill the space first. */
8953 next = elt->purpose;
8954 break;
8957 else
8959 /* Advance to the next bigger node. */
8960 if (elt->right)
8961 elt = elt->right;
8962 else
8964 /* We have reached the biggest node in a subtree. Find
8965 the parent of it, which is the next bigger node. */
8966 while (elt->parent && elt->parent->right == elt)
8967 elt = elt->parent;
8968 elt = elt->parent;
8969 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8970 elt->purpose))
8972 next = elt->purpose;
8973 break;
8978 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8980 tree ctor_unfilled_bitpos, elt_bitpos;
8982 /* If the current record is complete we are done. */
8983 if (constructor_unfilled_fields == 0)
8984 break;
8986 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8987 elt_bitpos = bit_position (elt->purpose);
8988 /* We can't compare fields here because there might be empty
8989 fields in between. */
8990 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8992 constructor_unfilled_fields = elt->purpose;
8993 output_init_element (input_location, elt->value, elt->origtype,
8994 true, TREE_TYPE (elt->purpose),
8995 elt->purpose, 0, false,
8996 braced_init_obstack);
8998 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9000 /* Advance to the next smaller node. */
9001 if (elt->left)
9002 elt = elt->left;
9003 else
9005 /* We have reached the smallest node bigger than the
9006 current unfilled field. Fill the space first. */
9007 next = elt->purpose;
9008 break;
9011 else
9013 /* Advance to the next bigger node. */
9014 if (elt->right)
9015 elt = elt->right;
9016 else
9018 /* We have reached the biggest node in a subtree. Find
9019 the parent of it, which is the next bigger node. */
9020 while (elt->parent && elt->parent->right == elt)
9021 elt = elt->parent;
9022 elt = elt->parent;
9023 if (elt
9024 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9025 bit_position (elt->purpose))))
9027 next = elt->purpose;
9028 break;
9035 /* Ordinarily return, but not if we want to output all
9036 and there are elements left. */
9037 if (!(all && next != 0))
9038 return;
9040 /* If it's not incremental, just skip over the gap, so that after
9041 jumping to retry we will output the next successive element. */
9042 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9043 constructor_unfilled_fields = next;
9044 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9045 constructor_unfilled_index = next;
9047 /* ELT now points to the node in the pending tree with the next
9048 initializer to output. */
9049 goto retry;
9052 /* Add one non-braced element to the current constructor level.
9053 This adjusts the current position within the constructor's type.
9054 This may also start or terminate implicit levels
9055 to handle a partly-braced initializer.
9057 Once this has found the correct level for the new element,
9058 it calls output_init_element.
9060 IMPLICIT is true if value comes from pop_init_level (1),
9061 the new initializer has been merged with the existing one
9062 and thus no warnings should be emitted about overriding an
9063 existing initializer. */
9065 void
9066 process_init_element (location_t loc, struct c_expr value, bool implicit,
9067 struct obstack * braced_init_obstack)
9069 tree orig_value = value.value;
9070 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9071 bool strict_string = value.original_code == STRING_CST;
9072 bool was_designated = designator_depth != 0;
9074 designator_depth = 0;
9075 designator_erroneous = 0;
9077 if (!implicit && value.value && !integer_zerop (value.value))
9078 constructor_zeroinit = 0;
9080 /* Handle superfluous braces around string cst as in
9081 char x[] = {"foo"}; */
9082 if (string_flag
9083 && constructor_type
9084 && !was_designated
9085 && TREE_CODE (constructor_type) == ARRAY_TYPE
9086 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9087 && integer_zerop (constructor_unfilled_index))
9089 if (constructor_stack->replacement_value.value)
9090 error_init (loc, "excess elements in char array initializer");
9091 constructor_stack->replacement_value = value;
9092 return;
9095 if (constructor_stack->replacement_value.value != 0)
9097 error_init (loc, "excess elements in struct initializer");
9098 return;
9101 /* Ignore elements of a brace group if it is entirely superfluous
9102 and has already been diagnosed. */
9103 if (constructor_type == 0)
9104 return;
9106 if (!implicit && warn_designated_init && !was_designated
9107 && TREE_CODE (constructor_type) == RECORD_TYPE
9108 && lookup_attribute ("designated_init",
9109 TYPE_ATTRIBUTES (constructor_type)))
9110 warning_init (loc,
9111 OPT_Wdesignated_init,
9112 "positional initialization of field "
9113 "in %<struct%> declared with %<designated_init%> attribute");
9115 /* If we've exhausted any levels that didn't have braces,
9116 pop them now. */
9117 while (constructor_stack->implicit)
9119 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9120 && constructor_fields == 0)
9121 process_init_element (loc,
9122 pop_init_level (loc, 1, braced_init_obstack),
9123 true, braced_init_obstack);
9124 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9125 || VECTOR_TYPE_P (constructor_type))
9126 && constructor_max_index
9127 && tree_int_cst_lt (constructor_max_index,
9128 constructor_index))
9129 process_init_element (loc,
9130 pop_init_level (loc, 1, braced_init_obstack),
9131 true, braced_init_obstack);
9132 else
9133 break;
9136 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9137 if (constructor_range_stack)
9139 /* If value is a compound literal and we'll be just using its
9140 content, don't put it into a SAVE_EXPR. */
9141 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9142 || !require_constant_value)
9144 tree semantic_type = NULL_TREE;
9145 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9147 semantic_type = TREE_TYPE (value.value);
9148 value.value = TREE_OPERAND (value.value, 0);
9150 value.value = c_save_expr (value.value);
9151 if (semantic_type)
9152 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9153 value.value);
9157 while (1)
9159 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9161 tree fieldtype;
9162 enum tree_code fieldcode;
9164 if (constructor_fields == 0)
9166 pedwarn_init (loc, 0, "excess elements in struct initializer");
9167 break;
9170 fieldtype = TREE_TYPE (constructor_fields);
9171 if (fieldtype != error_mark_node)
9172 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9173 fieldcode = TREE_CODE (fieldtype);
9175 /* Error for non-static initialization of a flexible array member. */
9176 if (fieldcode == ARRAY_TYPE
9177 && !require_constant_value
9178 && TYPE_SIZE (fieldtype) == NULL_TREE
9179 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9181 error_init (loc, "non-static initialization of a flexible "
9182 "array member");
9183 break;
9186 /* Error for initialization of a flexible array member with
9187 a string constant if the structure is in an array. E.g.:
9188 struct S { int x; char y[]; };
9189 struct S s[] = { { 1, "foo" } };
9190 is invalid. */
9191 if (string_flag
9192 && fieldcode == ARRAY_TYPE
9193 && constructor_depth > 1
9194 && TYPE_SIZE (fieldtype) == NULL_TREE
9195 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9197 bool in_array_p = false;
9198 for (struct constructor_stack *p = constructor_stack;
9199 p && p->type; p = p->next)
9200 if (TREE_CODE (p->type) == ARRAY_TYPE)
9202 in_array_p = true;
9203 break;
9205 if (in_array_p)
9207 error_init (loc, "initialization of flexible array "
9208 "member in a nested context");
9209 break;
9213 /* Accept a string constant to initialize a subarray. */
9214 if (value.value != 0
9215 && fieldcode == ARRAY_TYPE
9216 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9217 && string_flag)
9218 value.value = orig_value;
9219 /* Otherwise, if we have come to a subaggregate,
9220 and we don't have an element of its type, push into it. */
9221 else if (value.value != 0
9222 && value.value != error_mark_node
9223 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9224 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9225 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9227 push_init_level (loc, 1, braced_init_obstack);
9228 continue;
9231 if (value.value)
9233 push_member_name (constructor_fields);
9234 output_init_element (loc, value.value, value.original_type,
9235 strict_string, fieldtype,
9236 constructor_fields, 1, implicit,
9237 braced_init_obstack);
9238 RESTORE_SPELLING_DEPTH (constructor_depth);
9240 else
9241 /* Do the bookkeeping for an element that was
9242 directly output as a constructor. */
9244 /* For a record, keep track of end position of last field. */
9245 if (DECL_SIZE (constructor_fields))
9246 constructor_bit_index
9247 = size_binop_loc (input_location, PLUS_EXPR,
9248 bit_position (constructor_fields),
9249 DECL_SIZE (constructor_fields));
9251 /* If the current field was the first one not yet written out,
9252 it isn't now, so update. */
9253 if (constructor_unfilled_fields == constructor_fields)
9255 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9256 /* Skip any nameless bit fields. */
9257 while (constructor_unfilled_fields != 0
9258 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9259 && DECL_NAME (constructor_unfilled_fields) == 0)
9260 constructor_unfilled_fields =
9261 DECL_CHAIN (constructor_unfilled_fields);
9265 constructor_fields = DECL_CHAIN (constructor_fields);
9266 /* Skip any nameless bit fields at the beginning. */
9267 while (constructor_fields != 0
9268 && DECL_C_BIT_FIELD (constructor_fields)
9269 && DECL_NAME (constructor_fields) == 0)
9270 constructor_fields = DECL_CHAIN (constructor_fields);
9272 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9274 tree fieldtype;
9275 enum tree_code fieldcode;
9277 if (constructor_fields == 0)
9279 pedwarn_init (loc, 0,
9280 "excess elements in union initializer");
9281 break;
9284 fieldtype = TREE_TYPE (constructor_fields);
9285 if (fieldtype != error_mark_node)
9286 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9287 fieldcode = TREE_CODE (fieldtype);
9289 /* Warn that traditional C rejects initialization of unions.
9290 We skip the warning if the value is zero. This is done
9291 under the assumption that the zero initializer in user
9292 code appears conditioned on e.g. __STDC__ to avoid
9293 "missing initializer" warnings and relies on default
9294 initialization to zero in the traditional C case.
9295 We also skip the warning if the initializer is designated,
9296 again on the assumption that this must be conditional on
9297 __STDC__ anyway (and we've already complained about the
9298 member-designator already). */
9299 if (!in_system_header_at (input_location) && !constructor_designated
9300 && !(value.value && (integer_zerop (value.value)
9301 || real_zerop (value.value))))
9302 warning (OPT_Wtraditional, "traditional C rejects initialization "
9303 "of unions");
9305 /* Accept a string constant to initialize a subarray. */
9306 if (value.value != 0
9307 && fieldcode == ARRAY_TYPE
9308 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9309 && string_flag)
9310 value.value = orig_value;
9311 /* Otherwise, if we have come to a subaggregate,
9312 and we don't have an element of its type, push into it. */
9313 else if (value.value != 0
9314 && value.value != error_mark_node
9315 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9316 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9317 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9319 push_init_level (loc, 1, braced_init_obstack);
9320 continue;
9323 if (value.value)
9325 push_member_name (constructor_fields);
9326 output_init_element (loc, value.value, value.original_type,
9327 strict_string, fieldtype,
9328 constructor_fields, 1, implicit,
9329 braced_init_obstack);
9330 RESTORE_SPELLING_DEPTH (constructor_depth);
9332 else
9333 /* Do the bookkeeping for an element that was
9334 directly output as a constructor. */
9336 constructor_bit_index = DECL_SIZE (constructor_fields);
9337 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9340 constructor_fields = 0;
9342 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9344 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9345 enum tree_code eltcode = TREE_CODE (elttype);
9347 /* Accept a string constant to initialize a subarray. */
9348 if (value.value != 0
9349 && eltcode == ARRAY_TYPE
9350 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9351 && string_flag)
9352 value.value = orig_value;
9353 /* Otherwise, if we have come to a subaggregate,
9354 and we don't have an element of its type, push into it. */
9355 else if (value.value != 0
9356 && value.value != error_mark_node
9357 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9358 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9359 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9361 push_init_level (loc, 1, braced_init_obstack);
9362 continue;
9365 if (constructor_max_index != 0
9366 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9367 || integer_all_onesp (constructor_max_index)))
9369 pedwarn_init (loc, 0,
9370 "excess elements in array initializer");
9371 break;
9374 /* Now output the actual element. */
9375 if (value.value)
9377 push_array_bounds (tree_to_uhwi (constructor_index));
9378 output_init_element (loc, value.value, value.original_type,
9379 strict_string, elttype,
9380 constructor_index, 1, implicit,
9381 braced_init_obstack);
9382 RESTORE_SPELLING_DEPTH (constructor_depth);
9385 constructor_index
9386 = size_binop_loc (input_location, PLUS_EXPR,
9387 constructor_index, bitsize_one_node);
9389 if (!value.value)
9390 /* If we are doing the bookkeeping for an element that was
9391 directly output as a constructor, we must update
9392 constructor_unfilled_index. */
9393 constructor_unfilled_index = constructor_index;
9395 else if (VECTOR_TYPE_P (constructor_type))
9397 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9399 /* Do a basic check of initializer size. Note that vectors
9400 always have a fixed size derived from their type. */
9401 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9403 pedwarn_init (loc, 0,
9404 "excess elements in vector initializer");
9405 break;
9408 /* Now output the actual element. */
9409 if (value.value)
9411 if (TREE_CODE (value.value) == VECTOR_CST)
9412 elttype = TYPE_MAIN_VARIANT (constructor_type);
9413 output_init_element (loc, value.value, value.original_type,
9414 strict_string, elttype,
9415 constructor_index, 1, implicit,
9416 braced_init_obstack);
9419 constructor_index
9420 = size_binop_loc (input_location,
9421 PLUS_EXPR, constructor_index, bitsize_one_node);
9423 if (!value.value)
9424 /* If we are doing the bookkeeping for an element that was
9425 directly output as a constructor, we must update
9426 constructor_unfilled_index. */
9427 constructor_unfilled_index = constructor_index;
9430 /* Handle the sole element allowed in a braced initializer
9431 for a scalar variable. */
9432 else if (constructor_type != error_mark_node
9433 && constructor_fields == 0)
9435 pedwarn_init (loc, 0,
9436 "excess elements in scalar initializer");
9437 break;
9439 else
9441 if (value.value)
9442 output_init_element (loc, value.value, value.original_type,
9443 strict_string, constructor_type,
9444 NULL_TREE, 1, implicit,
9445 braced_init_obstack);
9446 constructor_fields = 0;
9449 /* Handle range initializers either at this level or anywhere higher
9450 in the designator stack. */
9451 if (constructor_range_stack)
9453 struct constructor_range_stack *p, *range_stack;
9454 int finish = 0;
9456 range_stack = constructor_range_stack;
9457 constructor_range_stack = 0;
9458 while (constructor_stack != range_stack->stack)
9460 gcc_assert (constructor_stack->implicit);
9461 process_init_element (loc,
9462 pop_init_level (loc, 1,
9463 braced_init_obstack),
9464 true, braced_init_obstack);
9466 for (p = range_stack;
9467 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9468 p = p->prev)
9470 gcc_assert (constructor_stack->implicit);
9471 process_init_element (loc,
9472 pop_init_level (loc, 1,
9473 braced_init_obstack),
9474 true, braced_init_obstack);
9477 p->index = size_binop_loc (input_location,
9478 PLUS_EXPR, p->index, bitsize_one_node);
9479 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9480 finish = 1;
9482 while (1)
9484 constructor_index = p->index;
9485 constructor_fields = p->fields;
9486 if (finish && p->range_end && p->index == p->range_start)
9488 finish = 0;
9489 p->prev = 0;
9491 p = p->next;
9492 if (!p)
9493 break;
9494 finish_implicit_inits (loc, braced_init_obstack);
9495 push_init_level (loc, 2, braced_init_obstack);
9496 p->stack = constructor_stack;
9497 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9498 p->index = p->range_start;
9501 if (!finish)
9502 constructor_range_stack = range_stack;
9503 continue;
9506 break;
9509 constructor_range_stack = 0;
9512 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9513 (guaranteed to be 'volatile' or null) and ARGS (represented using
9514 an ASM_EXPR node). */
9515 tree
9516 build_asm_stmt (tree cv_qualifier, tree args)
9518 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9519 ASM_VOLATILE_P (args) = 1;
9520 return add_stmt (args);
9523 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9524 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9525 SIMPLE indicates whether there was anything at all after the
9526 string in the asm expression -- asm("blah") and asm("blah" : )
9527 are subtly different. We use a ASM_EXPR node to represent this. */
9528 tree
9529 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9530 tree clobbers, tree labels, bool simple)
9532 tree tail;
9533 tree args;
9534 int i;
9535 const char *constraint;
9536 const char **oconstraints;
9537 bool allows_mem, allows_reg, is_inout;
9538 int ninputs, noutputs;
9540 ninputs = list_length (inputs);
9541 noutputs = list_length (outputs);
9542 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9544 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9546 /* Remove output conversions that change the type but not the mode. */
9547 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9549 tree output = TREE_VALUE (tail);
9551 output = c_fully_fold (output, false, NULL);
9553 /* ??? Really, this should not be here. Users should be using a
9554 proper lvalue, dammit. But there's a long history of using casts
9555 in the output operands. In cases like longlong.h, this becomes a
9556 primitive form of typechecking -- if the cast can be removed, then
9557 the output operand had a type of the proper width; otherwise we'll
9558 get an error. Gross, but ... */
9559 STRIP_NOPS (output);
9561 if (!lvalue_or_else (loc, output, lv_asm))
9562 output = error_mark_node;
9564 if (output != error_mark_node
9565 && (TREE_READONLY (output)
9566 || TYPE_READONLY (TREE_TYPE (output))
9567 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9568 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9569 readonly_error (loc, output, lv_asm);
9571 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9572 oconstraints[i] = constraint;
9574 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9575 &allows_mem, &allows_reg, &is_inout))
9577 /* If the operand is going to end up in memory,
9578 mark it addressable. */
9579 if (!allows_reg && !c_mark_addressable (output))
9580 output = error_mark_node;
9581 if (!(!allows_reg && allows_mem)
9582 && output != error_mark_node
9583 && VOID_TYPE_P (TREE_TYPE (output)))
9585 error_at (loc, "invalid use of void expression");
9586 output = error_mark_node;
9589 else
9590 output = error_mark_node;
9592 TREE_VALUE (tail) = output;
9595 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9597 tree input;
9599 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9600 input = TREE_VALUE (tail);
9602 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9603 oconstraints, &allows_mem, &allows_reg))
9605 /* If the operand is going to end up in memory,
9606 mark it addressable. */
9607 if (!allows_reg && allows_mem)
9609 input = c_fully_fold (input, false, NULL);
9611 /* Strip the nops as we allow this case. FIXME, this really
9612 should be rejected or made deprecated. */
9613 STRIP_NOPS (input);
9614 if (!c_mark_addressable (input))
9615 input = error_mark_node;
9617 else
9619 struct c_expr expr;
9620 memset (&expr, 0, sizeof (expr));
9621 expr.value = input;
9622 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9623 input = c_fully_fold (expr.value, false, NULL);
9625 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9627 error_at (loc, "invalid use of void expression");
9628 input = error_mark_node;
9632 else
9633 input = error_mark_node;
9635 TREE_VALUE (tail) = input;
9638 /* ASMs with labels cannot have outputs. This should have been
9639 enforced by the parser. */
9640 gcc_assert (outputs == NULL || labels == NULL);
9642 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9644 /* asm statements without outputs, including simple ones, are treated
9645 as volatile. */
9646 ASM_INPUT_P (args) = simple;
9647 ASM_VOLATILE_P (args) = (noutputs == 0);
9649 return args;
9652 /* Generate a goto statement to LABEL. LOC is the location of the
9653 GOTO. */
9655 tree
9656 c_finish_goto_label (location_t loc, tree label)
9658 tree decl = lookup_label_for_goto (loc, label);
9659 if (!decl)
9660 return NULL_TREE;
9661 TREE_USED (decl) = 1;
9663 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9664 SET_EXPR_LOCATION (t, loc);
9665 return add_stmt (t);
9669 /* Generate a computed goto statement to EXPR. LOC is the location of
9670 the GOTO. */
9672 tree
9673 c_finish_goto_ptr (location_t loc, tree expr)
9675 tree t;
9676 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9677 expr = c_fully_fold (expr, false, NULL);
9678 expr = convert (ptr_type_node, expr);
9679 t = build1 (GOTO_EXPR, void_type_node, expr);
9680 SET_EXPR_LOCATION (t, loc);
9681 return add_stmt (t);
9684 /* Generate a C `return' statement. RETVAL is the expression for what
9685 to return, or a null pointer for `return;' with no value. LOC is
9686 the location of the return statement, or the location of the expression,
9687 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9688 is the original type of RETVAL. */
9690 tree
9691 c_finish_return (location_t loc, tree retval, tree origtype)
9693 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9694 bool no_warning = false;
9695 bool npc = false;
9696 size_t rank = 0;
9698 /* Use the expansion point to handle cases such as returning NULL
9699 in a function returning void. */
9700 source_location xloc = expansion_point_location_if_in_system_header (loc);
9702 if (TREE_THIS_VOLATILE (current_function_decl))
9703 warning_at (xloc, 0,
9704 "function declared %<noreturn%> has a %<return%> statement");
9706 if (flag_cilkplus && contains_array_notation_expr (retval))
9708 /* Array notations are allowed in a return statement if it is inside a
9709 built-in array notation reduction function. */
9710 if (!find_rank (loc, retval, retval, false, &rank))
9711 return error_mark_node;
9712 if (rank >= 1)
9714 error_at (loc, "array notation expression cannot be used as a "
9715 "return value");
9716 return error_mark_node;
9719 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9721 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9722 "allowed");
9723 return error_mark_node;
9725 if (retval)
9727 tree semantic_type = NULL_TREE;
9728 npc = null_pointer_constant_p (retval);
9729 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9731 semantic_type = TREE_TYPE (retval);
9732 retval = TREE_OPERAND (retval, 0);
9734 retval = c_fully_fold (retval, false, NULL);
9735 if (semantic_type)
9736 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9739 if (!retval)
9741 current_function_returns_null = 1;
9742 if ((warn_return_type || flag_isoc99)
9743 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9745 bool warned_here;
9746 if (flag_isoc99)
9747 warned_here = pedwarn
9748 (loc, 0,
9749 "%<return%> with no value, in function returning non-void");
9750 else
9751 warned_here = warning_at
9752 (loc, OPT_Wreturn_type,
9753 "%<return%> with no value, in function returning non-void");
9754 no_warning = true;
9755 if (warned_here)
9756 inform (DECL_SOURCE_LOCATION (current_function_decl),
9757 "declared here");
9760 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9762 current_function_returns_null = 1;
9763 bool warned_here;
9764 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9765 warned_here = pedwarn
9766 (xloc, 0,
9767 "%<return%> with a value, in function returning void");
9768 else
9769 warned_here = pedwarn
9770 (xloc, OPT_Wpedantic, "ISO C forbids "
9771 "%<return%> with expression, in function returning void");
9772 if (warned_here)
9773 inform (DECL_SOURCE_LOCATION (current_function_decl),
9774 "declared here");
9776 else
9778 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9779 retval, origtype, ic_return,
9780 npc, NULL_TREE, NULL_TREE, 0);
9781 tree res = DECL_RESULT (current_function_decl);
9782 tree inner;
9783 bool save;
9785 current_function_returns_value = 1;
9786 if (t == error_mark_node)
9787 return NULL_TREE;
9789 save = in_late_binary_op;
9790 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9791 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9792 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9793 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9794 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9795 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9796 in_late_binary_op = true;
9797 inner = t = convert (TREE_TYPE (res), t);
9798 in_late_binary_op = save;
9800 /* Strip any conversions, additions, and subtractions, and see if
9801 we are returning the address of a local variable. Warn if so. */
9802 while (1)
9804 switch (TREE_CODE (inner))
9806 CASE_CONVERT:
9807 case NON_LVALUE_EXPR:
9808 case PLUS_EXPR:
9809 case POINTER_PLUS_EXPR:
9810 inner = TREE_OPERAND (inner, 0);
9811 continue;
9813 case MINUS_EXPR:
9814 /* If the second operand of the MINUS_EXPR has a pointer
9815 type (or is converted from it), this may be valid, so
9816 don't give a warning. */
9818 tree op1 = TREE_OPERAND (inner, 1);
9820 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9821 && (CONVERT_EXPR_P (op1)
9822 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9823 op1 = TREE_OPERAND (op1, 0);
9825 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9826 break;
9828 inner = TREE_OPERAND (inner, 0);
9829 continue;
9832 case ADDR_EXPR:
9833 inner = TREE_OPERAND (inner, 0);
9835 while (REFERENCE_CLASS_P (inner)
9836 && !INDIRECT_REF_P (inner))
9837 inner = TREE_OPERAND (inner, 0);
9839 if (DECL_P (inner)
9840 && !DECL_EXTERNAL (inner)
9841 && !TREE_STATIC (inner)
9842 && DECL_CONTEXT (inner) == current_function_decl)
9844 if (TREE_CODE (inner) == LABEL_DECL)
9845 warning_at (loc, OPT_Wreturn_local_addr,
9846 "function returns address of label");
9847 else
9849 warning_at (loc, OPT_Wreturn_local_addr,
9850 "function returns address of local variable");
9851 tree zero = build_zero_cst (TREE_TYPE (res));
9852 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9855 break;
9857 default:
9858 break;
9861 break;
9864 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9865 SET_EXPR_LOCATION (retval, loc);
9867 if (warn_sequence_point)
9868 verify_sequence_points (retval);
9871 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9872 TREE_NO_WARNING (ret_stmt) |= no_warning;
9873 return add_stmt (ret_stmt);
9876 struct c_switch {
9877 /* The SWITCH_EXPR being built. */
9878 tree switch_expr;
9880 /* The original type of the testing expression, i.e. before the
9881 default conversion is applied. */
9882 tree orig_type;
9884 /* A splay-tree mapping the low element of a case range to the high
9885 element, or NULL_TREE if there is no high element. Used to
9886 determine whether or not a new case label duplicates an old case
9887 label. We need a tree, rather than simply a hash table, because
9888 of the GNU case range extension. */
9889 splay_tree cases;
9891 /* The bindings at the point of the switch. This is used for
9892 warnings crossing decls when branching to a case label. */
9893 struct c_spot_bindings *bindings;
9895 /* The next node on the stack. */
9896 struct c_switch *next;
9898 /* Remember whether the controlling expression had boolean type
9899 before integer promotions for the sake of -Wswitch-bool. */
9900 bool bool_cond_p;
9902 /* Remember whether there was a case value that is outside the
9903 range of the ORIG_TYPE. */
9904 bool outside_range_p;
9907 /* A stack of the currently active switch statements. The innermost
9908 switch statement is on the top of the stack. There is no need to
9909 mark the stack for garbage collection because it is only active
9910 during the processing of the body of a function, and we never
9911 collect at that point. */
9913 struct c_switch *c_switch_stack;
9915 /* Start a C switch statement, testing expression EXP. Return the new
9916 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9917 SWITCH_COND_LOC is the location of the switch's condition.
9918 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9920 tree
9921 c_start_case (location_t switch_loc,
9922 location_t switch_cond_loc,
9923 tree exp, bool explicit_cast_p)
9925 tree orig_type = error_mark_node;
9926 bool bool_cond_p = false;
9927 struct c_switch *cs;
9929 if (exp != error_mark_node)
9931 orig_type = TREE_TYPE (exp);
9933 if (!INTEGRAL_TYPE_P (orig_type))
9935 if (orig_type != error_mark_node)
9937 error_at (switch_cond_loc, "switch quantity not an integer");
9938 orig_type = error_mark_node;
9940 exp = integer_zero_node;
9942 else
9944 tree type = TYPE_MAIN_VARIANT (orig_type);
9945 tree e = exp;
9947 /* Warn if the condition has boolean value. */
9948 while (TREE_CODE (e) == COMPOUND_EXPR)
9949 e = TREE_OPERAND (e, 1);
9951 if ((TREE_CODE (type) == BOOLEAN_TYPE
9952 || truth_value_p (TREE_CODE (e)))
9953 /* Explicit cast to int suppresses this warning. */
9954 && !(TREE_CODE (type) == INTEGER_TYPE
9955 && explicit_cast_p))
9956 bool_cond_p = true;
9958 if (!in_system_header_at (input_location)
9959 && (type == long_integer_type_node
9960 || type == long_unsigned_type_node))
9961 warning_at (switch_cond_loc,
9962 OPT_Wtraditional, "%<long%> switch expression not "
9963 "converted to %<int%> in ISO C");
9965 exp = c_fully_fold (exp, false, NULL);
9966 exp = default_conversion (exp);
9968 if (warn_sequence_point)
9969 verify_sequence_points (exp);
9973 /* Add this new SWITCH_EXPR to the stack. */
9974 cs = XNEW (struct c_switch);
9975 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9976 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9977 cs->orig_type = orig_type;
9978 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9979 cs->bindings = c_get_switch_bindings ();
9980 cs->bool_cond_p = bool_cond_p;
9981 cs->outside_range_p = false;
9982 cs->next = c_switch_stack;
9983 c_switch_stack = cs;
9985 return add_stmt (cs->switch_expr);
9988 /* Process a case label at location LOC. */
9990 tree
9991 do_case (location_t loc, tree low_value, tree high_value)
9993 tree label = NULL_TREE;
9995 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9997 low_value = c_fully_fold (low_value, false, NULL);
9998 if (TREE_CODE (low_value) == INTEGER_CST)
9999 pedwarn (loc, OPT_Wpedantic,
10000 "case label is not an integer constant expression");
10003 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10005 high_value = c_fully_fold (high_value, false, NULL);
10006 if (TREE_CODE (high_value) == INTEGER_CST)
10007 pedwarn (input_location, OPT_Wpedantic,
10008 "case label is not an integer constant expression");
10011 if (c_switch_stack == NULL)
10013 if (low_value)
10014 error_at (loc, "case label not within a switch statement");
10015 else
10016 error_at (loc, "%<default%> label not within a switch statement");
10017 return NULL_TREE;
10020 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10021 EXPR_LOCATION (c_switch_stack->switch_expr),
10022 loc))
10023 return NULL_TREE;
10025 label = c_add_case_label (loc, c_switch_stack->cases,
10026 SWITCH_COND (c_switch_stack->switch_expr),
10027 c_switch_stack->orig_type,
10028 low_value, high_value,
10029 &c_switch_stack->outside_range_p);
10030 if (label == error_mark_node)
10031 label = NULL_TREE;
10032 return label;
10035 /* Finish the switch statement. TYPE is the original type of the
10036 controlling expression of the switch, or NULL_TREE. */
10038 void
10039 c_finish_case (tree body, tree type)
10041 struct c_switch *cs = c_switch_stack;
10042 location_t switch_location;
10044 SWITCH_BODY (cs->switch_expr) = body;
10046 /* Emit warnings as needed. */
10047 switch_location = EXPR_LOCATION (cs->switch_expr);
10048 c_do_switch_warnings (cs->cases, switch_location,
10049 type ? type : TREE_TYPE (cs->switch_expr),
10050 SWITCH_COND (cs->switch_expr),
10051 cs->bool_cond_p, cs->outside_range_p);
10053 /* Pop the stack. */
10054 c_switch_stack = cs->next;
10055 splay_tree_delete (cs->cases);
10056 c_release_switch_bindings (cs->bindings);
10057 XDELETE (cs);
10060 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10061 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10062 may be null. */
10064 void
10065 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10066 tree else_block)
10068 tree stmt;
10070 /* If the condition has array notations, then the rank of the then_block and
10071 else_block must be either 0 or be equal to the rank of the condition. If
10072 the condition does not have array notations then break them up as it is
10073 broken up in a normal expression. */
10074 if (flag_cilkplus && contains_array_notation_expr (cond))
10076 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10077 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10078 return;
10079 if (then_block
10080 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10081 return;
10082 if (else_block
10083 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10084 return;
10085 if (cond_rank != then_rank && then_rank != 0)
10087 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10088 " and the then-block");
10089 return;
10091 else if (cond_rank != else_rank && else_rank != 0)
10093 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10094 " and the else-block");
10095 return;
10099 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10100 SET_EXPR_LOCATION (stmt, if_locus);
10101 add_stmt (stmt);
10104 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10105 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10106 is false for DO loops. INCR is the FOR increment expression. BODY is
10107 the statement controlled by the loop. BLAB is the break label. CLAB is
10108 the continue label. Everything is allowed to be NULL. */
10110 void
10111 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10112 tree blab, tree clab, bool cond_is_first)
10114 tree entry = NULL, exit = NULL, t;
10116 /* In theory could forbid cilk spawn for loop increment expression,
10117 but it should work just fine. */
10119 /* If the condition is zero don't generate a loop construct. */
10120 if (cond && integer_zerop (cond))
10122 if (cond_is_first)
10124 t = build_and_jump (&blab);
10125 SET_EXPR_LOCATION (t, start_locus);
10126 add_stmt (t);
10129 else
10131 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10133 /* If we have an exit condition, then we build an IF with gotos either
10134 out of the loop, or to the top of it. If there's no exit condition,
10135 then we just build a jump back to the top. */
10136 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10138 if (cond && !integer_nonzerop (cond))
10140 /* Canonicalize the loop condition to the end. This means
10141 generating a branch to the loop condition. Reuse the
10142 continue label, if possible. */
10143 if (cond_is_first)
10145 if (incr || !clab)
10147 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10148 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10150 else
10151 t = build1 (GOTO_EXPR, void_type_node, clab);
10152 SET_EXPR_LOCATION (t, start_locus);
10153 add_stmt (t);
10156 t = build_and_jump (&blab);
10157 if (cond_is_first)
10158 exit = fold_build3_loc (start_locus,
10159 COND_EXPR, void_type_node, cond, exit, t);
10160 else
10161 exit = fold_build3_loc (input_location,
10162 COND_EXPR, void_type_node, cond, exit, t);
10164 else
10166 /* For the backward-goto's location of an unconditional loop
10167 use the beginning of the body, or, if there is none, the
10168 top of the loop. */
10169 location_t loc = EXPR_LOCATION (expr_first (body));
10170 if (loc == UNKNOWN_LOCATION)
10171 loc = start_locus;
10172 SET_EXPR_LOCATION (exit, loc);
10175 add_stmt (top);
10178 if (body)
10179 add_stmt (body);
10180 if (clab)
10181 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10182 if (incr)
10183 add_stmt (incr);
10184 if (entry)
10185 add_stmt (entry);
10186 if (exit)
10187 add_stmt (exit);
10188 if (blab)
10189 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10192 tree
10193 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10195 bool skip;
10196 tree label = *label_p;
10198 /* In switch statements break is sometimes stylistically used after
10199 a return statement. This can lead to spurious warnings about
10200 control reaching the end of a non-void function when it is
10201 inlined. Note that we are calling block_may_fallthru with
10202 language specific tree nodes; this works because
10203 block_may_fallthru returns true when given something it does not
10204 understand. */
10205 skip = !block_may_fallthru (cur_stmt_list);
10207 if (!label)
10209 if (!skip)
10210 *label_p = label = create_artificial_label (loc);
10212 else if (TREE_CODE (label) == LABEL_DECL)
10214 else switch (TREE_INT_CST_LOW (label))
10216 case 0:
10217 if (is_break)
10218 error_at (loc, "break statement not within loop or switch");
10219 else
10220 error_at (loc, "continue statement not within a loop");
10221 return NULL_TREE;
10223 case 1:
10224 gcc_assert (is_break);
10225 error_at (loc, "break statement used with OpenMP for loop");
10226 return NULL_TREE;
10228 case 2:
10229 if (is_break)
10230 error ("break statement within %<#pragma simd%> loop body");
10231 else
10232 error ("continue statement within %<#pragma simd%> loop body");
10233 return NULL_TREE;
10235 default:
10236 gcc_unreachable ();
10239 if (skip)
10240 return NULL_TREE;
10242 if (!is_break)
10243 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10245 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10248 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10250 static void
10251 emit_side_effect_warnings (location_t loc, tree expr)
10253 if (expr == error_mark_node)
10255 else if (!TREE_SIDE_EFFECTS (expr))
10257 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10258 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10260 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10262 tree r = expr;
10263 location_t cloc = loc;
10264 while (TREE_CODE (r) == COMPOUND_EXPR)
10266 if (EXPR_HAS_LOCATION (r))
10267 cloc = EXPR_LOCATION (r);
10268 r = TREE_OPERAND (r, 1);
10270 if (!TREE_SIDE_EFFECTS (r)
10271 && !VOID_TYPE_P (TREE_TYPE (r))
10272 && !CONVERT_EXPR_P (r)
10273 && !TREE_NO_WARNING (r)
10274 && !TREE_NO_WARNING (expr))
10275 warning_at (cloc, OPT_Wunused_value,
10276 "right-hand operand of comma expression has no effect");
10278 else
10279 warn_if_unused_value (expr, loc);
10282 /* Process an expression as if it were a complete statement. Emit
10283 diagnostics, but do not call ADD_STMT. LOC is the location of the
10284 statement. */
10286 tree
10287 c_process_expr_stmt (location_t loc, tree expr)
10289 tree exprv;
10291 if (!expr)
10292 return NULL_TREE;
10294 expr = c_fully_fold (expr, false, NULL);
10296 if (warn_sequence_point)
10297 verify_sequence_points (expr);
10299 if (TREE_TYPE (expr) != error_mark_node
10300 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10301 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10302 error_at (loc, "expression statement has incomplete type");
10304 /* If we're not processing a statement expression, warn about unused values.
10305 Warnings for statement expressions will be emitted later, once we figure
10306 out which is the result. */
10307 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10308 && warn_unused_value)
10309 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10311 exprv = expr;
10312 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10313 exprv = TREE_OPERAND (exprv, 1);
10314 while (CONVERT_EXPR_P (exprv))
10315 exprv = TREE_OPERAND (exprv, 0);
10316 if (DECL_P (exprv)
10317 || handled_component_p (exprv)
10318 || TREE_CODE (exprv) == ADDR_EXPR)
10319 mark_exp_read (exprv);
10321 /* If the expression is not of a type to which we cannot assign a line
10322 number, wrap the thing in a no-op NOP_EXPR. */
10323 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10325 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10326 SET_EXPR_LOCATION (expr, loc);
10329 return expr;
10332 /* Emit an expression as a statement. LOC is the location of the
10333 expression. */
10335 tree
10336 c_finish_expr_stmt (location_t loc, tree expr)
10338 if (expr)
10339 return add_stmt (c_process_expr_stmt (loc, expr));
10340 else
10341 return NULL;
10344 /* Do the opposite and emit a statement as an expression. To begin,
10345 create a new binding level and return it. */
10347 tree
10348 c_begin_stmt_expr (void)
10350 tree ret;
10352 /* We must force a BLOCK for this level so that, if it is not expanded
10353 later, there is a way to turn off the entire subtree of blocks that
10354 are contained in it. */
10355 keep_next_level ();
10356 ret = c_begin_compound_stmt (true);
10358 c_bindings_start_stmt_expr (c_switch_stack == NULL
10359 ? NULL
10360 : c_switch_stack->bindings);
10362 /* Mark the current statement list as belonging to a statement list. */
10363 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10365 return ret;
10368 /* LOC is the location of the compound statement to which this body
10369 belongs. */
10371 tree
10372 c_finish_stmt_expr (location_t loc, tree body)
10374 tree last, type, tmp, val;
10375 tree *last_p;
10377 body = c_end_compound_stmt (loc, body, true);
10379 c_bindings_end_stmt_expr (c_switch_stack == NULL
10380 ? NULL
10381 : c_switch_stack->bindings);
10383 /* Locate the last statement in BODY. See c_end_compound_stmt
10384 about always returning a BIND_EXPR. */
10385 last_p = &BIND_EXPR_BODY (body);
10386 last = BIND_EXPR_BODY (body);
10388 continue_searching:
10389 if (TREE_CODE (last) == STATEMENT_LIST)
10391 tree_stmt_iterator i;
10393 /* This can happen with degenerate cases like ({ }). No value. */
10394 if (!TREE_SIDE_EFFECTS (last))
10395 return body;
10397 /* If we're supposed to generate side effects warnings, process
10398 all of the statements except the last. */
10399 if (warn_unused_value)
10401 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10403 location_t tloc;
10404 tree t = tsi_stmt (i);
10406 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10407 emit_side_effect_warnings (tloc, t);
10410 else
10411 i = tsi_last (last);
10412 last_p = tsi_stmt_ptr (i);
10413 last = *last_p;
10416 /* If the end of the list is exception related, then the list was split
10417 by a call to push_cleanup. Continue searching. */
10418 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10419 || TREE_CODE (last) == TRY_CATCH_EXPR)
10421 last_p = &TREE_OPERAND (last, 0);
10422 last = *last_p;
10423 goto continue_searching;
10426 if (last == error_mark_node)
10427 return last;
10429 /* In the case that the BIND_EXPR is not necessary, return the
10430 expression out from inside it. */
10431 if (last == BIND_EXPR_BODY (body)
10432 && BIND_EXPR_VARS (body) == NULL)
10434 /* Even if this looks constant, do not allow it in a constant
10435 expression. */
10436 last = c_wrap_maybe_const (last, true);
10437 /* Do not warn if the return value of a statement expression is
10438 unused. */
10439 TREE_NO_WARNING (last) = 1;
10440 return last;
10443 /* Extract the type of said expression. */
10444 type = TREE_TYPE (last);
10446 /* If we're not returning a value at all, then the BIND_EXPR that
10447 we already have is a fine expression to return. */
10448 if (!type || VOID_TYPE_P (type))
10449 return body;
10451 /* Now that we've located the expression containing the value, it seems
10452 silly to make voidify_wrapper_expr repeat the process. Create a
10453 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10454 tmp = create_tmp_var_raw (type);
10456 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10457 tree_expr_nonnegative_p giving up immediately. */
10458 val = last;
10459 if (TREE_CODE (val) == NOP_EXPR
10460 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10461 val = TREE_OPERAND (val, 0);
10463 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10464 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10467 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10468 SET_EXPR_LOCATION (t, loc);
10469 return t;
10473 /* Begin and end compound statements. This is as simple as pushing
10474 and popping new statement lists from the tree. */
10476 tree
10477 c_begin_compound_stmt (bool do_scope)
10479 tree stmt = push_stmt_list ();
10480 if (do_scope)
10481 push_scope ();
10482 return stmt;
10485 /* End a compound statement. STMT is the statement. LOC is the
10486 location of the compound statement-- this is usually the location
10487 of the opening brace. */
10489 tree
10490 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10492 tree block = NULL;
10494 if (do_scope)
10496 if (c_dialect_objc ())
10497 objc_clear_super_receiver ();
10498 block = pop_scope ();
10501 stmt = pop_stmt_list (stmt);
10502 stmt = c_build_bind_expr (loc, block, stmt);
10504 /* If this compound statement is nested immediately inside a statement
10505 expression, then force a BIND_EXPR to be created. Otherwise we'll
10506 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10507 STATEMENT_LISTs merge, and thus we can lose track of what statement
10508 was really last. */
10509 if (building_stmt_list_p ()
10510 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10511 && TREE_CODE (stmt) != BIND_EXPR)
10513 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10514 TREE_SIDE_EFFECTS (stmt) = 1;
10515 SET_EXPR_LOCATION (stmt, loc);
10518 return stmt;
10521 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10522 when the current scope is exited. EH_ONLY is true when this is not
10523 meant to apply to normal control flow transfer. */
10525 void
10526 push_cleanup (tree decl, tree cleanup, bool eh_only)
10528 enum tree_code code;
10529 tree stmt, list;
10530 bool stmt_expr;
10532 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10533 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10534 add_stmt (stmt);
10535 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10536 list = push_stmt_list ();
10537 TREE_OPERAND (stmt, 0) = list;
10538 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10541 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10542 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10544 static tree
10545 build_vec_cmp (tree_code code, tree type,
10546 tree arg0, tree arg1)
10548 tree zero_vec = build_zero_cst (type);
10549 tree minus_one_vec = build_minus_one_cst (type);
10550 tree cmp_type = build_same_sized_truth_vector_type (type);
10551 tree cmp = build2 (code, cmp_type, arg0, arg1);
10552 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10555 /* Build a binary-operation expression without default conversions.
10556 CODE is the kind of expression to build.
10557 LOCATION is the operator's location.
10558 This function differs from `build' in several ways:
10559 the data type of the result is computed and recorded in it,
10560 warnings are generated if arg data types are invalid,
10561 special handling for addition and subtraction of pointers is known,
10562 and some optimization is done (operations on narrow ints
10563 are done in the narrower type when that gives the same result).
10564 Constant folding is also done before the result is returned.
10566 Note that the operands will never have enumeral types, or function
10567 or array types, because either they will have the default conversions
10568 performed or they have both just been converted to some other type in which
10569 the arithmetic is to be done. */
10571 tree
10572 build_binary_op (location_t location, enum tree_code code,
10573 tree orig_op0, tree orig_op1, int convert_p)
10575 tree type0, type1, orig_type0, orig_type1;
10576 tree eptype;
10577 enum tree_code code0, code1;
10578 tree op0, op1;
10579 tree ret = error_mark_node;
10580 const char *invalid_op_diag;
10581 bool op0_int_operands, op1_int_operands;
10582 bool int_const, int_const_or_overflow, int_operands;
10584 /* Expression code to give to the expression when it is built.
10585 Normally this is CODE, which is what the caller asked for,
10586 but in some special cases we change it. */
10587 enum tree_code resultcode = code;
10589 /* Data type in which the computation is to be performed.
10590 In the simplest cases this is the common type of the arguments. */
10591 tree result_type = NULL;
10593 /* When the computation is in excess precision, the type of the
10594 final EXCESS_PRECISION_EXPR. */
10595 tree semantic_result_type = NULL;
10597 /* Nonzero means operands have already been type-converted
10598 in whatever way is necessary.
10599 Zero means they need to be converted to RESULT_TYPE. */
10600 int converted = 0;
10602 /* Nonzero means create the expression with this type, rather than
10603 RESULT_TYPE. */
10604 tree build_type = 0;
10606 /* Nonzero means after finally constructing the expression
10607 convert it to this type. */
10608 tree final_type = 0;
10610 /* Nonzero if this is an operation like MIN or MAX which can
10611 safely be computed in short if both args are promoted shorts.
10612 Also implies COMMON.
10613 -1 indicates a bitwise operation; this makes a difference
10614 in the exact conditions for when it is safe to do the operation
10615 in a narrower mode. */
10616 int shorten = 0;
10618 /* Nonzero if this is a comparison operation;
10619 if both args are promoted shorts, compare the original shorts.
10620 Also implies COMMON. */
10621 int short_compare = 0;
10623 /* Nonzero if this is a right-shift operation, which can be computed on the
10624 original short and then promoted if the operand is a promoted short. */
10625 int short_shift = 0;
10627 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10628 int common = 0;
10630 /* True means types are compatible as far as ObjC is concerned. */
10631 bool objc_ok;
10633 /* True means this is an arithmetic operation that may need excess
10634 precision. */
10635 bool may_need_excess_precision;
10637 /* True means this is a boolean operation that converts both its
10638 operands to truth-values. */
10639 bool boolean_op = false;
10641 /* Remember whether we're doing / or %. */
10642 bool doing_div_or_mod = false;
10644 /* Remember whether we're doing << or >>. */
10645 bool doing_shift = false;
10647 /* Tree holding instrumentation expression. */
10648 tree instrument_expr = NULL;
10650 if (location == UNKNOWN_LOCATION)
10651 location = input_location;
10653 op0 = orig_op0;
10654 op1 = orig_op1;
10656 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10657 if (op0_int_operands)
10658 op0 = remove_c_maybe_const_expr (op0);
10659 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10660 if (op1_int_operands)
10661 op1 = remove_c_maybe_const_expr (op1);
10662 int_operands = (op0_int_operands && op1_int_operands);
10663 if (int_operands)
10665 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10666 && TREE_CODE (orig_op1) == INTEGER_CST);
10667 int_const = (int_const_or_overflow
10668 && !TREE_OVERFLOW (orig_op0)
10669 && !TREE_OVERFLOW (orig_op1));
10671 else
10672 int_const = int_const_or_overflow = false;
10674 /* Do not apply default conversion in mixed vector/scalar expression. */
10675 if (convert_p
10676 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10678 op0 = default_conversion (op0);
10679 op1 = default_conversion (op1);
10682 /* When Cilk Plus is enabled and there are array notations inside op0, then
10683 we check to see if there are builtin array notation functions. If
10684 so, then we take on the type of the array notation inside it. */
10685 if (flag_cilkplus && contains_array_notation_expr (op0))
10686 orig_type0 = type0 = find_correct_array_notation_type (op0);
10687 else
10688 orig_type0 = type0 = TREE_TYPE (op0);
10690 if (flag_cilkplus && contains_array_notation_expr (op1))
10691 orig_type1 = type1 = find_correct_array_notation_type (op1);
10692 else
10693 orig_type1 = type1 = TREE_TYPE (op1);
10695 /* The expression codes of the data types of the arguments tell us
10696 whether the arguments are integers, floating, pointers, etc. */
10697 code0 = TREE_CODE (type0);
10698 code1 = TREE_CODE (type1);
10700 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10701 STRIP_TYPE_NOPS (op0);
10702 STRIP_TYPE_NOPS (op1);
10704 /* If an error was already reported for one of the arguments,
10705 avoid reporting another error. */
10707 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10708 return error_mark_node;
10710 if (code0 == POINTER_TYPE
10711 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10712 return error_mark_node;
10714 if (code1 == POINTER_TYPE
10715 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10716 return error_mark_node;
10718 if ((invalid_op_diag
10719 = targetm.invalid_binary_op (code, type0, type1)))
10721 error_at (location, invalid_op_diag);
10722 return error_mark_node;
10725 switch (code)
10727 case PLUS_EXPR:
10728 case MINUS_EXPR:
10729 case MULT_EXPR:
10730 case TRUNC_DIV_EXPR:
10731 case CEIL_DIV_EXPR:
10732 case FLOOR_DIV_EXPR:
10733 case ROUND_DIV_EXPR:
10734 case EXACT_DIV_EXPR:
10735 may_need_excess_precision = true;
10736 break;
10737 default:
10738 may_need_excess_precision = false;
10739 break;
10741 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10743 op0 = TREE_OPERAND (op0, 0);
10744 type0 = TREE_TYPE (op0);
10746 else if (may_need_excess_precision
10747 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10749 type0 = eptype;
10750 op0 = convert (eptype, op0);
10752 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10754 op1 = TREE_OPERAND (op1, 0);
10755 type1 = TREE_TYPE (op1);
10757 else if (may_need_excess_precision
10758 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10760 type1 = eptype;
10761 op1 = convert (eptype, op1);
10764 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10766 /* In case when one of the operands of the binary operation is
10767 a vector and another is a scalar -- convert scalar to vector. */
10768 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10770 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10771 true);
10773 switch (convert_flag)
10775 case stv_error:
10776 return error_mark_node;
10777 case stv_firstarg:
10779 bool maybe_const = true;
10780 tree sc;
10781 sc = c_fully_fold (op0, false, &maybe_const);
10782 sc = save_expr (sc);
10783 sc = convert (TREE_TYPE (type1), sc);
10784 op0 = build_vector_from_val (type1, sc);
10785 if (!maybe_const)
10786 op0 = c_wrap_maybe_const (op0, true);
10787 orig_type0 = type0 = TREE_TYPE (op0);
10788 code0 = TREE_CODE (type0);
10789 converted = 1;
10790 break;
10792 case stv_secondarg:
10794 bool maybe_const = true;
10795 tree sc;
10796 sc = c_fully_fold (op1, false, &maybe_const);
10797 sc = save_expr (sc);
10798 sc = convert (TREE_TYPE (type0), sc);
10799 op1 = build_vector_from_val (type0, sc);
10800 if (!maybe_const)
10801 op1 = c_wrap_maybe_const (op1, true);
10802 orig_type1 = type1 = TREE_TYPE (op1);
10803 code1 = TREE_CODE (type1);
10804 converted = 1;
10805 break;
10807 default:
10808 break;
10812 switch (code)
10814 case PLUS_EXPR:
10815 /* Handle the pointer + int case. */
10816 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10818 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10819 goto return_build_binary_op;
10821 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10823 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10824 goto return_build_binary_op;
10826 else
10827 common = 1;
10828 break;
10830 case MINUS_EXPR:
10831 /* Subtraction of two similar pointers.
10832 We must subtract them as integers, then divide by object size. */
10833 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10834 && comp_target_types (location, type0, type1))
10836 ret = pointer_diff (location, op0, op1);
10837 goto return_build_binary_op;
10839 /* Handle pointer minus int. Just like pointer plus int. */
10840 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10842 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10843 goto return_build_binary_op;
10845 else
10846 common = 1;
10847 break;
10849 case MULT_EXPR:
10850 common = 1;
10851 break;
10853 case TRUNC_DIV_EXPR:
10854 case CEIL_DIV_EXPR:
10855 case FLOOR_DIV_EXPR:
10856 case ROUND_DIV_EXPR:
10857 case EXACT_DIV_EXPR:
10858 doing_div_or_mod = true;
10859 warn_for_div_by_zero (location, op1);
10861 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10862 || code0 == FIXED_POINT_TYPE
10863 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10864 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10865 || code1 == FIXED_POINT_TYPE
10866 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10868 enum tree_code tcode0 = code0, tcode1 = code1;
10870 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10871 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10872 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10873 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10875 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10876 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10877 resultcode = RDIV_EXPR;
10878 else
10879 /* Although it would be tempting to shorten always here, that
10880 loses on some targets, since the modulo instruction is
10881 undefined if the quotient can't be represented in the
10882 computation mode. We shorten only if unsigned or if
10883 dividing by something we know != -1. */
10884 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10885 || (TREE_CODE (op1) == INTEGER_CST
10886 && !integer_all_onesp (op1)));
10887 common = 1;
10889 break;
10891 case BIT_AND_EXPR:
10892 case BIT_IOR_EXPR:
10893 case BIT_XOR_EXPR:
10894 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10895 shorten = -1;
10896 /* Allow vector types which are not floating point types. */
10897 else if (code0 == VECTOR_TYPE
10898 && code1 == VECTOR_TYPE
10899 && !VECTOR_FLOAT_TYPE_P (type0)
10900 && !VECTOR_FLOAT_TYPE_P (type1))
10901 common = 1;
10902 break;
10904 case TRUNC_MOD_EXPR:
10905 case FLOOR_MOD_EXPR:
10906 doing_div_or_mod = true;
10907 warn_for_div_by_zero (location, op1);
10909 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10910 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10911 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10912 common = 1;
10913 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10915 /* Although it would be tempting to shorten always here, that loses
10916 on some targets, since the modulo instruction is undefined if the
10917 quotient can't be represented in the computation mode. We shorten
10918 only if unsigned or if dividing by something we know != -1. */
10919 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10920 || (TREE_CODE (op1) == INTEGER_CST
10921 && !integer_all_onesp (op1)));
10922 common = 1;
10924 break;
10926 case TRUTH_ANDIF_EXPR:
10927 case TRUTH_ORIF_EXPR:
10928 case TRUTH_AND_EXPR:
10929 case TRUTH_OR_EXPR:
10930 case TRUTH_XOR_EXPR:
10931 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10932 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10933 || code0 == FIXED_POINT_TYPE)
10934 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10935 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10936 || code1 == FIXED_POINT_TYPE))
10938 /* Result of these operations is always an int,
10939 but that does not mean the operands should be
10940 converted to ints! */
10941 result_type = integer_type_node;
10942 if (op0_int_operands)
10944 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10945 op0 = remove_c_maybe_const_expr (op0);
10947 else
10948 op0 = c_objc_common_truthvalue_conversion (location, op0);
10949 if (op1_int_operands)
10951 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10952 op1 = remove_c_maybe_const_expr (op1);
10954 else
10955 op1 = c_objc_common_truthvalue_conversion (location, op1);
10956 converted = 1;
10957 boolean_op = true;
10959 if (code == TRUTH_ANDIF_EXPR)
10961 int_const_or_overflow = (int_operands
10962 && TREE_CODE (orig_op0) == INTEGER_CST
10963 && (op0 == truthvalue_false_node
10964 || TREE_CODE (orig_op1) == INTEGER_CST));
10965 int_const = (int_const_or_overflow
10966 && !TREE_OVERFLOW (orig_op0)
10967 && (op0 == truthvalue_false_node
10968 || !TREE_OVERFLOW (orig_op1)));
10970 else if (code == TRUTH_ORIF_EXPR)
10972 int_const_or_overflow = (int_operands
10973 && TREE_CODE (orig_op0) == INTEGER_CST
10974 && (op0 == truthvalue_true_node
10975 || TREE_CODE (orig_op1) == INTEGER_CST));
10976 int_const = (int_const_or_overflow
10977 && !TREE_OVERFLOW (orig_op0)
10978 && (op0 == truthvalue_true_node
10979 || !TREE_OVERFLOW (orig_op1)));
10981 break;
10983 /* Shift operations: result has same type as first operand;
10984 always convert second operand to int.
10985 Also set SHORT_SHIFT if shifting rightward. */
10987 case RSHIFT_EXPR:
10988 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10989 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10991 result_type = type0;
10992 converted = 1;
10994 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10995 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10996 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10997 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10999 result_type = type0;
11000 converted = 1;
11002 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
11003 && code1 == INTEGER_TYPE)
11005 doing_shift = true;
11006 if (TREE_CODE (op1) == INTEGER_CST)
11008 if (tree_int_cst_sgn (op1) < 0)
11010 int_const = false;
11011 if (c_inhibit_evaluation_warnings == 0)
11012 warning_at (location, OPT_Wshift_count_negative,
11013 "right shift count is negative");
11015 else
11017 if (!integer_zerop (op1))
11018 short_shift = 1;
11020 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11022 int_const = false;
11023 if (c_inhibit_evaluation_warnings == 0)
11024 warning_at (location, OPT_Wshift_count_overflow,
11025 "right shift count >= width of type");
11030 /* Use the type of the value to be shifted. */
11031 result_type = type0;
11032 /* Avoid converting op1 to result_type later. */
11033 converted = 1;
11035 break;
11037 case LSHIFT_EXPR:
11038 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
11039 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
11041 result_type = type0;
11042 converted = 1;
11044 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11045 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11046 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11047 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11049 result_type = type0;
11050 converted = 1;
11052 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
11053 && code1 == INTEGER_TYPE)
11055 doing_shift = true;
11056 if (TREE_CODE (op0) == INTEGER_CST
11057 && tree_int_cst_sgn (op0) < 0)
11059 /* Don't reject a left shift of a negative value in a context
11060 where a constant expression is needed in C90. */
11061 if (flag_isoc99)
11062 int_const = false;
11063 if (c_inhibit_evaluation_warnings == 0)
11064 warning_at (location, OPT_Wshift_negative_value,
11065 "left shift of negative value");
11067 if (TREE_CODE (op1) == INTEGER_CST)
11069 if (tree_int_cst_sgn (op1) < 0)
11071 int_const = false;
11072 if (c_inhibit_evaluation_warnings == 0)
11073 warning_at (location, OPT_Wshift_count_negative,
11074 "left shift count is negative");
11076 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11078 int_const = false;
11079 if (c_inhibit_evaluation_warnings == 0)
11080 warning_at (location, OPT_Wshift_count_overflow,
11081 "left shift count >= width of type");
11083 else if (TREE_CODE (op0) == INTEGER_CST
11084 && maybe_warn_shift_overflow (location, op0, op1)
11085 && flag_isoc99)
11086 int_const = false;
11089 /* Use the type of the value to be shifted. */
11090 result_type = type0;
11091 /* Avoid converting op1 to result_type later. */
11092 converted = 1;
11094 break;
11096 case EQ_EXPR:
11097 case NE_EXPR:
11098 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11100 tree intt;
11101 if (!vector_types_compatible_elements_p (type0, type1))
11103 error_at (location, "comparing vectors with different "
11104 "element types");
11105 return error_mark_node;
11108 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11110 error_at (location, "comparing vectors with different "
11111 "number of elements");
11112 return error_mark_node;
11115 /* It's not precisely specified how the usual arithmetic
11116 conversions apply to the vector types. Here, we use
11117 the unsigned type if one of the operands is signed and
11118 the other one is unsigned. */
11119 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11121 if (!TYPE_UNSIGNED (type0))
11122 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11123 else
11124 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11125 warning_at (location, OPT_Wsign_compare, "comparison between "
11126 "types %qT and %qT", type0, type1);
11129 /* Always construct signed integer vector type. */
11130 intt = c_common_type_for_size (GET_MODE_BITSIZE
11131 (TYPE_MODE (TREE_TYPE (type0))), 0);
11132 result_type = build_opaque_vector_type (intt,
11133 TYPE_VECTOR_SUBPARTS (type0));
11134 converted = 1;
11135 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11136 goto return_build_binary_op;
11138 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11139 warning_at (location,
11140 OPT_Wfloat_equal,
11141 "comparing floating point with == or != is unsafe");
11142 /* Result of comparison is always int,
11143 but don't convert the args to int! */
11144 build_type = integer_type_node;
11145 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11146 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11147 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11148 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11149 short_compare = 1;
11150 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11152 if (TREE_CODE (op0) == ADDR_EXPR
11153 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11154 && !from_macro_expansion_at (location))
11156 if (code == EQ_EXPR)
11157 warning_at (location,
11158 OPT_Waddress,
11159 "the comparison will always evaluate as %<false%> "
11160 "for the address of %qD will never be NULL",
11161 TREE_OPERAND (op0, 0));
11162 else
11163 warning_at (location,
11164 OPT_Waddress,
11165 "the comparison will always evaluate as %<true%> "
11166 "for the address of %qD will never be NULL",
11167 TREE_OPERAND (op0, 0));
11169 result_type = type0;
11171 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11173 if (TREE_CODE (op1) == ADDR_EXPR
11174 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11175 && !from_macro_expansion_at (location))
11177 if (code == EQ_EXPR)
11178 warning_at (location,
11179 OPT_Waddress,
11180 "the comparison will always evaluate as %<false%> "
11181 "for the address of %qD will never be NULL",
11182 TREE_OPERAND (op1, 0));
11183 else
11184 warning_at (location,
11185 OPT_Waddress,
11186 "the comparison will always evaluate as %<true%> "
11187 "for the address of %qD will never be NULL",
11188 TREE_OPERAND (op1, 0));
11190 result_type = type1;
11192 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11194 tree tt0 = TREE_TYPE (type0);
11195 tree tt1 = TREE_TYPE (type1);
11196 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11197 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11198 addr_space_t as_common = ADDR_SPACE_GENERIC;
11200 /* Anything compares with void *. void * compares with anything.
11201 Otherwise, the targets must be compatible
11202 and both must be object or both incomplete. */
11203 if (comp_target_types (location, type0, type1))
11204 result_type = common_pointer_type (type0, type1);
11205 else if (!addr_space_superset (as0, as1, &as_common))
11207 error_at (location, "comparison of pointers to "
11208 "disjoint address spaces");
11209 return error_mark_node;
11211 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11213 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11214 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11215 "comparison of %<void *%> with function pointer");
11217 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11219 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11220 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11221 "comparison of %<void *%> with function pointer");
11223 else
11224 /* Avoid warning about the volatile ObjC EH puts on decls. */
11225 if (!objc_ok)
11226 pedwarn (location, 0,
11227 "comparison of distinct pointer types lacks a cast");
11229 if (result_type == NULL_TREE)
11231 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11232 result_type = build_pointer_type
11233 (build_qualified_type (void_type_node, qual));
11236 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11238 result_type = type0;
11239 pedwarn (location, 0, "comparison between pointer and integer");
11241 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11243 result_type = type1;
11244 pedwarn (location, 0, "comparison between pointer and integer");
11246 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11247 || truth_value_p (TREE_CODE (orig_op0)))
11248 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11249 || truth_value_p (TREE_CODE (orig_op1))))
11250 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11251 break;
11253 case LE_EXPR:
11254 case GE_EXPR:
11255 case LT_EXPR:
11256 case GT_EXPR:
11257 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11259 tree intt;
11260 if (!vector_types_compatible_elements_p (type0, type1))
11262 error_at (location, "comparing vectors with different "
11263 "element types");
11264 return error_mark_node;
11267 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11269 error_at (location, "comparing vectors with different "
11270 "number of elements");
11271 return error_mark_node;
11274 /* It's not precisely specified how the usual arithmetic
11275 conversions apply to the vector types. Here, we use
11276 the unsigned type if one of the operands is signed and
11277 the other one is unsigned. */
11278 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11280 if (!TYPE_UNSIGNED (type0))
11281 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11282 else
11283 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11284 warning_at (location, OPT_Wsign_compare, "comparison between "
11285 "types %qT and %qT", type0, type1);
11288 /* Always construct signed integer vector type. */
11289 intt = c_common_type_for_size (GET_MODE_BITSIZE
11290 (TYPE_MODE (TREE_TYPE (type0))), 0);
11291 result_type = build_opaque_vector_type (intt,
11292 TYPE_VECTOR_SUBPARTS (type0));
11293 converted = 1;
11294 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11295 goto return_build_binary_op;
11297 build_type = integer_type_node;
11298 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11299 || code0 == FIXED_POINT_TYPE)
11300 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11301 || code1 == FIXED_POINT_TYPE))
11302 short_compare = 1;
11303 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11305 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11306 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11307 addr_space_t as_common;
11309 if (comp_target_types (location, type0, type1))
11311 result_type = common_pointer_type (type0, type1);
11312 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11313 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11314 pedwarn (location, 0,
11315 "comparison of complete and incomplete pointers");
11316 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11317 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11318 "ordered comparisons of pointers to functions");
11319 else if (null_pointer_constant_p (orig_op0)
11320 || null_pointer_constant_p (orig_op1))
11321 warning_at (location, OPT_Wextra,
11322 "ordered comparison of pointer with null pointer");
11325 else if (!addr_space_superset (as0, as1, &as_common))
11327 error_at (location, "comparison of pointers to "
11328 "disjoint address spaces");
11329 return error_mark_node;
11331 else
11333 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11334 result_type = build_pointer_type
11335 (build_qualified_type (void_type_node, qual));
11336 pedwarn (location, 0,
11337 "comparison of distinct pointer types lacks a cast");
11340 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11342 result_type = type0;
11343 if (pedantic)
11344 pedwarn (location, OPT_Wpedantic,
11345 "ordered comparison of pointer with integer zero");
11346 else if (extra_warnings)
11347 warning_at (location, OPT_Wextra,
11348 "ordered comparison of pointer with integer zero");
11350 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11352 result_type = type1;
11353 if (pedantic)
11354 pedwarn (location, OPT_Wpedantic,
11355 "ordered comparison of pointer with integer zero");
11356 else if (extra_warnings)
11357 warning_at (location, OPT_Wextra,
11358 "ordered comparison of pointer with integer zero");
11360 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11362 result_type = type0;
11363 pedwarn (location, 0, "comparison between pointer and integer");
11365 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11367 result_type = type1;
11368 pedwarn (location, 0, "comparison between pointer and integer");
11370 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11371 || truth_value_p (TREE_CODE (orig_op0)))
11372 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11373 || truth_value_p (TREE_CODE (orig_op1))))
11374 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11375 break;
11377 default:
11378 gcc_unreachable ();
11381 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11382 return error_mark_node;
11384 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11385 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11386 || !vector_types_compatible_elements_p (type0, type1)))
11388 gcc_rich_location richloc (location);
11389 richloc.maybe_add_expr (orig_op0);
11390 richloc.maybe_add_expr (orig_op1);
11391 binary_op_error (&richloc, code, type0, type1);
11392 return error_mark_node;
11395 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11396 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11398 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11399 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11401 bool first_complex = (code0 == COMPLEX_TYPE);
11402 bool second_complex = (code1 == COMPLEX_TYPE);
11403 int none_complex = (!first_complex && !second_complex);
11405 if (shorten || common || short_compare)
11407 result_type = c_common_type (type0, type1);
11408 do_warn_double_promotion (result_type, type0, type1,
11409 "implicit conversion from %qT to %qT "
11410 "to match other operand of binary "
11411 "expression",
11412 location);
11413 if (result_type == error_mark_node)
11414 return error_mark_node;
11417 if (first_complex != second_complex
11418 && (code == PLUS_EXPR
11419 || code == MINUS_EXPR
11420 || code == MULT_EXPR
11421 || (code == TRUNC_DIV_EXPR && first_complex))
11422 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11423 && flag_signed_zeros)
11425 /* An operation on mixed real/complex operands must be
11426 handled specially, but the language-independent code can
11427 more easily optimize the plain complex arithmetic if
11428 -fno-signed-zeros. */
11429 tree real_type = TREE_TYPE (result_type);
11430 tree real, imag;
11431 if (type0 != orig_type0 || type1 != orig_type1)
11433 gcc_assert (may_need_excess_precision && common);
11434 semantic_result_type = c_common_type (orig_type0, orig_type1);
11436 if (first_complex)
11438 if (TREE_TYPE (op0) != result_type)
11439 op0 = convert_and_check (location, result_type, op0);
11440 if (TREE_TYPE (op1) != real_type)
11441 op1 = convert_and_check (location, real_type, op1);
11443 else
11445 if (TREE_TYPE (op0) != real_type)
11446 op0 = convert_and_check (location, real_type, op0);
11447 if (TREE_TYPE (op1) != result_type)
11448 op1 = convert_and_check (location, result_type, op1);
11450 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11451 return error_mark_node;
11452 if (first_complex)
11454 op0 = c_save_expr (op0);
11455 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11456 op0, 1);
11457 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11458 op0, 1);
11459 switch (code)
11461 case MULT_EXPR:
11462 case TRUNC_DIV_EXPR:
11463 op1 = c_save_expr (op1);
11464 imag = build2 (resultcode, real_type, imag, op1);
11465 /* Fall through. */
11466 case PLUS_EXPR:
11467 case MINUS_EXPR:
11468 real = build2 (resultcode, real_type, real, op1);
11469 break;
11470 default:
11471 gcc_unreachable();
11474 else
11476 op1 = c_save_expr (op1);
11477 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11478 op1, 1);
11479 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11480 op1, 1);
11481 switch (code)
11483 case MULT_EXPR:
11484 op0 = c_save_expr (op0);
11485 imag = build2 (resultcode, real_type, op0, imag);
11486 /* Fall through. */
11487 case PLUS_EXPR:
11488 real = build2 (resultcode, real_type, op0, real);
11489 break;
11490 case MINUS_EXPR:
11491 real = build2 (resultcode, real_type, op0, real);
11492 imag = build1 (NEGATE_EXPR, real_type, imag);
11493 break;
11494 default:
11495 gcc_unreachable();
11498 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11499 goto return_build_binary_op;
11502 /* For certain operations (which identify themselves by shorten != 0)
11503 if both args were extended from the same smaller type,
11504 do the arithmetic in that type and then extend.
11506 shorten !=0 and !=1 indicates a bitwise operation.
11507 For them, this optimization is safe only if
11508 both args are zero-extended or both are sign-extended.
11509 Otherwise, we might change the result.
11510 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11511 but calculated in (unsigned short) it would be (unsigned short)-1. */
11513 if (shorten && none_complex)
11515 final_type = result_type;
11516 result_type = shorten_binary_op (result_type, op0, op1,
11517 shorten == -1);
11520 /* Shifts can be shortened if shifting right. */
11522 if (short_shift)
11524 int unsigned_arg;
11525 tree arg0 = get_narrower (op0, &unsigned_arg);
11527 final_type = result_type;
11529 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11530 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11532 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11533 && tree_int_cst_sgn (op1) > 0
11534 /* We can shorten only if the shift count is less than the
11535 number of bits in the smaller type size. */
11536 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11537 /* We cannot drop an unsigned shift after sign-extension. */
11538 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11540 /* Do an unsigned shift if the operand was zero-extended. */
11541 result_type
11542 = c_common_signed_or_unsigned_type (unsigned_arg,
11543 TREE_TYPE (arg0));
11544 /* Convert value-to-be-shifted to that type. */
11545 if (TREE_TYPE (op0) != result_type)
11546 op0 = convert (result_type, op0);
11547 converted = 1;
11551 /* Comparison operations are shortened too but differently.
11552 They identify themselves by setting short_compare = 1. */
11554 if (short_compare)
11556 /* Don't write &op0, etc., because that would prevent op0
11557 from being kept in a register.
11558 Instead, make copies of the our local variables and
11559 pass the copies by reference, then copy them back afterward. */
11560 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11561 enum tree_code xresultcode = resultcode;
11562 tree val
11563 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11564 &xresultcode);
11566 if (val != 0)
11568 ret = val;
11569 goto return_build_binary_op;
11572 op0 = xop0, op1 = xop1;
11573 converted = 1;
11574 resultcode = xresultcode;
11576 if (c_inhibit_evaluation_warnings == 0)
11578 bool op0_maybe_const = true;
11579 bool op1_maybe_const = true;
11580 tree orig_op0_folded, orig_op1_folded;
11582 if (in_late_binary_op)
11584 orig_op0_folded = orig_op0;
11585 orig_op1_folded = orig_op1;
11587 else
11589 /* Fold for the sake of possible warnings, as in
11590 build_conditional_expr. This requires the
11591 "original" values to be folded, not just op0 and
11592 op1. */
11593 c_inhibit_evaluation_warnings++;
11594 op0 = c_fully_fold (op0, require_constant_value,
11595 &op0_maybe_const);
11596 op1 = c_fully_fold (op1, require_constant_value,
11597 &op1_maybe_const);
11598 c_inhibit_evaluation_warnings--;
11599 orig_op0_folded = c_fully_fold (orig_op0,
11600 require_constant_value,
11601 NULL);
11602 orig_op1_folded = c_fully_fold (orig_op1,
11603 require_constant_value,
11604 NULL);
11607 if (warn_sign_compare)
11608 warn_for_sign_compare (location, orig_op0_folded,
11609 orig_op1_folded, op0, op1,
11610 result_type, resultcode);
11611 if (!in_late_binary_op && !int_operands)
11613 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11614 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11615 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11616 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11622 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11623 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11624 Then the expression will be built.
11625 It will be given type FINAL_TYPE if that is nonzero;
11626 otherwise, it will be given type RESULT_TYPE. */
11628 if (!result_type)
11630 gcc_rich_location richloc (location);
11631 richloc.maybe_add_expr (orig_op0);
11632 richloc.maybe_add_expr (orig_op1);
11633 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11634 return error_mark_node;
11637 if (build_type == NULL_TREE)
11639 build_type = result_type;
11640 if ((type0 != orig_type0 || type1 != orig_type1)
11641 && !boolean_op)
11643 gcc_assert (may_need_excess_precision && common);
11644 semantic_result_type = c_common_type (orig_type0, orig_type1);
11648 if (!converted)
11650 op0 = ep_convert_and_check (location, result_type, op0,
11651 semantic_result_type);
11652 op1 = ep_convert_and_check (location, result_type, op1,
11653 semantic_result_type);
11655 /* This can happen if one operand has a vector type, and the other
11656 has a different type. */
11657 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11658 return error_mark_node;
11661 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11662 | SANITIZE_FLOAT_DIVIDE))
11663 && do_ubsan_in_current_function ()
11664 && (doing_div_or_mod || doing_shift)
11665 && !require_constant_value)
11667 /* OP0 and/or OP1 might have side-effects. */
11668 op0 = c_save_expr (op0);
11669 op1 = c_save_expr (op1);
11670 op0 = c_fully_fold (op0, false, NULL);
11671 op1 = c_fully_fold (op1, false, NULL);
11672 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11673 | SANITIZE_FLOAT_DIVIDE)))
11674 instrument_expr = ubsan_instrument_division (location, op0, op1);
11675 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11676 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11679 /* Treat expressions in initializers specially as they can't trap. */
11680 if (int_const_or_overflow)
11681 ret = (require_constant_value
11682 ? fold_build2_initializer_loc (location, resultcode, build_type,
11683 op0, op1)
11684 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11685 else
11686 ret = build2 (resultcode, build_type, op0, op1);
11687 if (final_type != 0)
11688 ret = convert (final_type, ret);
11690 return_build_binary_op:
11691 gcc_assert (ret != error_mark_node);
11692 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11693 ret = (int_operands
11694 ? note_integer_operands (ret)
11695 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11696 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11697 && !in_late_binary_op)
11698 ret = note_integer_operands (ret);
11699 if (semantic_result_type)
11700 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11701 protected_set_expr_location (ret, location);
11703 if (instrument_expr != NULL)
11704 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11705 instrument_expr, ret);
11707 return ret;
11711 /* Convert EXPR to be a truth-value, validating its type for this
11712 purpose. LOCATION is the source location for the expression. */
11714 tree
11715 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11717 bool int_const, int_operands;
11719 switch (TREE_CODE (TREE_TYPE (expr)))
11721 case ARRAY_TYPE:
11722 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11723 return error_mark_node;
11725 case RECORD_TYPE:
11726 error_at (location, "used struct type value where scalar is required");
11727 return error_mark_node;
11729 case UNION_TYPE:
11730 error_at (location, "used union type value where scalar is required");
11731 return error_mark_node;
11733 case VOID_TYPE:
11734 error_at (location, "void value not ignored as it ought to be");
11735 return error_mark_node;
11737 case POINTER_TYPE:
11738 if (reject_gcc_builtin (expr))
11739 return error_mark_node;
11740 break;
11742 case FUNCTION_TYPE:
11743 gcc_unreachable ();
11745 case VECTOR_TYPE:
11746 error_at (location, "used vector type where scalar is required");
11747 return error_mark_node;
11749 default:
11750 break;
11753 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11754 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11755 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11757 expr = remove_c_maybe_const_expr (expr);
11758 expr = build2 (NE_EXPR, integer_type_node, expr,
11759 convert (TREE_TYPE (expr), integer_zero_node));
11760 expr = note_integer_operands (expr);
11762 else
11763 /* ??? Should we also give an error for vectors rather than leaving
11764 those to give errors later? */
11765 expr = c_common_truthvalue_conversion (location, expr);
11767 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11769 if (TREE_OVERFLOW (expr))
11770 return expr;
11771 else
11772 return note_integer_operands (expr);
11774 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11775 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11776 return expr;
11780 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11781 required. */
11783 tree
11784 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11786 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11788 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11789 /* Executing a compound literal inside a function reinitializes
11790 it. */
11791 if (!TREE_STATIC (decl))
11792 *se = true;
11793 return decl;
11795 else
11796 return expr;
11799 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11800 statement. LOC is the location of the construct. */
11802 tree
11803 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11804 tree clauses)
11806 body = c_end_compound_stmt (loc, body, true);
11808 tree stmt = make_node (code);
11809 TREE_TYPE (stmt) = void_type_node;
11810 OMP_BODY (stmt) = body;
11811 OMP_CLAUSES (stmt) = clauses;
11812 SET_EXPR_LOCATION (stmt, loc);
11814 return add_stmt (stmt);
11817 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11818 statement. LOC is the location of the OACC_DATA. */
11820 tree
11821 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11823 tree stmt;
11825 block = c_end_compound_stmt (loc, block, true);
11827 stmt = make_node (OACC_DATA);
11828 TREE_TYPE (stmt) = void_type_node;
11829 OACC_DATA_CLAUSES (stmt) = clauses;
11830 OACC_DATA_BODY (stmt) = block;
11831 SET_EXPR_LOCATION (stmt, loc);
11833 return add_stmt (stmt);
11836 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11837 statement. LOC is the location of the OACC_HOST_DATA. */
11839 tree
11840 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11842 tree stmt;
11844 block = c_end_compound_stmt (loc, block, true);
11846 stmt = make_node (OACC_HOST_DATA);
11847 TREE_TYPE (stmt) = void_type_node;
11848 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11849 OACC_HOST_DATA_BODY (stmt) = block;
11850 SET_EXPR_LOCATION (stmt, loc);
11852 return add_stmt (stmt);
11855 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11857 tree
11858 c_begin_omp_parallel (void)
11860 tree block;
11862 keep_next_level ();
11863 block = c_begin_compound_stmt (true);
11865 return block;
11868 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11869 statement. LOC is the location of the OMP_PARALLEL. */
11871 tree
11872 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11874 tree stmt;
11876 block = c_end_compound_stmt (loc, block, true);
11878 stmt = make_node (OMP_PARALLEL);
11879 TREE_TYPE (stmt) = void_type_node;
11880 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11881 OMP_PARALLEL_BODY (stmt) = block;
11882 SET_EXPR_LOCATION (stmt, loc);
11884 return add_stmt (stmt);
11887 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11889 tree
11890 c_begin_omp_task (void)
11892 tree block;
11894 keep_next_level ();
11895 block = c_begin_compound_stmt (true);
11897 return block;
11900 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11901 statement. LOC is the location of the #pragma. */
11903 tree
11904 c_finish_omp_task (location_t loc, tree clauses, tree block)
11906 tree stmt;
11908 block = c_end_compound_stmt (loc, block, true);
11910 stmt = make_node (OMP_TASK);
11911 TREE_TYPE (stmt) = void_type_node;
11912 OMP_TASK_CLAUSES (stmt) = clauses;
11913 OMP_TASK_BODY (stmt) = block;
11914 SET_EXPR_LOCATION (stmt, loc);
11916 return add_stmt (stmt);
11919 /* Generate GOMP_cancel call for #pragma omp cancel. */
11921 void
11922 c_finish_omp_cancel (location_t loc, tree clauses)
11924 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11925 int mask = 0;
11926 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11927 mask = 1;
11928 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11929 mask = 2;
11930 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11931 mask = 4;
11932 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11933 mask = 8;
11934 else
11936 error_at (loc, "%<#pragma omp cancel must specify one of "
11937 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11938 "clauses");
11939 return;
11941 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11942 if (ifc != NULL_TREE)
11944 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11945 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11946 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11947 build_zero_cst (type));
11949 else
11950 ifc = boolean_true_node;
11951 tree stmt = build_call_expr_loc (loc, fn, 2,
11952 build_int_cst (integer_type_node, mask),
11953 ifc);
11954 add_stmt (stmt);
11957 /* Generate GOMP_cancellation_point call for
11958 #pragma omp cancellation point. */
11960 void
11961 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11963 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11964 int mask = 0;
11965 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11966 mask = 1;
11967 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11968 mask = 2;
11969 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11970 mask = 4;
11971 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11972 mask = 8;
11973 else
11975 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11976 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11977 "clauses");
11978 return;
11980 tree stmt = build_call_expr_loc (loc, fn, 1,
11981 build_int_cst (integer_type_node, mask));
11982 add_stmt (stmt);
11985 /* Helper function for handle_omp_array_sections. Called recursively
11986 to handle multiple array-section-subscripts. C is the clause,
11987 T current expression (initially OMP_CLAUSE_DECL), which is either
11988 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11989 expression if specified, TREE_VALUE length expression if specified,
11990 TREE_CHAIN is what it has been specified after, or some decl.
11991 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11992 set to true if any of the array-section-subscript could have length
11993 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11994 first array-section-subscript which is known not to have length
11995 of one. Given say:
11996 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11997 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11998 all are or may have length of 1, array-section-subscript [:2] is the
11999 first one known not to have length 1. For array-section-subscript
12000 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12001 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12002 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12003 case though, as some lengths could be zero. */
12005 static tree
12006 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12007 bool &maybe_zero_len, unsigned int &first_non_one,
12008 enum c_omp_region_type ort)
12010 tree ret, low_bound, length, type;
12011 if (TREE_CODE (t) != TREE_LIST)
12013 if (error_operand_p (t))
12014 return error_mark_node;
12015 ret = t;
12016 if (TREE_CODE (t) == COMPONENT_REF
12017 && ort == C_ORT_OMP
12018 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12019 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12020 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12022 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12024 error_at (OMP_CLAUSE_LOCATION (c),
12025 "bit-field %qE in %qs clause",
12026 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12027 return error_mark_node;
12029 while (TREE_CODE (t) == COMPONENT_REF)
12031 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12033 error_at (OMP_CLAUSE_LOCATION (c),
12034 "%qE is a member of a union", t);
12035 return error_mark_node;
12037 t = TREE_OPERAND (t, 0);
12040 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12042 if (DECL_P (t))
12043 error_at (OMP_CLAUSE_LOCATION (c),
12044 "%qD is not a variable in %qs clause", t,
12045 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12046 else
12047 error_at (OMP_CLAUSE_LOCATION (c),
12048 "%qE is not a variable in %qs clause", t,
12049 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12050 return error_mark_node;
12052 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12053 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12055 error_at (OMP_CLAUSE_LOCATION (c),
12056 "%qD is threadprivate variable in %qs clause", t,
12057 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12058 return error_mark_node;
12060 return ret;
12063 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12064 maybe_zero_len, first_non_one, ort);
12065 if (ret == error_mark_node || ret == NULL_TREE)
12066 return ret;
12068 type = TREE_TYPE (ret);
12069 low_bound = TREE_PURPOSE (t);
12070 length = TREE_VALUE (t);
12072 if (low_bound == error_mark_node || length == error_mark_node)
12073 return error_mark_node;
12075 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12077 error_at (OMP_CLAUSE_LOCATION (c),
12078 "low bound %qE of array section does not have integral type",
12079 low_bound);
12080 return error_mark_node;
12082 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12084 error_at (OMP_CLAUSE_LOCATION (c),
12085 "length %qE of array section does not have integral type",
12086 length);
12087 return error_mark_node;
12089 if (low_bound
12090 && TREE_CODE (low_bound) == INTEGER_CST
12091 && TYPE_PRECISION (TREE_TYPE (low_bound))
12092 > TYPE_PRECISION (sizetype))
12093 low_bound = fold_convert (sizetype, low_bound);
12094 if (length
12095 && TREE_CODE (length) == INTEGER_CST
12096 && TYPE_PRECISION (TREE_TYPE (length))
12097 > TYPE_PRECISION (sizetype))
12098 length = fold_convert (sizetype, length);
12099 if (low_bound == NULL_TREE)
12100 low_bound = integer_zero_node;
12102 if (length != NULL_TREE)
12104 if (!integer_nonzerop (length))
12106 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12107 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12109 if (integer_zerop (length))
12111 error_at (OMP_CLAUSE_LOCATION (c),
12112 "zero length array section in %qs clause",
12113 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12114 return error_mark_node;
12117 else
12118 maybe_zero_len = true;
12120 if (first_non_one == types.length ()
12121 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12122 first_non_one++;
12124 if (TREE_CODE (type) == ARRAY_TYPE)
12126 if (length == NULL_TREE
12127 && (TYPE_DOMAIN (type) == NULL_TREE
12128 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12130 error_at (OMP_CLAUSE_LOCATION (c),
12131 "for unknown bound array type length expression must "
12132 "be specified");
12133 return error_mark_node;
12135 if (TREE_CODE (low_bound) == INTEGER_CST
12136 && tree_int_cst_sgn (low_bound) == -1)
12138 error_at (OMP_CLAUSE_LOCATION (c),
12139 "negative low bound in array section in %qs clause",
12140 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12141 return error_mark_node;
12143 if (length != NULL_TREE
12144 && TREE_CODE (length) == INTEGER_CST
12145 && tree_int_cst_sgn (length) == -1)
12147 error_at (OMP_CLAUSE_LOCATION (c),
12148 "negative length in array section in %qs clause",
12149 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12150 return error_mark_node;
12152 if (TYPE_DOMAIN (type)
12153 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12154 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12155 == INTEGER_CST)
12157 tree size = size_binop (PLUS_EXPR,
12158 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12159 size_one_node);
12160 if (TREE_CODE (low_bound) == INTEGER_CST)
12162 if (tree_int_cst_lt (size, low_bound))
12164 error_at (OMP_CLAUSE_LOCATION (c),
12165 "low bound %qE above array section size "
12166 "in %qs clause", low_bound,
12167 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12168 return error_mark_node;
12170 if (tree_int_cst_equal (size, low_bound))
12172 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12173 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12175 error_at (OMP_CLAUSE_LOCATION (c),
12176 "zero length array section in %qs clause",
12177 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12178 return error_mark_node;
12180 maybe_zero_len = true;
12182 else if (length == NULL_TREE
12183 && first_non_one == types.length ()
12184 && tree_int_cst_equal
12185 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12186 low_bound))
12187 first_non_one++;
12189 else if (length == NULL_TREE)
12191 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12192 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12193 maybe_zero_len = true;
12194 if (first_non_one == types.length ())
12195 first_non_one++;
12197 if (length && TREE_CODE (length) == INTEGER_CST)
12199 if (tree_int_cst_lt (size, length))
12201 error_at (OMP_CLAUSE_LOCATION (c),
12202 "length %qE above array section size "
12203 "in %qs clause", length,
12204 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12205 return error_mark_node;
12207 if (TREE_CODE (low_bound) == INTEGER_CST)
12209 tree lbpluslen
12210 = size_binop (PLUS_EXPR,
12211 fold_convert (sizetype, low_bound),
12212 fold_convert (sizetype, length));
12213 if (TREE_CODE (lbpluslen) == INTEGER_CST
12214 && tree_int_cst_lt (size, lbpluslen))
12216 error_at (OMP_CLAUSE_LOCATION (c),
12217 "high bound %qE above array section size "
12218 "in %qs clause", lbpluslen,
12219 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12220 return error_mark_node;
12225 else if (length == NULL_TREE)
12227 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12228 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12229 maybe_zero_len = true;
12230 if (first_non_one == types.length ())
12231 first_non_one++;
12234 /* For [lb:] we will need to evaluate lb more than once. */
12235 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12237 tree lb = c_save_expr (low_bound);
12238 if (lb != low_bound)
12240 TREE_PURPOSE (t) = lb;
12241 low_bound = lb;
12245 else if (TREE_CODE (type) == POINTER_TYPE)
12247 if (length == NULL_TREE)
12249 error_at (OMP_CLAUSE_LOCATION (c),
12250 "for pointer type length expression must be specified");
12251 return error_mark_node;
12253 if (length != NULL_TREE
12254 && TREE_CODE (length) == INTEGER_CST
12255 && tree_int_cst_sgn (length) == -1)
12257 error_at (OMP_CLAUSE_LOCATION (c),
12258 "negative length in array section in %qs clause",
12259 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12260 return error_mark_node;
12262 /* If there is a pointer type anywhere but in the very first
12263 array-section-subscript, the array section can't be contiguous. */
12264 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12265 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12267 error_at (OMP_CLAUSE_LOCATION (c),
12268 "array section is not contiguous in %qs clause",
12269 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12270 return error_mark_node;
12273 else
12275 error_at (OMP_CLAUSE_LOCATION (c),
12276 "%qE does not have pointer or array type", ret);
12277 return error_mark_node;
12279 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12280 types.safe_push (TREE_TYPE (ret));
12281 /* We will need to evaluate lb more than once. */
12282 tree lb = c_save_expr (low_bound);
12283 if (lb != low_bound)
12285 TREE_PURPOSE (t) = lb;
12286 low_bound = lb;
12288 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12289 return ret;
12292 /* Handle array sections for clause C. */
12294 static bool
12295 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12297 bool maybe_zero_len = false;
12298 unsigned int first_non_one = 0;
12299 auto_vec<tree, 10> types;
12300 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12301 maybe_zero_len, first_non_one,
12302 ort);
12303 if (first == error_mark_node)
12304 return true;
12305 if (first == NULL_TREE)
12306 return false;
12307 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12309 tree t = OMP_CLAUSE_DECL (c);
12310 tree tem = NULL_TREE;
12311 /* Need to evaluate side effects in the length expressions
12312 if any. */
12313 while (TREE_CODE (t) == TREE_LIST)
12315 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12317 if (tem == NULL_TREE)
12318 tem = TREE_VALUE (t);
12319 else
12320 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12321 TREE_VALUE (t), tem);
12323 t = TREE_CHAIN (t);
12325 if (tem)
12326 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12327 first = c_fully_fold (first, false, NULL);
12328 OMP_CLAUSE_DECL (c) = first;
12330 else
12332 unsigned int num = types.length (), i;
12333 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12334 tree condition = NULL_TREE;
12336 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12337 maybe_zero_len = true;
12339 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12340 t = TREE_CHAIN (t))
12342 tree low_bound = TREE_PURPOSE (t);
12343 tree length = TREE_VALUE (t);
12345 i--;
12346 if (low_bound
12347 && TREE_CODE (low_bound) == INTEGER_CST
12348 && TYPE_PRECISION (TREE_TYPE (low_bound))
12349 > TYPE_PRECISION (sizetype))
12350 low_bound = fold_convert (sizetype, low_bound);
12351 if (length
12352 && TREE_CODE (length) == INTEGER_CST
12353 && TYPE_PRECISION (TREE_TYPE (length))
12354 > TYPE_PRECISION (sizetype))
12355 length = fold_convert (sizetype, length);
12356 if (low_bound == NULL_TREE)
12357 low_bound = integer_zero_node;
12358 if (!maybe_zero_len && i > first_non_one)
12360 if (integer_nonzerop (low_bound))
12361 goto do_warn_noncontiguous;
12362 if (length != NULL_TREE
12363 && TREE_CODE (length) == INTEGER_CST
12364 && TYPE_DOMAIN (types[i])
12365 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12366 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12367 == INTEGER_CST)
12369 tree size;
12370 size = size_binop (PLUS_EXPR,
12371 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12372 size_one_node);
12373 if (!tree_int_cst_equal (length, size))
12375 do_warn_noncontiguous:
12376 error_at (OMP_CLAUSE_LOCATION (c),
12377 "array section is not contiguous in %qs "
12378 "clause",
12379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12380 return true;
12383 if (length != NULL_TREE
12384 && TREE_SIDE_EFFECTS (length))
12386 if (side_effects == NULL_TREE)
12387 side_effects = length;
12388 else
12389 side_effects = build2 (COMPOUND_EXPR,
12390 TREE_TYPE (side_effects),
12391 length, side_effects);
12394 else
12396 tree l;
12398 if (i > first_non_one
12399 && ((length && integer_nonzerop (length))
12400 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12401 continue;
12402 if (length)
12403 l = fold_convert (sizetype, length);
12404 else
12406 l = size_binop (PLUS_EXPR,
12407 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12408 size_one_node);
12409 l = size_binop (MINUS_EXPR, l,
12410 fold_convert (sizetype, low_bound));
12412 if (i > first_non_one)
12414 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12415 size_zero_node);
12416 if (condition == NULL_TREE)
12417 condition = l;
12418 else
12419 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12420 l, condition);
12422 else if (size == NULL_TREE)
12424 size = size_in_bytes (TREE_TYPE (types[i]));
12425 tree eltype = TREE_TYPE (types[num - 1]);
12426 while (TREE_CODE (eltype) == ARRAY_TYPE)
12427 eltype = TREE_TYPE (eltype);
12428 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12430 if (integer_zerop (size)
12431 || integer_zerop (size_in_bytes (eltype)))
12433 error_at (OMP_CLAUSE_LOCATION (c),
12434 "zero length array section in %qs clause",
12435 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12436 return error_mark_node;
12438 size = size_binop (EXACT_DIV_EXPR, size,
12439 size_in_bytes (eltype));
12441 size = size_binop (MULT_EXPR, size, l);
12442 if (condition)
12443 size = fold_build3 (COND_EXPR, sizetype, condition,
12444 size, size_zero_node);
12446 else
12447 size = size_binop (MULT_EXPR, size, l);
12450 if (side_effects)
12451 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12452 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12454 size = size_binop (MINUS_EXPR, size, size_one_node);
12455 size = c_fully_fold (size, false, NULL);
12456 tree index_type = build_index_type (size);
12457 tree eltype = TREE_TYPE (first);
12458 while (TREE_CODE (eltype) == ARRAY_TYPE)
12459 eltype = TREE_TYPE (eltype);
12460 tree type = build_array_type (eltype, index_type);
12461 tree ptype = build_pointer_type (eltype);
12462 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12463 t = build_fold_addr_expr (t);
12464 tree t2 = build_fold_addr_expr (first);
12465 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12466 ptrdiff_type_node, t2);
12467 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12468 ptrdiff_type_node, t2,
12469 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12470 ptrdiff_type_node, t));
12471 t2 = c_fully_fold (t2, false, NULL);
12472 if (tree_fits_shwi_p (t2))
12473 t = build2 (MEM_REF, type, t,
12474 build_int_cst (ptype, tree_to_shwi (t2)));
12475 else
12477 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12478 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12479 TREE_TYPE (t), t, t2);
12480 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12482 OMP_CLAUSE_DECL (c) = t;
12483 return false;
12485 first = c_fully_fold (first, false, NULL);
12486 OMP_CLAUSE_DECL (c) = first;
12487 if (size)
12488 size = c_fully_fold (size, false, NULL);
12489 OMP_CLAUSE_SIZE (c) = size;
12490 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12491 || (TREE_CODE (t) == COMPONENT_REF
12492 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12493 return false;
12494 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12495 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12496 switch (OMP_CLAUSE_MAP_KIND (c))
12498 case GOMP_MAP_ALLOC:
12499 case GOMP_MAP_TO:
12500 case GOMP_MAP_FROM:
12501 case GOMP_MAP_TOFROM:
12502 case GOMP_MAP_ALWAYS_TO:
12503 case GOMP_MAP_ALWAYS_FROM:
12504 case GOMP_MAP_ALWAYS_TOFROM:
12505 case GOMP_MAP_RELEASE:
12506 case GOMP_MAP_DELETE:
12507 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12508 break;
12509 default:
12510 break;
12512 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12513 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12514 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12515 else if (TREE_CODE (t) == COMPONENT_REF)
12516 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12517 else
12518 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12519 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12520 && !c_mark_addressable (t))
12521 return false;
12522 OMP_CLAUSE_DECL (c2) = t;
12523 t = build_fold_addr_expr (first);
12524 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12525 tree ptr = OMP_CLAUSE_DECL (c2);
12526 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12527 ptr = build_fold_addr_expr (ptr);
12528 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12529 ptrdiff_type_node, t,
12530 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12531 ptrdiff_type_node, ptr));
12532 t = c_fully_fold (t, false, NULL);
12533 OMP_CLAUSE_SIZE (c2) = t;
12534 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12535 OMP_CLAUSE_CHAIN (c) = c2;
12537 return false;
12540 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12541 an inline call. But, remap
12542 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12543 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12545 static tree
12546 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12547 tree decl, tree placeholder)
12549 copy_body_data id;
12550 hash_map<tree, tree> decl_map;
12552 decl_map.put (omp_decl1, placeholder);
12553 decl_map.put (omp_decl2, decl);
12554 memset (&id, 0, sizeof (id));
12555 id.src_fn = DECL_CONTEXT (omp_decl1);
12556 id.dst_fn = current_function_decl;
12557 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12558 id.decl_map = &decl_map;
12560 id.copy_decl = copy_decl_no_change;
12561 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12562 id.transform_new_cfg = true;
12563 id.transform_return_to_modify = false;
12564 id.transform_lang_insert_block = NULL;
12565 id.eh_lp_nr = 0;
12566 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12567 return stmt;
12570 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12571 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12573 static tree
12574 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12576 if (*tp == (tree) data)
12577 return *tp;
12578 return NULL_TREE;
12581 /* For all elements of CLAUSES, validate them against their constraints.
12582 Remove any elements from the list that are invalid. */
12584 tree
12585 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12587 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12588 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12589 tree c, t, type, *pc;
12590 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12591 bool branch_seen = false;
12592 bool copyprivate_seen = false;
12593 bool linear_variable_step_check = false;
12594 tree *nowait_clause = NULL;
12595 bool ordered_seen = false;
12596 tree schedule_clause = NULL_TREE;
12597 bool oacc_async = false;
12599 bitmap_obstack_initialize (NULL);
12600 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12601 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12602 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12603 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12604 bitmap_initialize (&map_head, &bitmap_default_obstack);
12605 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12606 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12608 if (ort & C_ORT_ACC)
12609 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12610 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12612 oacc_async = true;
12613 break;
12616 for (pc = &clauses, c = clauses; c ; c = *pc)
12618 bool remove = false;
12619 bool need_complete = false;
12620 bool need_implicitly_determined = false;
12622 switch (OMP_CLAUSE_CODE (c))
12624 case OMP_CLAUSE_SHARED:
12625 need_implicitly_determined = true;
12626 goto check_dup_generic;
12628 case OMP_CLAUSE_PRIVATE:
12629 need_complete = true;
12630 need_implicitly_determined = true;
12631 goto check_dup_generic;
12633 case OMP_CLAUSE_REDUCTION:
12634 need_implicitly_determined = true;
12635 t = OMP_CLAUSE_DECL (c);
12636 if (TREE_CODE (t) == TREE_LIST)
12638 if (handle_omp_array_sections (c, ort))
12640 remove = true;
12641 break;
12644 t = OMP_CLAUSE_DECL (c);
12646 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12647 if (t == error_mark_node)
12649 remove = true;
12650 break;
12652 if (oacc_async)
12653 c_mark_addressable (t);
12654 type = TREE_TYPE (t);
12655 if (TREE_CODE (t) == MEM_REF)
12656 type = TREE_TYPE (type);
12657 if (TREE_CODE (type) == ARRAY_TYPE)
12659 tree oatype = type;
12660 gcc_assert (TREE_CODE (t) != MEM_REF);
12661 while (TREE_CODE (type) == ARRAY_TYPE)
12662 type = TREE_TYPE (type);
12663 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12665 error_at (OMP_CLAUSE_LOCATION (c),
12666 "%qD in %<reduction%> clause is a zero size array",
12668 remove = true;
12669 break;
12671 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12672 TYPE_SIZE_UNIT (type));
12673 if (integer_zerop (size))
12675 error_at (OMP_CLAUSE_LOCATION (c),
12676 "%qD in %<reduction%> clause is a zero size array",
12678 remove = true;
12679 break;
12681 size = size_binop (MINUS_EXPR, size, size_one_node);
12682 tree index_type = build_index_type (size);
12683 tree atype = build_array_type (type, index_type);
12684 tree ptype = build_pointer_type (type);
12685 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12686 t = build_fold_addr_expr (t);
12687 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12688 OMP_CLAUSE_DECL (c) = t;
12690 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12691 && (FLOAT_TYPE_P (type)
12692 || TREE_CODE (type) == COMPLEX_TYPE))
12694 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12695 const char *r_name = NULL;
12697 switch (r_code)
12699 case PLUS_EXPR:
12700 case MULT_EXPR:
12701 case MINUS_EXPR:
12702 break;
12703 case MIN_EXPR:
12704 if (TREE_CODE (type) == COMPLEX_TYPE)
12705 r_name = "min";
12706 break;
12707 case MAX_EXPR:
12708 if (TREE_CODE (type) == COMPLEX_TYPE)
12709 r_name = "max";
12710 break;
12711 case BIT_AND_EXPR:
12712 r_name = "&";
12713 break;
12714 case BIT_XOR_EXPR:
12715 r_name = "^";
12716 break;
12717 case BIT_IOR_EXPR:
12718 r_name = "|";
12719 break;
12720 case TRUTH_ANDIF_EXPR:
12721 if (FLOAT_TYPE_P (type))
12722 r_name = "&&";
12723 break;
12724 case TRUTH_ORIF_EXPR:
12725 if (FLOAT_TYPE_P (type))
12726 r_name = "||";
12727 break;
12728 default:
12729 gcc_unreachable ();
12731 if (r_name)
12733 error_at (OMP_CLAUSE_LOCATION (c),
12734 "%qE has invalid type for %<reduction(%s)%>",
12735 t, r_name);
12736 remove = true;
12737 break;
12740 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12742 error_at (OMP_CLAUSE_LOCATION (c),
12743 "user defined reduction not found for %qE", t);
12744 remove = true;
12745 break;
12747 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12749 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12750 type = TYPE_MAIN_VARIANT (type);
12751 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12752 VAR_DECL, NULL_TREE, type);
12753 tree decl_placeholder = NULL_TREE;
12754 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12755 DECL_ARTIFICIAL (placeholder) = 1;
12756 DECL_IGNORED_P (placeholder) = 1;
12757 if (TREE_CODE (t) == MEM_REF)
12759 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12760 VAR_DECL, NULL_TREE, type);
12761 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12762 DECL_ARTIFICIAL (decl_placeholder) = 1;
12763 DECL_IGNORED_P (decl_placeholder) = 1;
12765 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12766 c_mark_addressable (placeholder);
12767 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12768 c_mark_addressable (decl_placeholder ? decl_placeholder
12769 : OMP_CLAUSE_DECL (c));
12770 OMP_CLAUSE_REDUCTION_MERGE (c)
12771 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12772 TREE_VEC_ELT (list, 0),
12773 TREE_VEC_ELT (list, 1),
12774 decl_placeholder ? decl_placeholder
12775 : OMP_CLAUSE_DECL (c), placeholder);
12776 OMP_CLAUSE_REDUCTION_MERGE (c)
12777 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12778 void_type_node, NULL_TREE,
12779 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12780 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12781 if (TREE_VEC_LENGTH (list) == 6)
12783 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12784 c_mark_addressable (decl_placeholder ? decl_placeholder
12785 : OMP_CLAUSE_DECL (c));
12786 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12787 c_mark_addressable (placeholder);
12788 tree init = TREE_VEC_ELT (list, 5);
12789 if (init == error_mark_node)
12790 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12791 OMP_CLAUSE_REDUCTION_INIT (c)
12792 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12793 TREE_VEC_ELT (list, 3),
12794 decl_placeholder ? decl_placeholder
12795 : OMP_CLAUSE_DECL (c), placeholder);
12796 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12798 tree v = decl_placeholder ? decl_placeholder : t;
12799 OMP_CLAUSE_REDUCTION_INIT (c)
12800 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12801 OMP_CLAUSE_REDUCTION_INIT (c));
12803 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12804 c_find_omp_placeholder_r,
12805 placeholder, NULL))
12806 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12808 else
12810 tree init;
12811 tree v = decl_placeholder ? decl_placeholder : t;
12812 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12813 init = build_constructor (TREE_TYPE (v), NULL);
12814 else
12815 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12816 OMP_CLAUSE_REDUCTION_INIT (c)
12817 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12819 OMP_CLAUSE_REDUCTION_INIT (c)
12820 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12821 void_type_node, NULL_TREE,
12822 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12823 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12825 if (TREE_CODE (t) == MEM_REF)
12827 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12828 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12829 != INTEGER_CST)
12831 sorry ("variable length element type in array "
12832 "%<reduction%> clause");
12833 remove = true;
12834 break;
12836 t = TREE_OPERAND (t, 0);
12837 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12838 t = TREE_OPERAND (t, 0);
12839 if (TREE_CODE (t) == ADDR_EXPR)
12840 t = TREE_OPERAND (t, 0);
12842 goto check_dup_generic_t;
12844 case OMP_CLAUSE_COPYPRIVATE:
12845 copyprivate_seen = true;
12846 if (nowait_clause)
12848 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12849 "%<nowait%> clause must not be used together "
12850 "with %<copyprivate%>");
12851 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12852 nowait_clause = NULL;
12854 goto check_dup_generic;
12856 case OMP_CLAUSE_COPYIN:
12857 t = OMP_CLAUSE_DECL (c);
12858 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12860 error_at (OMP_CLAUSE_LOCATION (c),
12861 "%qE must be %<threadprivate%> for %<copyin%>", t);
12862 remove = true;
12863 break;
12865 goto check_dup_generic;
12867 case OMP_CLAUSE_LINEAR:
12868 if (ort != C_ORT_OMP_DECLARE_SIMD)
12869 need_implicitly_determined = true;
12870 t = OMP_CLAUSE_DECL (c);
12871 if (ort != C_ORT_OMP_DECLARE_SIMD
12872 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12874 error_at (OMP_CLAUSE_LOCATION (c),
12875 "modifier should not be specified in %<linear%> "
12876 "clause on %<simd%> or %<for%> constructs");
12877 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12879 if (ort & C_ORT_CILK)
12881 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12882 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
12883 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12885 error_at (OMP_CLAUSE_LOCATION (c),
12886 "linear clause applied to non-integral, "
12887 "non-floating, non-pointer variable with type %qT",
12888 TREE_TYPE (t));
12889 remove = true;
12890 break;
12893 else
12895 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12896 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12898 error_at (OMP_CLAUSE_LOCATION (c),
12899 "linear clause applied to non-integral non-pointer "
12900 "variable with type %qT", TREE_TYPE (t));
12901 remove = true;
12902 break;
12905 if (ort == C_ORT_OMP_DECLARE_SIMD)
12907 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12908 if (TREE_CODE (s) == PARM_DECL)
12910 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12911 /* map_head bitmap is used as uniform_head if
12912 declare_simd. */
12913 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12914 linear_variable_step_check = true;
12915 goto check_dup_generic;
12917 if (TREE_CODE (s) != INTEGER_CST)
12919 error_at (OMP_CLAUSE_LOCATION (c),
12920 "%<linear%> clause step %qE is neither constant "
12921 "nor a parameter", s);
12922 remove = true;
12923 break;
12926 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12928 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12929 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12930 OMP_CLAUSE_DECL (c), s);
12931 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12932 sizetype, fold_convert (sizetype, s),
12933 fold_convert
12934 (sizetype, OMP_CLAUSE_DECL (c)));
12935 if (s == error_mark_node)
12936 s = size_one_node;
12937 OMP_CLAUSE_LINEAR_STEP (c) = s;
12939 else
12940 OMP_CLAUSE_LINEAR_STEP (c)
12941 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12942 goto check_dup_generic;
12944 check_dup_generic:
12945 t = OMP_CLAUSE_DECL (c);
12946 check_dup_generic_t:
12947 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12949 error_at (OMP_CLAUSE_LOCATION (c),
12950 "%qE is not a variable in clause %qs", t,
12951 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12952 remove = true;
12954 else if (ort == C_ORT_ACC
12955 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12957 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
12959 error ("%qD appears more than once in reduction clauses", t);
12960 remove = true;
12962 else
12963 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
12965 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12966 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12967 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12969 error_at (OMP_CLAUSE_LOCATION (c),
12970 "%qE appears more than once in data clauses", t);
12971 remove = true;
12973 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12974 && bitmap_bit_p (&map_head, DECL_UID (t)))
12976 if (ort == C_ORT_ACC)
12977 error ("%qD appears more than once in data clauses", t);
12978 else
12979 error ("%qD appears both in data and map clauses", t);
12980 remove = true;
12982 else
12983 bitmap_set_bit (&generic_head, DECL_UID (t));
12984 break;
12986 case OMP_CLAUSE_FIRSTPRIVATE:
12987 t = OMP_CLAUSE_DECL (c);
12988 need_complete = true;
12989 need_implicitly_determined = true;
12990 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12992 error_at (OMP_CLAUSE_LOCATION (c),
12993 "%qE is not a variable in clause %<firstprivate%>", t);
12994 remove = true;
12996 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12997 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12999 error_at (OMP_CLAUSE_LOCATION (c),
13000 "%qE appears more than once in data clauses", t);
13001 remove = true;
13003 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13005 if (ort == C_ORT_ACC)
13006 error ("%qD appears more than once in data clauses", t);
13007 else
13008 error ("%qD appears both in data and map clauses", t);
13009 remove = true;
13011 else
13012 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13013 break;
13015 case OMP_CLAUSE_LASTPRIVATE:
13016 t = OMP_CLAUSE_DECL (c);
13017 need_complete = true;
13018 need_implicitly_determined = true;
13019 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13021 error_at (OMP_CLAUSE_LOCATION (c),
13022 "%qE is not a variable in clause %<lastprivate%>", t);
13023 remove = true;
13025 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13026 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13028 error_at (OMP_CLAUSE_LOCATION (c),
13029 "%qE appears more than once in data clauses", t);
13030 remove = true;
13032 else
13033 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13034 break;
13036 case OMP_CLAUSE_ALIGNED:
13037 t = OMP_CLAUSE_DECL (c);
13038 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13040 error_at (OMP_CLAUSE_LOCATION (c),
13041 "%qE is not a variable in %<aligned%> clause", t);
13042 remove = true;
13044 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13045 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13047 error_at (OMP_CLAUSE_LOCATION (c),
13048 "%qE in %<aligned%> clause is neither a pointer nor "
13049 "an array", t);
13050 remove = true;
13052 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13054 error_at (OMP_CLAUSE_LOCATION (c),
13055 "%qE appears more than once in %<aligned%> clauses",
13057 remove = true;
13059 else
13060 bitmap_set_bit (&aligned_head, DECL_UID (t));
13061 break;
13063 case OMP_CLAUSE_DEPEND:
13064 t = OMP_CLAUSE_DECL (c);
13065 if (t == NULL_TREE)
13067 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13068 == OMP_CLAUSE_DEPEND_SOURCE);
13069 break;
13071 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13073 gcc_assert (TREE_CODE (t) == TREE_LIST);
13074 for (; t; t = TREE_CHAIN (t))
13076 tree decl = TREE_VALUE (t);
13077 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13079 tree offset = TREE_PURPOSE (t);
13080 bool neg = wi::neg_p ((wide_int) offset);
13081 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13082 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13083 neg ? MINUS_EXPR : PLUS_EXPR,
13084 decl, offset);
13085 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13086 sizetype,
13087 fold_convert (sizetype, t2),
13088 fold_convert (sizetype, decl));
13089 if (t2 == error_mark_node)
13091 remove = true;
13092 break;
13094 TREE_PURPOSE (t) = t2;
13097 break;
13099 if (TREE_CODE (t) == TREE_LIST)
13101 if (handle_omp_array_sections (c, ort))
13102 remove = true;
13103 break;
13105 if (t == error_mark_node)
13106 remove = true;
13107 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13109 error_at (OMP_CLAUSE_LOCATION (c),
13110 "%qE is not a variable in %<depend%> clause", t);
13111 remove = true;
13113 else if (!c_mark_addressable (t))
13114 remove = true;
13115 break;
13117 case OMP_CLAUSE_MAP:
13118 case OMP_CLAUSE_TO:
13119 case OMP_CLAUSE_FROM:
13120 case OMP_CLAUSE__CACHE_:
13121 t = OMP_CLAUSE_DECL (c);
13122 if (TREE_CODE (t) == TREE_LIST)
13124 if (handle_omp_array_sections (c, ort))
13125 remove = true;
13126 else
13128 t = OMP_CLAUSE_DECL (c);
13129 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13131 error_at (OMP_CLAUSE_LOCATION (c),
13132 "array section does not have mappable type "
13133 "in %qs clause",
13134 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13135 remove = true;
13137 while (TREE_CODE (t) == ARRAY_REF)
13138 t = TREE_OPERAND (t, 0);
13139 if (TREE_CODE (t) == COMPONENT_REF
13140 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13142 while (TREE_CODE (t) == COMPONENT_REF)
13143 t = TREE_OPERAND (t, 0);
13144 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13145 break;
13146 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13148 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13149 error ("%qD appears more than once in motion"
13150 " clauses", t);
13151 else if (ort == C_ORT_ACC)
13152 error ("%qD appears more than once in data"
13153 " clauses", t);
13154 else
13155 error ("%qD appears more than once in map"
13156 " clauses", t);
13157 remove = true;
13159 else
13161 bitmap_set_bit (&map_head, DECL_UID (t));
13162 bitmap_set_bit (&map_field_head, DECL_UID (t));
13166 break;
13168 if (t == error_mark_node)
13170 remove = true;
13171 break;
13173 if (TREE_CODE (t) == COMPONENT_REF
13174 && (ort & C_ORT_OMP)
13175 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13177 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13179 error_at (OMP_CLAUSE_LOCATION (c),
13180 "bit-field %qE in %qs clause",
13181 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13182 remove = true;
13184 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13186 error_at (OMP_CLAUSE_LOCATION (c),
13187 "%qE does not have a mappable type in %qs clause",
13188 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13189 remove = true;
13191 while (TREE_CODE (t) == COMPONENT_REF)
13193 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13194 == UNION_TYPE)
13196 error_at (OMP_CLAUSE_LOCATION (c),
13197 "%qE is a member of a union", t);
13198 remove = true;
13199 break;
13201 t = TREE_OPERAND (t, 0);
13203 if (remove)
13204 break;
13205 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13207 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13208 break;
13211 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13213 error_at (OMP_CLAUSE_LOCATION (c),
13214 "%qE is not a variable in %qs clause", t,
13215 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13216 remove = true;
13218 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13220 error_at (OMP_CLAUSE_LOCATION (c),
13221 "%qD is threadprivate variable in %qs clause", t,
13222 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13223 remove = true;
13225 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13226 || (OMP_CLAUSE_MAP_KIND (c)
13227 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13228 && !c_mark_addressable (t))
13229 remove = true;
13230 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13231 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13232 || (OMP_CLAUSE_MAP_KIND (c)
13233 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13234 || (OMP_CLAUSE_MAP_KIND (c)
13235 == GOMP_MAP_FORCE_DEVICEPTR)))
13236 && t == OMP_CLAUSE_DECL (c)
13237 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13239 error_at (OMP_CLAUSE_LOCATION (c),
13240 "%qD does not have a mappable type in %qs clause", t,
13241 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13242 remove = true;
13244 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13245 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13247 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13248 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13250 error ("%qD appears more than once in data clauses", t);
13251 remove = true;
13253 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13255 if (ort == C_ORT_ACC)
13256 error ("%qD appears more than once in data clauses", t);
13257 else
13258 error ("%qD appears both in data and map clauses", t);
13259 remove = true;
13261 else
13262 bitmap_set_bit (&generic_head, DECL_UID (t));
13264 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13266 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13267 error ("%qD appears more than once in motion clauses", t);
13268 else if (ort == C_ORT_ACC)
13269 error ("%qD appears more than once in data clauses", t);
13270 else
13271 error ("%qD appears more than once in map clauses", t);
13272 remove = true;
13274 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13275 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13277 if (ort == C_ORT_ACC)
13278 error ("%qD appears more than once in data clauses", t);
13279 else
13280 error ("%qD appears both in data and map clauses", t);
13281 remove = true;
13283 else
13285 bitmap_set_bit (&map_head, DECL_UID (t));
13286 if (t != OMP_CLAUSE_DECL (c)
13287 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13288 bitmap_set_bit (&map_field_head, DECL_UID (t));
13290 break;
13292 case OMP_CLAUSE_TO_DECLARE:
13293 case OMP_CLAUSE_LINK:
13294 t = OMP_CLAUSE_DECL (c);
13295 if (TREE_CODE (t) == FUNCTION_DECL
13296 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13298 else if (!VAR_P (t))
13300 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13301 error_at (OMP_CLAUSE_LOCATION (c),
13302 "%qE is neither a variable nor a function name in "
13303 "clause %qs", t,
13304 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13305 else
13306 error_at (OMP_CLAUSE_LOCATION (c),
13307 "%qE is not a variable in clause %qs", t,
13308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13309 remove = true;
13311 else if (DECL_THREAD_LOCAL_P (t))
13313 error_at (OMP_CLAUSE_LOCATION (c),
13314 "%qD is threadprivate variable in %qs clause", t,
13315 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13316 remove = true;
13318 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13320 error_at (OMP_CLAUSE_LOCATION (c),
13321 "%qD does not have a mappable type in %qs clause", t,
13322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13323 remove = true;
13325 if (remove)
13326 break;
13327 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13329 error_at (OMP_CLAUSE_LOCATION (c),
13330 "%qE appears more than once on the same "
13331 "%<declare target%> directive", t);
13332 remove = true;
13334 else
13335 bitmap_set_bit (&generic_head, DECL_UID (t));
13336 break;
13338 case OMP_CLAUSE_UNIFORM:
13339 t = OMP_CLAUSE_DECL (c);
13340 if (TREE_CODE (t) != PARM_DECL)
13342 if (DECL_P (t))
13343 error_at (OMP_CLAUSE_LOCATION (c),
13344 "%qD is not an argument in %<uniform%> clause", t);
13345 else
13346 error_at (OMP_CLAUSE_LOCATION (c),
13347 "%qE is not an argument in %<uniform%> clause", t);
13348 remove = true;
13349 break;
13351 /* map_head bitmap is used as uniform_head if declare_simd. */
13352 bitmap_set_bit (&map_head, DECL_UID (t));
13353 goto check_dup_generic;
13355 case OMP_CLAUSE_IS_DEVICE_PTR:
13356 case OMP_CLAUSE_USE_DEVICE_PTR:
13357 t = OMP_CLAUSE_DECL (c);
13358 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13359 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13361 error_at (OMP_CLAUSE_LOCATION (c),
13362 "%qs variable is neither a pointer nor an array",
13363 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13364 remove = true;
13366 goto check_dup_generic;
13368 case OMP_CLAUSE_NOWAIT:
13369 if (copyprivate_seen)
13371 error_at (OMP_CLAUSE_LOCATION (c),
13372 "%<nowait%> clause must not be used together "
13373 "with %<copyprivate%>");
13374 remove = true;
13375 break;
13377 nowait_clause = pc;
13378 pc = &OMP_CLAUSE_CHAIN (c);
13379 continue;
13381 case OMP_CLAUSE_IF:
13382 case OMP_CLAUSE_NUM_THREADS:
13383 case OMP_CLAUSE_NUM_TEAMS:
13384 case OMP_CLAUSE_THREAD_LIMIT:
13385 case OMP_CLAUSE_DEFAULT:
13386 case OMP_CLAUSE_UNTIED:
13387 case OMP_CLAUSE_COLLAPSE:
13388 case OMP_CLAUSE_FINAL:
13389 case OMP_CLAUSE_MERGEABLE:
13390 case OMP_CLAUSE_DEVICE:
13391 case OMP_CLAUSE_DIST_SCHEDULE:
13392 case OMP_CLAUSE_PARALLEL:
13393 case OMP_CLAUSE_FOR:
13394 case OMP_CLAUSE_SECTIONS:
13395 case OMP_CLAUSE_TASKGROUP:
13396 case OMP_CLAUSE_PROC_BIND:
13397 case OMP_CLAUSE_PRIORITY:
13398 case OMP_CLAUSE_GRAINSIZE:
13399 case OMP_CLAUSE_NUM_TASKS:
13400 case OMP_CLAUSE_NOGROUP:
13401 case OMP_CLAUSE_THREADS:
13402 case OMP_CLAUSE_SIMD:
13403 case OMP_CLAUSE_HINT:
13404 case OMP_CLAUSE_DEFAULTMAP:
13405 case OMP_CLAUSE__CILK_FOR_COUNT_:
13406 case OMP_CLAUSE_NUM_GANGS:
13407 case OMP_CLAUSE_NUM_WORKERS:
13408 case OMP_CLAUSE_VECTOR_LENGTH:
13409 case OMP_CLAUSE_ASYNC:
13410 case OMP_CLAUSE_WAIT:
13411 case OMP_CLAUSE_AUTO:
13412 case OMP_CLAUSE_INDEPENDENT:
13413 case OMP_CLAUSE_SEQ:
13414 case OMP_CLAUSE_GANG:
13415 case OMP_CLAUSE_WORKER:
13416 case OMP_CLAUSE_VECTOR:
13417 case OMP_CLAUSE_TILE:
13418 pc = &OMP_CLAUSE_CHAIN (c);
13419 continue;
13421 case OMP_CLAUSE_SCHEDULE:
13422 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13424 const char *p = NULL;
13425 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13427 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13428 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13429 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13430 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13431 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13432 default: gcc_unreachable ();
13434 if (p)
13436 error_at (OMP_CLAUSE_LOCATION (c),
13437 "%<nonmonotonic%> modifier specified for %qs "
13438 "schedule kind", p);
13439 OMP_CLAUSE_SCHEDULE_KIND (c)
13440 = (enum omp_clause_schedule_kind)
13441 (OMP_CLAUSE_SCHEDULE_KIND (c)
13442 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13445 schedule_clause = c;
13446 pc = &OMP_CLAUSE_CHAIN (c);
13447 continue;
13449 case OMP_CLAUSE_ORDERED:
13450 ordered_seen = true;
13451 pc = &OMP_CLAUSE_CHAIN (c);
13452 continue;
13454 case OMP_CLAUSE_SAFELEN:
13455 safelen = c;
13456 pc = &OMP_CLAUSE_CHAIN (c);
13457 continue;
13458 case OMP_CLAUSE_SIMDLEN:
13459 simdlen = c;
13460 pc = &OMP_CLAUSE_CHAIN (c);
13461 continue;
13463 case OMP_CLAUSE_INBRANCH:
13464 case OMP_CLAUSE_NOTINBRANCH:
13465 if (branch_seen)
13467 error_at (OMP_CLAUSE_LOCATION (c),
13468 "%<inbranch%> clause is incompatible with "
13469 "%<notinbranch%>");
13470 remove = true;
13471 break;
13473 branch_seen = true;
13474 pc = &OMP_CLAUSE_CHAIN (c);
13475 continue;
13477 default:
13478 gcc_unreachable ();
13481 if (!remove)
13483 t = OMP_CLAUSE_DECL (c);
13485 if (need_complete)
13487 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13488 if (t == error_mark_node)
13489 remove = true;
13492 if (need_implicitly_determined)
13494 const char *share_name = NULL;
13496 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13497 share_name = "threadprivate";
13498 else switch (c_omp_predetermined_sharing (t))
13500 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13501 break;
13502 case OMP_CLAUSE_DEFAULT_SHARED:
13503 /* const vars may be specified in firstprivate clause. */
13504 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13505 && TREE_READONLY (t))
13506 break;
13507 share_name = "shared";
13508 break;
13509 case OMP_CLAUSE_DEFAULT_PRIVATE:
13510 share_name = "private";
13511 break;
13512 default:
13513 gcc_unreachable ();
13515 if (share_name)
13517 error_at (OMP_CLAUSE_LOCATION (c),
13518 "%qE is predetermined %qs for %qs",
13519 t, share_name,
13520 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13521 remove = true;
13526 if (remove)
13527 *pc = OMP_CLAUSE_CHAIN (c);
13528 else
13529 pc = &OMP_CLAUSE_CHAIN (c);
13532 if (simdlen
13533 && safelen
13534 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13535 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13537 error_at (OMP_CLAUSE_LOCATION (simdlen),
13538 "%<simdlen%> clause value is bigger than "
13539 "%<safelen%> clause value");
13540 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13541 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13544 if (ordered_seen
13545 && schedule_clause
13546 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13547 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13549 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13550 "%<nonmonotonic%> schedule modifier specified together "
13551 "with %<ordered%> clause");
13552 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13553 = (enum omp_clause_schedule_kind)
13554 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13555 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13558 if (linear_variable_step_check)
13559 for (pc = &clauses, c = clauses; c ; c = *pc)
13561 bool remove = false;
13562 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13563 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13564 && !bitmap_bit_p (&map_head,
13565 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13567 error_at (OMP_CLAUSE_LOCATION (c),
13568 "%<linear%> clause step is a parameter %qD not "
13569 "specified in %<uniform%> clause",
13570 OMP_CLAUSE_LINEAR_STEP (c));
13571 remove = true;
13574 if (remove)
13575 *pc = OMP_CLAUSE_CHAIN (c);
13576 else
13577 pc = &OMP_CLAUSE_CHAIN (c);
13580 bitmap_obstack_release (NULL);
13581 return clauses;
13584 /* Create a transaction node. */
13586 tree
13587 c_finish_transaction (location_t loc, tree block, int flags)
13589 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13590 if (flags & TM_STMT_ATTR_OUTER)
13591 TRANSACTION_EXPR_OUTER (stmt) = 1;
13592 if (flags & TM_STMT_ATTR_RELAXED)
13593 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13594 return add_stmt (stmt);
13597 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13598 down to the element type of an array. If ORIG_QUAL_TYPE is not
13599 NULL, then it should be used as the qualified type
13600 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13601 preserve information about the typedef name from which an array
13602 type was derived). */
13604 tree
13605 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13606 size_t orig_qual_indirect)
13608 if (type == error_mark_node)
13609 return type;
13611 if (TREE_CODE (type) == ARRAY_TYPE)
13613 tree t;
13614 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13615 type_quals, orig_qual_type,
13616 orig_qual_indirect - 1);
13618 /* See if we already have an identically qualified type. */
13619 if (orig_qual_type && orig_qual_indirect == 0)
13620 t = orig_qual_type;
13621 else
13622 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13624 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13625 && TYPE_NAME (t) == TYPE_NAME (type)
13626 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13627 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13628 TYPE_ATTRIBUTES (type)))
13629 break;
13631 if (!t)
13633 tree domain = TYPE_DOMAIN (type);
13635 t = build_variant_type_copy (type);
13636 TREE_TYPE (t) = element_type;
13638 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13639 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13640 SET_TYPE_STRUCTURAL_EQUALITY (t);
13641 else if (TYPE_CANONICAL (element_type) != element_type
13642 || (domain && TYPE_CANONICAL (domain) != domain))
13644 tree unqualified_canon
13645 = build_array_type (TYPE_CANONICAL (element_type),
13646 domain? TYPE_CANONICAL (domain)
13647 : NULL_TREE);
13648 if (TYPE_REVERSE_STORAGE_ORDER (type))
13650 unqualified_canon
13651 = build_distinct_type_copy (unqualified_canon);
13652 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13654 TYPE_CANONICAL (t)
13655 = c_build_qualified_type (unqualified_canon, type_quals);
13657 else
13658 TYPE_CANONICAL (t) = t;
13660 return t;
13663 /* A restrict-qualified pointer type must be a pointer to object or
13664 incomplete type. Note that the use of POINTER_TYPE_P also allows
13665 REFERENCE_TYPEs, which is appropriate for C++. */
13666 if ((type_quals & TYPE_QUAL_RESTRICT)
13667 && (!POINTER_TYPE_P (type)
13668 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13670 error ("invalid use of %<restrict%>");
13671 type_quals &= ~TYPE_QUAL_RESTRICT;
13674 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13675 ? orig_qual_type
13676 : build_qualified_type (type, type_quals));
13677 /* A variant type does not inherit the list of incomplete vars from the
13678 type main variant. */
13679 if (RECORD_OR_UNION_TYPE_P (var_type))
13680 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13681 return var_type;
13684 /* Build a VA_ARG_EXPR for the C parser. */
13686 tree
13687 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13689 if (error_operand_p (type))
13690 return error_mark_node;
13691 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13692 order because it takes the address of the expression. */
13693 else if (handled_component_p (expr)
13694 && reverse_storage_order_for_component_p (expr))
13696 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13697 return error_mark_node;
13699 else if (!COMPLETE_TYPE_P (type))
13701 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13702 "type %qT", type);
13703 return error_mark_node;
13705 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13706 warning_at (loc2, OPT_Wc___compat,
13707 "C++ requires promoted type, not enum type, in %<va_arg%>");
13708 return build_va_arg (loc2, expr, type);
13711 /* Return truthvalue of whether T1 is the same tree structure as T2.
13712 Return 1 if they are the same. Return 0 if they are different. */
13714 bool
13715 c_tree_equal (tree t1, tree t2)
13717 enum tree_code code1, code2;
13719 if (t1 == t2)
13720 return true;
13721 if (!t1 || !t2)
13722 return false;
13724 for (code1 = TREE_CODE (t1);
13725 CONVERT_EXPR_CODE_P (code1)
13726 || code1 == NON_LVALUE_EXPR;
13727 code1 = TREE_CODE (t1))
13728 t1 = TREE_OPERAND (t1, 0);
13729 for (code2 = TREE_CODE (t2);
13730 CONVERT_EXPR_CODE_P (code2)
13731 || code2 == NON_LVALUE_EXPR;
13732 code2 = TREE_CODE (t2))
13733 t2 = TREE_OPERAND (t2, 0);
13735 /* They might have become equal now. */
13736 if (t1 == t2)
13737 return true;
13739 if (code1 != code2)
13740 return false;
13742 switch (code1)
13744 case INTEGER_CST:
13745 return wi::eq_p (t1, t2);
13747 case REAL_CST:
13748 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13750 case STRING_CST:
13751 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13752 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13753 TREE_STRING_LENGTH (t1));
13755 case FIXED_CST:
13756 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13757 TREE_FIXED_CST (t2));
13759 case COMPLEX_CST:
13760 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13761 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13763 case VECTOR_CST:
13764 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13766 case CONSTRUCTOR:
13767 /* We need to do this when determining whether or not two
13768 non-type pointer to member function template arguments
13769 are the same. */
13770 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13771 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13772 return false;
13774 tree field, value;
13775 unsigned int i;
13776 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13778 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13779 if (!c_tree_equal (field, elt2->index)
13780 || !c_tree_equal (value, elt2->value))
13781 return false;
13784 return true;
13786 case TREE_LIST:
13787 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13788 return false;
13789 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13790 return false;
13791 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13793 case SAVE_EXPR:
13794 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13796 case CALL_EXPR:
13798 tree arg1, arg2;
13799 call_expr_arg_iterator iter1, iter2;
13800 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13801 return false;
13802 for (arg1 = first_call_expr_arg (t1, &iter1),
13803 arg2 = first_call_expr_arg (t2, &iter2);
13804 arg1 && arg2;
13805 arg1 = next_call_expr_arg (&iter1),
13806 arg2 = next_call_expr_arg (&iter2))
13807 if (!c_tree_equal (arg1, arg2))
13808 return false;
13809 if (arg1 || arg2)
13810 return false;
13811 return true;
13814 case TARGET_EXPR:
13816 tree o1 = TREE_OPERAND (t1, 0);
13817 tree o2 = TREE_OPERAND (t2, 0);
13819 /* Special case: if either target is an unallocated VAR_DECL,
13820 it means that it's going to be unified with whatever the
13821 TARGET_EXPR is really supposed to initialize, so treat it
13822 as being equivalent to anything. */
13823 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
13824 && !DECL_RTL_SET_P (o1))
13825 /*Nop*/;
13826 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
13827 && !DECL_RTL_SET_P (o2))
13828 /*Nop*/;
13829 else if (!c_tree_equal (o1, o2))
13830 return false;
13832 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13835 case COMPONENT_REF:
13836 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13837 return false;
13838 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13840 case PARM_DECL:
13841 case VAR_DECL:
13842 case CONST_DECL:
13843 case FIELD_DECL:
13844 case FUNCTION_DECL:
13845 case IDENTIFIER_NODE:
13846 case SSA_NAME:
13847 return false;
13849 case TREE_VEC:
13851 unsigned ix;
13852 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13853 return false;
13854 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13855 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13856 TREE_VEC_ELT (t2, ix)))
13857 return false;
13858 return true;
13861 default:
13862 break;
13865 switch (TREE_CODE_CLASS (code1))
13867 case tcc_unary:
13868 case tcc_binary:
13869 case tcc_comparison:
13870 case tcc_expression:
13871 case tcc_vl_exp:
13872 case tcc_reference:
13873 case tcc_statement:
13875 int i, n = TREE_OPERAND_LENGTH (t1);
13877 switch (code1)
13879 case PREINCREMENT_EXPR:
13880 case PREDECREMENT_EXPR:
13881 case POSTINCREMENT_EXPR:
13882 case POSTDECREMENT_EXPR:
13883 n = 1;
13884 break;
13885 case ARRAY_REF:
13886 n = 2;
13887 break;
13888 default:
13889 break;
13892 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13893 && n != TREE_OPERAND_LENGTH (t2))
13894 return false;
13896 for (i = 0; i < n; ++i)
13897 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13898 return false;
13900 return true;
13903 case tcc_type:
13904 return comptypes (t1, t2);
13905 default:
13906 gcc_unreachable ();
13908 /* We can get here with --disable-checking. */
13909 return false;
13912 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13913 spawn-helper and BODY is the newly created body for FNDECL. */
13915 void
13916 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13918 tree list = alloc_stmt_list ();
13919 tree frame = make_cilk_frame (fndecl);
13920 tree dtor = create_cilk_function_exit (frame, false, true);
13921 add_local_decl (cfun, frame);
13923 DECL_SAVED_TREE (fndecl) = list;
13924 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13925 frame);
13926 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13927 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13929 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13930 append_to_statement_list (detach_expr, &body_list);
13932 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13933 body = fold_build_cleanup_point_expr (void_type_node, body);
13935 append_to_statement_list (body, &body_list);
13936 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13937 body_list, dtor), &list);
13940 /* Returns true when the function declaration FNDECL is implicit,
13941 introduced as a result of a call to an otherwise undeclared
13942 function, and false otherwise. */
13944 bool
13945 c_decl_implicit (const_tree fndecl)
13947 return C_DECL_IMPLICIT (fndecl);