Merge trunk version 221445 into gupc branch.
[official-gcc.git] / gcc / c / c-typeck.c
blobc6303f9b97774eb0ec327fe3b51e5b8675ef31e0
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "hash-set.h"
31 #include "vec.h"
32 #include "symtab.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "double-int.h"
36 #include "machmode.h"
37 #include "inchash.h"
38 #include "real.h"
39 #include "fixed-value.h"
40 #include "tree.h"
41 #include "tree-upc.h"
42 #include "fold-const.h"
43 #include "stor-layout.h"
44 #include "trans-mem.h"
45 #include "varasm.h"
46 #include "stmt.h"
47 #include "langhooks.h"
48 #include "c-tree.h"
49 #include "c-lang.h"
50 #include "flags.h"
51 #include "intl.h"
52 #include "target.h"
53 #include "tree-iterator.h"
54 #include "bitmap.h"
55 #include "predict.h"
56 #include "vec.h"
57 #include "hashtab.h"
58 #include "hash-set.h"
59 #include "machmode.h"
60 #include "hard-reg-set.h"
61 #include "input.h"
62 #include "function.h"
63 #include "gimple-expr.h"
64 #include "gimplify.h"
65 #include "tree-inline.h"
66 #include "omp-low.h"
67 #include "c-family/c-upc-low.h"
68 #include "c-family/c-objc.h"
69 #include "c-family/c-upc.h"
70 #include "c-family/c-common.h"
71 #include "c-family/c-ubsan.h"
72 #include "cilk.h"
73 #include "wide-int.h"
74 #include "gomp-constants.h"
76 /* Possible cases of implicit bad conversions. Used to select
77 diagnostic messages in convert_for_assignment. */
78 enum impl_conv {
79 ic_argpass,
80 ic_assign,
81 ic_init,
82 ic_return
85 /* The level of nesting inside "__alignof__". */
86 int in_alignof;
88 /* The level of nesting inside "sizeof". */
89 int in_sizeof;
91 /* The level of nesting inside "typeof". */
92 int in_typeof;
94 /* The argument of last parsed sizeof expression, only to be tested
95 if expr.original_code == SIZEOF_EXPR. */
96 tree c_last_sizeof_arg;
98 /* Nonzero if we might need to print a "missing braces around
99 initializer" message within this initializer. */
100 static int found_missing_braces;
102 static int require_constant_value;
103 static int require_constant_elements;
105 static bool null_pointer_constant_p (const_tree);
106 static tree qualify_type (tree, tree);
107 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
108 bool *);
109 static int comp_target_types (location_t, tree, tree);
110 static int function_types_compatible_p (const_tree, const_tree, bool *,
111 bool *);
112 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
113 static tree lookup_field (tree, tree);
114 static int convert_arguments (location_t, vec<location_t>, tree,
115 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
116 tree);
117 static tree c_pointer_int_sum (location_t, enum tree_code, tree, tree);
118 static tree pointer_diff (location_t, tree, tree);
119 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
120 enum impl_conv, bool, tree, tree, int);
121 static tree valid_compound_expr_initializer (tree, tree);
122 static void push_string (const char *);
123 static void push_member_name (tree);
124 static int spelling_length (void);
125 static char *print_spelling (char *);
126 static void warning_init (location_t, int, const char *);
127 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
128 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
129 bool, struct obstack *);
130 static void output_pending_init_elements (int, struct obstack *);
131 static int set_designator (location_t, int, struct obstack *);
132 static void push_range_stack (tree, struct obstack *);
133 static void add_pending_init (location_t, tree, tree, tree, bool,
134 struct obstack *);
135 static void set_nonincremental_init (struct obstack *);
136 static void set_nonincremental_init_from_string (tree, struct obstack *);
137 static tree find_init_member (tree, struct obstack *);
138 static void readonly_warning (tree, enum lvalue_use);
139 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
140 static void record_maybe_used_decl (tree);
141 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
143 /* Return true if EXP is a null pointer constant, false otherwise. */
145 static bool
146 null_pointer_constant_p (const_tree expr)
148 /* This should really operate on c_expr structures, but they aren't
149 yet available everywhere required. */
150 tree type = TREE_TYPE (expr);
151 return (TREE_CODE (expr) == INTEGER_CST
152 && !TREE_OVERFLOW (expr)
153 && integer_zerop (expr)
154 && (INTEGRAL_TYPE_P (type)
155 || (TREE_CODE (type) == POINTER_TYPE
156 && VOID_TYPE_P (TREE_TYPE (type))
157 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
160 /* EXPR may appear in an unevaluated part of an integer constant
161 expression, but not in an evaluated part. Wrap it in a
162 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
163 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
165 static tree
166 note_integer_operands (tree expr)
168 tree ret;
169 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
171 ret = copy_node (expr);
172 TREE_OVERFLOW (ret) = 1;
174 else
176 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
177 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
179 return ret;
182 /* Having checked whether EXPR may appear in an unevaluated part of an
183 integer constant expression and found that it may, remove any
184 C_MAYBE_CONST_EXPR noting this fact and return the resulting
185 expression. */
187 static inline tree
188 remove_c_maybe_const_expr (tree expr)
190 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
191 return C_MAYBE_CONST_EXPR_EXPR (expr);
192 else
193 return expr;
196 \f/* This is a cache to hold if two types are compatible or not. */
198 struct tagged_tu_seen_cache {
199 const struct tagged_tu_seen_cache * next;
200 const_tree t1;
201 const_tree t2;
202 /* The return value of tagged_types_tu_compatible_p if we had seen
203 these two types already. */
204 int val;
207 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
208 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
210 /* Do `exp = require_complete_type (exp);' to make sure exp
211 does not have an incomplete type. (That includes void types.) */
213 tree
214 require_complete_type (tree value)
216 tree type = TREE_TYPE (value);
218 if (error_operand_p (value))
219 return error_mark_node;
221 /* First, detect a valid value with a complete type. */
222 if (COMPLETE_TYPE_P (type))
223 return value;
225 c_incomplete_type_error (value, type);
226 return error_mark_node;
229 /* Print an error message for invalid use of an incomplete type.
230 VALUE is the expression that was used (or 0 if that isn't known)
231 and TYPE is the type that was invalid. */
233 void
234 c_incomplete_type_error (const_tree value, const_tree type)
236 const char *type_code_string;
238 /* Avoid duplicate error message. */
239 if (TREE_CODE (type) == ERROR_MARK)
240 return;
242 if (value != 0 && (TREE_CODE (value) == VAR_DECL
243 || TREE_CODE (value) == PARM_DECL))
244 error ("%qD has an incomplete type", value);
245 else
247 retry:
248 /* We must print an error message. Be clever about what it says. */
250 switch (TREE_CODE (type))
252 case RECORD_TYPE:
253 type_code_string = "struct";
254 break;
256 case UNION_TYPE:
257 type_code_string = "union";
258 break;
260 case ENUMERAL_TYPE:
261 type_code_string = "enum";
262 break;
264 case VOID_TYPE:
265 error ("invalid use of void expression");
266 return;
268 case ARRAY_TYPE:
269 if (TYPE_DOMAIN (type))
271 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
273 error ("invalid use of flexible array member");
274 return;
276 type = TREE_TYPE (type);
277 goto retry;
279 error ("invalid use of array with unspecified bounds");
280 return;
282 default:
283 gcc_unreachable ();
286 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
287 error ("invalid use of undefined type %<%s %E%>",
288 type_code_string, TYPE_NAME (type));
289 else
290 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
291 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
295 /* Given a type, apply default promotions wrt unnamed function
296 arguments and return the new type. */
298 tree
299 c_type_promotes_to (tree type)
301 tree ret = NULL_TREE;
303 if (TYPE_MAIN_VARIANT (type) == float_type_node)
304 ret = double_type_node;
305 else if (c_promoting_integer_type_p (type))
307 /* Preserve unsignedness if not really getting any wider. */
308 if (TYPE_UNSIGNED (type)
309 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
310 ret = unsigned_type_node;
311 else
312 ret = integer_type_node;
315 if (ret != NULL_TREE)
316 return (TYPE_ATOMIC (type)
317 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
318 : ret);
320 return type;
323 /* Return true if between two named address spaces, whether there is a superset
324 named address space that encompasses both address spaces. If there is a
325 superset, return which address space is the superset. */
327 static bool
328 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
330 if (as1 == as2)
332 *common = as1;
333 return true;
335 else if (targetm.addr_space.subset_p (as1, as2))
337 *common = as2;
338 return true;
340 else if (targetm.addr_space.subset_p (as2, as1))
342 *common = as1;
343 return true;
345 else
346 return false;
349 /* Return a variant of TYPE which has all the type qualifiers of LIKE
350 as well as those of TYPE. */
352 static tree
353 qualify_type (tree type, tree like)
355 tree result_type;
356 addr_space_t as_type = TYPE_ADDR_SPACE (type);
357 addr_space_t as_like = TYPE_ADDR_SPACE (like);
358 addr_space_t as_common;
359 int result_quals;
360 tree result_block_factor = NULL_TREE;
362 gcc_assert (type && TREE_CODE (type) != ARRAY_TYPE);
363 gcc_assert (like && TREE_CODE (like) != ARRAY_TYPE);
365 /* If the two named address spaces are different, determine the common
366 superset address space. If there isn't one, raise an error. */
367 if (!addr_space_superset (as_type, as_like, &as_common))
369 as_common = as_type;
370 error ("%qT and %qT are in disjoint named address spaces",
371 type, like);
374 result_quals = TYPE_QUALS_NO_ADDR_SPACE (type)
375 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
376 | ENCODE_QUAL_ADDR_SPACE (as_common);
378 if (result_quals & TYPE_QUAL_UPC_SHARED)
380 tree b1 = TYPE_UPC_BLOCK_FACTOR (type);
381 tree b2 = TYPE_UPC_BLOCK_FACTOR (like);
382 /* We can merge in a new UPC blocking factor only
383 if one/other is NULL. Otherwise, they must match. */
384 if (b1 != b2)
386 if (b1 && !b2)
387 result_block_factor = b1;
388 else if (!b1 && b2)
389 result_block_factor = b2;
390 else
391 gcc_unreachable ();
395 result_type = c_build_qualified_type_1 (type, result_quals,
396 result_block_factor);
398 return result_type;
401 /* Return true iff the given tree T is a variable length array. */
403 bool
404 c_vla_type_p (const_tree t)
406 if (TREE_CODE (t) == ARRAY_TYPE
407 && C_TYPE_VARIABLE_SIZE (t))
408 return true;
409 return false;
412 /* Return the composite type of two compatible types.
414 We assume that comptypes has already been done and returned
415 nonzero; if that isn't so, this may crash. In particular, we
416 assume that qualifiers match. */
418 tree
419 composite_type (tree t1, tree t2)
421 enum tree_code code1;
422 enum tree_code code2;
423 tree attributes;
425 /* Save time if the two types are the same. */
427 if (t1 == t2) return t1;
429 /* If one type is nonsense, use the other. */
430 if (t1 == error_mark_node)
431 return t2;
432 if (t2 == error_mark_node)
433 return t1;
435 code1 = TREE_CODE (t1);
436 code2 = TREE_CODE (t2);
438 /* Merge the attributes. */
439 attributes = targetm.merge_type_attributes (t1, t2);
441 /* If one is an enumerated type and the other is the compatible
442 integer type, the composite type might be either of the two
443 (DR#013 question 3). For consistency, use the enumerated type as
444 the composite type. */
446 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
447 return t1;
448 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
449 return t2;
451 gcc_assert (code1 == code2);
453 switch (code1)
455 case POINTER_TYPE:
456 /* For two pointers, do this recursively on the target type. */
458 tree pointed_to_1 = TREE_TYPE (t1);
459 tree pointed_to_2 = TREE_TYPE (t2);
460 tree target = composite_type (pointed_to_1, pointed_to_2);
461 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
462 t1 = build_type_attribute_variant (t1, attributes);
463 return qualify_type (t1, t2);
466 case ARRAY_TYPE:
468 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
469 int quals;
470 tree unqual_elt;
471 tree d1 = TYPE_DOMAIN (t1);
472 tree d2 = TYPE_DOMAIN (t2);
473 bool d1_variable, d2_variable;
474 bool d1_zero, d2_zero;
475 bool t1_complete, t2_complete;
477 /* We should not have any type quals on arrays at all. */
478 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
479 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
481 t1_complete = COMPLETE_TYPE_P (t1);
482 t2_complete = COMPLETE_TYPE_P (t2);
484 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
485 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
487 d1_variable = (!d1_zero
488 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
489 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
490 d2_variable = (!d2_zero
491 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
492 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
493 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
494 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
496 /* Save space: see if the result is identical to one of the args. */
497 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
498 && (d2_variable || d2_zero || !d1_variable))
499 return build_type_attribute_variant (t1, attributes);
500 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
501 && (d1_variable || d1_zero || !d2_variable))
502 return build_type_attribute_variant (t2, attributes);
504 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
505 return build_type_attribute_variant (t1, attributes);
506 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
507 return build_type_attribute_variant (t2, attributes);
509 /* Merge the element types, and have a size if either arg has
510 one. We may have qualifiers on the element types. To set
511 up TYPE_MAIN_VARIANT correctly, we need to form the
512 composite of the unqualified types and add the qualifiers
513 back at the end. */
514 quals = TYPE_QUALS (strip_array_types (elt));
515 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
516 t1 = build_array_type (unqual_elt,
517 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
518 && (d2_variable
519 || d2_zero
520 || !d1_variable))
521 ? t1
522 : t2));
523 /* Ensure a composite type involving a zero-length array type
524 is a zero-length type not an incomplete type. */
525 if (d1_zero && d2_zero
526 && (t1_complete || t2_complete)
527 && !COMPLETE_TYPE_P (t1))
529 TYPE_SIZE (t1) = bitsize_zero_node;
530 TYPE_SIZE_UNIT (t1) = size_zero_node;
532 t1 = c_build_qualified_type (t1, quals);
533 return build_type_attribute_variant (t1, attributes);
536 case ENUMERAL_TYPE:
537 case RECORD_TYPE:
538 case UNION_TYPE:
539 if (attributes != NULL)
541 /* Try harder not to create a new aggregate type. */
542 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
543 return t1;
544 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
545 return t2;
547 return build_type_attribute_variant (t1, attributes);
549 case FUNCTION_TYPE:
550 /* Function types: prefer the one that specified arg types.
551 If both do, merge the arg types. Also merge the return types. */
553 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
554 tree p1 = TYPE_ARG_TYPES (t1);
555 tree p2 = TYPE_ARG_TYPES (t2);
556 int len;
557 tree newargs, n;
558 int i;
560 /* Save space: see if the result is identical to one of the args. */
561 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
562 return build_type_attribute_variant (t1, attributes);
563 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
564 return build_type_attribute_variant (t2, attributes);
566 /* Simple way if one arg fails to specify argument types. */
567 if (TYPE_ARG_TYPES (t1) == 0)
569 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
570 t1 = build_type_attribute_variant (t1, attributes);
571 return qualify_type (t1, t2);
573 if (TYPE_ARG_TYPES (t2) == 0)
575 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
576 t1 = build_type_attribute_variant (t1, attributes);
577 return qualify_type (t1, t2);
580 /* If both args specify argument types, we must merge the two
581 lists, argument by argument. */
583 len = list_length (p1);
584 newargs = 0;
586 for (i = 0; i < len; i++)
587 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
589 n = newargs;
591 for (; p1;
592 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
594 /* A null type means arg type is not specified.
595 Take whatever the other function type has. */
596 if (TREE_VALUE (p1) == 0)
598 TREE_VALUE (n) = TREE_VALUE (p2);
599 goto parm_done;
601 if (TREE_VALUE (p2) == 0)
603 TREE_VALUE (n) = TREE_VALUE (p1);
604 goto parm_done;
607 /* Given wait (union {union wait *u; int *i} *)
608 and wait (union wait *),
609 prefer union wait * as type of parm. */
610 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
611 && TREE_VALUE (p1) != TREE_VALUE (p2))
613 tree memb;
614 tree mv2 = TREE_VALUE (p2);
615 if (mv2 && mv2 != error_mark_node
616 && TREE_CODE (mv2) != ARRAY_TYPE)
617 mv2 = TYPE_MAIN_VARIANT (mv2);
618 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
619 memb; memb = DECL_CHAIN (memb))
621 tree mv3 = TREE_TYPE (memb);
622 if (mv3 && mv3 != error_mark_node
623 && TREE_CODE (mv3) != ARRAY_TYPE)
624 mv3 = TYPE_MAIN_VARIANT (mv3);
625 if (comptypes (mv3, mv2))
627 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
628 TREE_VALUE (p2));
629 pedwarn (input_location, OPT_Wpedantic,
630 "function types not truly compatible in ISO C");
631 goto parm_done;
635 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
636 && TREE_VALUE (p2) != TREE_VALUE (p1))
638 tree memb;
639 tree mv1 = TREE_VALUE (p1);
640 if (mv1 && mv1 != error_mark_node
641 && TREE_CODE (mv1) != ARRAY_TYPE)
642 mv1 = TYPE_MAIN_VARIANT (mv1);
643 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
644 memb; memb = DECL_CHAIN (memb))
646 tree mv3 = TREE_TYPE (memb);
647 if (mv3 && mv3 != error_mark_node
648 && TREE_CODE (mv3) != ARRAY_TYPE)
649 mv3 = TYPE_MAIN_VARIANT (mv3);
650 if (comptypes (mv3, mv1))
652 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
653 TREE_VALUE (p1));
654 pedwarn (input_location, OPT_Wpedantic,
655 "function types not truly compatible in ISO C");
656 goto parm_done;
660 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
661 parm_done: ;
664 t1 = build_function_type (valtype, newargs);
665 t1 = qualify_type (t1, t2);
666 /* ... falls through ... */
669 default:
670 return build_type_attribute_variant (t1, attributes);
675 /* Return the type of a conditional expression between pointers to
676 possibly differently qualified versions of compatible types.
678 We assume that comp_target_types has already been done and returned
679 nonzero; if that isn't so, this may crash. */
681 static tree
682 common_pointer_type (tree t1, tree t2)
684 tree attributes;
685 tree pointed_to_1, mv1;
686 tree pointed_to_2, mv2;
687 tree target;
688 unsigned target_quals;
689 tree target_block_factor = NULL_TREE;
690 addr_space_t as1, as2, as_common;
691 int quals1, quals2;
693 /* Save time if the two types are the same. */
695 if (t1 == t2) return t1;
697 /* If one type is nonsense, use the other. */
698 if (t1 == error_mark_node)
699 return t2;
700 if (t2 == error_mark_node)
701 return t1;
703 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
704 && TREE_CODE (t2) == POINTER_TYPE);
706 /* Merge the attributes. */
707 attributes = targetm.merge_type_attributes (t1, t2);
709 /* Find the composite type of the target types, and combine the
710 qualifiers of the two types' targets. Do not lose qualifiers on
711 array element types by taking the TYPE_MAIN_VARIANT. */
712 mv1 = pointed_to_1 = TREE_TYPE (t1);
713 mv2 = pointed_to_2 = TREE_TYPE (t2);
714 if (TREE_CODE (mv1) != ARRAY_TYPE)
715 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
716 if (TREE_CODE (mv2) != ARRAY_TYPE)
717 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
718 target = composite_type (mv1, mv2);
720 /* Strip array types to get correct qualifier for pointers to arrays */
721 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
722 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
724 /* For function types do not merge const qualifiers, but drop them
725 if used inconsistently. The middle-end uses these to mark const
726 and noreturn functions. */
727 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
728 target_quals = (quals1 & quals2);
729 else
730 target_quals = (quals1 | quals2);
732 if (target_quals & TYPE_QUAL_UPC_SHARED)
733 target_block_factor = TYPE_UPC_BLOCK_FACTOR (
734 strip_array_types (pointed_to_1));
736 /* If the two named address spaces are different, determine the common
737 superset address space. This is guaranteed to exist due to the
738 assumption that comp_target_type returned non-zero. */
739 as1 = TYPE_ADDR_SPACE (pointed_to_1);
740 as2 = TYPE_ADDR_SPACE (pointed_to_2);
741 if (!addr_space_superset (as1, as2, &as_common))
742 gcc_unreachable ();
744 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
745 t2 = c_build_qualified_type_1 (target, target_quals, target_block_factor);
747 t1 = build_pointer_type (t2);
748 return build_type_attribute_variant (t1, attributes);
751 /* Return the common type for two arithmetic types under the usual
752 arithmetic conversions. The default conversions have already been
753 applied, and enumerated types converted to their compatible integer
754 types. The resulting type is unqualified and has no attributes.
756 This is the type for the result of most arithmetic operations
757 if the operands have the given two types. */
759 static tree
760 c_common_type (tree t1, tree t2)
762 enum tree_code code1;
763 enum tree_code code2;
765 /* If one type is nonsense, use the other. */
766 if (t1 == error_mark_node)
767 return t2;
768 if (t2 == error_mark_node)
769 return t1;
771 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
772 t1 = TYPE_MAIN_VARIANT (t1);
774 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
775 t2 = TYPE_MAIN_VARIANT (t2);
777 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
778 t1 = build_type_attribute_variant (t1, NULL_TREE);
780 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
781 t2 = build_type_attribute_variant (t2, NULL_TREE);
783 /* Save time if the two types are the same. */
785 if (t1 == t2) return t1;
787 code1 = TREE_CODE (t1);
788 code2 = TREE_CODE (t2);
790 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
791 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
792 || code1 == INTEGER_TYPE);
793 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
794 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
795 || code2 == INTEGER_TYPE);
797 /* When one operand is a decimal float type, the other operand cannot be
798 a generic float type or a complex type. We also disallow vector types
799 here. */
800 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
801 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
803 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
805 error ("can%'t mix operands of decimal float and vector types");
806 return error_mark_node;
808 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
810 error ("can%'t mix operands of decimal float and complex types");
811 return error_mark_node;
813 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
815 error ("can%'t mix operands of decimal float and other float types");
816 return error_mark_node;
820 /* If one type is a vector type, return that type. (How the usual
821 arithmetic conversions apply to the vector types extension is not
822 precisely specified.) */
823 if (code1 == VECTOR_TYPE)
824 return t1;
826 if (code2 == VECTOR_TYPE)
827 return t2;
829 /* If one type is complex, form the common type of the non-complex
830 components, then make that complex. Use T1 or T2 if it is the
831 required type. */
832 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
834 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
835 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
836 tree subtype = c_common_type (subtype1, subtype2);
838 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
839 return t1;
840 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
841 return t2;
842 else
843 return build_complex_type (subtype);
846 /* If only one is real, use it as the result. */
848 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
849 return t1;
851 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
852 return t2;
854 /* If both are real and either are decimal floating point types, use
855 the decimal floating point type with the greater precision. */
857 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
859 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
860 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
861 return dfloat128_type_node;
862 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
863 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
864 return dfloat64_type_node;
865 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
866 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
867 return dfloat32_type_node;
870 /* Deal with fixed-point types. */
871 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
873 unsigned int unsignedp = 0, satp = 0;
874 machine_mode m1, m2;
875 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
877 m1 = TYPE_MODE (t1);
878 m2 = TYPE_MODE (t2);
880 /* If one input type is saturating, the result type is saturating. */
881 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
882 satp = 1;
884 /* If both fixed-point types are unsigned, the result type is unsigned.
885 When mixing fixed-point and integer types, follow the sign of the
886 fixed-point type.
887 Otherwise, the result type is signed. */
888 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
889 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
890 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
891 && TYPE_UNSIGNED (t1))
892 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
893 && TYPE_UNSIGNED (t2)))
894 unsignedp = 1;
896 /* The result type is signed. */
897 if (unsignedp == 0)
899 /* If the input type is unsigned, we need to convert to the
900 signed type. */
901 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
903 enum mode_class mclass = (enum mode_class) 0;
904 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
905 mclass = MODE_FRACT;
906 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
907 mclass = MODE_ACCUM;
908 else
909 gcc_unreachable ();
910 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
912 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
914 enum mode_class mclass = (enum mode_class) 0;
915 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
916 mclass = MODE_FRACT;
917 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
918 mclass = MODE_ACCUM;
919 else
920 gcc_unreachable ();
921 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
925 if (code1 == FIXED_POINT_TYPE)
927 fbit1 = GET_MODE_FBIT (m1);
928 ibit1 = GET_MODE_IBIT (m1);
930 else
932 fbit1 = 0;
933 /* Signed integers need to subtract one sign bit. */
934 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
937 if (code2 == FIXED_POINT_TYPE)
939 fbit2 = GET_MODE_FBIT (m2);
940 ibit2 = GET_MODE_IBIT (m2);
942 else
944 fbit2 = 0;
945 /* Signed integers need to subtract one sign bit. */
946 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
949 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
950 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
951 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
952 satp);
955 /* Both real or both integers; use the one with greater precision. */
957 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
958 return t1;
959 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
960 return t2;
962 /* Same precision. Prefer long longs to longs to ints when the
963 same precision, following the C99 rules on integer type rank
964 (which are equivalent to the C90 rules for C90 types). */
966 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
967 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
968 return long_long_unsigned_type_node;
970 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
971 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
973 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
974 return long_long_unsigned_type_node;
975 else
976 return long_long_integer_type_node;
979 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
980 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
981 return long_unsigned_type_node;
983 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
984 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
986 /* But preserve unsignedness from the other type,
987 since long cannot hold all the values of an unsigned int. */
988 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
989 return long_unsigned_type_node;
990 else
991 return long_integer_type_node;
994 /* Likewise, prefer long double to double even if same size. */
995 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
996 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
997 return long_double_type_node;
999 /* Likewise, prefer double to float even if same size.
1000 We got a couple of embedded targets with 32 bit doubles, and the
1001 pdp11 might have 64 bit floats. */
1002 if (TYPE_MAIN_VARIANT (t1) == double_type_node
1003 || TYPE_MAIN_VARIANT (t2) == double_type_node)
1004 return double_type_node;
1006 /* Otherwise prefer the unsigned one. */
1008 if (TYPE_UNSIGNED (t1))
1009 return t1;
1010 else
1011 return t2;
1014 /* Wrapper around c_common_type that is used by c-common.c and other
1015 front end optimizations that remove promotions. ENUMERAL_TYPEs
1016 are allowed here and are converted to their compatible integer types.
1017 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1018 preferably a non-Boolean type as the common type. */
1019 tree
1020 common_type (tree t1, tree t2)
1022 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1023 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1024 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1025 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1027 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1028 if (TREE_CODE (t1) == BOOLEAN_TYPE
1029 && TREE_CODE (t2) == BOOLEAN_TYPE)
1030 return boolean_type_node;
1032 /* If either type is BOOLEAN_TYPE, then return the other. */
1033 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1034 return t2;
1035 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1036 return t1;
1038 return c_common_type (t1, t2);
1041 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1042 or various other operations. Return 2 if they are compatible
1043 but a warning may be needed if you use them together. */
1046 comptypes (tree type1, tree type2)
1048 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1049 int val;
1051 val = comptypes_internal (type1, type2, NULL, NULL);
1052 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1054 return val;
1057 /* Like comptypes, but if it returns non-zero because enum and int are
1058 compatible, it sets *ENUM_AND_INT_P to true. */
1060 static int
1061 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1063 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1064 int val;
1066 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1067 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1069 return val;
1072 /* Like comptypes, but if it returns nonzero for different types, it
1073 sets *DIFFERENT_TYPES_P to true. */
1076 comptypes_check_different_types (tree type1, tree type2,
1077 bool *different_types_p)
1079 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1080 int val;
1082 val = comptypes_internal (type1, type2, NULL, different_types_p);
1083 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1085 return val;
1088 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1089 or various other operations. Return 2 if they are compatible
1090 but a warning may be needed if you use them together. If
1091 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1092 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1093 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1094 NULL, and the types are compatible but different enough not to be
1095 permitted in C11 typedef redeclarations, then this sets
1096 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1097 false, but may or may not be set if the types are incompatible.
1098 This differs from comptypes, in that we don't free the seen
1099 types. */
1101 static int
1102 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1103 bool *different_types_p)
1105 const_tree t1 = type1;
1106 const_tree t2 = type2;
1107 int attrval, val;
1109 /* Suppress errors caused by previously reported errors. */
1111 if (t1 == t2 || !t1 || !t2
1112 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1113 return 1;
1115 /* Enumerated types are compatible with integer types, but this is
1116 not transitive: two enumerated types in the same translation unit
1117 are compatible with each other only if they are the same type. */
1119 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1121 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1122 if (TREE_CODE (t2) != VOID_TYPE)
1124 if (enum_and_int_p != NULL)
1125 *enum_and_int_p = true;
1126 if (different_types_p != NULL)
1127 *different_types_p = true;
1130 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1132 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1133 if (TREE_CODE (t1) != VOID_TYPE)
1135 if (enum_and_int_p != NULL)
1136 *enum_and_int_p = true;
1137 if (different_types_p != NULL)
1138 *different_types_p = true;
1142 if (t1 == t2)
1143 return 1;
1145 /* Different classes of types can't be compatible. */
1147 if (TREE_CODE (t1) != TREE_CODE (t2))
1148 return 0;
1150 /* Qualifiers must match. C99 6.7.3p9 */
1152 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1153 return 0;
1155 /* If the type is UPC qualified, the block sizes have
1156 to be equal. The block sizes are either NULL
1157 or are the same integer constant. */
1158 if ((TYPE_QUALS (t1) & TYPE_QUAL_UPC_SHARED)
1159 && (TYPE_UPC_BLOCK_FACTOR (t1) != TYPE_UPC_BLOCK_FACTOR (t2)))
1160 return 0;
1162 /* Allow for two different type nodes which have essentially the same
1163 definition. Note that we already checked for equality of the type
1164 qualifiers (just above). */
1166 if (TREE_CODE (t1) != ARRAY_TYPE
1167 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1168 return 1;
1170 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1171 if (!(attrval = comp_type_attributes (t1, t2)))
1172 return 0;
1174 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1175 val = 0;
1177 switch (TREE_CODE (t1))
1179 case POINTER_TYPE:
1180 /* Do not remove mode or aliasing information. */
1181 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1182 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1183 break;
1184 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1185 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1186 enum_and_int_p, different_types_p));
1187 break;
1189 case FUNCTION_TYPE:
1190 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1191 different_types_p);
1192 break;
1194 case ARRAY_TYPE:
1196 tree d1 = TYPE_DOMAIN (t1);
1197 tree d2 = TYPE_DOMAIN (t2);
1198 bool d1_variable, d2_variable;
1199 bool d1_zero, d2_zero;
1200 val = 1;
1202 /* Target types must match incl. qualifiers. */
1203 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1204 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1205 enum_and_int_p,
1206 different_types_p)))
1207 return 0;
1209 if (different_types_p != NULL
1210 && (d1 == 0) != (d2 == 0))
1211 *different_types_p = true;
1212 /* Sizes must match unless one is missing or variable. */
1213 if (d1 == 0 || d2 == 0 || d1 == d2)
1214 break;
1216 d1_zero = !TYPE_MAX_VALUE (d1);
1217 d2_zero = !TYPE_MAX_VALUE (d2);
1219 d1_variable = (!d1_zero
1220 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1221 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1222 d2_variable = (!d2_zero
1223 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1224 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1225 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1226 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1228 if (different_types_p != NULL
1229 && d1_variable != d2_variable)
1230 *different_types_p = true;
1231 if (d1_variable || d2_variable)
1232 break;
1233 if (d1_zero && d2_zero)
1234 break;
1235 if (d1_zero || d2_zero
1236 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1237 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1238 val = 0;
1240 break;
1243 case ENUMERAL_TYPE:
1244 case RECORD_TYPE:
1245 case UNION_TYPE:
1246 if (val != 1 && !same_translation_unit_p (t1, t2))
1248 tree a1 = TYPE_ATTRIBUTES (t1);
1249 tree a2 = TYPE_ATTRIBUTES (t2);
1251 if (! attribute_list_contained (a1, a2)
1252 && ! attribute_list_contained (a2, a1))
1253 break;
1255 if (attrval != 2)
1256 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1257 different_types_p);
1258 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1259 different_types_p);
1261 break;
1263 case VECTOR_TYPE:
1264 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1265 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1266 enum_and_int_p, different_types_p));
1267 break;
1269 default:
1270 break;
1272 return attrval == 2 && val == 1 ? 2 : val;
1275 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1276 their qualifiers, except for named address spaces. If the pointers point to
1277 different named addresses, then we must determine if one address space is a
1278 subset of the other. */
1280 static int
1281 comp_target_types (location_t location, tree ttl, tree ttr)
1283 int val;
1284 int val_ped;
1285 tree mvl = TREE_TYPE (ttl);
1286 tree mvr = TREE_TYPE (ttr);
1287 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1288 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1289 addr_space_t as_common;
1290 bool enum_and_int_p;
1292 /* Fail if pointers point to incompatible address spaces. */
1293 if (!addr_space_superset (asl, asr, &as_common))
1294 return 0;
1296 /* For pedantic record result of comptypes on arrays before losing
1297 qualifiers on the element type below. */
1298 val_ped = 1;
1300 if (TREE_CODE (mvl) == ARRAY_TYPE
1301 && TREE_CODE (mvr) == ARRAY_TYPE)
1302 val_ped = comptypes (mvl, mvr);
1304 /* Qualifiers on element types of array types that are
1305 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1307 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1308 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1309 : TYPE_MAIN_VARIANT (mvl));
1311 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1312 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1313 : TYPE_MAIN_VARIANT (mvr));
1315 enum_and_int_p = false;
1316 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1318 if (val == 1 && val_ped != 1)
1319 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1320 "are incompatible in ISO C");
1322 if (val == 2)
1323 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1325 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1326 warning_at (location, OPT_Wc___compat,
1327 "pointer target types incompatible in C++");
1329 return val;
1332 /* Subroutines of `comptypes'. */
1334 /* Determine whether two trees derive from the same translation unit.
1335 If the CONTEXT chain ends in a null, that tree's context is still
1336 being parsed, so if two trees have context chains ending in null,
1337 they're in the same translation unit. */
1339 same_translation_unit_p (const_tree t1, const_tree t2)
1341 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1342 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1344 case tcc_declaration:
1345 t1 = DECL_CONTEXT (t1); break;
1346 case tcc_type:
1347 t1 = TYPE_CONTEXT (t1); break;
1348 case tcc_exceptional:
1349 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1350 default: gcc_unreachable ();
1353 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1354 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1356 case tcc_declaration:
1357 t2 = DECL_CONTEXT (t2); break;
1358 case tcc_type:
1359 t2 = TYPE_CONTEXT (t2); break;
1360 case tcc_exceptional:
1361 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1362 default: gcc_unreachable ();
1365 return t1 == t2;
1368 /* Allocate the seen two types, assuming that they are compatible. */
1370 static struct tagged_tu_seen_cache *
1371 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1373 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1374 tu->next = tagged_tu_seen_base;
1375 tu->t1 = t1;
1376 tu->t2 = t2;
1378 tagged_tu_seen_base = tu;
1380 /* The C standard says that two structures in different translation
1381 units are compatible with each other only if the types of their
1382 fields are compatible (among other things). We assume that they
1383 are compatible until proven otherwise when building the cache.
1384 An example where this can occur is:
1385 struct a
1387 struct a *next;
1389 If we are comparing this against a similar struct in another TU,
1390 and did not assume they were compatible, we end up with an infinite
1391 loop. */
1392 tu->val = 1;
1393 return tu;
1396 /* Free the seen types until we get to TU_TIL. */
1398 static void
1399 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1401 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1402 while (tu != tu_til)
1404 const struct tagged_tu_seen_cache *const tu1
1405 = (const struct tagged_tu_seen_cache *) tu;
1406 tu = tu1->next;
1407 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1409 tagged_tu_seen_base = tu_til;
1412 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1413 compatible. If the two types are not the same (which has been
1414 checked earlier), this can only happen when multiple translation
1415 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1416 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1417 comptypes_internal. */
1419 static int
1420 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1421 bool *enum_and_int_p, bool *different_types_p)
1423 tree s1, s2;
1424 bool needs_warning = false;
1426 /* We have to verify that the tags of the types are the same. This
1427 is harder than it looks because this may be a typedef, so we have
1428 to go look at the original type. It may even be a typedef of a
1429 typedef...
1430 In the case of compiler-created builtin structs the TYPE_DECL
1431 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1432 while (TYPE_NAME (t1)
1433 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1434 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1435 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1437 while (TYPE_NAME (t2)
1438 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1439 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1440 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1442 /* C90 didn't have the requirement that the two tags be the same. */
1443 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1444 return 0;
1446 /* C90 didn't say what happened if one or both of the types were
1447 incomplete; we choose to follow C99 rules here, which is that they
1448 are compatible. */
1449 if (TYPE_SIZE (t1) == NULL
1450 || TYPE_SIZE (t2) == NULL)
1451 return 1;
1454 const struct tagged_tu_seen_cache * tts_i;
1455 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1456 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1457 return tts_i->val;
1460 switch (TREE_CODE (t1))
1462 case ENUMERAL_TYPE:
1464 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1465 /* Speed up the case where the type values are in the same order. */
1466 tree tv1 = TYPE_VALUES (t1);
1467 tree tv2 = TYPE_VALUES (t2);
1469 if (tv1 == tv2)
1471 return 1;
1474 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1476 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1477 break;
1478 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1480 tu->val = 0;
1481 return 0;
1485 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1487 return 1;
1489 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1491 tu->val = 0;
1492 return 0;
1495 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1497 tu->val = 0;
1498 return 0;
1501 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1503 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1504 if (s2 == NULL
1505 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1507 tu->val = 0;
1508 return 0;
1511 return 1;
1514 case UNION_TYPE:
1516 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1517 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1519 tu->val = 0;
1520 return 0;
1523 /* Speed up the common case where the fields are in the same order. */
1524 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1525 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1527 int result;
1529 if (DECL_NAME (s1) != DECL_NAME (s2))
1530 break;
1531 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1532 enum_and_int_p, different_types_p);
1534 if (result != 1 && !DECL_NAME (s1))
1535 break;
1536 if (result == 0)
1538 tu->val = 0;
1539 return 0;
1541 if (result == 2)
1542 needs_warning = true;
1544 if (TREE_CODE (s1) == FIELD_DECL
1545 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1546 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1548 tu->val = 0;
1549 return 0;
1552 if (!s1 && !s2)
1554 tu->val = needs_warning ? 2 : 1;
1555 return tu->val;
1558 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1560 bool ok = false;
1562 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1563 if (DECL_NAME (s1) == DECL_NAME (s2))
1565 int result;
1567 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1568 enum_and_int_p,
1569 different_types_p);
1571 if (result != 1 && !DECL_NAME (s1))
1572 continue;
1573 if (result == 0)
1575 tu->val = 0;
1576 return 0;
1578 if (result == 2)
1579 needs_warning = true;
1581 if (TREE_CODE (s1) == FIELD_DECL
1582 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1583 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1584 break;
1586 ok = true;
1587 break;
1589 if (!ok)
1591 tu->val = 0;
1592 return 0;
1595 tu->val = needs_warning ? 2 : 10;
1596 return tu->val;
1599 case RECORD_TYPE:
1601 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1603 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1604 s1 && s2;
1605 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1607 int result;
1608 if (TREE_CODE (s1) != TREE_CODE (s2)
1609 || DECL_NAME (s1) != DECL_NAME (s2))
1610 break;
1611 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1612 enum_and_int_p, different_types_p);
1613 if (result == 0)
1614 break;
1615 if (result == 2)
1616 needs_warning = true;
1618 if (TREE_CODE (s1) == FIELD_DECL
1619 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1620 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1621 break;
1623 if (s1 && s2)
1624 tu->val = 0;
1625 else
1626 tu->val = needs_warning ? 2 : 1;
1627 return tu->val;
1630 default:
1631 gcc_unreachable ();
1635 /* Return 1 if two function types F1 and F2 are compatible.
1636 If either type specifies no argument types,
1637 the other must specify a fixed number of self-promoting arg types.
1638 Otherwise, if one type specifies only the number of arguments,
1639 the other must specify that number of self-promoting arg types.
1640 Otherwise, the argument types must match.
1641 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1643 static int
1644 function_types_compatible_p (const_tree f1, const_tree f2,
1645 bool *enum_and_int_p, bool *different_types_p)
1647 tree args1, args2;
1648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1649 int val = 1;
1650 int val1;
1651 tree ret1, ret2;
1653 ret1 = TREE_TYPE (f1);
1654 ret2 = TREE_TYPE (f2);
1656 /* 'volatile' qualifiers on a function's return type used to mean
1657 the function is noreturn. */
1658 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1659 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1660 if (TYPE_VOLATILE (ret1))
1661 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1662 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1663 if (TYPE_VOLATILE (ret2))
1664 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1665 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1666 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1667 if (val == 0)
1668 return 0;
1670 args1 = TYPE_ARG_TYPES (f1);
1671 args2 = TYPE_ARG_TYPES (f2);
1673 if (different_types_p != NULL
1674 && (args1 == 0) != (args2 == 0))
1675 *different_types_p = true;
1677 /* An unspecified parmlist matches any specified parmlist
1678 whose argument types don't need default promotions. */
1680 if (args1 == 0)
1682 if (!self_promoting_args_p (args2))
1683 return 0;
1684 /* If one of these types comes from a non-prototype fn definition,
1685 compare that with the other type's arglist.
1686 If they don't match, ask for a warning (but no error). */
1687 if (TYPE_ACTUAL_ARG_TYPES (f1)
1688 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1689 enum_and_int_p, different_types_p))
1690 val = 2;
1691 return val;
1693 if (args2 == 0)
1695 if (!self_promoting_args_p (args1))
1696 return 0;
1697 if (TYPE_ACTUAL_ARG_TYPES (f2)
1698 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1699 enum_and_int_p, different_types_p))
1700 val = 2;
1701 return val;
1704 /* Both types have argument lists: compare them and propagate results. */
1705 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1706 different_types_p);
1707 return val1 != 1 ? val1 : val;
1710 /* Check two lists of types for compatibility, returning 0 for
1711 incompatible, 1 for compatible, or 2 for compatible with
1712 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1713 comptypes_internal. */
1715 static int
1716 type_lists_compatible_p (const_tree args1, const_tree args2,
1717 bool *enum_and_int_p, bool *different_types_p)
1719 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1720 int val = 1;
1721 int newval = 0;
1723 while (1)
1725 tree a1, mv1, a2, mv2;
1726 if (args1 == 0 && args2 == 0)
1727 return val;
1728 /* If one list is shorter than the other,
1729 they fail to match. */
1730 if (args1 == 0 || args2 == 0)
1731 return 0;
1732 mv1 = a1 = TREE_VALUE (args1);
1733 mv2 = a2 = TREE_VALUE (args2);
1734 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1735 mv1 = (TYPE_ATOMIC (mv1)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (mv1));
1739 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1740 mv2 = (TYPE_ATOMIC (mv2)
1741 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1742 TYPE_QUAL_ATOMIC)
1743 : TYPE_MAIN_VARIANT (mv2));
1744 /* A null pointer instead of a type
1745 means there is supposed to be an argument
1746 but nothing is specified about what type it has.
1747 So match anything that self-promotes. */
1748 if (different_types_p != NULL
1749 && (a1 == 0) != (a2 == 0))
1750 *different_types_p = true;
1751 if (a1 == 0)
1753 if (c_type_promotes_to (a2) != a2)
1754 return 0;
1756 else if (a2 == 0)
1758 if (c_type_promotes_to (a1) != a1)
1759 return 0;
1761 /* If one of the lists has an error marker, ignore this arg. */
1762 else if (TREE_CODE (a1) == ERROR_MARK
1763 || TREE_CODE (a2) == ERROR_MARK)
1765 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1766 different_types_p)))
1768 if (different_types_p != NULL)
1769 *different_types_p = true;
1770 /* Allow wait (union {union wait *u; int *i} *)
1771 and wait (union wait *) to be compatible. */
1772 if (TREE_CODE (a1) == UNION_TYPE
1773 && (TYPE_NAME (a1) == 0
1774 || TYPE_TRANSPARENT_AGGR (a1))
1775 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1776 && tree_int_cst_equal (TYPE_SIZE (a1),
1777 TYPE_SIZE (a2)))
1779 tree memb;
1780 for (memb = TYPE_FIELDS (a1);
1781 memb; memb = DECL_CHAIN (memb))
1783 tree mv3 = TREE_TYPE (memb);
1784 if (mv3 && mv3 != error_mark_node
1785 && TREE_CODE (mv3) != ARRAY_TYPE)
1786 mv3 = (TYPE_ATOMIC (mv3)
1787 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1788 TYPE_QUAL_ATOMIC)
1789 : TYPE_MAIN_VARIANT (mv3));
1790 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1791 different_types_p))
1792 break;
1794 if (memb == 0)
1795 return 0;
1797 else if (TREE_CODE (a2) == UNION_TYPE
1798 && (TYPE_NAME (a2) == 0
1799 || TYPE_TRANSPARENT_AGGR (a2))
1800 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1801 && tree_int_cst_equal (TYPE_SIZE (a2),
1802 TYPE_SIZE (a1)))
1804 tree memb;
1805 for (memb = TYPE_FIELDS (a2);
1806 memb; memb = DECL_CHAIN (memb))
1808 tree mv3 = TREE_TYPE (memb);
1809 if (mv3 && mv3 != error_mark_node
1810 && TREE_CODE (mv3) != ARRAY_TYPE)
1811 mv3 = (TYPE_ATOMIC (mv3)
1812 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1813 TYPE_QUAL_ATOMIC)
1814 : TYPE_MAIN_VARIANT (mv3));
1815 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1816 different_types_p))
1817 break;
1819 if (memb == 0)
1820 return 0;
1822 else
1823 return 0;
1826 /* comptypes said ok, but record if it said to warn. */
1827 if (newval > val)
1828 val = newval;
1830 args1 = TREE_CHAIN (args1);
1831 args2 = TREE_CHAIN (args2);
1835 /* Compute the size to increment a pointer by. When a function type or void
1836 type or incomplete type is passed, size_one_node is returned.
1837 This function does not emit any diagnostics; the caller is responsible
1838 for that. */
1840 static tree
1841 c_size_in_bytes (const_tree type)
1843 enum tree_code code = TREE_CODE (type);
1845 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1846 || !COMPLETE_TYPE_P (type))
1847 return size_one_node;
1849 /* Convert in case a char is more than one unit. */
1850 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1851 size_int (TYPE_PRECISION (char_type_node)
1852 / BITS_PER_UNIT));
1855 /* Return either DECL or its known constant value (if it has one). */
1857 tree
1858 decl_constant_value (tree decl)
1860 if (/* Don't change a variable array bound or initial value to a constant
1861 in a place where a variable is invalid. Note that DECL_INITIAL
1862 isn't valid for a PARM_DECL. */
1863 current_function_decl != 0
1864 && TREE_CODE (decl) != PARM_DECL
1865 && !TREE_THIS_VOLATILE (decl)
1866 && TREE_READONLY (decl)
1867 && DECL_INITIAL (decl) != 0
1868 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1869 /* This is invalid if initial value is not constant.
1870 If it has either a function call, a memory reference,
1871 or a variable, then re-evaluating it could give different results. */
1872 && TREE_CONSTANT (DECL_INITIAL (decl))
1873 /* Check for cases where this is sub-optimal, even though valid. */
1874 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1875 return DECL_INITIAL (decl);
1876 return decl;
1879 /* Convert the array expression EXP to a pointer. */
1880 static tree
1881 array_to_pointer_conversion (location_t loc, tree exp)
1883 tree orig_exp = exp;
1884 tree type = TREE_TYPE (exp);
1885 tree adr;
1886 tree restype = TREE_TYPE (type);
1887 tree ptrtype;
1889 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1891 STRIP_TYPE_NOPS (exp);
1893 if (TREE_NO_WARNING (orig_exp))
1894 TREE_NO_WARNING (exp) = 1;
1896 ptrtype = build_pointer_type (restype);
1898 if (TREE_CODE (exp) == INDIRECT_REF)
1899 return convert (ptrtype, TREE_OPERAND (exp, 0));
1901 /* In C++ array compound literals are temporary objects unless they are
1902 const or appear in namespace scope, so they are destroyed too soon
1903 to use them for much of anything (c++/53220). */
1904 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1906 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1907 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1908 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1909 "converting an array compound literal to a pointer "
1910 "is ill-formed in C++");
1913 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1914 return convert (ptrtype, adr);
1917 /* Convert the function expression EXP to a pointer. */
1918 static tree
1919 function_to_pointer_conversion (location_t loc, tree exp)
1921 tree orig_exp = exp;
1923 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1925 STRIP_TYPE_NOPS (exp);
1927 if (TREE_NO_WARNING (orig_exp))
1928 TREE_NO_WARNING (exp) = 1;
1930 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1933 /* Mark EXP as read, not just set, for set but not used -Wunused
1934 warning purposes. */
1936 void
1937 mark_exp_read (tree exp)
1939 switch (TREE_CODE (exp))
1941 case VAR_DECL:
1942 case PARM_DECL:
1943 DECL_READ_P (exp) = 1;
1944 break;
1945 case ARRAY_REF:
1946 case COMPONENT_REF:
1947 case MODIFY_EXPR:
1948 case REALPART_EXPR:
1949 case IMAGPART_EXPR:
1950 CASE_CONVERT:
1951 case ADDR_EXPR:
1952 mark_exp_read (TREE_OPERAND (exp, 0));
1953 break;
1954 case COMPOUND_EXPR:
1955 case C_MAYBE_CONST_EXPR:
1956 mark_exp_read (TREE_OPERAND (exp, 1));
1957 break;
1958 default:
1959 break;
1963 /* Perform the default conversion of arrays and functions to pointers.
1964 Return the result of converting EXP. For any other expression, just
1965 return EXP.
1967 LOC is the location of the expression. */
1969 struct c_expr
1970 default_function_array_conversion (location_t loc, struct c_expr exp)
1972 tree orig_exp = exp.value;
1973 tree type = TREE_TYPE (exp.value);
1974 enum tree_code code = TREE_CODE (type);
1976 switch (code)
1978 case ARRAY_TYPE:
1980 bool not_lvalue = false;
1981 bool lvalue_array_p;
1983 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1984 || CONVERT_EXPR_P (exp.value))
1985 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1987 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1988 not_lvalue = true;
1989 exp.value = TREE_OPERAND (exp.value, 0);
1992 if (TREE_NO_WARNING (orig_exp))
1993 TREE_NO_WARNING (exp.value) = 1;
1995 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1996 if (!flag_isoc99 && !lvalue_array_p)
1998 /* Before C99, non-lvalue arrays do not decay to pointers.
1999 Normally, using such an array would be invalid; but it can
2000 be used correctly inside sizeof or as a statement expression.
2001 Thus, do not give an error here; an error will result later. */
2002 return exp;
2005 exp.value = array_to_pointer_conversion (loc, exp.value);
2007 break;
2008 case FUNCTION_TYPE:
2009 exp.value = function_to_pointer_conversion (loc, exp.value);
2010 break;
2011 default:
2012 break;
2015 return exp;
2018 struct c_expr
2019 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2021 mark_exp_read (exp.value);
2022 return default_function_array_conversion (loc, exp);
2025 /* Return whether EXPR should be treated as an atomic lvalue for the
2026 purposes of load and store handling. */
2028 static bool
2029 really_atomic_lvalue (tree expr)
2031 if (error_operand_p (expr))
2032 return false;
2033 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2034 return false;
2035 if (!lvalue_p (expr))
2036 return false;
2038 /* Ignore _Atomic on register variables, since their addresses can't
2039 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2040 sequences wouldn't work. Ignore _Atomic on structures containing
2041 bit-fields, since accessing elements of atomic structures or
2042 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2043 it's undefined at translation time or execution time, and the
2044 normal atomic sequences again wouldn't work. */
2045 while (handled_component_p (expr))
2047 if (TREE_CODE (expr) == COMPONENT_REF
2048 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2049 return false;
2050 expr = TREE_OPERAND (expr, 0);
2052 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2053 return false;
2054 return true;
2057 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2058 including converting functions and arrays to pointers if CONVERT_P.
2059 If READ_P, also mark the expression as having been read. */
2061 struct c_expr
2062 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2063 bool convert_p, bool read_p)
2065 if (read_p)
2066 mark_exp_read (exp.value);
2067 if (convert_p)
2068 exp = default_function_array_conversion (loc, exp);
2069 if (really_atomic_lvalue (exp.value))
2071 vec<tree, va_gc> *params;
2072 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2073 tree expr_type = TREE_TYPE (exp.value);
2074 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2075 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2077 gcc_assert (TYPE_ATOMIC (expr_type));
2079 /* Expansion of a generic atomic load may require an addition
2080 element, so allocate enough to prevent a resize. */
2081 vec_alloc (params, 4);
2083 /* Remove the qualifiers for the rest of the expressions and
2084 create the VAL temp variable to hold the RHS. */
2085 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2086 tmp = create_tmp_var (nonatomic_type);
2087 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2088 TREE_ADDRESSABLE (tmp) = 1;
2089 TREE_NO_WARNING (tmp) = 1;
2091 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2092 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2093 params->quick_push (expr_addr);
2094 params->quick_push (tmp_addr);
2095 params->quick_push (seq_cst);
2096 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2098 /* EXPR is always read. */
2099 mark_exp_read (exp.value);
2101 /* Return tmp which contains the value loaded. */
2102 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2104 return exp;
2107 /* EXP is an expression of integer type. Apply the integer promotions
2108 to it and return the promoted value. */
2110 tree
2111 perform_integral_promotions (tree exp)
2113 tree type = TREE_TYPE (exp);
2114 enum tree_code code = TREE_CODE (type);
2116 gcc_assert (INTEGRAL_TYPE_P (type));
2118 /* Normally convert enums to int,
2119 but convert wide enums to something wider. */
2120 if (code == ENUMERAL_TYPE)
2122 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2123 TYPE_PRECISION (integer_type_node)),
2124 ((TYPE_PRECISION (type)
2125 >= TYPE_PRECISION (integer_type_node))
2126 && TYPE_UNSIGNED (type)));
2128 return convert (type, exp);
2131 /* ??? This should no longer be needed now bit-fields have their
2132 proper types. */
2133 if (TREE_CODE (exp) == COMPONENT_REF
2134 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2135 /* If it's thinner than an int, promote it like a
2136 c_promoting_integer_type_p, otherwise leave it alone. */
2137 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2138 TYPE_PRECISION (integer_type_node)))
2139 return convert (integer_type_node, exp);
2141 if (c_promoting_integer_type_p (type))
2143 /* Preserve unsignedness if not really getting any wider. */
2144 if (TYPE_UNSIGNED (type)
2145 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2146 return convert (unsigned_type_node, exp);
2148 return convert (integer_type_node, exp);
2151 return exp;
2155 /* Perform default promotions for C data used in expressions.
2156 Enumeral types or short or char are converted to int.
2157 In addition, manifest constants symbols are replaced by their values. */
2159 tree
2160 default_conversion (tree exp)
2162 tree orig_exp;
2163 tree type = TREE_TYPE (exp);
2164 enum tree_code code = TREE_CODE (type);
2165 tree promoted_type;
2167 mark_exp_read (exp);
2169 /* Functions and arrays have been converted during parsing. */
2170 gcc_assert (code != FUNCTION_TYPE);
2172 if (code == ARRAY_TYPE && upc_shared_type_p (type))
2173 return array_to_pointer_conversion (input_location, exp);
2175 if (code == ARRAY_TYPE)
2176 return exp;
2178 /* Constants can be used directly unless they're not loadable. */
2179 if (TREE_CODE (exp) == CONST_DECL)
2180 exp = DECL_INITIAL (exp);
2182 /* Strip no-op conversions. */
2183 orig_exp = exp;
2184 STRIP_TYPE_NOPS (exp);
2186 if (TREE_NO_WARNING (orig_exp))
2187 TREE_NO_WARNING (exp) = 1;
2189 if (code == VOID_TYPE)
2191 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2192 "void value not ignored as it ought to be");
2193 return error_mark_node;
2196 exp = require_complete_type (exp);
2197 if (exp == error_mark_node)
2198 return error_mark_node;
2200 promoted_type = targetm.promoted_type (type);
2201 if (promoted_type)
2202 return convert (promoted_type, exp);
2204 if (INTEGRAL_TYPE_P (type))
2205 return perform_integral_promotions (exp);
2207 return exp;
2210 /* Look up COMPONENT in a structure or union TYPE.
2212 If the component name is not found, returns NULL_TREE. Otherwise,
2213 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2214 stepping down the chain to the component, which is in the last
2215 TREE_VALUE of the list. Normally the list is of length one, but if
2216 the component is embedded within (nested) anonymous structures or
2217 unions, the list steps down the chain to the component. */
2219 static tree
2220 lookup_field (tree type, tree component)
2222 tree field;
2224 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2225 to the field elements. Use a binary search on this array to quickly
2226 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2227 will always be set for structures which have many elements. */
2229 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2231 int bot, top, half;
2232 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2234 field = TYPE_FIELDS (type);
2235 bot = 0;
2236 top = TYPE_LANG_SPECIFIC (type)->s->len;
2237 while (top - bot > 1)
2239 half = (top - bot + 1) >> 1;
2240 field = field_array[bot+half];
2242 if (DECL_NAME (field) == NULL_TREE)
2244 /* Step through all anon unions in linear fashion. */
2245 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2247 field = field_array[bot++];
2248 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2249 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2251 tree anon = lookup_field (TREE_TYPE (field), component);
2253 if (anon)
2254 return tree_cons (NULL_TREE, field, anon);
2256 /* The Plan 9 compiler permits referring
2257 directly to an anonymous struct/union field
2258 using a typedef name. */
2259 if (flag_plan9_extensions
2260 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2261 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2262 == TYPE_DECL)
2263 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2264 == component))
2265 break;
2269 /* Entire record is only anon unions. */
2270 if (bot > top)
2271 return NULL_TREE;
2273 /* Restart the binary search, with new lower bound. */
2274 continue;
2277 if (DECL_NAME (field) == component)
2278 break;
2279 if (DECL_NAME (field) < component)
2280 bot += half;
2281 else
2282 top = bot + half;
2285 if (DECL_NAME (field_array[bot]) == component)
2286 field = field_array[bot];
2287 else if (DECL_NAME (field) != component)
2288 return NULL_TREE;
2290 else
2292 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2294 if (DECL_NAME (field) == NULL_TREE
2295 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2296 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2298 tree anon = lookup_field (TREE_TYPE (field), component);
2300 if (anon)
2301 return tree_cons (NULL_TREE, field, anon);
2303 /* The Plan 9 compiler permits referring directly to an
2304 anonymous struct/union field using a typedef
2305 name. */
2306 if (flag_plan9_extensions
2307 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2308 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2309 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2310 == component))
2311 break;
2314 if (DECL_NAME (field) == component)
2315 break;
2318 if (field == NULL_TREE)
2319 return NULL_TREE;
2322 return tree_cons (NULL_TREE, field, NULL_TREE);
2325 /* Make an expression to refer to the COMPONENT field of structure or
2326 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2327 location of the COMPONENT_REF. */
2329 tree
2330 build_component_ref (location_t loc, tree datum, tree component)
2332 tree type = TREE_TYPE (datum);
2333 enum tree_code code = TREE_CODE (type);
2334 tree field = NULL;
2335 tree ref;
2336 bool datum_lvalue = lvalue_p (datum);
2338 if (!objc_is_public (datum, component))
2339 return error_mark_node;
2341 /* Detect Objective-C property syntax object.property. */
2342 if (c_dialect_objc ()
2343 && (ref = objc_maybe_build_component_ref (datum, component)))
2344 return ref;
2346 /* See if there is a field or component with name COMPONENT. */
2348 if (code == RECORD_TYPE || code == UNION_TYPE)
2350 if (!COMPLETE_TYPE_P (type))
2352 c_incomplete_type_error (NULL_TREE, type);
2353 return error_mark_node;
2356 field = lookup_field (type, component);
2358 if (!field)
2360 error_at (loc, "%qT has no member named %qE", type, component);
2361 return error_mark_node;
2363 gcc_assert (!TREE_SHARED (field));
2365 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2366 This might be better solved in future the way the C++ front
2367 end does it - by giving the anonymous entities each a
2368 separate name and type, and then have build_component_ref
2369 recursively call itself. We can't do that here. */
2372 tree subdatum = TREE_VALUE (field);
2373 tree sub_elem_type = strip_array_types (TREE_TYPE (subdatum));
2374 tree upc_block_factor = NULL_TREE;
2375 int quals;
2376 tree subtype;
2377 bool use_datum_quals;
2379 if (TREE_TYPE (subdatum) == error_mark_node)
2380 return error_mark_node;
2382 /* If this is an rvalue, it does not have qualifiers in C
2383 standard terms and we must avoid propagating such
2384 qualifiers down to a non-lvalue array that is then
2385 converted to a pointer. */
2386 use_datum_quals = (datum_lvalue
2387 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2389 quals = TYPE_QUALS (sub_elem_type);
2390 if (use_datum_quals)
2391 quals |= TYPE_QUALS (TREE_TYPE (datum));
2392 /* All references to UPC shared struct components
2393 are defined to have an indefinite (zero) blocking factor. */
2394 if (quals & TYPE_QUAL_UPC_SHARED)
2395 upc_block_factor = size_zero_node;
2396 subtype = c_build_qualified_type_1 (TREE_TYPE (subdatum),
2397 quals, upc_block_factor);
2399 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2400 NULL_TREE);
2401 SET_EXPR_LOCATION (ref, loc);
2402 if (TREE_READONLY (subdatum)
2403 || (use_datum_quals && TREE_READONLY (datum)))
2404 TREE_READONLY (ref) = 1;
2405 if (TREE_THIS_VOLATILE (subdatum)
2406 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2407 TREE_THIS_VOLATILE (ref) = 1;
2408 if (TREE_SHARED (datum))
2409 TREE_SHARED (ref) = 1;
2411 if (TREE_DEPRECATED (subdatum))
2412 warn_deprecated_use (subdatum, NULL_TREE);
2414 datum = ref;
2416 field = TREE_CHAIN (field);
2418 while (field);
2420 return ref;
2422 else if (code != ERROR_MARK)
2423 error_at (loc,
2424 "request for member %qE in something not a structure or union",
2425 component);
2427 return error_mark_node;
2430 /* Given an expression PTR for a pointer, return an expression
2431 for the value pointed to.
2432 ERRORSTRING is the name of the operator to appear in error messages.
2434 LOC is the location to use for the generated tree. */
2436 tree
2437 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2439 tree pointer = default_conversion (ptr);
2440 tree type = TREE_TYPE (pointer);
2441 tree ref;
2443 if (TREE_CODE (type) == POINTER_TYPE)
2445 if (CONVERT_EXPR_P (pointer)
2446 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2448 /* If a warning is issued, mark it to avoid duplicates from
2449 the backend. This only needs to be done at
2450 warn_strict_aliasing > 2. */
2451 if (warn_strict_aliasing > 2)
2452 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2453 type, TREE_OPERAND (pointer, 0)))
2454 TREE_NO_WARNING (pointer) = 1;
2457 if (TREE_CODE (pointer) == ADDR_EXPR
2458 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2459 == TREE_TYPE (type)))
2461 ref = TREE_OPERAND (pointer, 0);
2462 protected_set_expr_location (ref, loc);
2463 return ref;
2465 else
2467 tree t = TREE_TYPE (type);
2469 ref = build1 (INDIRECT_REF, t, pointer);
2471 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2473 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2475 error_at (loc, "dereferencing pointer to incomplete type "
2476 "%qT", t);
2477 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2479 return error_mark_node;
2481 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2482 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2484 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2485 so that we get the proper error message if the result is used
2486 to assign to. Also, &* is supposed to be a no-op.
2487 And ANSI C seems to specify that the type of the result
2488 should be the const type. */
2489 /* A de-reference of a pointer to const is not a const. It is valid
2490 to change it via some other pointer. */
2491 TREE_READONLY (ref) = TYPE_READONLY (t);
2492 TREE_SIDE_EFFECTS (ref)
2493 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2494 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2495 TREE_SHARED (ref) = upc_shared_type_p (t);
2496 protected_set_expr_location (ref, loc);
2497 return ref;
2500 else if (TREE_CODE (pointer) != ERROR_MARK)
2501 invalid_indirection_error (loc, type, errstring);
2503 return error_mark_node;
2506 /* This handles expressions of the form "a[i]", which denotes
2507 an array reference.
2509 This is logically equivalent in C to *(a+i), but we may do it differently.
2510 If A is a variable or a member, we generate a primitive ARRAY_REF.
2511 This avoids forcing the array out of registers, and can work on
2512 arrays that are not lvalues (for example, members of structures returned
2513 by functions).
2515 For vector types, allow vector[i] but not i[vector], and create
2516 *(((type*)&vectortype) + i) for the expression.
2518 LOC is the location to use for the returned expression. */
2520 tree
2521 build_array_ref (location_t loc, tree array, tree index)
2523 tree ret;
2524 bool swapped = false;
2525 if (TREE_TYPE (array) == error_mark_node
2526 || TREE_TYPE (index) == error_mark_node)
2527 return error_mark_node;
2529 if (flag_cilkplus && contains_array_notation_expr (index))
2531 size_t rank = 0;
2532 if (!find_rank (loc, index, index, true, &rank))
2533 return error_mark_node;
2534 if (rank > 1)
2536 error_at (loc, "rank of the array's index is greater than 1");
2537 return error_mark_node;
2540 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2541 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2542 /* Allow vector[index] but not index[vector]. */
2543 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2545 tree temp;
2546 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2547 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2549 error_at (loc,
2550 "subscripted value is neither array nor pointer nor vector");
2552 return error_mark_node;
2554 temp = array;
2555 array = index;
2556 index = temp;
2557 swapped = true;
2560 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2562 error_at (loc, "array subscript is not an integer");
2563 return error_mark_node;
2566 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2568 error_at (loc, "subscripted value is pointer to function");
2569 return error_mark_node;
2572 /* ??? Existing practice has been to warn only when the char
2573 index is syntactically the index, not for char[array]. */
2574 if (!swapped)
2575 warn_array_subscript_with_type_char (loc, index);
2577 /* Apply default promotions *after* noticing character types. */
2578 index = default_conversion (index);
2579 if (index == error_mark_node)
2580 return error_mark_node;
2582 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2584 bool non_lvalue
2585 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2586 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2587 && !upc_shared_type_p (TREE_TYPE (array)))
2589 tree rval, type;
2591 /* An array that is indexed by a non-constant
2592 cannot be stored in a register; we must be able to do
2593 address arithmetic on its address.
2594 Likewise an array of elements of variable size. */
2595 if (TREE_CODE (index) != INTEGER_CST
2596 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2597 && TREE_CODE (TYPE_SIZE (TREE_TYPE (
2598 TREE_TYPE (array)))) != INTEGER_CST))
2600 if (!c_mark_addressable (array))
2601 return error_mark_node;
2603 /* An array that is indexed by a constant value which is not within
2604 the array bounds cannot be stored in a register either; because we
2605 would get a crash in store_bit_field/extract_bit_field when trying
2606 to access a non-existent part of the register. */
2607 if (TREE_CODE (index) == INTEGER_CST
2608 && TYPE_DOMAIN (TREE_TYPE (array))
2609 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2611 if (!c_mark_addressable (array))
2612 return error_mark_node;
2615 if (pedantic || warn_c90_c99_compat)
2617 tree foo = array;
2618 while (TREE_CODE (foo) == COMPONENT_REF)
2619 foo = TREE_OPERAND (foo, 0);
2620 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2621 pedwarn (loc, OPT_Wpedantic,
2622 "ISO C forbids subscripting %<register%> array");
2623 else if (!lvalue_p (foo))
2624 pedwarn_c90 (loc, OPT_Wpedantic,
2625 "ISO C90 forbids subscripting non-lvalue "
2626 "array");
2629 type = TREE_TYPE (TREE_TYPE (array));
2630 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2631 /* Array ref is const/volatile if the array elements are
2632 or if the array is. */
2633 TREE_READONLY (rval)
2634 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2635 | TREE_READONLY (array));
2636 TREE_SIDE_EFFECTS (rval)
2637 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2638 | TREE_SIDE_EFFECTS (array));
2639 TREE_THIS_VOLATILE (rval)
2640 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2641 /* This was added by rms on 16 Nov 91.
2642 It fixes vol struct foo *a; a->elts[1]
2643 in an inline function.
2644 Hope it doesn't break something else. */
2645 | TREE_THIS_VOLATILE (array));
2646 ret = require_complete_type (rval);
2647 protected_set_expr_location (ret, loc);
2648 if (non_lvalue)
2649 ret = non_lvalue_loc (loc, ret);
2650 return ret;
2652 else
2654 tree ar = default_conversion (array);
2656 if (ar == error_mark_node)
2657 return ar;
2659 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2660 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2662 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2663 index, 0),
2664 RO_ARRAY_INDEXING);
2665 if (non_lvalue)
2666 ret = non_lvalue_loc (loc, ret);
2667 return ret;
2671 /* Build an external reference to identifier ID. FUN indicates
2672 whether this will be used for a function call. LOC is the source
2673 location of the identifier. This sets *TYPE to the type of the
2674 identifier, which is not the same as the type of the returned value
2675 for CONST_DECLs defined as enum constants. If the type of the
2676 identifier is not available, *TYPE is set to NULL. */
2677 tree
2678 build_external_ref (location_t loc, tree id, int fun, tree *type)
2680 tree ref;
2681 tree decl = lookup_name (id);
2683 /* In Objective-C, an instance variable (ivar) may be preferred to
2684 whatever lookup_name() found. */
2685 decl = objc_lookup_ivar (decl, id);
2687 *type = NULL;
2688 if (decl && decl != error_mark_node)
2690 ref = decl;
2691 *type = TREE_TYPE (ref);
2693 else if (fun)
2694 /* Implicit function declaration. */
2695 ref = implicitly_declare (loc, id);
2696 else if (decl == error_mark_node)
2697 /* Don't complain about something that's already been
2698 complained about. */
2699 return error_mark_node;
2700 else
2702 undeclared_variable (loc, id);
2703 return error_mark_node;
2706 if (TREE_TYPE (ref) == error_mark_node)
2707 return error_mark_node;
2709 if (TREE_DEPRECATED (ref))
2710 warn_deprecated_use (ref, NULL_TREE);
2712 /* Recursive call does not count as usage. */
2713 if (ref != current_function_decl)
2715 TREE_USED (ref) = 1;
2718 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2720 if (!in_sizeof && !in_typeof)
2721 C_DECL_USED (ref) = 1;
2722 else if (DECL_INITIAL (ref) == 0
2723 && DECL_EXTERNAL (ref)
2724 && !TREE_PUBLIC (ref))
2725 record_maybe_used_decl (ref);
2728 if (TREE_CODE (ref) == CONST_DECL)
2730 used_types_insert (TREE_TYPE (ref));
2732 if (warn_cxx_compat
2733 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2734 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2736 warning_at (loc, OPT_Wc___compat,
2737 ("enum constant defined in struct or union "
2738 "is not visible in C++"));
2739 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2742 ref = DECL_INITIAL (ref);
2743 TREE_CONSTANT (ref) = 1;
2745 else if (current_function_decl != 0
2746 && !DECL_FILE_SCOPE_P (current_function_decl)
2747 && (TREE_CODE (ref) == VAR_DECL
2748 || TREE_CODE (ref) == PARM_DECL
2749 || TREE_CODE (ref) == FUNCTION_DECL))
2751 tree context = decl_function_context (ref);
2753 if (context != 0 && context != current_function_decl)
2754 DECL_NONLOCAL (ref) = 1;
2756 /* C99 6.7.4p3: An inline definition of a function with external
2757 linkage ... shall not contain a reference to an identifier with
2758 internal linkage. */
2759 else if (current_function_decl != 0
2760 && DECL_DECLARED_INLINE_P (current_function_decl)
2761 && DECL_EXTERNAL (current_function_decl)
2762 && VAR_OR_FUNCTION_DECL_P (ref)
2763 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2764 && ! TREE_PUBLIC (ref)
2765 && DECL_CONTEXT (ref) != current_function_decl)
2766 record_inline_static (loc, current_function_decl, ref,
2767 csi_internal);
2769 return ref;
2772 /* Record details of decls possibly used inside sizeof or typeof. */
2773 struct maybe_used_decl
2775 /* The decl. */
2776 tree decl;
2777 /* The level seen at (in_sizeof + in_typeof). */
2778 int level;
2779 /* The next one at this level or above, or NULL. */
2780 struct maybe_used_decl *next;
2783 static struct maybe_used_decl *maybe_used_decls;
2785 /* Record that DECL, an undefined static function reference seen
2786 inside sizeof or typeof, might be used if the operand of sizeof is
2787 a VLA type or the operand of typeof is a variably modified
2788 type. */
2790 static void
2791 record_maybe_used_decl (tree decl)
2793 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2794 t->decl = decl;
2795 t->level = in_sizeof + in_typeof;
2796 t->next = maybe_used_decls;
2797 maybe_used_decls = t;
2800 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2801 USED is false, just discard them. If it is true, mark them used
2802 (if no longer inside sizeof or typeof) or move them to the next
2803 level up (if still inside sizeof or typeof). */
2805 void
2806 pop_maybe_used (bool used)
2808 struct maybe_used_decl *p = maybe_used_decls;
2809 int cur_level = in_sizeof + in_typeof;
2810 while (p && p->level > cur_level)
2812 if (used)
2814 if (cur_level == 0)
2815 C_DECL_USED (p->decl) = 1;
2816 else
2817 p->level = cur_level;
2819 p = p->next;
2821 if (!used || cur_level == 0)
2822 maybe_used_decls = p;
2825 /* Return the result of sizeof applied to EXPR. */
2827 struct c_expr
2828 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2830 struct c_expr ret;
2831 if (expr.value == error_mark_node)
2833 ret.value = error_mark_node;
2834 ret.original_code = ERROR_MARK;
2835 ret.original_type = NULL;
2836 pop_maybe_used (false);
2838 else
2840 bool expr_const_operands = true;
2842 if (TREE_CODE (expr.value) == PARM_DECL
2843 && C_ARRAY_PARAMETER (expr.value))
2845 if (warning_at (loc, OPT_Wsizeof_array_argument,
2846 "%<sizeof%> on array function parameter %qE will "
2847 "return size of %qT", expr.value,
2848 expr.original_type))
2849 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2851 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2852 &expr_const_operands);
2853 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2854 c_last_sizeof_arg = expr.value;
2855 ret.original_code = SIZEOF_EXPR;
2856 ret.original_type = NULL;
2857 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2859 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2860 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2861 folded_expr, ret.value);
2862 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2863 SET_EXPR_LOCATION (ret.value, loc);
2865 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2867 return ret;
2870 /* Return the result of sizeof applied to T, a structure for the type
2871 name passed to sizeof (rather than the type itself). LOC is the
2872 location of the original expression. */
2874 struct c_expr
2875 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2877 tree type;
2878 struct c_expr ret;
2879 tree type_expr = NULL_TREE;
2880 bool type_expr_const = true;
2881 type = groktypename (t, &type_expr, &type_expr_const);
2882 ret.value = c_sizeof (loc, type);
2883 c_last_sizeof_arg = type;
2884 ret.original_code = SIZEOF_EXPR;
2885 ret.original_type = NULL;
2886 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2887 && c_vla_type_p (type))
2889 /* If the type is a [*] array, it is a VLA but is represented as
2890 having a size of zero. In such a case we must ensure that
2891 the result of sizeof does not get folded to a constant by
2892 c_fully_fold, because if the size is evaluated the result is
2893 not constant and so constraints on zero or negative size
2894 arrays must not be applied when this sizeof call is inside
2895 another array declarator. */
2896 if (!type_expr)
2897 type_expr = integer_zero_node;
2898 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2899 type_expr, ret.value);
2900 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2902 pop_maybe_used (type != error_mark_node
2903 ? C_TYPE_VARIABLE_SIZE (type) : false);
2904 return ret;
2907 /* Build a function call to function FUNCTION with parameters PARAMS.
2908 The function call is at LOC.
2909 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2910 TREE_VALUE of each node is a parameter-expression.
2911 FUNCTION's data type may be a function type or a pointer-to-function. */
2913 tree
2914 build_function_call (location_t loc, tree function, tree params)
2916 vec<tree, va_gc> *v;
2917 tree ret;
2919 vec_alloc (v, list_length (params));
2920 for (; params; params = TREE_CHAIN (params))
2921 v->quick_push (TREE_VALUE (params));
2922 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2923 vec_free (v);
2924 return ret;
2927 /* Give a note about the location of the declaration of DECL. */
2929 static void inform_declaration (tree decl)
2931 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2932 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2935 /* Build a function call to function FUNCTION with parameters PARAMS.
2936 ORIGTYPES, if not NULL, is a vector of types; each element is
2937 either NULL or the original type of the corresponding element in
2938 PARAMS. The original type may differ from TREE_TYPE of the
2939 parameter for enums. FUNCTION's data type may be a function type
2940 or pointer-to-function. This function changes the elements of
2941 PARAMS. */
2943 tree
2944 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2945 tree function, vec<tree, va_gc> *params,
2946 vec<tree, va_gc> *origtypes)
2948 tree fntype, fundecl = 0;
2949 tree name = NULL_TREE, result;
2950 tree tem;
2951 int nargs;
2952 tree *argarray;
2955 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2956 STRIP_TYPE_NOPS (function);
2958 /* Convert anything with function type to a pointer-to-function. */
2959 if (TREE_CODE (function) == FUNCTION_DECL)
2961 name = DECL_NAME (function);
2963 if (flag_tm)
2964 tm_malloc_replacement (function);
2965 fundecl = function;
2966 /* Atomic functions have type checking/casting already done. They are
2967 often rewritten and don't match the original parameter list. */
2968 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2969 origtypes = NULL;
2971 if (flag_cilkplus
2972 && is_cilkplus_reduce_builtin (function))
2973 origtypes = NULL;
2975 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2976 function = function_to_pointer_conversion (loc, function);
2978 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2979 expressions, like those used for ObjC messenger dispatches. */
2980 if (params && !params->is_empty ())
2981 function = objc_rewrite_function_call (function, (*params)[0]);
2983 function = c_fully_fold (function, false, NULL);
2985 fntype = TREE_TYPE (function);
2987 if (TREE_CODE (fntype) == ERROR_MARK)
2988 return error_mark_node;
2990 if (!(TREE_CODE (fntype) == POINTER_TYPE
2991 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2993 if (!flag_diagnostics_show_caret)
2994 error_at (loc,
2995 "called object %qE is not a function or function pointer",
2996 function);
2997 else if (DECL_P (function))
2999 error_at (loc,
3000 "called object %qD is not a function or function pointer",
3001 function);
3002 inform_declaration (function);
3004 else
3005 error_at (loc,
3006 "called object is not a function or function pointer");
3007 return error_mark_node;
3010 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3011 current_function_returns_abnormally = 1;
3013 /* fntype now gets the type of function pointed to. */
3014 fntype = TREE_TYPE (fntype);
3016 /* Convert the parameters to the types declared in the
3017 function prototype, or apply default promotions. */
3019 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3020 origtypes, function, fundecl);
3021 if (nargs < 0)
3022 return error_mark_node;
3024 /* Check that the function is called through a compatible prototype.
3025 If it is not, warn. */
3026 if (CONVERT_EXPR_P (function)
3027 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3028 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3029 && !comptypes (fntype, TREE_TYPE (tem)))
3031 tree return_type = TREE_TYPE (fntype);
3033 /* This situation leads to run-time undefined behavior. We can't,
3034 therefore, simply error unless we can prove that all possible
3035 executions of the program must execute the code. */
3036 warning_at (loc, 0, "function called through a non-compatible type");
3038 if (VOID_TYPE_P (return_type)
3039 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3040 pedwarn (loc, 0,
3041 "function with qualified void return type called");
3044 argarray = vec_safe_address (params);
3046 /* Check that arguments to builtin functions match the expectations. */
3047 if (fundecl
3048 && DECL_BUILT_IN (fundecl)
3049 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3050 && !check_builtin_function_arguments (fundecl, nargs, argarray))
3051 return error_mark_node;
3053 /* Check that the arguments to the function are valid. */
3054 check_function_arguments (fntype, nargs, argarray);
3056 if (name != NULL_TREE
3057 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3059 if (require_constant_value)
3060 result =
3061 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3062 function, nargs, argarray);
3063 else
3064 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3065 function, nargs, argarray);
3066 if (TREE_CODE (result) == NOP_EXPR
3067 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3068 STRIP_TYPE_NOPS (result);
3070 else
3071 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3072 function, nargs, argarray);
3074 if (VOID_TYPE_P (TREE_TYPE (result)))
3076 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3077 pedwarn (loc, 0,
3078 "function with qualified void return type called");
3079 return result;
3081 return require_complete_type (result);
3084 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3086 tree
3087 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3088 tree function, vec<tree, va_gc> *params,
3089 vec<tree, va_gc> *origtypes)
3091 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3092 STRIP_TYPE_NOPS (function);
3094 /* Convert anything with function type to a pointer-to-function. */
3095 if (TREE_CODE (function) == FUNCTION_DECL)
3097 /* Implement type-directed function overloading for builtins.
3098 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3099 handle all the type checking. The result is a complete expression
3100 that implements this function call. */
3101 tree tem = resolve_overloaded_builtin (loc, function, params);
3102 if (tem)
3103 return tem;
3105 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3108 /* Convert the argument expressions in the vector VALUES
3109 to the types in the list TYPELIST.
3111 If TYPELIST is exhausted, or when an element has NULL as its type,
3112 perform the default conversions.
3114 ORIGTYPES is the original types of the expressions in VALUES. This
3115 holds the type of enum values which have been converted to integral
3116 types. It may be NULL.
3118 FUNCTION is a tree for the called function. It is used only for
3119 error messages, where it is formatted with %qE.
3121 This is also where warnings about wrong number of args are generated.
3123 ARG_LOC are locations of function arguments (if any).
3125 Returns the actual number of arguments processed (which may be less
3126 than the length of VALUES in some error situations), or -1 on
3127 failure. */
3129 static int
3130 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3131 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3132 tree function, tree fundecl)
3134 tree typetail, val;
3135 unsigned int parmnum;
3136 bool error_args = false;
3137 const bool type_generic = fundecl
3138 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3139 bool type_generic_remove_excess_precision = false;
3140 tree selector;
3142 /* Change pointer to function to the function itself for
3143 diagnostics. */
3144 if (TREE_CODE (function) == ADDR_EXPR
3145 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3146 function = TREE_OPERAND (function, 0);
3148 /* Handle an ObjC selector specially for diagnostics. */
3149 selector = objc_message_selector ();
3151 /* For type-generic built-in functions, determine whether excess
3152 precision should be removed (classification) or not
3153 (comparison). */
3154 if (type_generic
3155 && DECL_BUILT_IN (fundecl)
3156 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3158 switch (DECL_FUNCTION_CODE (fundecl))
3160 case BUILT_IN_ISFINITE:
3161 case BUILT_IN_ISINF:
3162 case BUILT_IN_ISINF_SIGN:
3163 case BUILT_IN_ISNAN:
3164 case BUILT_IN_ISNORMAL:
3165 case BUILT_IN_FPCLASSIFY:
3166 type_generic_remove_excess_precision = true;
3167 break;
3169 default:
3170 type_generic_remove_excess_precision = false;
3171 break;
3174 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3175 return vec_safe_length (values);
3177 /* Scan the given expressions and types, producing individual
3178 converted arguments. */
3180 for (typetail = typelist, parmnum = 0;
3181 values && values->iterate (parmnum, &val);
3182 ++parmnum)
3184 tree type = typetail ? TREE_VALUE (typetail) : 0;
3185 tree valtype = TREE_TYPE (val);
3186 tree rname = function;
3187 int argnum = parmnum + 1;
3188 const char *invalid_func_diag;
3189 bool excess_precision = false;
3190 bool npc;
3191 tree parmval;
3192 /* Some __atomic_* builtins have additional hidden argument at
3193 position 0. */
3194 location_t ploc
3195 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3196 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3197 : input_location;
3199 if (type == void_type_node)
3201 if (selector)
3202 error_at (loc, "too many arguments to method %qE", selector);
3203 else
3204 error_at (loc, "too many arguments to function %qE", function);
3205 inform_declaration (fundecl);
3206 return error_args ? -1 : (int) parmnum;
3209 if (selector && argnum > 2)
3211 rname = selector;
3212 argnum -= 2;
3215 npc = null_pointer_constant_p (val);
3217 /* If there is excess precision and a prototype, convert once to
3218 the required type rather than converting via the semantic
3219 type. Likewise without a prototype a float value represented
3220 as long double should be converted once to double. But for
3221 type-generic classification functions excess precision must
3222 be removed here. */
3223 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3224 && (type || !type_generic || !type_generic_remove_excess_precision))
3226 val = TREE_OPERAND (val, 0);
3227 excess_precision = true;
3229 val = c_fully_fold (val, false, NULL);
3230 STRIP_TYPE_NOPS (val);
3232 val = require_complete_type (val);
3234 if (type != 0)
3236 /* Formal parm type is specified by a function prototype. */
3238 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3240 error_at (ploc, "type of formal parameter %d is incomplete",
3241 parmnum + 1);
3242 parmval = val;
3244 else
3246 tree origtype;
3248 /* Optionally warn about conversions that
3249 differ from the default conversions. */
3250 if (warn_traditional_conversion || warn_traditional)
3252 unsigned int formal_prec = TYPE_PRECISION (type);
3254 if (INTEGRAL_TYPE_P (type)
3255 && TREE_CODE (valtype) == REAL_TYPE)
3256 warning_at (ploc, OPT_Wtraditional_conversion,
3257 "passing argument %d of %qE as integer rather "
3258 "than floating due to prototype",
3259 argnum, rname);
3260 if (INTEGRAL_TYPE_P (type)
3261 && TREE_CODE (valtype) == COMPLEX_TYPE)
3262 warning_at (ploc, OPT_Wtraditional_conversion,
3263 "passing argument %d of %qE as integer rather "
3264 "than complex due to prototype",
3265 argnum, rname);
3266 else if (TREE_CODE (type) == COMPLEX_TYPE
3267 && TREE_CODE (valtype) == REAL_TYPE)
3268 warning_at (ploc, OPT_Wtraditional_conversion,
3269 "passing argument %d of %qE as complex rather "
3270 "than floating due to prototype",
3271 argnum, rname);
3272 else if (TREE_CODE (type) == REAL_TYPE
3273 && INTEGRAL_TYPE_P (valtype))
3274 warning_at (ploc, OPT_Wtraditional_conversion,
3275 "passing argument %d of %qE as floating rather "
3276 "than integer due to prototype",
3277 argnum, rname);
3278 else if (TREE_CODE (type) == COMPLEX_TYPE
3279 && INTEGRAL_TYPE_P (valtype))
3280 warning_at (ploc, OPT_Wtraditional_conversion,
3281 "passing argument %d of %qE as complex rather "
3282 "than integer due to prototype",
3283 argnum, rname);
3284 else if (TREE_CODE (type) == REAL_TYPE
3285 && TREE_CODE (valtype) == COMPLEX_TYPE)
3286 warning_at (ploc, OPT_Wtraditional_conversion,
3287 "passing argument %d of %qE as floating rather "
3288 "than complex due to prototype",
3289 argnum, rname);
3290 /* ??? At some point, messages should be written about
3291 conversions between complex types, but that's too messy
3292 to do now. */
3293 else if (TREE_CODE (type) == REAL_TYPE
3294 && TREE_CODE (valtype) == REAL_TYPE)
3296 /* Warn if any argument is passed as `float',
3297 since without a prototype it would be `double'. */
3298 if (formal_prec == TYPE_PRECISION (float_type_node)
3299 && type != dfloat32_type_node)
3300 warning_at (ploc, 0,
3301 "passing argument %d of %qE as %<float%> "
3302 "rather than %<double%> due to prototype",
3303 argnum, rname);
3305 /* Warn if mismatch between argument and prototype
3306 for decimal float types. Warn of conversions with
3307 binary float types and of precision narrowing due to
3308 prototype. */
3309 else if (type != valtype
3310 && (type == dfloat32_type_node
3311 || type == dfloat64_type_node
3312 || type == dfloat128_type_node
3313 || valtype == dfloat32_type_node
3314 || valtype == dfloat64_type_node
3315 || valtype == dfloat128_type_node)
3316 && (formal_prec
3317 <= TYPE_PRECISION (valtype)
3318 || (type == dfloat128_type_node
3319 && (valtype
3320 != dfloat64_type_node
3321 && (valtype
3322 != dfloat32_type_node)))
3323 || (type == dfloat64_type_node
3324 && (valtype
3325 != dfloat32_type_node))))
3326 warning_at (ploc, 0,
3327 "passing argument %d of %qE as %qT "
3328 "rather than %qT due to prototype",
3329 argnum, rname, type, valtype);
3332 /* Detect integer changing in width or signedness.
3333 These warnings are only activated with
3334 -Wtraditional-conversion, not with -Wtraditional. */
3335 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3336 && INTEGRAL_TYPE_P (valtype))
3338 tree would_have_been = default_conversion (val);
3339 tree type1 = TREE_TYPE (would_have_been);
3341 if (TREE_CODE (type) == ENUMERAL_TYPE
3342 && (TYPE_MAIN_VARIANT (type)
3343 == TYPE_MAIN_VARIANT (valtype)))
3344 /* No warning if function asks for enum
3345 and the actual arg is that enum type. */
3347 else if (formal_prec != TYPE_PRECISION (type1))
3348 warning_at (ploc, OPT_Wtraditional_conversion,
3349 "passing argument %d of %qE "
3350 "with different width due to prototype",
3351 argnum, rname);
3352 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3354 /* Don't complain if the formal parameter type
3355 is an enum, because we can't tell now whether
3356 the value was an enum--even the same enum. */
3357 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3359 else if (TREE_CODE (val) == INTEGER_CST
3360 && int_fits_type_p (val, type))
3361 /* Change in signedness doesn't matter
3362 if a constant value is unaffected. */
3364 /* If the value is extended from a narrower
3365 unsigned type, it doesn't matter whether we
3366 pass it as signed or unsigned; the value
3367 certainly is the same either way. */
3368 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3369 && TYPE_UNSIGNED (valtype))
3371 else if (TYPE_UNSIGNED (type))
3372 warning_at (ploc, OPT_Wtraditional_conversion,
3373 "passing argument %d of %qE "
3374 "as unsigned due to prototype",
3375 argnum, rname);
3376 else
3377 warning_at (ploc, OPT_Wtraditional_conversion,
3378 "passing argument %d of %qE "
3379 "as signed due to prototype",
3380 argnum, rname);
3384 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3385 sake of better warnings from convert_and_check. */
3386 if (excess_precision)
3387 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3388 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3389 parmval = convert_for_assignment (loc, ploc, type,
3390 val, origtype, ic_argpass,
3391 npc, fundecl, function,
3392 parmnum + 1);
3394 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3395 && INTEGRAL_TYPE_P (type)
3396 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3397 parmval = default_conversion (parmval);
3400 else if (TREE_CODE (valtype) == REAL_TYPE
3401 && (TYPE_PRECISION (valtype)
3402 <= TYPE_PRECISION (double_type_node))
3403 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3404 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3405 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3407 if (type_generic)
3408 parmval = val;
3409 else
3411 /* Convert `float' to `double'. */
3412 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3413 warning_at (ploc, OPT_Wdouble_promotion,
3414 "implicit conversion from %qT to %qT when passing "
3415 "argument to function",
3416 valtype, double_type_node);
3417 parmval = convert (double_type_node, val);
3420 else if (excess_precision && !type_generic)
3421 /* A "double" argument with excess precision being passed
3422 without a prototype or in variable arguments. */
3423 parmval = convert (valtype, val);
3424 else if ((invalid_func_diag =
3425 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3427 error (invalid_func_diag);
3428 return -1;
3430 else
3431 /* Convert `short' and `char' to full-size `int'. */
3432 parmval = default_conversion (val);
3434 (*values)[parmnum] = parmval;
3435 if (parmval == error_mark_node)
3436 error_args = true;
3438 if (typetail)
3439 typetail = TREE_CHAIN (typetail);
3442 gcc_assert (parmnum == vec_safe_length (values));
3444 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3446 error_at (loc, "too few arguments to function %qE", function);
3447 inform_declaration (fundecl);
3448 return -1;
3451 return error_args ? -1 : (int) parmnum;
3454 /* This is the entry point used by the parser to build unary operators
3455 in the input. CODE, a tree_code, specifies the unary operator, and
3456 ARG is the operand. For unary plus, the C parser currently uses
3457 CONVERT_EXPR for code.
3459 LOC is the location to use for the tree generated.
3462 struct c_expr
3463 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3465 struct c_expr result;
3467 result.value = build_unary_op (loc, code, arg.value, 0);
3468 result.original_code = code;
3469 result.original_type = NULL;
3471 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3472 overflow_warning (loc, result.value);
3474 return result;
3477 /* This is the entry point used by the parser to build binary operators
3478 in the input. CODE, a tree_code, specifies the binary operator, and
3479 ARG1 and ARG2 are the operands. In addition to constructing the
3480 expression, we check for operands that were written with other binary
3481 operators in a way that is likely to confuse the user.
3483 LOCATION is the location of the binary operator. */
3485 struct c_expr
3486 parser_build_binary_op (location_t location, enum tree_code code,
3487 struct c_expr arg1, struct c_expr arg2)
3489 struct c_expr result;
3491 enum tree_code code1 = arg1.original_code;
3492 enum tree_code code2 = arg2.original_code;
3493 tree type1 = (arg1.original_type
3494 ? arg1.original_type
3495 : TREE_TYPE (arg1.value));
3496 tree type2 = (arg2.original_type
3497 ? arg2.original_type
3498 : TREE_TYPE (arg2.value));
3500 result.value = build_binary_op (location, code,
3501 arg1.value, arg2.value, 1);
3502 result.original_code = code;
3503 result.original_type = NULL;
3505 if (TREE_CODE (result.value) == ERROR_MARK)
3506 return result;
3508 if (location != UNKNOWN_LOCATION)
3509 protected_set_expr_location (result.value, location);
3511 /* Check for cases such as x+y<<z which users are likely
3512 to misinterpret. */
3513 if (warn_parentheses)
3514 warn_about_parentheses (location, code, code1, arg1.value, code2,
3515 arg2.value);
3517 if (warn_logical_op)
3518 warn_logical_operator (location, code, TREE_TYPE (result.value),
3519 code1, arg1.value, code2, arg2.value);
3521 if (warn_logical_not_paren
3522 && TREE_CODE_CLASS (code) == tcc_comparison
3523 && code1 == TRUTH_NOT_EXPR
3524 && code2 != TRUTH_NOT_EXPR
3525 /* Avoid warning for !!x == y. */
3526 && (TREE_CODE (arg1.value) != NE_EXPR
3527 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3529 /* Avoid warning for !b == y where b has _Bool type. */
3530 tree t = integer_zero_node;
3531 if (TREE_CODE (arg1.value) == EQ_EXPR
3532 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3533 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3535 t = TREE_OPERAND (arg1.value, 0);
3538 if (TREE_TYPE (t) != integer_type_node)
3539 break;
3540 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3541 t = C_MAYBE_CONST_EXPR_EXPR (t);
3542 else if (CONVERT_EXPR_P (t))
3543 t = TREE_OPERAND (t, 0);
3544 else
3545 break;
3547 while (1);
3549 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3550 warn_logical_not_parentheses (location, code, arg2.value);
3553 /* Warn about comparisons against string literals, with the exception
3554 of testing for equality or inequality of a string literal with NULL. */
3555 if (code == EQ_EXPR || code == NE_EXPR)
3557 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3558 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3559 warning_at (location, OPT_Waddress,
3560 "comparison with string literal results in unspecified behavior");
3562 else if (TREE_CODE_CLASS (code) == tcc_comparison
3563 && (code1 == STRING_CST || code2 == STRING_CST))
3564 warning_at (location, OPT_Waddress,
3565 "comparison with string literal results in unspecified behavior");
3567 if (TREE_OVERFLOW_P (result.value)
3568 && !TREE_OVERFLOW_P (arg1.value)
3569 && !TREE_OVERFLOW_P (arg2.value))
3570 overflow_warning (location, result.value);
3572 /* Warn about comparisons of different enum types. */
3573 if (warn_enum_compare
3574 && TREE_CODE_CLASS (code) == tcc_comparison
3575 && TREE_CODE (type1) == ENUMERAL_TYPE
3576 && TREE_CODE (type2) == ENUMERAL_TYPE
3577 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3578 warning_at (location, OPT_Wenum_compare,
3579 "comparison between %qT and %qT",
3580 type1, type2);
3582 return result;
3585 /* Return a tree for the sum or difference (RESULTCODE says which)
3586 of pointer PTROP and integer INTOP. */
3588 static
3589 tree
3590 c_pointer_int_sum (location_t location, enum tree_code resultcode,
3591 tree ptrop, tree intop)
3593 /* The result is a pointer of the same type that is being added. */
3594 tree result_type = TREE_TYPE (ptrop);
3596 if (upc_shared_type_p (TREE_TYPE (result_type)))
3597 return upc_pts_int_sum (location, resultcode, ptrop, intop);
3599 return pointer_int_sum (location, resultcode, ptrop, intop);
3602 /* Return a tree for the difference of pointers OP0 and OP1.
3603 The resulting tree has type int. */
3605 static tree
3606 pointer_diff (location_t loc, tree op0, tree op1)
3608 tree restype = ptrdiff_type_node;
3609 tree result, inttype;
3611 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3612 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3613 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3614 tree subtrahend_type = TREE_TYPE (TREE_TYPE (op1));
3615 tree orig_op1 = op1;
3617 /* If the operands point into different address spaces, we need to
3618 explicitly convert them to pointers into the common address space
3619 before we can subtract the numerical address values. */
3620 if (as0 != as1)
3622 addr_space_t as_common;
3623 tree common_type;
3625 /* Determine the common superset address space. This is guaranteed
3626 to exist because the caller verified that comp_target_types
3627 returned non-zero. */
3628 if (!addr_space_superset (as0, as1, &as_common))
3629 gcc_unreachable ();
3631 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3632 op0 = convert (common_type, op0);
3633 op1 = convert (common_type, op1);
3636 /* Determine integer type to perform computations in. This will usually
3637 be the same as the result type (ptrdiff_t), but may need to be a wider
3638 type if pointers for the address space are wider than ptrdiff_t. */
3639 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3640 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3641 else
3642 inttype = restype;
3644 if (TREE_CODE (target_type) == VOID_TYPE)
3645 pedwarn (loc, OPT_Wpointer_arith,
3646 "pointer of type %<void *%> used in subtraction");
3647 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3648 pedwarn (loc, OPT_Wpointer_arith,
3649 "pointer to a function used in subtraction");
3651 if (upc_shared_type_p (target_type) || upc_shared_type_p (subtrahend_type))
3652 return upc_pts_diff (op0, op1);
3654 /* First do the subtraction as integers;
3655 then drop through to build the divide operator.
3656 Do not do default conversions on the minus operator
3657 in case restype is a short type. */
3659 op0 = build_binary_op (loc,
3660 MINUS_EXPR, convert (inttype, op0),
3661 convert (inttype, op1), 0);
3662 /* This generates an error if op1 is pointer to incomplete type. */
3663 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3664 error_at (loc, "arithmetic on pointer to an incomplete type");
3666 op1 = c_size_in_bytes (target_type);
3668 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3669 error_at (loc, "arithmetic on pointer to an empty aggregate");
3671 /* Divide by the size, in easiest possible way. */
3672 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3673 op0, convert (inttype, op1));
3675 /* Convert to final result type if necessary. */
3676 return convert (restype, result);
3679 /* Expand atomic compound assignments into an approriate sequence as
3680 specified by the C11 standard section 6.5.16.2.
3681 given
3682 _Atomic T1 E1
3683 T2 E2
3684 E1 op= E2
3686 This sequence is used for all types for which these operations are
3687 supported.
3689 In addition, built-in versions of the 'fe' prefixed routines may
3690 need to be invoked for floating point (real, complex or vector) when
3691 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3693 T1 newval;
3694 T1 old;
3695 T1 *addr
3696 T2 val
3697 fenv_t fenv
3699 addr = &E1;
3700 val = (E2);
3701 __atomic_load (addr, &old, SEQ_CST);
3702 feholdexcept (&fenv);
3703 loop:
3704 newval = old op val;
3705 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3706 SEQ_CST))
3707 goto done;
3708 feclearexcept (FE_ALL_EXCEPT);
3709 goto loop:
3710 done:
3711 feupdateenv (&fenv);
3713 Also note that the compiler is simply issuing the generic form of
3714 the atomic operations. This requires temp(s) and has their address
3715 taken. The atomic processing is smart enough to figure out when the
3716 size of an object can utilize a lock-free version, and convert the
3717 built-in call to the appropriate lock-free routine. The optimizers
3718 will then dispose of any temps that are no longer required, and
3719 lock-free implementations are utilized as long as there is target
3720 support for the required size.
3722 If the operator is NOP_EXPR, then this is a simple assignment, and
3723 an __atomic_store is issued to perform the assignment rather than
3724 the above loop.
3728 /* Build an atomic assignment at LOC, expanding into the proper
3729 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3730 the result of the operation, unless RETURN_OLD_P in which case
3731 return the old value of LHS (this is only for postincrement and
3732 postdecrement). */
3733 static tree
3734 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3735 tree rhs, bool return_old_p)
3737 tree fndecl, func_call;
3738 vec<tree, va_gc> *params;
3739 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3740 tree old, old_addr;
3741 tree compound_stmt;
3742 tree stmt, goto_stmt;
3743 tree loop_label, loop_decl, done_label, done_decl;
3745 tree lhs_type = TREE_TYPE (lhs);
3746 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3747 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3748 tree rhs_type = TREE_TYPE (rhs);
3750 gcc_assert (TYPE_ATOMIC (lhs_type));
3752 if (return_old_p)
3753 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3755 /* Allocate enough vector items for a compare_exchange. */
3756 vec_alloc (params, 6);
3758 /* Create a compound statement to hold the sequence of statements
3759 with a loop. */
3760 compound_stmt = c_begin_compound_stmt (false);
3762 /* Fold the RHS if it hasn't already been folded. */
3763 if (modifycode != NOP_EXPR)
3764 rhs = c_fully_fold (rhs, false, NULL);
3766 /* Remove the qualifiers for the rest of the expressions and create
3767 the VAL temp variable to hold the RHS. */
3768 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3769 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3770 val = create_tmp_var (nonatomic_rhs_type);
3771 TREE_ADDRESSABLE (val) = 1;
3772 TREE_NO_WARNING (val) = 1;
3773 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3774 SET_EXPR_LOCATION (rhs, loc);
3775 add_stmt (rhs);
3777 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3778 an atomic_store. */
3779 if (modifycode == NOP_EXPR)
3781 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3782 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3783 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3784 params->quick_push (lhs_addr);
3785 params->quick_push (rhs);
3786 params->quick_push (seq_cst);
3787 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3788 add_stmt (func_call);
3790 /* Finish the compound statement. */
3791 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3793 /* VAL is the value which was stored, return a COMPOUND_STMT of
3794 the statement and that value. */
3795 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3798 /* Create the variables and labels required for the op= form. */
3799 old = create_tmp_var (nonatomic_lhs_type);
3800 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3801 TREE_ADDRESSABLE (old) = 1;
3802 TREE_NO_WARNING (old) = 1;
3804 newval = create_tmp_var (nonatomic_lhs_type);
3805 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3806 TREE_ADDRESSABLE (newval) = 1;
3808 loop_decl = create_artificial_label (loc);
3809 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3811 done_decl = create_artificial_label (loc);
3812 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3814 /* __atomic_load (addr, &old, SEQ_CST). */
3815 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3816 params->quick_push (lhs_addr);
3817 params->quick_push (old_addr);
3818 params->quick_push (seq_cst);
3819 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3820 add_stmt (func_call);
3821 params->truncate (0);
3823 /* Create the expressions for floating-point environment
3824 manipulation, if required. */
3825 bool need_fenv = (flag_trapping_math
3826 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3827 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3828 if (need_fenv)
3829 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3831 if (hold_call)
3832 add_stmt (hold_call);
3834 /* loop: */
3835 add_stmt (loop_label);
3837 /* newval = old + val; */
3838 rhs = build_binary_op (loc, modifycode, old, val, 1);
3839 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3840 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3841 NULL_TREE, 0);
3842 if (rhs != error_mark_node)
3844 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3845 SET_EXPR_LOCATION (rhs, loc);
3846 add_stmt (rhs);
3849 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3850 goto done; */
3851 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3852 params->quick_push (lhs_addr);
3853 params->quick_push (old_addr);
3854 params->quick_push (newval_addr);
3855 params->quick_push (integer_zero_node);
3856 params->quick_push (seq_cst);
3857 params->quick_push (seq_cst);
3858 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3860 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3861 SET_EXPR_LOCATION (goto_stmt, loc);
3863 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3864 SET_EXPR_LOCATION (stmt, loc);
3865 add_stmt (stmt);
3867 if (clear_call)
3868 add_stmt (clear_call);
3870 /* goto loop; */
3871 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3872 SET_EXPR_LOCATION (goto_stmt, loc);
3873 add_stmt (goto_stmt);
3875 /* done: */
3876 add_stmt (done_label);
3878 if (update_call)
3879 add_stmt (update_call);
3881 /* Finish the compound statement. */
3882 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3884 /* NEWVAL is the value that was successfully stored, return a
3885 COMPOUND_EXPR of the statement and the appropriate value. */
3886 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3887 return_old_p ? old : newval);
3890 /* Construct and perhaps optimize a tree representation
3891 for a unary operation. CODE, a tree_code, specifies the operation
3892 and XARG is the operand.
3893 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3894 the default promotions (such as from short to int).
3895 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3896 allows non-lvalues; this is only used to handle conversion of non-lvalue
3897 arrays to pointers in C99.
3899 LOCATION is the location of the operator. */
3901 tree
3902 build_unary_op (location_t location,
3903 enum tree_code code, tree xarg, int flag)
3905 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3906 tree arg = xarg;
3907 tree argtype = 0;
3908 enum tree_code typecode;
3909 tree val;
3910 tree ret = error_mark_node;
3911 tree eptype = NULL_TREE;
3912 int noconvert = flag;
3913 const char *invalid_op_diag;
3914 bool int_operands;
3916 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3917 if (int_operands)
3918 arg = remove_c_maybe_const_expr (arg);
3920 if (code != ADDR_EXPR)
3921 arg = require_complete_type (arg);
3923 typecode = TREE_CODE (TREE_TYPE (arg));
3924 if (typecode == ERROR_MARK)
3925 return error_mark_node;
3926 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3927 typecode = INTEGER_TYPE;
3929 if ((invalid_op_diag
3930 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3932 error_at (location, invalid_op_diag);
3933 return error_mark_node;
3936 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3938 eptype = TREE_TYPE (arg);
3939 arg = TREE_OPERAND (arg, 0);
3942 switch (code)
3944 case CONVERT_EXPR:
3945 /* This is used for unary plus, because a CONVERT_EXPR
3946 is enough to prevent anybody from looking inside for
3947 associativity, but won't generate any code. */
3948 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3949 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3950 || typecode == VECTOR_TYPE))
3952 error_at (location, "wrong type argument to unary plus");
3953 return error_mark_node;
3955 else if (!noconvert)
3956 arg = default_conversion (arg);
3957 arg = non_lvalue_loc (location, arg);
3958 break;
3960 case NEGATE_EXPR:
3961 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3962 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3963 || typecode == VECTOR_TYPE))
3965 error_at (location, "wrong type argument to unary minus");
3966 return error_mark_node;
3968 else if (!noconvert)
3969 arg = default_conversion (arg);
3970 break;
3972 case BIT_NOT_EXPR:
3973 /* ~ works on integer types and non float vectors. */
3974 if (typecode == INTEGER_TYPE
3975 || (typecode == VECTOR_TYPE
3976 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3978 if (!noconvert)
3979 arg = default_conversion (arg);
3981 else if (typecode == COMPLEX_TYPE)
3983 code = CONJ_EXPR;
3984 pedwarn (location, OPT_Wpedantic,
3985 "ISO C does not support %<~%> for complex conjugation");
3986 if (!noconvert)
3987 arg = default_conversion (arg);
3989 else
3991 error_at (location, "wrong type argument to bit-complement");
3992 return error_mark_node;
3994 break;
3996 case ABS_EXPR:
3997 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3999 error_at (location, "wrong type argument to abs");
4000 return error_mark_node;
4002 else if (!noconvert)
4003 arg = default_conversion (arg);
4004 break;
4006 case CONJ_EXPR:
4007 /* Conjugating a real value is a no-op, but allow it anyway. */
4008 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4009 || typecode == COMPLEX_TYPE))
4011 error_at (location, "wrong type argument to conjugation");
4012 return error_mark_node;
4014 else if (!noconvert)
4015 arg = default_conversion (arg);
4016 break;
4018 case TRUTH_NOT_EXPR:
4019 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4020 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4021 && typecode != COMPLEX_TYPE)
4023 error_at (location,
4024 "wrong type argument to unary exclamation mark");
4025 return error_mark_node;
4027 if (int_operands)
4029 arg = c_objc_common_truthvalue_conversion (location, xarg);
4030 arg = remove_c_maybe_const_expr (arg);
4032 else
4033 arg = c_objc_common_truthvalue_conversion (location, arg);
4034 ret = invert_truthvalue_loc (location, arg);
4035 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4036 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4037 location = EXPR_LOCATION (ret);
4038 goto return_build_unary_op;
4040 case REALPART_EXPR:
4041 case IMAGPART_EXPR:
4042 ret = build_real_imag_expr (location, code, arg);
4043 if (ret == error_mark_node)
4044 return error_mark_node;
4045 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4046 eptype = TREE_TYPE (eptype);
4047 goto return_build_unary_op;
4049 case PREINCREMENT_EXPR:
4050 case POSTINCREMENT_EXPR:
4051 case PREDECREMENT_EXPR:
4052 case POSTDECREMENT_EXPR:
4054 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4056 tree inner = build_unary_op (location, code,
4057 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4058 if (inner == error_mark_node)
4059 return error_mark_node;
4060 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4061 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4062 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4063 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4064 goto return_build_unary_op;
4067 /* Complain about anything that is not a true lvalue. In
4068 Objective-C, skip this check for property_refs. */
4069 if (!objc_is_property_ref (arg)
4070 && !lvalue_or_else (location,
4071 arg, ((code == PREINCREMENT_EXPR
4072 || code == POSTINCREMENT_EXPR)
4073 ? lv_increment
4074 : lv_decrement)))
4075 return error_mark_node;
4077 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4079 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4080 warning_at (location, OPT_Wc___compat,
4081 "increment of enumeration value is invalid in C++");
4082 else
4083 warning_at (location, OPT_Wc___compat,
4084 "decrement of enumeration value is invalid in C++");
4087 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4088 arg = c_fully_fold (arg, false, NULL);
4090 bool atomic_op;
4091 atomic_op = really_atomic_lvalue (arg);
4093 /* Increment or decrement the real part of the value,
4094 and don't change the imaginary part. */
4095 if (typecode == COMPLEX_TYPE)
4097 tree real, imag;
4099 pedwarn (location, OPT_Wpedantic,
4100 "ISO C does not support %<++%> and %<--%> on complex types");
4102 if (!atomic_op)
4104 arg = stabilize_reference (arg);
4105 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4106 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4107 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4108 if (real == error_mark_node || imag == error_mark_node)
4109 return error_mark_node;
4110 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4111 real, imag);
4112 goto return_build_unary_op;
4116 /* Report invalid types. */
4118 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4119 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4120 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4122 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4123 error_at (location, "wrong type argument to increment");
4124 else
4125 error_at (location, "wrong type argument to decrement");
4127 return error_mark_node;
4131 tree inc;
4133 argtype = TREE_TYPE (arg);
4135 /* Compute the increment. */
4137 if (typecode == POINTER_TYPE)
4139 /* If pointer target is an incomplete type,
4140 we just cannot know how to do the arithmetic. */
4141 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4143 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4144 error_at (location,
4145 "increment of pointer to an incomplete type %qT",
4146 TREE_TYPE (argtype));
4147 else
4148 error_at (location,
4149 "decrement of pointer to an incomplete type %qT",
4150 TREE_TYPE (argtype));
4152 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4153 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4155 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4156 pedwarn (location, OPT_Wpointer_arith,
4157 "wrong type argument to increment");
4158 else
4159 pedwarn (location, OPT_Wpointer_arith,
4160 "wrong type argument to decrement");
4163 /* UPC pointer-to-shared types cannot be
4164 incremented/decrmented directly. */
4165 if (upc_shared_type_p (TREE_TYPE (argtype)))
4166 return upc_pts_increment (location, code, arg);
4168 inc = c_size_in_bytes (TREE_TYPE (argtype));
4169 inc = convert_to_ptrofftype_loc (location, inc);
4171 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4173 /* For signed fract types, we invert ++ to -- or
4174 -- to ++, and change inc from 1 to -1, because
4175 it is not possible to represent 1 in signed fract constants.
4176 For unsigned fract types, the result always overflows and
4177 we get an undefined (original) or the maximum value. */
4178 if (code == PREINCREMENT_EXPR)
4179 code = PREDECREMENT_EXPR;
4180 else if (code == PREDECREMENT_EXPR)
4181 code = PREINCREMENT_EXPR;
4182 else if (code == POSTINCREMENT_EXPR)
4183 code = POSTDECREMENT_EXPR;
4184 else /* code == POSTDECREMENT_EXPR */
4185 code = POSTINCREMENT_EXPR;
4187 inc = integer_minus_one_node;
4188 inc = convert (argtype, inc);
4190 else
4192 inc = VECTOR_TYPE_P (argtype)
4193 ? build_one_cst (argtype)
4194 : integer_one_node;
4195 inc = convert (argtype, inc);
4198 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4199 need to ask Objective-C to build the increment or decrement
4200 expression for it. */
4201 if (objc_is_property_ref (arg))
4202 return objc_build_incr_expr_for_property_ref (location, code,
4203 arg, inc);
4205 /* Report a read-only lvalue. */
4206 if (TYPE_READONLY (argtype))
4208 readonly_error (location, arg,
4209 ((code == PREINCREMENT_EXPR
4210 || code == POSTINCREMENT_EXPR)
4211 ? lv_increment : lv_decrement));
4212 return error_mark_node;
4214 else if (TREE_READONLY (arg))
4215 readonly_warning (arg,
4216 ((code == PREINCREMENT_EXPR
4217 || code == POSTINCREMENT_EXPR)
4218 ? lv_increment : lv_decrement));
4220 /* If the argument is atomic, use the special code sequences for
4221 atomic compound assignment. */
4222 if (atomic_op)
4224 arg = stabilize_reference (arg);
4225 ret = build_atomic_assign (location, arg,
4226 ((code == PREINCREMENT_EXPR
4227 || code == POSTINCREMENT_EXPR)
4228 ? PLUS_EXPR
4229 : MINUS_EXPR),
4230 (FRACT_MODE_P (TYPE_MODE (argtype))
4231 ? inc
4232 : integer_one_node),
4233 (code == POSTINCREMENT_EXPR
4234 || code == POSTDECREMENT_EXPR));
4235 goto return_build_unary_op;
4238 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4239 val = boolean_increment (code, arg);
4240 else
4241 val = build2 (code, TREE_TYPE (arg), arg, inc);
4242 TREE_SIDE_EFFECTS (val) = 1;
4243 if (TREE_CODE (val) != code)
4244 TREE_NO_WARNING (val) = 1;
4245 ret = val;
4246 goto return_build_unary_op;
4249 case ADDR_EXPR:
4250 /* Note that this operation never does default_conversion. */
4252 /* The operand of unary '&' must be an lvalue (which excludes
4253 expressions of type void), or, in C99, the result of a [] or
4254 unary '*' operator. */
4255 if (VOID_TYPE_P (TREE_TYPE (arg))
4256 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4257 && (TREE_CODE (arg) != INDIRECT_REF
4258 || !flag_isoc99))
4259 pedwarn (location, 0, "taking address of expression of type %<void%>");
4261 /* Let &* cancel out to simplify resulting code. */
4262 if (TREE_CODE (arg) == INDIRECT_REF)
4264 /* Don't let this be an lvalue. */
4265 if (lvalue_p (TREE_OPERAND (arg, 0)))
4266 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4267 ret = TREE_OPERAND (arg, 0);
4268 goto return_build_unary_op;
4271 /* For &x[y], return x+y */
4272 if (TREE_CODE (arg) == ARRAY_REF)
4274 tree op0 = TREE_OPERAND (arg, 0);
4275 if (!c_mark_addressable (op0))
4276 return error_mark_node;
4277 /* Taking the address of a UPC shared array element
4278 cannot be performed as a simple addition.
4279 Return an ADDR_EXPR node, and let upc_genericize()
4280 implement the proper semantics. */
4281 if (TREE_SHARED (arg))
4282 return build1 (ADDR_EXPR, TREE_TYPE (arg), arg);
4285 /* Anything not already handled and not a true memory reference
4286 or a non-lvalue array is an error. */
4287 else if (typecode != FUNCTION_TYPE && !flag
4288 && !lvalue_or_else (location, arg, lv_addressof))
4289 return error_mark_node;
4291 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4292 folding later. */
4293 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4295 tree inner = build_unary_op (location, code,
4296 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4297 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4298 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4299 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4300 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4301 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4302 goto return_build_unary_op;
4305 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4306 argtype = TREE_TYPE (arg);
4308 /* If the lvalue is const or volatile, merge that into the type
4309 to which the address will point. This is only needed
4310 for function types. */
4311 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4312 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4313 && TREE_CODE (argtype) == FUNCTION_TYPE)
4315 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4316 int quals = orig_quals;
4318 if (TREE_READONLY (arg))
4319 quals |= TYPE_QUAL_CONST;
4320 if (TREE_THIS_VOLATILE (arg))
4321 quals |= TYPE_QUAL_VOLATILE;
4323 argtype = c_build_qualified_type (argtype, quals);
4326 if (!c_mark_addressable (arg))
4327 return error_mark_node;
4329 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4330 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4332 argtype = build_pointer_type (argtype);
4334 /* ??? Cope with user tricks that amount to offsetof. Delete this
4335 when we have proper support for integer constant expressions. */
4336 val = get_base_address (arg);
4337 if (val && TREE_CODE (val) == INDIRECT_REF
4338 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4340 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4341 goto return_build_unary_op;
4344 val = build1 (ADDR_EXPR, argtype, arg);
4346 ret = val;
4347 goto return_build_unary_op;
4349 default:
4350 gcc_unreachable ();
4353 if (argtype == 0)
4354 argtype = TREE_TYPE (arg);
4355 if (TREE_CODE (arg) == INTEGER_CST)
4356 ret = (require_constant_value
4357 ? fold_build1_initializer_loc (location, code, argtype, arg)
4358 : fold_build1_loc (location, code, argtype, arg));
4359 else
4360 ret = build1 (code, argtype, arg);
4361 return_build_unary_op:
4362 gcc_assert (ret != error_mark_node);
4363 /* The result of an operation on objects that
4364 are UPC shared qualified, must not be shared qualified. */
4365 if (upc_shared_type_p (TREE_TYPE (ret)))
4366 TREE_TYPE (ret) = build_upc_unshared_type (TREE_TYPE (ret));
4367 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4368 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4369 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4370 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4371 ret = note_integer_operands (ret);
4372 if (eptype)
4373 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4374 protected_set_expr_location (ret, location);
4375 return ret;
4378 /* Return nonzero if REF is an lvalue valid for this language.
4379 Lvalues can be assigned, unless their type has TYPE_READONLY.
4380 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4382 bool
4383 lvalue_p (const_tree ref)
4385 const enum tree_code code = TREE_CODE (ref);
4387 switch (code)
4389 case REALPART_EXPR:
4390 case IMAGPART_EXPR:
4391 case COMPONENT_REF:
4392 return lvalue_p (TREE_OPERAND (ref, 0));
4394 case C_MAYBE_CONST_EXPR:
4395 return lvalue_p (TREE_OPERAND (ref, 1));
4397 case COMPOUND_LITERAL_EXPR:
4398 case STRING_CST:
4399 return 1;
4401 case INDIRECT_REF:
4402 case ARRAY_REF:
4403 case ARRAY_NOTATION_REF:
4404 case VAR_DECL:
4405 case PARM_DECL:
4406 case RESULT_DECL:
4407 case ERROR_MARK:
4408 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4409 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4411 case BIND_EXPR:
4412 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4414 default:
4415 return 0;
4419 /* Give a warning for storing in something that is read-only in GCC
4420 terms but not const in ISO C terms. */
4422 static void
4423 readonly_warning (tree arg, enum lvalue_use use)
4425 switch (use)
4427 case lv_assign:
4428 warning (0, "assignment of read-only location %qE", arg);
4429 break;
4430 case lv_increment:
4431 warning (0, "increment of read-only location %qE", arg);
4432 break;
4433 case lv_decrement:
4434 warning (0, "decrement of read-only location %qE", arg);
4435 break;
4436 default:
4437 gcc_unreachable ();
4439 return;
4443 /* Return nonzero if REF is an lvalue valid for this language;
4444 otherwise, print an error message and return zero. USE says
4445 how the lvalue is being used and so selects the error message.
4446 LOCATION is the location at which any error should be reported. */
4448 static int
4449 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4451 int win = lvalue_p (ref);
4453 if (!win)
4454 lvalue_error (loc, use);
4456 return win;
4459 /* Mark EXP saying that we need to be able to take the
4460 address of it; it should not be allocated in a register.
4461 Returns true if successful. */
4463 bool
4464 c_mark_addressable (tree exp)
4466 tree x = exp;
4468 while (1)
4469 switch (TREE_CODE (x))
4471 case COMPONENT_REF:
4472 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4474 error
4475 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4476 return false;
4479 /* ... fall through ... */
4481 case ADDR_EXPR:
4482 case ARRAY_REF:
4483 case REALPART_EXPR:
4484 case IMAGPART_EXPR:
4485 x = TREE_OPERAND (x, 0);
4486 break;
4488 case COMPOUND_LITERAL_EXPR:
4489 case CONSTRUCTOR:
4490 TREE_ADDRESSABLE (x) = 1;
4491 return true;
4493 case VAR_DECL:
4494 case CONST_DECL:
4495 case PARM_DECL:
4496 case RESULT_DECL:
4497 if (C_DECL_REGISTER (x)
4498 && DECL_NONLOCAL (x))
4500 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4502 error
4503 ("global register variable %qD used in nested function", x);
4504 return false;
4506 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4508 else if (C_DECL_REGISTER (x))
4510 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4511 error ("address of global register variable %qD requested", x);
4512 else
4513 error ("address of register variable %qD requested", x);
4514 return false;
4517 /* drops in */
4518 case FUNCTION_DECL:
4519 TREE_ADDRESSABLE (x) = 1;
4520 /* drops out */
4521 default:
4522 return true;
4526 /* Convert EXPR to TYPE, warning about conversion problems with
4527 constants. SEMANTIC_TYPE is the type this conversion would use
4528 without excess precision. If SEMANTIC_TYPE is NULL, this function
4529 is equivalent to convert_and_check. This function is a wrapper that
4530 handles conversions that may be different than
4531 the usual ones because of excess precision. */
4533 static tree
4534 ep_convert_and_check (location_t loc, tree type, tree expr,
4535 tree semantic_type)
4537 if (TREE_TYPE (expr) == type)
4538 return expr;
4540 if (!semantic_type)
4541 return convert_and_check (loc, type, expr);
4543 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4544 && TREE_TYPE (expr) != semantic_type)
4546 /* For integers, we need to check the real conversion, not
4547 the conversion to the excess precision type. */
4548 expr = convert_and_check (loc, semantic_type, expr);
4550 /* Result type is the excess precision type, which should be
4551 large enough, so do not check. */
4552 return convert (type, expr);
4555 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4556 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4557 if folded to an integer constant then the unselected half may
4558 contain arbitrary operations not normally permitted in constant
4559 expressions. Set the location of the expression to LOC. */
4561 tree
4562 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4563 tree op1, tree op1_original_type, tree op2,
4564 tree op2_original_type)
4566 tree type1;
4567 tree type2;
4568 enum tree_code code1;
4569 enum tree_code code2;
4570 tree result_type = NULL;
4571 tree semantic_result_type = NULL;
4572 tree orig_op1 = op1, orig_op2 = op2;
4573 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4574 bool ifexp_int_operands;
4575 tree ret;
4577 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4578 if (op1_int_operands)
4579 op1 = remove_c_maybe_const_expr (op1);
4580 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4581 if (op2_int_operands)
4582 op2 = remove_c_maybe_const_expr (op2);
4583 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4584 if (ifexp_int_operands)
4585 ifexp = remove_c_maybe_const_expr (ifexp);
4587 /* Promote both alternatives. */
4589 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4590 op1 = default_conversion (op1);
4591 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4592 op2 = default_conversion (op2);
4594 if (TREE_CODE (ifexp) == ERROR_MARK
4595 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4596 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4597 return error_mark_node;
4599 type1 = TREE_TYPE (op1);
4600 code1 = TREE_CODE (type1);
4601 type2 = TREE_TYPE (op2);
4602 code2 = TREE_CODE (type2);
4604 /* C90 does not permit non-lvalue arrays in conditional expressions.
4605 In C99 they will be pointers by now. */
4606 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4608 error_at (colon_loc, "non-lvalue array in conditional expression");
4609 return error_mark_node;
4612 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4613 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4614 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4615 || code1 == COMPLEX_TYPE)
4616 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4617 || code2 == COMPLEX_TYPE))
4619 semantic_result_type = c_common_type (type1, type2);
4620 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4622 op1 = TREE_OPERAND (op1, 0);
4623 type1 = TREE_TYPE (op1);
4624 gcc_assert (TREE_CODE (type1) == code1);
4626 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4628 op2 = TREE_OPERAND (op2, 0);
4629 type2 = TREE_TYPE (op2);
4630 gcc_assert (TREE_CODE (type2) == code2);
4634 if (warn_cxx_compat)
4636 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4637 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4639 if (TREE_CODE (t1) == ENUMERAL_TYPE
4640 && TREE_CODE (t2) == ENUMERAL_TYPE
4641 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4642 warning_at (colon_loc, OPT_Wc___compat,
4643 ("different enum types in conditional is "
4644 "invalid in C++: %qT vs %qT"),
4645 t1, t2);
4648 /* Quickly detect the usual case where op1 and op2 have the same type
4649 after promotion. */
4650 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4652 if (type1 == type2)
4653 result_type = type1;
4654 else
4655 result_type = TYPE_MAIN_VARIANT (type1);
4657 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4658 || code1 == COMPLEX_TYPE)
4659 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4660 || code2 == COMPLEX_TYPE))
4662 result_type = c_common_type (type1, type2);
4663 do_warn_double_promotion (result_type, type1, type2,
4664 "implicit conversion from %qT to %qT to "
4665 "match other result of conditional",
4666 colon_loc);
4668 /* If -Wsign-compare, warn here if type1 and type2 have
4669 different signedness. We'll promote the signed to unsigned
4670 and later code won't know it used to be different.
4671 Do this check on the original types, so that explicit casts
4672 will be considered, but default promotions won't. */
4673 if (c_inhibit_evaluation_warnings == 0)
4675 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4676 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4678 if (unsigned_op1 ^ unsigned_op2)
4680 bool ovf;
4682 /* Do not warn if the result type is signed, since the
4683 signed type will only be chosen if it can represent
4684 all the values of the unsigned type. */
4685 if (!TYPE_UNSIGNED (result_type))
4686 /* OK */;
4687 else
4689 bool op1_maybe_const = true;
4690 bool op2_maybe_const = true;
4692 /* Do not warn if the signed quantity is an
4693 unsuffixed integer literal (or some static
4694 constant expression involving such literals) and
4695 it is non-negative. This warning requires the
4696 operands to be folded for best results, so do
4697 that folding in this case even without
4698 warn_sign_compare to avoid warning options
4699 possibly affecting code generation. */
4700 c_inhibit_evaluation_warnings
4701 += (ifexp == truthvalue_false_node);
4702 op1 = c_fully_fold (op1, require_constant_value,
4703 &op1_maybe_const);
4704 c_inhibit_evaluation_warnings
4705 -= (ifexp == truthvalue_false_node);
4707 c_inhibit_evaluation_warnings
4708 += (ifexp == truthvalue_true_node);
4709 op2 = c_fully_fold (op2, require_constant_value,
4710 &op2_maybe_const);
4711 c_inhibit_evaluation_warnings
4712 -= (ifexp == truthvalue_true_node);
4714 if (warn_sign_compare)
4716 if ((unsigned_op2
4717 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4718 || (unsigned_op1
4719 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4720 /* OK */;
4721 else
4722 warning_at (colon_loc, OPT_Wsign_compare,
4723 ("signed and unsigned type in "
4724 "conditional expression"));
4726 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4727 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4728 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4729 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4734 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4736 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4737 pedwarn (colon_loc, OPT_Wpedantic,
4738 "ISO C forbids conditional expr with only one void side");
4739 result_type = void_type_node;
4741 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4743 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4744 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4745 addr_space_t as_common;
4747 if (comp_target_types (colon_loc, type1, type2))
4748 result_type = common_pointer_type (type1, type2);
4749 else if (null_pointer_constant_p (orig_op1))
4750 result_type = type2;
4751 else if (null_pointer_constant_p (orig_op2))
4752 result_type = type1;
4753 else if (!addr_space_superset (as1, as2, &as_common))
4755 error_at (colon_loc, "pointers to disjoint address spaces "
4756 "used in conditional expression");
4757 return error_mark_node;
4759 else if (VOID_TYPE_P (TREE_TYPE (type1))
4760 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4762 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4763 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4764 & ~TYPE_QUALS (TREE_TYPE (type1))))
4765 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4766 "pointer to array loses qualifier "
4767 "in conditional expression");
4769 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4770 pedwarn (colon_loc, OPT_Wpedantic,
4771 "ISO C forbids conditional expr between "
4772 "%<void *%> and function pointer");
4773 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4774 TREE_TYPE (type2)));
4776 else if (VOID_TYPE_P (TREE_TYPE (type2))
4777 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4779 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4780 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4781 & ~TYPE_QUALS (TREE_TYPE (type2))))
4782 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4783 "pointer to array loses qualifier "
4784 "in conditional expression");
4786 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4787 pedwarn (colon_loc, OPT_Wpedantic,
4788 "ISO C forbids conditional expr between "
4789 "%<void *%> and function pointer");
4790 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4791 TREE_TYPE (type1)));
4793 /* Objective-C pointer comparisons are a bit more lenient. */
4794 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4795 result_type = objc_common_type (type1, type2);
4796 else
4798 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4800 pedwarn (colon_loc, 0,
4801 "pointer type mismatch in conditional expression");
4802 result_type = build_pointer_type
4803 (build_qualified_type (void_type_node, qual));
4806 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4808 if (!null_pointer_constant_p (orig_op2))
4809 pedwarn (colon_loc, 0,
4810 "pointer/integer type mismatch in conditional expression");
4811 else
4813 op2 = !upc_shared_type_p (TREE_TYPE (type1))
4814 ? null_pointer_node : upc_null_pts_node;
4816 result_type = type1;
4818 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4820 if (!null_pointer_constant_p (orig_op1))
4821 pedwarn (colon_loc, 0,
4822 "pointer/integer type mismatch in conditional expression");
4823 else
4825 op1 = !upc_shared_type_p (TREE_TYPE (type2))
4826 ? null_pointer_node : upc_null_pts_node;
4828 result_type = type2;
4831 if (!result_type)
4833 if (flag_cond_mismatch)
4834 result_type = void_type_node;
4835 else
4837 error_at (colon_loc, "type mismatch in conditional expression");
4838 return error_mark_node;
4842 /* Merge const and volatile flags of the incoming types. */
4843 result_type
4844 = build_type_variant (result_type,
4845 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4846 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4848 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4849 semantic_result_type);
4850 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4851 semantic_result_type);
4853 if (ifexp_bcp && ifexp == truthvalue_true_node)
4855 op2_int_operands = true;
4856 op1 = c_fully_fold (op1, require_constant_value, NULL);
4858 if (ifexp_bcp && ifexp == truthvalue_false_node)
4860 op1_int_operands = true;
4861 op2 = c_fully_fold (op2, require_constant_value, NULL);
4863 int_const = int_operands = (ifexp_int_operands
4864 && op1_int_operands
4865 && op2_int_operands);
4866 if (int_operands)
4868 int_const = ((ifexp == truthvalue_true_node
4869 && TREE_CODE (orig_op1) == INTEGER_CST
4870 && !TREE_OVERFLOW (orig_op1))
4871 || (ifexp == truthvalue_false_node
4872 && TREE_CODE (orig_op2) == INTEGER_CST
4873 && !TREE_OVERFLOW (orig_op2)));
4875 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4876 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4877 else
4879 if (int_operands)
4881 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4882 nested inside of the expression. */
4883 op1 = c_fully_fold (op1, false, NULL);
4884 op2 = c_fully_fold (op2, false, NULL);
4886 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4887 if (int_operands)
4888 ret = note_integer_operands (ret);
4890 if (semantic_result_type)
4891 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4893 protected_set_expr_location (ret, colon_loc);
4894 return ret;
4897 /* Return a compound expression that performs two expressions and
4898 returns the value of the second of them.
4900 LOC is the location of the COMPOUND_EXPR. */
4902 tree
4903 build_compound_expr (location_t loc, tree expr1, tree expr2)
4905 bool expr1_int_operands, expr2_int_operands;
4906 tree eptype = NULL_TREE;
4907 tree ret;
4909 if (flag_cilkplus
4910 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4911 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4913 error_at (loc,
4914 "spawned function call cannot be part of a comma expression");
4915 return error_mark_node;
4917 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4918 if (expr1_int_operands)
4919 expr1 = remove_c_maybe_const_expr (expr1);
4920 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4921 if (expr2_int_operands)
4922 expr2 = remove_c_maybe_const_expr (expr2);
4924 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4925 expr1 = TREE_OPERAND (expr1, 0);
4926 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4928 eptype = TREE_TYPE (expr2);
4929 expr2 = TREE_OPERAND (expr2, 0);
4932 if (!TREE_SIDE_EFFECTS (expr1))
4934 /* The left-hand operand of a comma expression is like an expression
4935 statement: with -Wunused, we should warn if it doesn't have
4936 any side-effects, unless it was explicitly cast to (void). */
4937 if (warn_unused_value)
4939 if (VOID_TYPE_P (TREE_TYPE (expr1))
4940 && CONVERT_EXPR_P (expr1))
4941 ; /* (void) a, b */
4942 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4943 && TREE_CODE (expr1) == COMPOUND_EXPR
4944 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4945 ; /* (void) a, (void) b, c */
4946 else
4947 warning_at (loc, OPT_Wunused_value,
4948 "left-hand operand of comma expression has no effect");
4951 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4952 && warn_unused_value)
4954 tree r = expr1;
4955 location_t cloc = loc;
4956 while (TREE_CODE (r) == COMPOUND_EXPR)
4958 if (EXPR_HAS_LOCATION (r))
4959 cloc = EXPR_LOCATION (r);
4960 r = TREE_OPERAND (r, 1);
4962 if (!TREE_SIDE_EFFECTS (r)
4963 && !VOID_TYPE_P (TREE_TYPE (r))
4964 && !CONVERT_EXPR_P (r))
4965 warning_at (cloc, OPT_Wunused_value,
4966 "right-hand operand of comma expression has no effect");
4969 /* With -Wunused, we should also warn if the left-hand operand does have
4970 side-effects, but computes a value which is not used. For example, in
4971 `foo() + bar(), baz()' the result of the `+' operator is not used,
4972 so we should issue a warning. */
4973 else if (warn_unused_value)
4974 warn_if_unused_value (expr1, loc);
4976 if (expr2 == error_mark_node)
4977 return error_mark_node;
4979 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4981 if (flag_isoc99
4982 && expr1_int_operands
4983 && expr2_int_operands)
4984 ret = note_integer_operands (ret);
4986 if (eptype)
4987 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4989 protected_set_expr_location (ret, loc);
4990 return ret;
4993 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4994 which we are casting. OTYPE is the type of the expression being
4995 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4996 of the cast. -Wcast-qual appeared on the command line. Named
4997 address space qualifiers are not handled here, because they result
4998 in different warnings. */
5000 static void
5001 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5003 tree in_type = type;
5004 tree in_otype = otype;
5005 int added = 0;
5006 int discarded = 0;
5007 bool is_const;
5009 /* Check that the qualifiers on IN_TYPE are a superset of the
5010 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5011 nodes is uninteresting and we stop as soon as we hit a
5012 non-POINTER_TYPE node on either type. */
5015 in_otype = TREE_TYPE (in_otype);
5016 in_type = TREE_TYPE (in_type);
5018 /* GNU C allows cv-qualified function types. 'const' means the
5019 function is very pure, 'volatile' means it can't return. We
5020 need to warn when such qualifiers are added, not when they're
5021 taken away. */
5022 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5023 && TREE_CODE (in_type) == FUNCTION_TYPE)
5024 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5025 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5026 else
5027 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5028 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5030 while (TREE_CODE (in_type) == POINTER_TYPE
5031 && TREE_CODE (in_otype) == POINTER_TYPE);
5033 if (added)
5034 warning_at (loc, OPT_Wcast_qual,
5035 "cast adds %q#v qualifier to function type", added);
5037 if (discarded)
5038 /* There are qualifiers present in IN_OTYPE that are not present
5039 in IN_TYPE. */
5040 warning_at (loc, OPT_Wcast_qual,
5041 "cast discards %qv qualifier from pointer target type",
5042 discarded);
5044 if (added || discarded)
5045 return;
5047 /* A cast from **T to const **T is unsafe, because it can cause a
5048 const value to be changed with no additional warning. We only
5049 issue this warning if T is the same on both sides, and we only
5050 issue the warning if there are the same number of pointers on
5051 both sides, as otherwise the cast is clearly unsafe anyhow. A
5052 cast is unsafe when a qualifier is added at one level and const
5053 is not present at all outer levels.
5055 To issue this warning, we check at each level whether the cast
5056 adds new qualifiers not already seen. We don't need to special
5057 case function types, as they won't have the same
5058 TYPE_MAIN_VARIANT. */
5060 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5061 return;
5062 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5063 return;
5065 in_type = type;
5066 in_otype = otype;
5067 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5070 in_type = TREE_TYPE (in_type);
5071 in_otype = TREE_TYPE (in_otype);
5072 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5073 && !is_const)
5075 warning_at (loc, OPT_Wcast_qual,
5076 "to be safe all intermediate pointers in cast from "
5077 "%qT to %qT must be %<const%> qualified",
5078 otype, type);
5079 break;
5081 if (is_const)
5082 is_const = TYPE_READONLY (in_type);
5084 while (TREE_CODE (in_type) == POINTER_TYPE);
5087 /* Build an expression representing a cast to type TYPE of expression EXPR.
5088 LOC is the location of the cast-- typically the open paren of the cast. */
5090 tree
5091 build_c_cast (location_t loc, tree type, tree expr)
5093 tree value;
5095 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5096 expr = TREE_OPERAND (expr, 0);
5098 value = expr;
5100 if (type == error_mark_node || expr == error_mark_node)
5101 return error_mark_node;
5103 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5104 only in <protocol> qualifications. But when constructing cast expressions,
5105 the protocols do matter and must be kept around. */
5106 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5107 return build1 (NOP_EXPR, type, expr);
5109 if (upc_shared_type_p (type))
5111 error ("UPC does not allow casts to a shared type");
5112 return error_mark_node;
5115 type = TYPE_MAIN_VARIANT (type);
5117 if (TREE_CODE (type) == ARRAY_TYPE)
5119 error_at (loc, "cast specifies array type");
5120 return error_mark_node;
5123 if (TREE_CODE (type) == FUNCTION_TYPE)
5125 error_at (loc, "cast specifies function type");
5126 return error_mark_node;
5129 if (!VOID_TYPE_P (type))
5131 value = require_complete_type (value);
5132 if (value == error_mark_node)
5133 return error_mark_node;
5136 if (integer_zerop (value)
5137 && POINTER_TYPE_P (type)
5138 && upc_shared_type_p (TREE_TYPE (type))
5139 && POINTER_TYPE_P (TREE_TYPE (expr))
5140 && ! upc_shared_type_p (TREE_TYPE (TREE_TYPE (expr))))
5142 value = upc_null_pts_node;
5145 if (!upc_shared_type_p (type) && upc_shared_type_p (TREE_TYPE (expr)))
5147 /* UPC disallows things like:
5148 (int)p = <expr>; (where p is a shared int) */
5149 value = non_lvalue (value);
5152 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5154 if (TREE_CODE (type) == RECORD_TYPE
5155 || TREE_CODE (type) == UNION_TYPE)
5156 pedwarn (loc, OPT_Wpedantic,
5157 "ISO C forbids casting nonscalar to the same type");
5159 /* Convert to remove any qualifiers from VALUE's type. */
5160 value = convert (type, value);
5162 else if (TREE_CODE (type) == UNION_TYPE)
5164 tree field;
5166 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5167 if (TREE_TYPE (field) != error_mark_node
5168 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5169 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5170 break;
5172 if (field)
5174 tree t;
5175 bool maybe_const = true;
5177 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5178 t = c_fully_fold (value, false, &maybe_const);
5179 t = build_constructor_single (type, field, t);
5180 if (!maybe_const)
5181 t = c_wrap_maybe_const (t, true);
5182 t = digest_init (loc, type, t,
5183 NULL_TREE, false, true, 0);
5184 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5185 return t;
5187 error_at (loc, "cast to union type from type not present in union");
5188 return error_mark_node;
5190 else
5192 tree otype, ovalue;
5194 if (type == void_type_node)
5196 tree t = build1 (CONVERT_EXPR, type, value);
5197 SET_EXPR_LOCATION (t, loc);
5198 return t;
5201 otype = TREE_TYPE (value);
5203 if (TREE_CODE (type) == POINTER_TYPE
5204 && TREE_CODE (otype) == POINTER_TYPE)
5206 int t_shared = upc_shared_type_p (TREE_TYPE (type));
5207 int o_shared = upc_shared_type_p (TREE_TYPE (otype));
5208 if ((!t_shared && o_shared)
5209 || (t_shared && o_shared
5210 && !lang_hooks.types_compatible_p (type, otype)))
5211 return build1 (CONVERT_EXPR, type, value);
5214 /* Optionally warn about potentially worrisome casts. */
5215 if (warn_cast_qual
5216 && TREE_CODE (type) == POINTER_TYPE
5217 && TREE_CODE (otype) == POINTER_TYPE)
5218 handle_warn_cast_qual (loc, type, otype);
5220 /* Warn about conversions between pointers to disjoint
5221 address spaces. */
5222 if (TREE_CODE (type) == POINTER_TYPE
5223 && TREE_CODE (otype) == POINTER_TYPE
5224 && !null_pointer_constant_p (value))
5226 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5227 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5228 addr_space_t as_common;
5230 if (!addr_space_superset (as_to, as_from, &as_common))
5232 if (ADDR_SPACE_GENERIC_P (as_from))
5233 warning_at (loc, 0, "cast to %s address space pointer "
5234 "from disjoint generic address space pointer",
5235 c_addr_space_name (as_to));
5237 else if (ADDR_SPACE_GENERIC_P (as_to))
5238 warning_at (loc, 0, "cast to generic address space pointer "
5239 "from disjoint %s address space pointer",
5240 c_addr_space_name (as_from));
5242 else
5243 warning_at (loc, 0, "cast to %s address space pointer "
5244 "from disjoint %s address space pointer",
5245 c_addr_space_name (as_to),
5246 c_addr_space_name (as_from));
5250 /* Warn about possible alignment problems. */
5251 if (STRICT_ALIGNMENT
5252 && TREE_CODE (type) == POINTER_TYPE
5253 && TREE_CODE (otype) == POINTER_TYPE
5254 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5255 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5256 /* Don't warn about opaque types, where the actual alignment
5257 restriction is unknown. */
5258 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5259 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5260 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5261 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5262 warning_at (loc, OPT_Wcast_align,
5263 "cast increases required alignment of target type");
5265 if (POINTER_TYPE_P (type)
5266 && upc_shared_type_p (TREE_TYPE (type))
5267 && POINTER_TYPE_P (otype)
5268 && !upc_shared_type_p (TREE_TYPE (otype)))
5270 error_at (loc, "UPC does not allow casts from a local pointer to a pointer-to-shared");
5271 return error_mark_node;
5274 if (TREE_CODE (type) == POINTER_TYPE
5275 && TREE_CODE (otype) == INTEGER_TYPE
5276 && upc_shared_type_p (TREE_TYPE (type))
5277 && !integer_zerop (value))
5279 error_at (loc, "UPC does not allow casts from an integer to a pointer-to-shared");
5280 return error_mark_node;
5283 if (TREE_CODE (type) == INTEGER_TYPE
5284 && TREE_CODE (otype) == POINTER_TYPE
5285 && upc_shared_type_p (TREE_TYPE (otype)))
5287 /* UPC pointer-to-shared -> integer
5288 This will be lowered by the genericize pass. */
5289 return build1 (CONVERT_EXPR, type, value);
5292 if (TREE_CODE (type) == INTEGER_TYPE
5293 && TREE_CODE (otype) == POINTER_TYPE
5294 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5295 /* Unlike conversion of integers to pointers, where the
5296 warning is disabled for converting constants because
5297 of cases such as SIG_*, warn about converting constant
5298 pointers to integers. In some cases it may cause unwanted
5299 sign extension, and a warning is appropriate. */
5300 warning_at (loc, OPT_Wpointer_to_int_cast,
5301 "cast from pointer to integer of different size");
5303 if (TREE_CODE (value) == CALL_EXPR
5304 && TREE_CODE (type) != TREE_CODE (otype))
5305 warning_at (loc, OPT_Wbad_function_cast,
5306 "cast from function call of type %qT "
5307 "to non-matching type %qT", otype, type);
5309 if (TREE_CODE (type) == POINTER_TYPE
5310 && TREE_CODE (otype) == INTEGER_TYPE
5311 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5312 /* Don't warn about converting any constant. */
5313 && !TREE_CONSTANT (value))
5314 warning_at (loc,
5315 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5316 "of different size");
5318 if (warn_strict_aliasing <= 2)
5319 strict_aliasing_warning (otype, type, expr);
5321 /* If pedantic, warn for conversions between function and object
5322 pointer types, except for converting a null pointer constant
5323 to function pointer type. */
5324 if (pedantic
5325 && TREE_CODE (type) == POINTER_TYPE
5326 && TREE_CODE (otype) == POINTER_TYPE
5327 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5328 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5329 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5330 "conversion of function pointer to object pointer type");
5332 if (pedantic
5333 && TREE_CODE (type) == POINTER_TYPE
5334 && TREE_CODE (otype) == POINTER_TYPE
5335 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5336 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5337 && !null_pointer_constant_p (value))
5338 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5339 "conversion of object pointer to function pointer type");
5341 ovalue = value;
5342 value = convert (type, value);
5344 /* Ignore any integer overflow caused by the cast. */
5345 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5347 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5349 if (!TREE_OVERFLOW (value))
5351 /* Avoid clobbering a shared constant. */
5352 value = copy_node (value);
5353 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5356 else if (TREE_OVERFLOW (value))
5357 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5358 value = wide_int_to_tree (TREE_TYPE (value), value);
5362 /* Don't let a cast be an lvalue. */
5363 if (value == expr)
5364 value = non_lvalue_loc (loc, value);
5366 /* Don't allow the results of casting to floating-point or complex
5367 types be confused with actual constants, or casts involving
5368 integer and pointer types other than direct integer-to-integer
5369 and integer-to-pointer be confused with integer constant
5370 expressions and null pointer constants. */
5371 if (TREE_CODE (value) == REAL_CST
5372 || TREE_CODE (value) == COMPLEX_CST
5373 || (TREE_CODE (value) == INTEGER_CST
5374 && !((TREE_CODE (expr) == INTEGER_CST
5375 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5376 || TREE_CODE (expr) == REAL_CST
5377 || TREE_CODE (expr) == COMPLEX_CST)))
5378 value = build1 (NOP_EXPR, type, value);
5380 if (CAN_HAVE_LOCATION_P (value))
5381 SET_EXPR_LOCATION (value, loc);
5382 return value;
5385 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5386 location of the open paren of the cast, or the position of the cast
5387 expr. */
5388 tree
5389 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5391 tree type;
5392 tree type_expr = NULL_TREE;
5393 bool type_expr_const = true;
5394 tree ret;
5395 int saved_wsp = warn_strict_prototypes;
5397 /* This avoids warnings about unprototyped casts on
5398 integers. E.g. "#define SIG_DFL (void(*)())0". */
5399 if (TREE_CODE (expr) == INTEGER_CST)
5400 warn_strict_prototypes = 0;
5401 type = groktypename (type_name, &type_expr, &type_expr_const);
5402 warn_strict_prototypes = saved_wsp;
5404 ret = build_c_cast (loc, type, expr);
5405 if (type_expr)
5407 bool inner_expr_const = true;
5408 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5409 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5410 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5411 && inner_expr_const);
5412 SET_EXPR_LOCATION (ret, loc);
5415 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5416 SET_EXPR_LOCATION (ret, loc);
5418 /* C++ does not permits types to be defined in a cast, but it
5419 allows references to incomplete types. */
5420 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5421 warning_at (loc, OPT_Wc___compat,
5422 "defining a type in a cast is invalid in C++");
5424 return ret;
5427 /* Build an assignment expression of lvalue LHS from value RHS.
5428 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5429 may differ from TREE_TYPE (LHS) for an enum bitfield.
5430 MODIFYCODE is the code for a binary operator that we use
5431 to combine the old value of LHS with RHS to get the new value.
5432 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5433 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5434 which may differ from TREE_TYPE (RHS) for an enum value.
5436 LOCATION is the location of the MODIFYCODE operator.
5437 RHS_LOC is the location of the RHS. */
5439 tree
5440 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5441 enum tree_code modifycode,
5442 location_t rhs_loc, tree rhs, tree rhs_origtype)
5444 tree result;
5445 tree newrhs;
5446 tree rhseval = NULL_TREE;
5447 tree rhs_semantic_type = NULL_TREE;
5448 tree lhstype = TREE_TYPE (lhs);
5449 tree olhstype = lhstype;
5450 bool npc;
5451 bool is_atomic_op;
5453 /* Types that aren't fully specified cannot be used in assignments. */
5454 lhs = require_complete_type (lhs);
5456 /* Avoid duplicate error messages from operands that had errors. */
5457 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5458 return error_mark_node;
5460 /* Ensure an error for assigning a non-lvalue array to an array in
5461 C90. */
5462 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5464 error_at (location, "assignment to expression with array type");
5465 return error_mark_node;
5468 /* For ObjC properties, defer this check. */
5469 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5470 return error_mark_node;
5472 is_atomic_op = really_atomic_lvalue (lhs);
5474 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5476 rhs_semantic_type = TREE_TYPE (rhs);
5477 rhs = TREE_OPERAND (rhs, 0);
5480 newrhs = rhs;
5482 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5484 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5485 lhs_origtype, modifycode, rhs_loc, rhs,
5486 rhs_origtype);
5487 if (inner == error_mark_node)
5488 return error_mark_node;
5489 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5490 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5491 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5492 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5493 protected_set_expr_location (result, location);
5494 return result;
5497 /* If a binary op has been requested, combine the old LHS value with the RHS
5498 producing the value we should actually store into the LHS. */
5500 if (modifycode != NOP_EXPR)
5502 lhs = c_fully_fold (lhs, false, NULL);
5503 lhs = stabilize_reference (lhs);
5505 /* Construct the RHS for any non-atomic compound assignemnt. */
5506 if (!is_atomic_op)
5508 /* If in LHS op= RHS the RHS has side-effects, ensure they
5509 are preevaluated before the rest of the assignment expression's
5510 side-effects, because RHS could contain e.g. function calls
5511 that modify LHS. */
5512 if (TREE_SIDE_EFFECTS (rhs))
5514 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5515 rhseval = newrhs;
5517 newrhs = build_binary_op (location,
5518 modifycode, lhs, newrhs, 1);
5520 /* The original type of the right hand side is no longer
5521 meaningful. */
5522 rhs_origtype = NULL_TREE;
5526 if (c_dialect_objc ())
5528 /* Check if we are modifying an Objective-C property reference;
5529 if so, we need to generate setter calls. */
5530 result = objc_maybe_build_modify_expr (lhs, newrhs);
5531 if (result)
5532 goto return_result;
5534 /* Else, do the check that we postponed for Objective-C. */
5535 if (!lvalue_or_else (location, lhs, lv_assign))
5536 return error_mark_node;
5539 /* Give an error for storing in something that is 'const'. */
5541 if (TYPE_READONLY (lhstype)
5542 || ((TREE_CODE (lhstype) == RECORD_TYPE
5543 || TREE_CODE (lhstype) == UNION_TYPE)
5544 && C_TYPE_FIELDS_READONLY (lhstype)))
5546 readonly_error (location, lhs, lv_assign);
5547 return error_mark_node;
5549 else if (TREE_READONLY (lhs))
5550 readonly_warning (lhs, lv_assign);
5552 /* If storing into a structure or union member,
5553 it has probably been given type `int'.
5554 Compute the type that would go with
5555 the actual amount of storage the member occupies. */
5557 if (TREE_CODE (lhs) == COMPONENT_REF
5558 && (TREE_CODE (lhstype) == INTEGER_TYPE
5559 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5560 || TREE_CODE (lhstype) == REAL_TYPE
5561 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5562 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5564 /* If storing in a field that is in actuality a short or narrower than one,
5565 we must store in the field in its actual type. */
5567 if (lhstype != TREE_TYPE (lhs))
5569 lhs = copy_node (lhs);
5570 TREE_TYPE (lhs) = lhstype;
5573 /* Issue -Wc++-compat warnings about an assignment to an enum type
5574 when LHS does not have its original type. This happens for,
5575 e.g., an enum bitfield in a struct. */
5576 if (warn_cxx_compat
5577 && lhs_origtype != NULL_TREE
5578 && lhs_origtype != lhstype
5579 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5581 tree checktype = (rhs_origtype != NULL_TREE
5582 ? rhs_origtype
5583 : TREE_TYPE (rhs));
5584 if (checktype != error_mark_node
5585 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5586 || (is_atomic_op && modifycode != NOP_EXPR)))
5587 warning_at (location, OPT_Wc___compat,
5588 "enum conversion in assignment is invalid in C++");
5591 /* If the lhs is atomic, remove that qualifier. */
5592 if (is_atomic_op)
5594 lhstype = build_qualified_type (lhstype,
5595 (TYPE_QUALS (lhstype)
5596 & ~TYPE_QUAL_ATOMIC));
5597 olhstype = build_qualified_type (olhstype,
5598 (TYPE_QUALS (lhstype)
5599 & ~TYPE_QUAL_ATOMIC));
5602 /* Convert new value to destination type. Fold it first, then
5603 restore any excess precision information, for the sake of
5604 conversion warnings. */
5606 if (!(is_atomic_op && modifycode != NOP_EXPR))
5608 npc = null_pointer_constant_p (newrhs);
5609 newrhs = c_fully_fold (newrhs, false, NULL);
5610 if (rhs_semantic_type)
5611 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5612 /* If the lhs is UPC 'shared' qualified, we drop the qualifier
5613 for the purposes of conversions from rhstype to lhstype.
5614 This will prevent the inadvertent creation of temporaries
5615 with "shared" asserted. */
5616 if (upc_shared_type_p (lhstype))
5617 lhstype = build_upc_unshared_type (lhstype);
5618 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5619 rhs_origtype, ic_assign, npc,
5620 NULL_TREE, NULL_TREE, 0);
5621 if (TREE_CODE (newrhs) == ERROR_MARK)
5622 return error_mark_node;
5625 /* Emit ObjC write barrier, if necessary. */
5626 if (c_dialect_objc () && flag_objc_gc)
5628 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5629 if (result)
5631 protected_set_expr_location (result, location);
5632 goto return_result;
5636 /* Scan operands. */
5638 if (is_atomic_op)
5639 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5640 else
5642 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5643 TREE_SIDE_EFFECTS (result) = 1;
5644 protected_set_expr_location (result, location);
5647 /* If we got the LHS in a different type for storing in,
5648 convert the result back to the nominal type of LHS
5649 so that the value we return always has the same type
5650 as the LHS argument. */
5652 if (olhstype == TREE_TYPE (result))
5653 goto return_result;
5655 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5656 rhs_origtype, ic_assign, false, NULL_TREE,
5657 NULL_TREE, 0);
5658 protected_set_expr_location (result, location);
5660 return_result:
5661 if (rhseval)
5662 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5663 return result;
5666 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5667 This is used to implement -fplan9-extensions. */
5669 static bool
5670 find_anonymous_field_with_type (tree struct_type, tree type)
5672 tree field;
5673 bool found;
5675 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5676 || TREE_CODE (struct_type) == UNION_TYPE);
5677 found = false;
5678 for (field = TYPE_FIELDS (struct_type);
5679 field != NULL_TREE;
5680 field = TREE_CHAIN (field))
5682 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5683 ? c_build_qualified_type (TREE_TYPE (field),
5684 TYPE_QUAL_ATOMIC)
5685 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5686 if (DECL_NAME (field) == NULL
5687 && comptypes (type, fieldtype))
5689 if (found)
5690 return false;
5691 found = true;
5693 else if (DECL_NAME (field) == NULL
5694 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5695 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5696 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5698 if (found)
5699 return false;
5700 found = true;
5703 return found;
5706 /* RHS is an expression whose type is pointer to struct. If there is
5707 an anonymous field in RHS with type TYPE, then return a pointer to
5708 that field in RHS. This is used with -fplan9-extensions. This
5709 returns NULL if no conversion could be found. */
5711 static tree
5712 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5714 tree rhs_struct_type, lhs_main_type;
5715 tree field, found_field;
5716 bool found_sub_field;
5717 tree ret;
5719 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5720 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5721 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5722 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5724 gcc_assert (POINTER_TYPE_P (type));
5725 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5726 ? c_build_qualified_type (TREE_TYPE (type),
5727 TYPE_QUAL_ATOMIC)
5728 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5730 found_field = NULL_TREE;
5731 found_sub_field = false;
5732 for (field = TYPE_FIELDS (rhs_struct_type);
5733 field != NULL_TREE;
5734 field = TREE_CHAIN (field))
5736 if (DECL_NAME (field) != NULL_TREE
5737 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5738 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5739 continue;
5740 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5741 ? c_build_qualified_type (TREE_TYPE (field),
5742 TYPE_QUAL_ATOMIC)
5743 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5744 if (comptypes (lhs_main_type, fieldtype))
5746 if (found_field != NULL_TREE)
5747 return NULL_TREE;
5748 found_field = field;
5750 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5751 lhs_main_type))
5753 if (found_field != NULL_TREE)
5754 return NULL_TREE;
5755 found_field = field;
5756 found_sub_field = true;
5760 if (found_field == NULL_TREE)
5761 return NULL_TREE;
5763 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5764 build_fold_indirect_ref (rhs), found_field,
5765 NULL_TREE);
5766 ret = build_fold_addr_expr_loc (location, ret);
5768 if (found_sub_field)
5770 ret = convert_to_anonymous_field (location, type, ret);
5771 gcc_assert (ret != NULL_TREE);
5774 return ret;
5777 /* Issue an error message for a bad initializer component.
5778 GMSGID identifies the message.
5779 The component name is taken from the spelling stack. */
5781 static void
5782 error_init (location_t loc, const char *gmsgid)
5784 char *ofwhat;
5786 /* The gmsgid may be a format string with %< and %>. */
5787 error_at (loc, gmsgid);
5788 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5789 if (*ofwhat)
5790 inform (loc, "(near initialization for %qs)", ofwhat);
5793 /* Issue a pedantic warning for a bad initializer component. OPT is
5794 the option OPT_* (from options.h) controlling this warning or 0 if
5795 it is unconditionally given. GMSGID identifies the message. The
5796 component name is taken from the spelling stack. */
5798 static void
5799 pedwarn_init (location_t location, int opt, const char *gmsgid)
5801 char *ofwhat;
5802 bool warned;
5804 /* The gmsgid may be a format string with %< and %>. */
5805 warned = pedwarn (location, opt, gmsgid);
5806 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5807 if (*ofwhat && warned)
5808 inform (location, "(near initialization for %qs)", ofwhat);
5811 /* Issue a warning for a bad initializer component.
5813 OPT is the OPT_W* value corresponding to the warning option that
5814 controls this warning. GMSGID identifies the message. The
5815 component name is taken from the spelling stack. */
5817 static void
5818 warning_init (location_t loc, int opt, const char *gmsgid)
5820 char *ofwhat;
5821 bool warned;
5823 /* The gmsgid may be a format string with %< and %>. */
5824 warned = warning_at (loc, opt, gmsgid);
5825 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5826 if (*ofwhat && warned)
5827 inform (loc, "(near initialization for %qs)", ofwhat);
5830 /* If TYPE is an array type and EXPR is a parenthesized string
5831 constant, warn if pedantic that EXPR is being used to initialize an
5832 object of type TYPE. */
5834 void
5835 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5837 if (pedantic
5838 && TREE_CODE (type) == ARRAY_TYPE
5839 && TREE_CODE (expr.value) == STRING_CST
5840 && expr.original_code != STRING_CST)
5841 pedwarn_init (loc, OPT_Wpedantic,
5842 "array initialized from parenthesized string constant");
5845 /* Convert value RHS to type TYPE as preparation for an assignment to
5846 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5847 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5848 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5849 constant before any folding.
5850 The real work of conversion is done by `convert'.
5851 The purpose of this function is to generate error messages
5852 for assignments that are not allowed in C.
5853 ERRTYPE says whether it is argument passing, assignment,
5854 initialization or return.
5856 LOCATION is the location of the assignment, EXPR_LOC is the location of
5857 the RHS or, for a function, location of an argument.
5858 FUNCTION is a tree for the function being called.
5859 PARMNUM is the number of the argument, for printing in error messages. */
5861 static tree
5862 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5863 tree rhs, tree origtype, enum impl_conv errtype,
5864 bool null_pointer_constant, tree fundecl,
5865 tree function, int parmnum)
5867 enum tree_code codel = TREE_CODE (type);
5868 tree orig_rhs = rhs;
5869 tree rhstype;
5870 enum tree_code coder;
5871 tree rname = NULL_TREE;
5872 bool objc_ok = false;
5874 if (errtype == ic_argpass)
5876 tree selector;
5877 /* Change pointer to function to the function itself for
5878 diagnostics. */
5879 if (TREE_CODE (function) == ADDR_EXPR
5880 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5881 function = TREE_OPERAND (function, 0);
5883 /* Handle an ObjC selector specially for diagnostics. */
5884 selector = objc_message_selector ();
5885 rname = function;
5886 if (selector && parmnum > 2)
5888 rname = selector;
5889 parmnum -= 2;
5893 /* This macro is used to emit diagnostics to ensure that all format
5894 strings are complete sentences, visible to gettext and checked at
5895 compile time. */
5896 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5897 do { \
5898 switch (errtype) \
5900 case ic_argpass: \
5901 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5902 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5903 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5904 "expected %qT but argument is of type %qT", \
5905 type, rhstype); \
5906 break; \
5907 case ic_assign: \
5908 pedwarn (LOCATION, OPT, AS); \
5909 break; \
5910 case ic_init: \
5911 pedwarn_init (LOCATION, OPT, IN); \
5912 break; \
5913 case ic_return: \
5914 pedwarn (LOCATION, OPT, RE); \
5915 break; \
5916 default: \
5917 gcc_unreachable (); \
5919 } while (0)
5921 /* Similar to WARN_FOR_ASSIGNMENT, but used to diagnose certain
5922 error conditions defined by the UPC language specification
5923 when converting between pointer-to-shared types and other types. */
5924 #define ERROR_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5925 do { \
5926 switch (errtype) \
5928 case ic_argpass: \
5929 error_at (LOCATION, AR, parmnum, rname); \
5930 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5931 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5932 "expected %qT but argument is of type %qT", \
5933 type, rhstype); \
5934 break; \
5935 case ic_assign: \
5936 error_at (LOCATION, AS); \
5937 break; \
5938 case ic_init: \
5939 error_at (LOCATION, IN); \
5940 break; \
5941 case ic_return: \
5942 error_at (LOCATION, RE); \
5943 break; \
5944 default: \
5945 gcc_unreachable (); \
5947 } while (0)
5949 /* This macro is used to emit diagnostics to ensure that all format
5950 strings are complete sentences, visible to gettext and checked at
5951 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5952 extra parameter to enumerate qualifiers. */
5953 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5954 do { \
5955 switch (errtype) \
5957 case ic_argpass: \
5958 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5959 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5960 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5961 "expected %qT but argument is of type %qT", \
5962 type, rhstype); \
5963 break; \
5964 case ic_assign: \
5965 pedwarn (LOCATION, OPT, AS, QUALS); \
5966 break; \
5967 case ic_init: \
5968 pedwarn (LOCATION, OPT, IN, QUALS); \
5969 break; \
5970 case ic_return: \
5971 pedwarn (LOCATION, OPT, RE, QUALS); \
5972 break; \
5973 default: \
5974 gcc_unreachable (); \
5976 } while (0)
5978 /* This macro is used to emit diagnostics to ensure that all format
5979 strings are complete sentences, visible to gettext and checked at
5980 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5981 warning_at instead of pedwarn. */
5982 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5983 do { \
5984 switch (errtype) \
5986 case ic_argpass: \
5987 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5988 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5989 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5990 "expected %qT but argument is of type %qT", \
5991 type, rhstype); \
5992 break; \
5993 case ic_assign: \
5994 warning_at (LOCATION, OPT, AS, QUALS); \
5995 break; \
5996 case ic_init: \
5997 warning_at (LOCATION, OPT, IN, QUALS); \
5998 break; \
5999 case ic_return: \
6000 warning_at (LOCATION, OPT, RE, QUALS); \
6001 break; \
6002 default: \
6003 gcc_unreachable (); \
6005 } while (0)
6007 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6008 rhs = TREE_OPERAND (rhs, 0);
6010 rhstype = TREE_TYPE (rhs);
6011 coder = TREE_CODE (rhstype);
6013 if (coder == ERROR_MARK)
6014 return error_mark_node;
6016 if (c_dialect_objc ())
6018 int parmno;
6020 switch (errtype)
6022 case ic_return:
6023 parmno = 0;
6024 break;
6026 case ic_assign:
6027 parmno = -1;
6028 break;
6030 case ic_init:
6031 parmno = -2;
6032 break;
6034 default:
6035 parmno = parmnum;
6036 break;
6039 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6042 if (warn_cxx_compat)
6044 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6045 if (checktype != error_mark_node
6046 && TREE_CODE (type) == ENUMERAL_TYPE
6047 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6049 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6050 G_("enum conversion when passing argument "
6051 "%d of %qE is invalid in C++"),
6052 G_("enum conversion in assignment is "
6053 "invalid in C++"),
6054 G_("enum conversion in initialization is "
6055 "invalid in C++"),
6056 G_("enum conversion in return is "
6057 "invalid in C++"));
6061 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6062 return rhs;
6064 if (coder == VOID_TYPE)
6066 /* Except for passing an argument to an unprototyped function,
6067 this is a constraint violation. When passing an argument to
6068 an unprototyped function, it is compile-time undefined;
6069 making it a constraint in that case was rejected in
6070 DR#252. */
6071 error_at (location, "void value not ignored as it ought to be");
6072 return error_mark_node;
6074 rhs = require_complete_type (rhs);
6075 if (rhs == error_mark_node)
6076 return error_mark_node;
6077 /* A non-reference type can convert to a reference. This handles
6078 va_start, va_copy and possibly port built-ins. */
6079 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6081 if (!lvalue_p (rhs))
6083 error_at (location, "cannot pass rvalue to reference parameter");
6084 return error_mark_node;
6086 if (!c_mark_addressable (rhs))
6087 return error_mark_node;
6088 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6089 SET_EXPR_LOCATION (rhs, location);
6091 rhs = convert_for_assignment (location, expr_loc,
6092 build_pointer_type (TREE_TYPE (type)),
6093 rhs, origtype, errtype,
6094 null_pointer_constant, fundecl, function,
6095 parmnum);
6096 if (rhs == error_mark_node)
6097 return error_mark_node;
6099 rhs = build1 (NOP_EXPR, type, rhs);
6100 SET_EXPR_LOCATION (rhs, location);
6101 return rhs;
6103 /* Some types can interconvert without explicit casts. */
6104 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6105 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6106 return convert (type, rhs);
6107 /* Arithmetic types all interconvert, and enum is treated like int. */
6108 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6109 || codel == FIXED_POINT_TYPE
6110 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6111 || codel == BOOLEAN_TYPE)
6112 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6113 || coder == FIXED_POINT_TYPE
6114 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6115 || coder == BOOLEAN_TYPE))
6117 tree ret;
6118 bool save = in_late_binary_op;
6119 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6120 || (coder == REAL_TYPE
6121 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6122 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6123 in_late_binary_op = true;
6124 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6125 ? expr_loc : location, type, orig_rhs);
6126 in_late_binary_op = save;
6127 return ret;
6130 /* Aggregates in different TUs might need conversion. */
6131 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6132 && codel == coder
6133 && comptypes (type, rhstype))
6134 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6135 ? expr_loc : location, type, rhs);
6137 /* Conversion to a transparent union or record from its member types.
6138 This applies only to function arguments. */
6139 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6140 && TYPE_TRANSPARENT_AGGR (type))
6141 && errtype == ic_argpass)
6143 tree memb, marginal_memb = NULL_TREE;
6145 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6147 tree memb_type = TREE_TYPE (memb);
6149 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6150 TYPE_MAIN_VARIANT (rhstype)))
6151 break;
6153 if (TREE_CODE (memb_type) != POINTER_TYPE)
6154 continue;
6156 if (coder == POINTER_TYPE)
6158 tree ttl = TREE_TYPE (memb_type);
6159 tree ttr = TREE_TYPE (rhstype);
6161 /* Any non-function converts to a [const][volatile] void *
6162 and vice versa; otherwise, targets must be the same.
6163 Meanwhile, the lhs target must have all the qualifiers of
6164 the rhs. */
6165 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6166 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6167 || comp_target_types (location, memb_type, rhstype))
6169 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6170 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6171 /* If this type won't generate any warnings, use it. */
6172 if (lquals == rquals
6173 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6174 && TREE_CODE (ttl) == FUNCTION_TYPE)
6175 ? ((lquals | rquals) == rquals)
6176 : ((lquals | rquals) == lquals)))
6177 break;
6179 /* Keep looking for a better type, but remember this one. */
6180 if (!marginal_memb)
6181 marginal_memb = memb;
6185 /* Can convert integer zero to any pointer type. */
6186 if (null_pointer_constant)
6188 tree ttl = TREE_TYPE (memb_type);
6189 rhs = !upc_shared_type_p (ttl)
6190 ? null_pointer_node : upc_null_pts_node;
6191 break;
6195 if (memb || marginal_memb)
6197 if (!memb)
6199 /* We have only a marginally acceptable member type;
6200 it needs a warning. */
6201 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6202 tree ttr = TREE_TYPE (rhstype);
6204 /* Const and volatile mean something different for function
6205 types, so the usual warnings are not appropriate. */
6206 if (TREE_CODE (ttr) == FUNCTION_TYPE
6207 && TREE_CODE (ttl) == FUNCTION_TYPE)
6209 /* Because const and volatile on functions are
6210 restrictions that say the function will not do
6211 certain things, it is okay to use a const or volatile
6212 function where an ordinary one is wanted, but not
6213 vice-versa. */
6214 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6215 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6216 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6217 OPT_Wdiscarded_qualifiers,
6218 G_("passing argument %d of %qE "
6219 "makes %q#v qualified function "
6220 "pointer from unqualified"),
6221 G_("assignment makes %q#v qualified "
6222 "function pointer from "
6223 "unqualified"),
6224 G_("initialization makes %q#v qualified "
6225 "function pointer from "
6226 "unqualified"),
6227 G_("return makes %q#v qualified function "
6228 "pointer from unqualified"),
6229 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6231 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6232 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6233 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6234 OPT_Wdiscarded_qualifiers,
6235 G_("passing argument %d of %qE discards "
6236 "%qv qualifier from pointer target type"),
6237 G_("assignment discards %qv qualifier "
6238 "from pointer target type"),
6239 G_("initialization discards %qv qualifier "
6240 "from pointer target type"),
6241 G_("return discards %qv qualifier from "
6242 "pointer target type"),
6243 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6245 memb = marginal_memb;
6248 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6249 pedwarn (location, OPT_Wpedantic,
6250 "ISO C prohibits argument conversion to union type");
6252 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6253 return build_constructor_single (type, memb, rhs);
6257 /* Conversions among pointers */
6258 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6259 && (coder == codel))
6261 tree ttl = TREE_TYPE (type);
6262 tree ttr = TREE_TYPE (rhstype);
6263 tree mvl = ttl;
6264 tree mvr = ttr;
6265 bool is_opaque_pointer;
6266 int target_cmp = 0; /* Cache comp_target_types () result. */
6267 addr_space_t asl;
6268 addr_space_t asr;
6270 if (TREE_CODE (mvl) != ARRAY_TYPE)
6271 mvl = (TYPE_ATOMIC (mvl)
6272 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6273 TYPE_QUAL_ATOMIC)
6274 : TYPE_MAIN_VARIANT (mvl));
6275 if (TREE_CODE (mvr) != ARRAY_TYPE)
6276 mvr = (TYPE_ATOMIC (mvr)
6277 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6278 TYPE_QUAL_ATOMIC)
6279 : TYPE_MAIN_VARIANT (mvr));
6280 if ((upc_shared_type_p (ttl) && !upc_shared_type_p (ttr))
6281 && !integer_zerop (rhs))
6283 error_at (location, "UPC does not allow assignments from a local pointer "
6284 "to a pointer-to-shared");
6285 return error_mark_node;
6287 if (!upc_shared_type_p (ttl) && upc_shared_type_p (ttr))
6289 if (upc_is_null_pts_p (rhs))
6291 return null_pointer_node;
6293 else
6295 error_at (location, "UPC does not allow assignments "
6296 "from a pointer-to-shared to a local pointer");
6297 return error_mark_node;
6300 if (upc_shared_type_p (ttl) && upc_shared_type_p (ttr) && (ttl != ttr)
6301 && !(VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)))
6303 const tree bs_l = upc_get_block_factor (ttl);
6304 const tree bs_r = upc_get_block_factor (ttr);
6305 /* Both source and destination are non-void pointers to shared,
6306 whose target types are not equal.
6307 UPC dictates that their blocking factors must be equal. */
6308 if (!tree_int_cst_equal (bs_l, bs_r))
6310 error_at (location, "UPC does not allow assignment "
6311 "between pointers to shared with "
6312 "differing block sizes without a cast");
6313 return error_mark_node;
6317 /* Opaque pointers are treated like void pointers. */
6318 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6320 /* The Plan 9 compiler permits a pointer to a struct to be
6321 automatically converted into a pointer to an anonymous field
6322 within the struct. */
6323 if (flag_plan9_extensions
6324 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6325 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6326 && mvl != mvr)
6328 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6329 if (new_rhs != NULL_TREE)
6331 rhs = new_rhs;
6332 rhstype = TREE_TYPE (rhs);
6333 coder = TREE_CODE (rhstype);
6334 ttr = TREE_TYPE (rhstype);
6335 mvr = TYPE_MAIN_VARIANT (ttr);
6339 /* C++ does not allow the implicit conversion void* -> T*. However,
6340 for the purpose of reducing the number of false positives, we
6341 tolerate the special case of
6343 int *p = NULL;
6345 where NULL is typically defined in C to be '(void *) 0'. */
6346 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6347 warning_at (errtype == ic_argpass ? expr_loc : location,
6348 OPT_Wc___compat,
6349 "request for implicit conversion "
6350 "from %qT to %qT not permitted in C++", rhstype, type);
6352 /* See if the pointers point to incompatible address spaces. */
6353 asl = TYPE_ADDR_SPACE (ttl);
6354 asr = TYPE_ADDR_SPACE (ttr);
6355 if (!null_pointer_constant_p (rhs)
6356 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6358 switch (errtype)
6360 case ic_argpass:
6361 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6362 "non-enclosed address space", parmnum, rname);
6363 break;
6364 case ic_assign:
6365 error_at (location, "assignment from pointer to "
6366 "non-enclosed address space");
6367 break;
6368 case ic_init:
6369 error_at (location, "initialization from pointer to "
6370 "non-enclosed address space");
6371 break;
6372 case ic_return:
6373 error_at (location, "return from pointer to "
6374 "non-enclosed address space");
6375 break;
6376 default:
6377 gcc_unreachable ();
6379 return error_mark_node;
6382 /* Check if the right-hand side has a format attribute but the
6383 left-hand side doesn't. */
6384 if (warn_suggest_attribute_format
6385 && check_missing_format_attribute (type, rhstype))
6387 switch (errtype)
6389 case ic_argpass:
6390 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6391 "argument %d of %qE might be "
6392 "a candidate for a format attribute",
6393 parmnum, rname);
6394 break;
6395 case ic_assign:
6396 warning_at (location, OPT_Wsuggest_attribute_format,
6397 "assignment left-hand side might be "
6398 "a candidate for a format attribute");
6399 break;
6400 case ic_init:
6401 warning_at (location, OPT_Wsuggest_attribute_format,
6402 "initialization left-hand side might be "
6403 "a candidate for a format attribute");
6404 break;
6405 case ic_return:
6406 warning_at (location, OPT_Wsuggest_attribute_format,
6407 "return type might be "
6408 "a candidate for a format attribute");
6409 break;
6410 default:
6411 gcc_unreachable ();
6415 /* Any non-function converts to a [const][volatile] void *
6416 and vice versa; otherwise, targets must be the same.
6417 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6418 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6419 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6420 || (target_cmp = comp_target_types (location, type, rhstype))
6421 || is_opaque_pointer
6422 || ((c_common_unsigned_type (mvl)
6423 == c_common_unsigned_type (mvr))
6424 && (c_common_signed_type (mvl)
6425 == c_common_signed_type (mvr))
6426 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6428 /* Warn about loss of qualifers from pointers to arrays with
6429 qualifiers on the element type. */
6430 if (TREE_CODE (ttr) == ARRAY_TYPE)
6432 ttr = strip_array_types (ttr);
6433 ttl = strip_array_types (ttl);
6435 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6436 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6437 WARNING_FOR_QUALIFIERS (location, expr_loc,
6438 OPT_Wdiscarded_array_qualifiers,
6439 G_("passing argument %d of %qE discards "
6440 "%qv qualifier from pointer target type"),
6441 G_("assignment discards %qv qualifier "
6442 "from pointer target type"),
6443 G_("initialization discards %qv qualifier "
6444 "from pointer target type"),
6445 G_("return discards %qv qualifier from "
6446 "pointer target type"),
6447 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6449 else if (pedantic
6450 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6452 (VOID_TYPE_P (ttr)
6453 && !null_pointer_constant
6454 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6455 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6456 G_("ISO C forbids passing argument %d of "
6457 "%qE between function pointer "
6458 "and %<void *%>"),
6459 G_("ISO C forbids assignment between "
6460 "function pointer and %<void *%>"),
6461 G_("ISO C forbids initialization between "
6462 "function pointer and %<void *%>"),
6463 G_("ISO C forbids return between function "
6464 "pointer and %<void *%>"));
6465 /* Const and volatile mean something different for function types,
6466 so the usual warnings are not appropriate. */
6467 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6468 && TREE_CODE (ttl) != FUNCTION_TYPE)
6470 /* Don't warn about loss of qualifier for conversions from
6471 qualified void* to pointers to arrays with corresponding
6472 qualifier on the element type. */
6473 if (!pedantic)
6474 ttl = strip_array_types (ttl);
6476 /* Assignments between atomic and non-atomic objects are OK. */
6477 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6478 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6480 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6481 OPT_Wdiscarded_qualifiers,
6482 G_("passing argument %d of %qE discards "
6483 "%qv qualifier from pointer target type"),
6484 G_("assignment discards %qv qualifier "
6485 "from pointer target type"),
6486 G_("initialization discards %qv qualifier "
6487 "from pointer target type"),
6488 G_("return discards %qv qualifier from "
6489 "pointer target type"),
6490 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6492 /* If this is not a case of ignoring a mismatch in signedness,
6493 no warning. */
6494 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6495 || target_cmp)
6497 /* If there is a mismatch, do warn. */
6498 else if (warn_pointer_sign)
6499 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6500 G_("pointer targets in passing argument "
6501 "%d of %qE differ in signedness"),
6502 G_("pointer targets in assignment "
6503 "differ in signedness"),
6504 G_("pointer targets in initialization "
6505 "differ in signedness"),
6506 G_("pointer targets in return differ "
6507 "in signedness"));
6509 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6510 && TREE_CODE (ttr) == FUNCTION_TYPE)
6512 /* Because const and volatile on functions are restrictions
6513 that say the function will not do certain things,
6514 it is okay to use a const or volatile function
6515 where an ordinary one is wanted, but not vice-versa. */
6516 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6517 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6518 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6519 OPT_Wdiscarded_qualifiers,
6520 G_("passing argument %d of %qE makes "
6521 "%q#v qualified function pointer "
6522 "from unqualified"),
6523 G_("assignment makes %q#v qualified function "
6524 "pointer from unqualified"),
6525 G_("initialization makes %q#v qualified "
6526 "function pointer from unqualified"),
6527 G_("return makes %q#v qualified function "
6528 "pointer from unqualified"),
6529 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6532 else
6533 /* Avoid warning about the volatile ObjC EH puts on decls. */
6534 if (!objc_ok)
6535 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6536 OPT_Wincompatible_pointer_types,
6537 G_("passing argument %d of %qE from "
6538 "incompatible pointer type"),
6539 G_("assignment from incompatible pointer type"),
6540 G_("initialization from incompatible "
6541 "pointer type"),
6542 G_("return from incompatible pointer type"));
6544 return convert (type, rhs);
6546 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6548 /* ??? This should not be an error when inlining calls to
6549 unprototyped functions. */
6550 error_at (location, "invalid use of non-lvalue array");
6551 return error_mark_node;
6553 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6555 if (!null_pointer_constant && upc_shared_type_p (TREE_TYPE (type)))
6557 ERROR_FOR_ASSIGNMENT (location, 0,
6558 G_("passing argument %d of %qE attempts to make "
6559 "a UPC pointer-to-shared value from an integer"),
6560 G_("assignment attempts to make a UPC pointer-to-shared "
6561 "value from an integer"),
6562 G_("initialization attempts to make a UPC pointer-to-shared "
6563 "value from an integer without a cast"),
6564 G_("return makes a UPC pointer-to-shared value from an "
6565 "integer"));
6566 return error_mark_node;
6568 /* An explicit constant 0 can convert to a pointer,
6569 or one that results from arithmetic, even including
6570 a cast to integer type. */
6571 if (!null_pointer_constant)
6572 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6573 OPT_Wint_conversion,
6574 G_("passing argument %d of %qE makes "
6575 "pointer from integer without a cast"),
6576 G_("assignment makes pointer from integer "
6577 "without a cast"),
6578 G_("initialization makes pointer from "
6579 "integer without a cast"),
6580 G_("return makes pointer from integer "
6581 "without a cast"));
6583 return convert (type, rhs);
6585 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6587 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6588 OPT_Wint_conversion,
6589 G_("passing argument %d of %qE makes integer "
6590 "from pointer without a cast"),
6591 G_("assignment makes integer from pointer "
6592 "without a cast"),
6593 G_("initialization makes integer from pointer "
6594 "without a cast"),
6595 G_("return makes integer from pointer "
6596 "without a cast"));
6597 return convert (type, rhs);
6599 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6601 tree ret;
6602 bool save = in_late_binary_op;
6603 in_late_binary_op = true;
6604 ret = convert (type, rhs);
6605 in_late_binary_op = save;
6606 return ret;
6609 switch (errtype)
6611 case ic_argpass:
6612 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6613 rname);
6614 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6615 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6616 "expected %qT but argument is of type %qT", type, rhstype);
6617 break;
6618 case ic_assign:
6619 error_at (location, "incompatible types when assigning to type %qT from "
6620 "type %qT", type, rhstype);
6621 break;
6622 case ic_init:
6623 error_at (location,
6624 "incompatible types when initializing type %qT using type %qT",
6625 type, rhstype);
6626 break;
6627 case ic_return:
6628 error_at (location,
6629 "incompatible types when returning type %qT but %qT was "
6630 "expected", rhstype, type);
6631 break;
6632 default:
6633 gcc_unreachable ();
6636 return error_mark_node;
6639 /* If VALUE is a compound expr all of whose expressions are constant, then
6640 return its value. Otherwise, return error_mark_node.
6642 This is for handling COMPOUND_EXPRs as initializer elements
6643 which is allowed with a warning when -pedantic is specified. */
6645 static tree
6646 valid_compound_expr_initializer (tree value, tree endtype)
6648 if (TREE_CODE (value) == COMPOUND_EXPR)
6650 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6651 == error_mark_node)
6652 return error_mark_node;
6653 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6654 endtype);
6656 else if (!initializer_constant_valid_p (value, endtype))
6657 return error_mark_node;
6658 else
6659 return value;
6662 /* Perform appropriate conversions on the initial value of a variable,
6663 store it in the declaration DECL,
6664 and print any error messages that are appropriate.
6665 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6666 If the init is invalid, store an ERROR_MARK.
6668 INIT_LOC is the location of the initial value. */
6670 void
6671 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6673 const bool npc = init && null_pointer_constant_p (init);
6674 const bool is_upc_decl_init = upc_check_decl_init (decl, init);
6675 const bool require_constant = TREE_STATIC (decl) && !is_upc_decl_init;
6676 tree type = TREE_TYPE (decl);
6677 tree value;
6679 /* If variable's type was invalidly declared, just ignore it. */
6681 if (TREE_CODE (type) == ERROR_MARK)
6682 return;
6684 /* Digest the specified initializer into an expression. */
6686 value = digest_init (init_loc, type, init, origtype, npc,
6687 true, require_constant);
6689 /* UPC cannot initialize certain values at compile time.
6690 For example, the address of a UPC 'shared' variable must
6691 be evaluated at runtime. */
6693 if (is_upc_decl_init)
6695 upc_decl_init (decl, value);
6696 return;
6699 /* Store the expression if valid; else report error. */
6701 if (!in_system_header_at (input_location)
6702 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6703 warning (OPT_Wtraditional, "traditional C rejects automatic "
6704 "aggregate initialization");
6706 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6707 DECL_INITIAL (decl) = value;
6709 /* ANSI wants warnings about out-of-range constant initializers. */
6710 STRIP_TYPE_NOPS (value);
6711 if (TREE_STATIC (decl))
6712 constant_expression_warning (value);
6714 /* Check if we need to set array size from compound literal size. */
6715 if (TREE_CODE (type) == ARRAY_TYPE
6716 && TYPE_DOMAIN (type) == 0
6717 && value != error_mark_node)
6719 tree inside_init = init;
6721 STRIP_TYPE_NOPS (inside_init);
6722 inside_init = fold (inside_init);
6724 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6726 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6728 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6730 /* For int foo[] = (int [3]){1}; we need to set array size
6731 now since later on array initializer will be just the
6732 brace enclosed list of the compound literal. */
6733 tree etype = strip_array_types (TREE_TYPE (decl));
6734 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6735 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6736 layout_type (type);
6737 layout_decl (cldecl, 0);
6738 TREE_TYPE (decl)
6739 = c_build_qualified_type (type, TYPE_QUALS (etype));
6745 /* Methods for storing and printing names for error messages. */
6747 /* Implement a spelling stack that allows components of a name to be pushed
6748 and popped. Each element on the stack is this structure. */
6750 struct spelling
6752 int kind;
6753 union
6755 unsigned HOST_WIDE_INT i;
6756 const char *s;
6757 } u;
6760 #define SPELLING_STRING 1
6761 #define SPELLING_MEMBER 2
6762 #define SPELLING_BOUNDS 3
6764 static struct spelling *spelling; /* Next stack element (unused). */
6765 static struct spelling *spelling_base; /* Spelling stack base. */
6766 static int spelling_size; /* Size of the spelling stack. */
6768 /* Macros to save and restore the spelling stack around push_... functions.
6769 Alternative to SAVE_SPELLING_STACK. */
6771 #define SPELLING_DEPTH() (spelling - spelling_base)
6772 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6774 /* Push an element on the spelling stack with type KIND and assign VALUE
6775 to MEMBER. */
6777 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6779 int depth = SPELLING_DEPTH (); \
6781 if (depth >= spelling_size) \
6783 spelling_size += 10; \
6784 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6785 spelling_size); \
6786 RESTORE_SPELLING_DEPTH (depth); \
6789 spelling->kind = (KIND); \
6790 spelling->MEMBER = (VALUE); \
6791 spelling++; \
6794 /* Push STRING on the stack. Printed literally. */
6796 static void
6797 push_string (const char *string)
6799 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6802 /* Push a member name on the stack. Printed as '.' STRING. */
6804 static void
6805 push_member_name (tree decl)
6807 const char *const string
6808 = (DECL_NAME (decl)
6809 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6810 : _("<anonymous>"));
6811 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6814 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6816 static void
6817 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6819 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6822 /* Compute the maximum size in bytes of the printed spelling. */
6824 static int
6825 spelling_length (void)
6827 int size = 0;
6828 struct spelling *p;
6830 for (p = spelling_base; p < spelling; p++)
6832 if (p->kind == SPELLING_BOUNDS)
6833 size += 25;
6834 else
6835 size += strlen (p->u.s) + 1;
6838 return size;
6841 /* Print the spelling to BUFFER and return it. */
6843 static char *
6844 print_spelling (char *buffer)
6846 char *d = buffer;
6847 struct spelling *p;
6849 for (p = spelling_base; p < spelling; p++)
6850 if (p->kind == SPELLING_BOUNDS)
6852 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6853 d += strlen (d);
6855 else
6857 const char *s;
6858 if (p->kind == SPELLING_MEMBER)
6859 *d++ = '.';
6860 for (s = p->u.s; (*d = *s++); d++)
6863 *d++ = '\0';
6864 return buffer;
6867 /* Digest the parser output INIT as an initializer for type TYPE.
6868 Return a C expression of type TYPE to represent the initial value.
6870 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6872 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6874 If INIT is a string constant, STRICT_STRING is true if it is
6875 unparenthesized or we should not warn here for it being parenthesized.
6876 For other types of INIT, STRICT_STRING is not used.
6878 INIT_LOC is the location of the INIT.
6880 REQUIRE_CONSTANT requests an error if non-constant initializers or
6881 elements are seen. */
6883 static tree
6884 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6885 bool null_pointer_constant, bool strict_string,
6886 int require_constant)
6888 enum tree_code code = TREE_CODE (type);
6889 tree inside_init = init;
6890 tree semantic_type = NULL_TREE;
6891 bool maybe_const = true;
6893 if (type == error_mark_node
6894 || !init
6895 || error_operand_p (init))
6896 return error_mark_node;
6898 STRIP_TYPE_NOPS (inside_init);
6900 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6902 semantic_type = TREE_TYPE (inside_init);
6903 inside_init = TREE_OPERAND (inside_init, 0);
6905 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6906 inside_init = decl_constant_value_for_optimization (inside_init);
6908 /* Initialization of an array of chars from a string constant
6909 optionally enclosed in braces. */
6911 if (code == ARRAY_TYPE && inside_init
6912 && TREE_CODE (inside_init) == STRING_CST)
6914 tree typ1
6915 = (TYPE_ATOMIC (TREE_TYPE (type))
6916 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6917 TYPE_QUAL_ATOMIC)
6918 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6919 /* Note that an array could be both an array of character type
6920 and an array of wchar_t if wchar_t is signed char or unsigned
6921 char. */
6922 bool char_array = (typ1 == char_type_node
6923 || typ1 == signed_char_type_node
6924 || typ1 == unsigned_char_type_node);
6925 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6926 bool char16_array = !!comptypes (typ1, char16_type_node);
6927 bool char32_array = !!comptypes (typ1, char32_type_node);
6929 if (char_array || wchar_array || char16_array || char32_array)
6931 struct c_expr expr;
6932 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6933 expr.value = inside_init;
6934 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6935 expr.original_type = NULL;
6936 maybe_warn_string_init (init_loc, type, expr);
6938 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6939 pedwarn_init (init_loc, OPT_Wpedantic,
6940 "initialization of a flexible array member");
6942 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6943 TYPE_MAIN_VARIANT (type)))
6944 return inside_init;
6946 if (char_array)
6948 if (typ2 != char_type_node)
6950 error_init (init_loc, "char-array initialized from wide "
6951 "string");
6952 return error_mark_node;
6955 else
6957 if (typ2 == char_type_node)
6959 error_init (init_loc, "wide character array initialized "
6960 "from non-wide string");
6961 return error_mark_node;
6963 else if (!comptypes(typ1, typ2))
6965 error_init (init_loc, "wide character array initialized "
6966 "from incompatible wide string");
6967 return error_mark_node;
6971 TREE_TYPE (inside_init) = type;
6972 if (TYPE_DOMAIN (type) != 0
6973 && TYPE_SIZE (type) != 0
6974 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6976 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6978 /* Subtract the size of a single (possibly wide) character
6979 because it's ok to ignore the terminating null char
6980 that is counted in the length of the constant. */
6981 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6982 (len
6983 - (TYPE_PRECISION (typ1)
6984 / BITS_PER_UNIT))))
6985 pedwarn_init (init_loc, 0,
6986 ("initializer-string for array of chars "
6987 "is too long"));
6988 else if (warn_cxx_compat
6989 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6990 warning_at (init_loc, OPT_Wc___compat,
6991 ("initializer-string for array chars "
6992 "is too long for C++"));
6995 return inside_init;
6997 else if (INTEGRAL_TYPE_P (typ1))
6999 error_init (init_loc, "array of inappropriate type initialized "
7000 "from string constant");
7001 return error_mark_node;
7005 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7006 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7007 below and handle as a constructor. */
7008 if (code == VECTOR_TYPE
7009 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
7010 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7011 && TREE_CONSTANT (inside_init))
7013 if (TREE_CODE (inside_init) == VECTOR_CST
7014 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7015 TYPE_MAIN_VARIANT (type)))
7016 return inside_init;
7018 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7020 unsigned HOST_WIDE_INT ix;
7021 tree value;
7022 bool constant_p = true;
7024 /* Iterate through elements and check if all constructor
7025 elements are *_CSTs. */
7026 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7027 if (!CONSTANT_CLASS_P (value))
7029 constant_p = false;
7030 break;
7033 if (constant_p)
7034 return build_vector_from_ctor (type,
7035 CONSTRUCTOR_ELTS (inside_init));
7039 if (warn_sequence_point)
7040 verify_sequence_points (inside_init);
7042 /* Any type can be initialized
7043 from an expression of the same type, optionally with braces. */
7045 if (inside_init && TREE_TYPE (inside_init) != 0
7046 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7047 TYPE_MAIN_VARIANT (type))
7048 || (code == ARRAY_TYPE
7049 && comptypes (TREE_TYPE (inside_init), type))
7050 || (code == VECTOR_TYPE
7051 && comptypes (TREE_TYPE (inside_init), type))
7052 || (code == POINTER_TYPE
7053 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7054 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7055 TREE_TYPE (type)))))
7057 if (code == POINTER_TYPE)
7059 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7061 if (TREE_CODE (inside_init) == STRING_CST
7062 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7063 inside_init = array_to_pointer_conversion
7064 (init_loc, inside_init);
7065 else
7067 error_init (init_loc, "invalid use of non-lvalue array");
7068 return error_mark_node;
7073 if (code == VECTOR_TYPE)
7074 /* Although the types are compatible, we may require a
7075 conversion. */
7076 inside_init = convert (type, inside_init);
7078 if (require_constant
7079 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7081 /* As an extension, allow initializing objects with static storage
7082 duration with compound literals (which are then treated just as
7083 the brace enclosed list they contain). Also allow this for
7084 vectors, as we can only assign them with compound literals. */
7085 if (flag_isoc99 && code != VECTOR_TYPE)
7086 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7087 "is not constant");
7088 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7089 inside_init = DECL_INITIAL (decl);
7092 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7093 && TREE_CODE (inside_init) != CONSTRUCTOR)
7095 error_init (init_loc, "array initialized from non-constant array "
7096 "expression");
7097 return error_mark_node;
7100 /* Compound expressions can only occur here if -Wpedantic or
7101 -pedantic-errors is specified. In the later case, we always want
7102 an error. In the former case, we simply want a warning. */
7103 if (require_constant && pedantic
7104 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7106 inside_init
7107 = valid_compound_expr_initializer (inside_init,
7108 TREE_TYPE (inside_init));
7109 if (inside_init == error_mark_node)
7110 error_init (init_loc, "initializer element is not constant");
7111 else
7112 pedwarn_init (init_loc, OPT_Wpedantic,
7113 "initializer element is not constant");
7114 if (flag_pedantic_errors)
7115 inside_init = error_mark_node;
7117 else if (require_constant
7118 && !initializer_constant_valid_p (inside_init,
7119 TREE_TYPE (inside_init)))
7121 error_init (init_loc, "initializer element is not constant");
7122 inside_init = error_mark_node;
7124 else if (require_constant && !maybe_const)
7125 pedwarn_init (init_loc, 0,
7126 "initializer element is not a constant expression");
7128 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7129 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7130 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7131 type, inside_init, origtype,
7132 ic_init, null_pointer_constant,
7133 NULL_TREE, NULL_TREE, 0);
7134 return inside_init;
7137 /* Handle scalar types, including conversions. */
7139 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7140 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7141 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7143 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7144 && (TREE_CODE (init) == STRING_CST
7145 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7146 inside_init = init = array_to_pointer_conversion (init_loc, init);
7147 if (semantic_type)
7148 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7149 inside_init);
7150 inside_init
7151 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7152 inside_init, origtype, ic_init,
7153 null_pointer_constant, NULL_TREE, NULL_TREE,
7156 /* Check to see if we have already given an error message. */
7157 if (inside_init == error_mark_node)
7159 else if (require_constant && !TREE_CONSTANT (inside_init))
7161 error_init (init_loc, "initializer element is not constant");
7162 inside_init = error_mark_node;
7164 else if (require_constant
7165 && !initializer_constant_valid_p (inside_init,
7166 TREE_TYPE (inside_init)))
7168 error_init (init_loc, "initializer element is not computable at "
7169 "load time");
7170 inside_init = error_mark_node;
7172 else if (require_constant && !maybe_const)
7173 pedwarn_init (init_loc, 0,
7174 "initializer element is not a constant expression");
7176 return inside_init;
7179 /* Come here only for records and arrays. */
7181 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7183 error_init (init_loc, "variable-sized object may not be initialized");
7184 return error_mark_node;
7187 error_init (init_loc, "invalid initializer");
7188 return error_mark_node;
7191 /* Handle initializers that use braces. */
7193 /* Type of object we are accumulating a constructor for.
7194 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7195 static tree constructor_type;
7197 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7198 left to fill. */
7199 static tree constructor_fields;
7201 /* For an ARRAY_TYPE, this is the specified index
7202 at which to store the next element we get. */
7203 static tree constructor_index;
7205 /* For an ARRAY_TYPE, this is the maximum index. */
7206 static tree constructor_max_index;
7208 /* For a RECORD_TYPE, this is the first field not yet written out. */
7209 static tree constructor_unfilled_fields;
7211 /* For an ARRAY_TYPE, this is the index of the first element
7212 not yet written out. */
7213 static tree constructor_unfilled_index;
7215 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7216 This is so we can generate gaps between fields, when appropriate. */
7217 static tree constructor_bit_index;
7219 /* If we are saving up the elements rather than allocating them,
7220 this is the list of elements so far (in reverse order,
7221 most recent first). */
7222 static vec<constructor_elt, va_gc> *constructor_elements;
7224 /* 1 if constructor should be incrementally stored into a constructor chain,
7225 0 if all the elements should be kept in AVL tree. */
7226 static int constructor_incremental;
7228 /* 1 if so far this constructor's elements are all compile-time constants. */
7229 static int constructor_constant;
7231 /* 1 if so far this constructor's elements are all valid address constants. */
7232 static int constructor_simple;
7234 /* 1 if this constructor has an element that cannot be part of a
7235 constant expression. */
7236 static int constructor_nonconst;
7238 /* 1 if this constructor is erroneous so far. */
7239 static int constructor_erroneous;
7241 /* 1 if this constructor is the universal zero initializer { 0 }. */
7242 static int constructor_zeroinit;
7244 /* Structure for managing pending initializer elements, organized as an
7245 AVL tree. */
7247 struct init_node
7249 struct init_node *left, *right;
7250 struct init_node *parent;
7251 int balance;
7252 tree purpose;
7253 tree value;
7254 tree origtype;
7257 /* Tree of pending elements at this constructor level.
7258 These are elements encountered out of order
7259 which belong at places we haven't reached yet in actually
7260 writing the output.
7261 Will never hold tree nodes across GC runs. */
7262 static struct init_node *constructor_pending_elts;
7264 /* The SPELLING_DEPTH of this constructor. */
7265 static int constructor_depth;
7267 /* DECL node for which an initializer is being read.
7268 0 means we are reading a constructor expression
7269 such as (struct foo) {...}. */
7270 static tree constructor_decl;
7272 /* Nonzero if this is an initializer for a top-level decl. */
7273 static int constructor_top_level;
7275 /* Nonzero if there were any member designators in this initializer. */
7276 static int constructor_designated;
7278 /* Nesting depth of designator list. */
7279 static int designator_depth;
7281 /* Nonzero if there were diagnosed errors in this designator list. */
7282 static int designator_erroneous;
7285 /* This stack has a level for each implicit or explicit level of
7286 structuring in the initializer, including the outermost one. It
7287 saves the values of most of the variables above. */
7289 struct constructor_range_stack;
7291 struct constructor_stack
7293 struct constructor_stack *next;
7294 tree type;
7295 tree fields;
7296 tree index;
7297 tree max_index;
7298 tree unfilled_index;
7299 tree unfilled_fields;
7300 tree bit_index;
7301 vec<constructor_elt, va_gc> *elements;
7302 struct init_node *pending_elts;
7303 int offset;
7304 int depth;
7305 /* If value nonzero, this value should replace the entire
7306 constructor at this level. */
7307 struct c_expr replacement_value;
7308 struct constructor_range_stack *range_stack;
7309 char constant;
7310 char simple;
7311 char nonconst;
7312 char implicit;
7313 char erroneous;
7314 char outer;
7315 char incremental;
7316 char designated;
7317 int designator_depth;
7320 static struct constructor_stack *constructor_stack;
7322 /* This stack represents designators from some range designator up to
7323 the last designator in the list. */
7325 struct constructor_range_stack
7327 struct constructor_range_stack *next, *prev;
7328 struct constructor_stack *stack;
7329 tree range_start;
7330 tree index;
7331 tree range_end;
7332 tree fields;
7335 static struct constructor_range_stack *constructor_range_stack;
7337 /* This stack records separate initializers that are nested.
7338 Nested initializers can't happen in ANSI C, but GNU C allows them
7339 in cases like { ... (struct foo) { ... } ... }. */
7341 struct initializer_stack
7343 struct initializer_stack *next;
7344 tree decl;
7345 struct constructor_stack *constructor_stack;
7346 struct constructor_range_stack *constructor_range_stack;
7347 vec<constructor_elt, va_gc> *elements;
7348 struct spelling *spelling;
7349 struct spelling *spelling_base;
7350 int spelling_size;
7351 char top_level;
7352 char require_constant_value;
7353 char require_constant_elements;
7356 static struct initializer_stack *initializer_stack;
7358 /* Prepare to parse and output the initializer for variable DECL. */
7360 void
7361 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7363 const char *locus;
7364 struct initializer_stack *p = XNEW (struct initializer_stack);
7366 p->decl = constructor_decl;
7367 p->require_constant_value = require_constant_value;
7368 p->require_constant_elements = require_constant_elements;
7369 p->constructor_stack = constructor_stack;
7370 p->constructor_range_stack = constructor_range_stack;
7371 p->elements = constructor_elements;
7372 p->spelling = spelling;
7373 p->spelling_base = spelling_base;
7374 p->spelling_size = spelling_size;
7375 p->top_level = constructor_top_level;
7376 p->next = initializer_stack;
7377 initializer_stack = p;
7379 constructor_decl = decl;
7380 constructor_designated = 0;
7381 constructor_top_level = top_level;
7383 if (decl != 0 && decl != error_mark_node)
7385 require_constant_value = TREE_STATIC (decl);
7386 require_constant_elements
7387 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7388 /* For a scalar, you can always use any value to initialize,
7389 even within braces. */
7390 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7391 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7392 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7393 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7394 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7396 else
7398 require_constant_value = 0;
7399 require_constant_elements = 0;
7400 locus = _("(anonymous)");
7403 constructor_stack = 0;
7404 constructor_range_stack = 0;
7406 found_missing_braces = 0;
7408 spelling_base = 0;
7409 spelling_size = 0;
7410 RESTORE_SPELLING_DEPTH (0);
7412 if (locus)
7413 push_string (locus);
7416 void
7417 finish_init (void)
7419 struct initializer_stack *p = initializer_stack;
7421 /* Free the whole constructor stack of this initializer. */
7422 while (constructor_stack)
7424 struct constructor_stack *q = constructor_stack;
7425 constructor_stack = q->next;
7426 free (q);
7429 gcc_assert (!constructor_range_stack);
7431 /* Pop back to the data of the outer initializer (if any). */
7432 free (spelling_base);
7434 constructor_decl = p->decl;
7435 require_constant_value = p->require_constant_value;
7436 require_constant_elements = p->require_constant_elements;
7437 constructor_stack = p->constructor_stack;
7438 constructor_range_stack = p->constructor_range_stack;
7439 constructor_elements = p->elements;
7440 spelling = p->spelling;
7441 spelling_base = p->spelling_base;
7442 spelling_size = p->spelling_size;
7443 constructor_top_level = p->top_level;
7444 initializer_stack = p->next;
7445 free (p);
7448 /* Call here when we see the initializer is surrounded by braces.
7449 This is instead of a call to push_init_level;
7450 it is matched by a call to pop_init_level.
7452 TYPE is the type to initialize, for a constructor expression.
7453 For an initializer for a decl, TYPE is zero. */
7455 void
7456 really_start_incremental_init (tree type)
7458 struct constructor_stack *p = XNEW (struct constructor_stack);
7460 if (type == 0)
7461 type = TREE_TYPE (constructor_decl);
7463 if (TREE_CODE (type) == VECTOR_TYPE
7464 && TYPE_VECTOR_OPAQUE (type))
7465 error ("opaque vector types cannot be initialized");
7467 p->type = constructor_type;
7468 p->fields = constructor_fields;
7469 p->index = constructor_index;
7470 p->max_index = constructor_max_index;
7471 p->unfilled_index = constructor_unfilled_index;
7472 p->unfilled_fields = constructor_unfilled_fields;
7473 p->bit_index = constructor_bit_index;
7474 p->elements = constructor_elements;
7475 p->constant = constructor_constant;
7476 p->simple = constructor_simple;
7477 p->nonconst = constructor_nonconst;
7478 p->erroneous = constructor_erroneous;
7479 p->pending_elts = constructor_pending_elts;
7480 p->depth = constructor_depth;
7481 p->replacement_value.value = 0;
7482 p->replacement_value.original_code = ERROR_MARK;
7483 p->replacement_value.original_type = NULL;
7484 p->implicit = 0;
7485 p->range_stack = 0;
7486 p->outer = 0;
7487 p->incremental = constructor_incremental;
7488 p->designated = constructor_designated;
7489 p->designator_depth = designator_depth;
7490 p->next = 0;
7491 constructor_stack = p;
7493 constructor_constant = 1;
7494 constructor_simple = 1;
7495 constructor_nonconst = 0;
7496 constructor_depth = SPELLING_DEPTH ();
7497 constructor_elements = NULL;
7498 constructor_pending_elts = 0;
7499 constructor_type = type;
7500 constructor_incremental = 1;
7501 constructor_designated = 0;
7502 constructor_zeroinit = 1;
7503 designator_depth = 0;
7504 designator_erroneous = 0;
7506 /* The result of the constructor must not be UPC shared qualified */
7507 if (upc_shared_type_p (constructor_type))
7508 constructor_type = build_upc_unshared_type (constructor_type);
7509 if (TREE_CODE (constructor_type) == RECORD_TYPE
7510 || TREE_CODE (constructor_type) == UNION_TYPE)
7512 constructor_fields = TYPE_FIELDS (constructor_type);
7513 /* Skip any nameless bit fields at the beginning. */
7514 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7515 && DECL_NAME (constructor_fields) == 0)
7516 constructor_fields = DECL_CHAIN (constructor_fields);
7518 constructor_unfilled_fields = constructor_fields;
7519 constructor_bit_index = bitsize_zero_node;
7521 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7523 if (TYPE_DOMAIN (constructor_type))
7525 constructor_max_index
7526 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7528 /* Detect non-empty initializations of zero-length arrays. */
7529 if (constructor_max_index == NULL_TREE
7530 && TYPE_SIZE (constructor_type))
7531 constructor_max_index = integer_minus_one_node;
7533 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7534 to initialize VLAs will cause a proper error; avoid tree
7535 checking errors as well by setting a safe value. */
7536 if (constructor_max_index
7537 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7538 constructor_max_index = integer_minus_one_node;
7540 constructor_index
7541 = convert (bitsizetype,
7542 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7544 else
7546 constructor_index = bitsize_zero_node;
7547 constructor_max_index = NULL_TREE;
7550 constructor_unfilled_index = constructor_index;
7552 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7554 /* Vectors are like simple fixed-size arrays. */
7555 constructor_max_index =
7556 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7557 constructor_index = bitsize_zero_node;
7558 constructor_unfilled_index = constructor_index;
7560 else
7562 /* Handle the case of int x = {5}; */
7563 constructor_fields = constructor_type;
7564 constructor_unfilled_fields = constructor_type;
7568 /* Push down into a subobject, for initialization.
7569 If this is for an explicit set of braces, IMPLICIT is 0.
7570 If it is because the next element belongs at a lower level,
7571 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7573 void
7574 push_init_level (location_t loc, int implicit,
7575 struct obstack *braced_init_obstack)
7577 struct constructor_stack *p;
7578 tree value = NULL_TREE;
7580 /* If we've exhausted any levels that didn't have braces,
7581 pop them now. If implicit == 1, this will have been done in
7582 process_init_element; do not repeat it here because in the case
7583 of excess initializers for an empty aggregate this leads to an
7584 infinite cycle of popping a level and immediately recreating
7585 it. */
7586 if (implicit != 1)
7588 while (constructor_stack->implicit)
7590 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7591 || TREE_CODE (constructor_type) == UNION_TYPE)
7592 && constructor_fields == 0)
7593 process_init_element (input_location,
7594 pop_init_level (loc, 1, braced_init_obstack),
7595 true, braced_init_obstack);
7596 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7597 && constructor_max_index
7598 && tree_int_cst_lt (constructor_max_index,
7599 constructor_index))
7600 process_init_element (input_location,
7601 pop_init_level (loc, 1, braced_init_obstack),
7602 true, braced_init_obstack);
7603 else
7604 break;
7608 /* Unless this is an explicit brace, we need to preserve previous
7609 content if any. */
7610 if (implicit)
7612 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7613 || TREE_CODE (constructor_type) == UNION_TYPE)
7614 && constructor_fields)
7615 value = find_init_member (constructor_fields, braced_init_obstack);
7616 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7617 value = find_init_member (constructor_index, braced_init_obstack);
7620 p = XNEW (struct constructor_stack);
7621 p->type = constructor_type;
7622 p->fields = constructor_fields;
7623 p->index = constructor_index;
7624 p->max_index = constructor_max_index;
7625 p->unfilled_index = constructor_unfilled_index;
7626 p->unfilled_fields = constructor_unfilled_fields;
7627 p->bit_index = constructor_bit_index;
7628 p->elements = constructor_elements;
7629 p->constant = constructor_constant;
7630 p->simple = constructor_simple;
7631 p->nonconst = constructor_nonconst;
7632 p->erroneous = constructor_erroneous;
7633 p->pending_elts = constructor_pending_elts;
7634 p->depth = constructor_depth;
7635 p->replacement_value.value = 0;
7636 p->replacement_value.original_code = ERROR_MARK;
7637 p->replacement_value.original_type = NULL;
7638 p->implicit = implicit;
7639 p->outer = 0;
7640 p->incremental = constructor_incremental;
7641 p->designated = constructor_designated;
7642 p->designator_depth = designator_depth;
7643 p->next = constructor_stack;
7644 p->range_stack = 0;
7645 constructor_stack = p;
7647 constructor_constant = 1;
7648 constructor_simple = 1;
7649 constructor_nonconst = 0;
7650 constructor_depth = SPELLING_DEPTH ();
7651 constructor_elements = NULL;
7652 constructor_incremental = 1;
7653 constructor_designated = 0;
7654 constructor_pending_elts = 0;
7655 if (!implicit)
7657 p->range_stack = constructor_range_stack;
7658 constructor_range_stack = 0;
7659 designator_depth = 0;
7660 designator_erroneous = 0;
7663 /* Don't die if an entire brace-pair level is superfluous
7664 in the containing level. */
7665 if (constructor_type == 0)
7667 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7668 || TREE_CODE (constructor_type) == UNION_TYPE)
7670 /* Don't die if there are extra init elts at the end. */
7671 if (constructor_fields == 0)
7672 constructor_type = 0;
7673 else
7675 constructor_type = TREE_TYPE (constructor_fields);
7676 push_member_name (constructor_fields);
7677 constructor_depth++;
7679 /* If upper initializer is designated, then mark this as
7680 designated too to prevent bogus warnings. */
7681 constructor_designated = p->designated;
7683 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7685 constructor_type = TREE_TYPE (constructor_type);
7686 push_array_bounds (tree_to_uhwi (constructor_index));
7687 constructor_depth++;
7690 if (constructor_type == 0)
7692 error_init (loc, "extra brace group at end of initializer");
7693 constructor_fields = 0;
7694 constructor_unfilled_fields = 0;
7695 return;
7698 if (value && TREE_CODE (value) == CONSTRUCTOR)
7700 constructor_constant = TREE_CONSTANT (value);
7701 constructor_simple = TREE_STATIC (value);
7702 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7703 constructor_elements = CONSTRUCTOR_ELTS (value);
7704 if (!vec_safe_is_empty (constructor_elements)
7705 && (TREE_CODE (constructor_type) == RECORD_TYPE
7706 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7707 set_nonincremental_init (braced_init_obstack);
7710 if (implicit == 1)
7711 found_missing_braces = 1;
7713 if (TREE_CODE (constructor_type) == RECORD_TYPE
7714 || TREE_CODE (constructor_type) == UNION_TYPE)
7716 constructor_fields = TYPE_FIELDS (constructor_type);
7717 /* Skip any nameless bit fields at the beginning. */
7718 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7719 && DECL_NAME (constructor_fields) == 0)
7720 constructor_fields = DECL_CHAIN (constructor_fields);
7722 constructor_unfilled_fields = constructor_fields;
7723 constructor_bit_index = bitsize_zero_node;
7725 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7727 /* Vectors are like simple fixed-size arrays. */
7728 constructor_max_index =
7729 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7730 constructor_index = bitsize_int (0);
7731 constructor_unfilled_index = constructor_index;
7733 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7735 if (TYPE_DOMAIN (constructor_type))
7737 constructor_max_index
7738 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7740 /* Detect non-empty initializations of zero-length arrays. */
7741 if (constructor_max_index == NULL_TREE
7742 && TYPE_SIZE (constructor_type))
7743 constructor_max_index = integer_minus_one_node;
7745 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7746 to initialize VLAs will cause a proper error; avoid tree
7747 checking errors as well by setting a safe value. */
7748 if (constructor_max_index
7749 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7750 constructor_max_index = integer_minus_one_node;
7752 constructor_index
7753 = convert (bitsizetype,
7754 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7756 else
7757 constructor_index = bitsize_zero_node;
7759 constructor_unfilled_index = constructor_index;
7760 if (value && TREE_CODE (value) == STRING_CST)
7762 /* We need to split the char/wchar array into individual
7763 characters, so that we don't have to special case it
7764 everywhere. */
7765 set_nonincremental_init_from_string (value, braced_init_obstack);
7768 else
7770 if (constructor_type != error_mark_node)
7771 warning_init (input_location, 0, "braces around scalar initializer");
7772 constructor_fields = constructor_type;
7773 constructor_unfilled_fields = constructor_type;
7777 /* At the end of an implicit or explicit brace level,
7778 finish up that level of constructor. If a single expression
7779 with redundant braces initialized that level, return the
7780 c_expr structure for that expression. Otherwise, the original_code
7781 element is set to ERROR_MARK.
7782 If we were outputting the elements as they are read, return 0 as the value
7783 from inner levels (process_init_element ignores that),
7784 but return error_mark_node as the value from the outermost level
7785 (that's what we want to put in DECL_INITIAL).
7786 Otherwise, return a CONSTRUCTOR expression as the value. */
7788 struct c_expr
7789 pop_init_level (location_t loc, int implicit,
7790 struct obstack *braced_init_obstack)
7792 struct constructor_stack *p;
7793 struct c_expr ret;
7794 ret.value = 0;
7795 ret.original_code = ERROR_MARK;
7796 ret.original_type = NULL;
7798 if (implicit == 0)
7800 /* When we come to an explicit close brace,
7801 pop any inner levels that didn't have explicit braces. */
7802 while (constructor_stack->implicit)
7803 process_init_element (input_location,
7804 pop_init_level (loc, 1, braced_init_obstack),
7805 true, braced_init_obstack);
7806 gcc_assert (!constructor_range_stack);
7809 /* Now output all pending elements. */
7810 constructor_incremental = 1;
7811 output_pending_init_elements (1, braced_init_obstack);
7813 p = constructor_stack;
7815 /* Error for initializing a flexible array member, or a zero-length
7816 array member in an inappropriate context. */
7817 if (constructor_type && constructor_fields
7818 && TREE_CODE (constructor_type) == ARRAY_TYPE
7819 && TYPE_DOMAIN (constructor_type)
7820 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7822 /* Silently discard empty initializations. The parser will
7823 already have pedwarned for empty brackets. */
7824 if (integer_zerop (constructor_unfilled_index))
7825 constructor_type = NULL_TREE;
7826 else
7828 gcc_assert (!TYPE_SIZE (constructor_type));
7830 if (constructor_depth > 2)
7831 error_init (loc, "initialization of flexible array member in a nested context");
7832 else
7833 pedwarn_init (loc, OPT_Wpedantic,
7834 "initialization of a flexible array member");
7836 /* We have already issued an error message for the existence
7837 of a flexible array member not at the end of the structure.
7838 Discard the initializer so that we do not die later. */
7839 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7840 constructor_type = NULL_TREE;
7844 switch (vec_safe_length (constructor_elements))
7846 case 0:
7847 /* Initialization with { } counts as zeroinit. */
7848 constructor_zeroinit = 1;
7849 break;
7850 case 1:
7851 /* This might be zeroinit as well. */
7852 if (integer_zerop ((*constructor_elements)[0].value))
7853 constructor_zeroinit = 1;
7854 break;
7855 default:
7856 /* If the constructor has more than one element, it can't be { 0 }. */
7857 constructor_zeroinit = 0;
7858 break;
7861 /* Warn when some structs are initialized with direct aggregation. */
7862 if (!implicit && found_missing_braces && warn_missing_braces
7863 && !constructor_zeroinit)
7864 warning_init (loc, OPT_Wmissing_braces,
7865 "missing braces around initializer");
7867 /* Warn when some struct elements are implicitly initialized to zero. */
7868 if (warn_missing_field_initializers
7869 && constructor_type
7870 && TREE_CODE (constructor_type) == RECORD_TYPE
7871 && constructor_unfilled_fields)
7873 /* Do not warn for flexible array members or zero-length arrays. */
7874 while (constructor_unfilled_fields
7875 && (!DECL_SIZE (constructor_unfilled_fields)
7876 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7877 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7879 if (constructor_unfilled_fields
7880 /* Do not warn if this level of the initializer uses member
7881 designators; it is likely to be deliberate. */
7882 && !constructor_designated
7883 /* Do not warn about initializing with { 0 } or with { }. */
7884 && !constructor_zeroinit)
7886 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7887 "missing initializer for field %qD of %qT",
7888 constructor_unfilled_fields,
7889 constructor_type))
7890 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7891 "%qD declared here", constructor_unfilled_fields);
7895 /* Pad out the end of the structure. */
7896 if (p->replacement_value.value)
7897 /* If this closes a superfluous brace pair,
7898 just pass out the element between them. */
7899 ret = p->replacement_value;
7900 else if (constructor_type == 0)
7902 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7903 && TREE_CODE (constructor_type) != UNION_TYPE
7904 && TREE_CODE (constructor_type) != ARRAY_TYPE
7905 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7907 /* A nonincremental scalar initializer--just return
7908 the element, after verifying there is just one. */
7909 if (vec_safe_is_empty (constructor_elements))
7911 if (!constructor_erroneous)
7912 error_init (loc, "empty scalar initializer");
7913 ret.value = error_mark_node;
7915 else if (vec_safe_length (constructor_elements) != 1)
7917 error_init (loc, "extra elements in scalar initializer");
7918 ret.value = (*constructor_elements)[0].value;
7920 else
7921 ret.value = (*constructor_elements)[0].value;
7923 else
7925 if (constructor_erroneous)
7926 ret.value = error_mark_node;
7927 else
7929 ret.value = build_constructor (constructor_type,
7930 constructor_elements);
7931 if (constructor_constant)
7932 TREE_CONSTANT (ret.value) = 1;
7933 if (constructor_constant && constructor_simple)
7934 TREE_STATIC (ret.value) = 1;
7935 if (constructor_nonconst)
7936 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7940 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7942 if (constructor_nonconst)
7943 ret.original_code = C_MAYBE_CONST_EXPR;
7944 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7945 ret.original_code = ERROR_MARK;
7948 constructor_type = p->type;
7949 constructor_fields = p->fields;
7950 constructor_index = p->index;
7951 constructor_max_index = p->max_index;
7952 constructor_unfilled_index = p->unfilled_index;
7953 constructor_unfilled_fields = p->unfilled_fields;
7954 constructor_bit_index = p->bit_index;
7955 constructor_elements = p->elements;
7956 constructor_constant = p->constant;
7957 constructor_simple = p->simple;
7958 constructor_nonconst = p->nonconst;
7959 constructor_erroneous = p->erroneous;
7960 constructor_incremental = p->incremental;
7961 constructor_designated = p->designated;
7962 designator_depth = p->designator_depth;
7963 constructor_pending_elts = p->pending_elts;
7964 constructor_depth = p->depth;
7965 if (!p->implicit)
7966 constructor_range_stack = p->range_stack;
7967 RESTORE_SPELLING_DEPTH (constructor_depth);
7969 constructor_stack = p->next;
7970 free (p);
7972 if (ret.value == 0 && constructor_stack == 0)
7973 ret.value = error_mark_node;
7974 return ret;
7977 /* Common handling for both array range and field name designators.
7978 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7980 static int
7981 set_designator (location_t loc, int array,
7982 struct obstack *braced_init_obstack)
7984 tree subtype;
7985 enum tree_code subcode;
7987 /* Don't die if an entire brace-pair level is superfluous
7988 in the containing level. */
7989 if (constructor_type == 0)
7990 return 1;
7992 /* If there were errors in this designator list already, bail out
7993 silently. */
7994 if (designator_erroneous)
7995 return 1;
7997 if (!designator_depth)
7999 gcc_assert (!constructor_range_stack);
8001 /* Designator list starts at the level of closest explicit
8002 braces. */
8003 while (constructor_stack->implicit)
8004 process_init_element (input_location,
8005 pop_init_level (loc, 1, braced_init_obstack),
8006 true, braced_init_obstack);
8007 constructor_designated = 1;
8008 return 0;
8011 switch (TREE_CODE (constructor_type))
8013 case RECORD_TYPE:
8014 case UNION_TYPE:
8015 subtype = TREE_TYPE (constructor_fields);
8016 if (subtype != error_mark_node)
8017 subtype = TYPE_MAIN_VARIANT (subtype);
8018 break;
8019 case ARRAY_TYPE:
8020 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8021 break;
8022 default:
8023 gcc_unreachable ();
8026 subcode = TREE_CODE (subtype);
8027 if (array && subcode != ARRAY_TYPE)
8029 error_init (loc, "array index in non-array initializer");
8030 return 1;
8032 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8034 error_init (loc, "field name not in record or union initializer");
8035 return 1;
8038 constructor_designated = 1;
8039 push_init_level (loc, 2, braced_init_obstack);
8040 return 0;
8043 /* If there are range designators in designator list, push a new designator
8044 to constructor_range_stack. RANGE_END is end of such stack range or
8045 NULL_TREE if there is no range designator at this level. */
8047 static void
8048 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8050 struct constructor_range_stack *p;
8052 p = (struct constructor_range_stack *)
8053 obstack_alloc (braced_init_obstack,
8054 sizeof (struct constructor_range_stack));
8055 p->prev = constructor_range_stack;
8056 p->next = 0;
8057 p->fields = constructor_fields;
8058 p->range_start = constructor_index;
8059 p->index = constructor_index;
8060 p->stack = constructor_stack;
8061 p->range_end = range_end;
8062 if (constructor_range_stack)
8063 constructor_range_stack->next = p;
8064 constructor_range_stack = p;
8067 /* Within an array initializer, specify the next index to be initialized.
8068 FIRST is that index. If LAST is nonzero, then initialize a range
8069 of indices, running from FIRST through LAST. */
8071 void
8072 set_init_index (location_t loc, tree first, tree last,
8073 struct obstack *braced_init_obstack)
8075 if (set_designator (loc, 1, braced_init_obstack))
8076 return;
8078 designator_erroneous = 1;
8080 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8081 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8083 error_init (loc, "array index in initializer not of integer type");
8084 return;
8087 if (TREE_CODE (first) != INTEGER_CST)
8089 first = c_fully_fold (first, false, NULL);
8090 if (TREE_CODE (first) == INTEGER_CST)
8091 pedwarn_init (loc, OPT_Wpedantic,
8092 "array index in initializer is not "
8093 "an integer constant expression");
8096 if (last && TREE_CODE (last) != INTEGER_CST)
8098 last = c_fully_fold (last, false, NULL);
8099 if (TREE_CODE (last) == INTEGER_CST)
8100 pedwarn_init (loc, OPT_Wpedantic,
8101 "array index in initializer is not "
8102 "an integer constant expression");
8105 if (TREE_CODE (first) != INTEGER_CST)
8106 error_init (loc, "nonconstant array index in initializer");
8107 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8108 error_init (loc, "nonconstant array index in initializer");
8109 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8110 error_init (loc, "array index in non-array initializer");
8111 else if (tree_int_cst_sgn (first) == -1)
8112 error_init (loc, "array index in initializer exceeds array bounds");
8113 else if (constructor_max_index
8114 && tree_int_cst_lt (constructor_max_index, first))
8115 error_init (loc, "array index in initializer exceeds array bounds");
8116 else
8118 constant_expression_warning (first);
8119 if (last)
8120 constant_expression_warning (last);
8121 constructor_index = convert (bitsizetype, first);
8122 if (tree_int_cst_lt (constructor_index, first))
8124 constructor_index = copy_node (constructor_index);
8125 TREE_OVERFLOW (constructor_index) = 1;
8128 if (last)
8130 if (tree_int_cst_equal (first, last))
8131 last = 0;
8132 else if (tree_int_cst_lt (last, first))
8134 error_init (loc, "empty index range in initializer");
8135 last = 0;
8137 else
8139 last = convert (bitsizetype, last);
8140 if (constructor_max_index != 0
8141 && tree_int_cst_lt (constructor_max_index, last))
8143 error_init (loc, "array index range in initializer exceeds "
8144 "array bounds");
8145 last = 0;
8150 designator_depth++;
8151 designator_erroneous = 0;
8152 if (constructor_range_stack || last)
8153 push_range_stack (last, braced_init_obstack);
8157 /* Within a struct initializer, specify the next field to be initialized. */
8159 void
8160 set_init_label (location_t loc, tree fieldname,
8161 struct obstack *braced_init_obstack)
8163 tree field;
8165 if (set_designator (loc, 0, braced_init_obstack))
8166 return;
8168 designator_erroneous = 1;
8170 if (TREE_CODE (constructor_type) != RECORD_TYPE
8171 && TREE_CODE (constructor_type) != UNION_TYPE)
8173 error_init (loc, "field name not in record or union initializer");
8174 return;
8177 field = lookup_field (constructor_type, fieldname);
8179 if (field == 0)
8180 error ("unknown field %qE specified in initializer", fieldname);
8181 else
8184 constructor_fields = TREE_VALUE (field);
8185 designator_depth++;
8186 designator_erroneous = 0;
8187 if (constructor_range_stack)
8188 push_range_stack (NULL_TREE, braced_init_obstack);
8189 field = TREE_CHAIN (field);
8190 if (field)
8192 if (set_designator (loc, 0, braced_init_obstack))
8193 return;
8196 while (field != NULL_TREE);
8199 /* Add a new initializer to the tree of pending initializers. PURPOSE
8200 identifies the initializer, either array index or field in a structure.
8201 VALUE is the value of that index or field. If ORIGTYPE is not
8202 NULL_TREE, it is the original type of VALUE.
8204 IMPLICIT is true if value comes from pop_init_level (1),
8205 the new initializer has been merged with the existing one
8206 and thus no warnings should be emitted about overriding an
8207 existing initializer. */
8209 static void
8210 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8211 bool implicit, struct obstack *braced_init_obstack)
8213 struct init_node *p, **q, *r;
8215 q = &constructor_pending_elts;
8216 p = 0;
8218 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8220 while (*q != 0)
8222 p = *q;
8223 if (tree_int_cst_lt (purpose, p->purpose))
8224 q = &p->left;
8225 else if (tree_int_cst_lt (p->purpose, purpose))
8226 q = &p->right;
8227 else
8229 if (!implicit)
8231 if (TREE_SIDE_EFFECTS (p->value))
8232 warning_init (loc, 0,
8233 "initialized field with side-effects "
8234 "overwritten");
8235 else if (warn_override_init)
8236 warning_init (loc, OPT_Woverride_init,
8237 "initialized field overwritten");
8239 p->value = value;
8240 p->origtype = origtype;
8241 return;
8245 else
8247 tree bitpos;
8249 bitpos = bit_position (purpose);
8250 while (*q != NULL)
8252 p = *q;
8253 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8254 q = &p->left;
8255 else if (p->purpose != purpose)
8256 q = &p->right;
8257 else
8259 if (!implicit)
8261 if (TREE_SIDE_EFFECTS (p->value))
8262 warning_init (loc, 0,
8263 "initialized field with side-effects "
8264 "overwritten");
8265 else if (warn_override_init)
8266 warning_init (loc, OPT_Woverride_init,
8267 "initialized field overwritten");
8269 p->value = value;
8270 p->origtype = origtype;
8271 return;
8276 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8277 sizeof (struct init_node));
8278 r->purpose = purpose;
8279 r->value = value;
8280 r->origtype = origtype;
8282 *q = r;
8283 r->parent = p;
8284 r->left = 0;
8285 r->right = 0;
8286 r->balance = 0;
8288 while (p)
8290 struct init_node *s;
8292 if (r == p->left)
8294 if (p->balance == 0)
8295 p->balance = -1;
8296 else if (p->balance < 0)
8298 if (r->balance < 0)
8300 /* L rotation. */
8301 p->left = r->right;
8302 if (p->left)
8303 p->left->parent = p;
8304 r->right = p;
8306 p->balance = 0;
8307 r->balance = 0;
8309 s = p->parent;
8310 p->parent = r;
8311 r->parent = s;
8312 if (s)
8314 if (s->left == p)
8315 s->left = r;
8316 else
8317 s->right = r;
8319 else
8320 constructor_pending_elts = r;
8322 else
8324 /* LR rotation. */
8325 struct init_node *t = r->right;
8327 r->right = t->left;
8328 if (r->right)
8329 r->right->parent = r;
8330 t->left = r;
8332 p->left = t->right;
8333 if (p->left)
8334 p->left->parent = p;
8335 t->right = p;
8337 p->balance = t->balance < 0;
8338 r->balance = -(t->balance > 0);
8339 t->balance = 0;
8341 s = p->parent;
8342 p->parent = t;
8343 r->parent = t;
8344 t->parent = s;
8345 if (s)
8347 if (s->left == p)
8348 s->left = t;
8349 else
8350 s->right = t;
8352 else
8353 constructor_pending_elts = t;
8355 break;
8357 else
8359 /* p->balance == +1; growth of left side balances the node. */
8360 p->balance = 0;
8361 break;
8364 else /* r == p->right */
8366 if (p->balance == 0)
8367 /* Growth propagation from right side. */
8368 p->balance++;
8369 else if (p->balance > 0)
8371 if (r->balance > 0)
8373 /* R rotation. */
8374 p->right = r->left;
8375 if (p->right)
8376 p->right->parent = p;
8377 r->left = p;
8379 p->balance = 0;
8380 r->balance = 0;
8382 s = p->parent;
8383 p->parent = r;
8384 r->parent = s;
8385 if (s)
8387 if (s->left == p)
8388 s->left = r;
8389 else
8390 s->right = r;
8392 else
8393 constructor_pending_elts = r;
8395 else /* r->balance == -1 */
8397 /* RL rotation */
8398 struct init_node *t = r->left;
8400 r->left = t->right;
8401 if (r->left)
8402 r->left->parent = r;
8403 t->right = r;
8405 p->right = t->left;
8406 if (p->right)
8407 p->right->parent = p;
8408 t->left = p;
8410 r->balance = (t->balance < 0);
8411 p->balance = -(t->balance > 0);
8412 t->balance = 0;
8414 s = p->parent;
8415 p->parent = t;
8416 r->parent = t;
8417 t->parent = s;
8418 if (s)
8420 if (s->left == p)
8421 s->left = t;
8422 else
8423 s->right = t;
8425 else
8426 constructor_pending_elts = t;
8428 break;
8430 else
8432 /* p->balance == -1; growth of right side balances the node. */
8433 p->balance = 0;
8434 break;
8438 r = p;
8439 p = p->parent;
8443 /* Build AVL tree from a sorted chain. */
8445 static void
8446 set_nonincremental_init (struct obstack * braced_init_obstack)
8448 unsigned HOST_WIDE_INT ix;
8449 tree index, value;
8451 if (TREE_CODE (constructor_type) != RECORD_TYPE
8452 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8453 return;
8455 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8456 add_pending_init (input_location, index, value, NULL_TREE, true,
8457 braced_init_obstack);
8458 constructor_elements = NULL;
8459 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8461 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8462 /* Skip any nameless bit fields at the beginning. */
8463 while (constructor_unfilled_fields != 0
8464 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8465 && DECL_NAME (constructor_unfilled_fields) == 0)
8466 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8469 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8471 if (TYPE_DOMAIN (constructor_type))
8472 constructor_unfilled_index
8473 = convert (bitsizetype,
8474 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8475 else
8476 constructor_unfilled_index = bitsize_zero_node;
8478 constructor_incremental = 0;
8481 /* Build AVL tree from a string constant. */
8483 static void
8484 set_nonincremental_init_from_string (tree str,
8485 struct obstack * braced_init_obstack)
8487 tree value, purpose, type;
8488 HOST_WIDE_INT val[2];
8489 const char *p, *end;
8490 int byte, wchar_bytes, charwidth, bitpos;
8492 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8494 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8495 charwidth = TYPE_PRECISION (char_type_node);
8496 type = TREE_TYPE (constructor_type);
8497 p = TREE_STRING_POINTER (str);
8498 end = p + TREE_STRING_LENGTH (str);
8500 for (purpose = bitsize_zero_node;
8501 p < end
8502 && !(constructor_max_index
8503 && tree_int_cst_lt (constructor_max_index, purpose));
8504 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8506 if (wchar_bytes == 1)
8508 val[0] = (unsigned char) *p++;
8509 val[1] = 0;
8511 else
8513 val[1] = 0;
8514 val[0] = 0;
8515 for (byte = 0; byte < wchar_bytes; byte++)
8517 if (BYTES_BIG_ENDIAN)
8518 bitpos = (wchar_bytes - byte - 1) * charwidth;
8519 else
8520 bitpos = byte * charwidth;
8521 val[bitpos % HOST_BITS_PER_WIDE_INT]
8522 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8523 << (bitpos % HOST_BITS_PER_WIDE_INT);
8527 if (!TYPE_UNSIGNED (type))
8529 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8530 if (bitpos < HOST_BITS_PER_WIDE_INT)
8532 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8534 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8535 val[1] = -1;
8538 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8540 if (val[0] < 0)
8541 val[1] = -1;
8543 else if (val[1] & (((HOST_WIDE_INT) 1)
8544 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8545 val[1] |= ((HOST_WIDE_INT) -1)
8546 << (bitpos - HOST_BITS_PER_WIDE_INT);
8549 value = wide_int_to_tree (type,
8550 wide_int::from_array (val, 2,
8551 HOST_BITS_PER_WIDE_INT * 2));
8552 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8553 braced_init_obstack);
8556 constructor_incremental = 0;
8559 /* Return value of FIELD in pending initializer or zero if the field was
8560 not initialized yet. */
8562 static tree
8563 find_init_member (tree field, struct obstack * braced_init_obstack)
8565 struct init_node *p;
8567 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8569 if (constructor_incremental
8570 && tree_int_cst_lt (field, constructor_unfilled_index))
8571 set_nonincremental_init (braced_init_obstack);
8573 p = constructor_pending_elts;
8574 while (p)
8576 if (tree_int_cst_lt (field, p->purpose))
8577 p = p->left;
8578 else if (tree_int_cst_lt (p->purpose, field))
8579 p = p->right;
8580 else
8581 return p->value;
8584 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8586 tree bitpos = bit_position (field);
8588 if (constructor_incremental
8589 && (!constructor_unfilled_fields
8590 || tree_int_cst_lt (bitpos,
8591 bit_position (constructor_unfilled_fields))))
8592 set_nonincremental_init (braced_init_obstack);
8594 p = constructor_pending_elts;
8595 while (p)
8597 if (field == p->purpose)
8598 return p->value;
8599 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8600 p = p->left;
8601 else
8602 p = p->right;
8605 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8607 if (!vec_safe_is_empty (constructor_elements)
8608 && (constructor_elements->last ().index == field))
8609 return constructor_elements->last ().value;
8611 return 0;
8614 /* "Output" the next constructor element.
8615 At top level, really output it to assembler code now.
8616 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8617 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8618 TYPE is the data type that the containing data type wants here.
8619 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8620 If VALUE is a string constant, STRICT_STRING is true if it is
8621 unparenthesized or we should not warn here for it being parenthesized.
8622 For other types of VALUE, STRICT_STRING is not used.
8624 PENDING if non-nil means output pending elements that belong
8625 right after this element. (PENDING is normally 1;
8626 it is 0 while outputting pending elements, to avoid recursion.)
8628 IMPLICIT is true if value comes from pop_init_level (1),
8629 the new initializer has been merged with the existing one
8630 and thus no warnings should be emitted about overriding an
8631 existing initializer. */
8633 static void
8634 output_init_element (location_t loc, tree value, tree origtype,
8635 bool strict_string, tree type, tree field, int pending,
8636 bool implicit, struct obstack * braced_init_obstack)
8638 tree semantic_type = NULL_TREE;
8639 bool maybe_const = true;
8640 bool npc;
8642 if (type == error_mark_node || value == error_mark_node)
8644 constructor_erroneous = 1;
8645 return;
8647 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8648 && (TREE_CODE (value) == STRING_CST
8649 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8650 && !(TREE_CODE (value) == STRING_CST
8651 && TREE_CODE (type) == ARRAY_TYPE
8652 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8653 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8654 TYPE_MAIN_VARIANT (type)))
8655 value = array_to_pointer_conversion (input_location, value);
8657 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8658 && require_constant_value && pending)
8660 /* As an extension, allow initializing objects with static storage
8661 duration with compound literals (which are then treated just as
8662 the brace enclosed list they contain). */
8663 if (flag_isoc99)
8664 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8665 "constant");
8666 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8667 value = DECL_INITIAL (decl);
8670 npc = null_pointer_constant_p (value);
8671 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8673 semantic_type = TREE_TYPE (value);
8674 value = TREE_OPERAND (value, 0);
8676 value = c_fully_fold (value, require_constant_value, &maybe_const);
8678 if (value == error_mark_node)
8679 constructor_erroneous = 1;
8680 else if (!TREE_CONSTANT (value))
8681 constructor_constant = 0;
8682 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8683 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8684 || TREE_CODE (constructor_type) == UNION_TYPE)
8685 && DECL_C_BIT_FIELD (field)
8686 && TREE_CODE (value) != INTEGER_CST))
8687 constructor_simple = 0;
8688 if (!maybe_const)
8689 constructor_nonconst = 1;
8691 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8693 if (require_constant_value)
8695 error_init (loc, "initializer element is not constant");
8696 value = error_mark_node;
8698 else if (require_constant_elements)
8699 pedwarn (loc, OPT_Wpedantic,
8700 "initializer element is not computable at load time");
8702 else if (!maybe_const
8703 && (require_constant_value || require_constant_elements))
8704 pedwarn_init (loc, OPT_Wpedantic,
8705 "initializer element is not a constant expression");
8707 /* Issue -Wc++-compat warnings about initializing a bitfield with
8708 enum type. */
8709 if (warn_cxx_compat
8710 && field != NULL_TREE
8711 && TREE_CODE (field) == FIELD_DECL
8712 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8713 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8714 != TYPE_MAIN_VARIANT (type))
8715 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8717 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8718 if (checktype != error_mark_node
8719 && (TYPE_MAIN_VARIANT (checktype)
8720 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8721 warning_init (loc, OPT_Wc___compat,
8722 "enum conversion in initialization is invalid in C++");
8725 /* If this field is empty (and not at the end of structure),
8726 don't do anything other than checking the initializer. */
8727 if (field
8728 && (TREE_TYPE (field) == error_mark_node
8729 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8730 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8731 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8732 || DECL_CHAIN (field)))))
8733 return;
8735 if (semantic_type)
8736 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8737 value = digest_init (loc, type, value, origtype, npc, strict_string,
8738 require_constant_value);
8739 if (value == error_mark_node)
8741 constructor_erroneous = 1;
8742 return;
8744 if (require_constant_value || require_constant_elements)
8745 constant_expression_warning (value);
8747 /* If this element doesn't come next in sequence,
8748 put it on constructor_pending_elts. */
8749 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8750 && (!constructor_incremental
8751 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8753 if (constructor_incremental
8754 && tree_int_cst_lt (field, constructor_unfilled_index))
8755 set_nonincremental_init (braced_init_obstack);
8757 add_pending_init (loc, field, value, origtype, implicit,
8758 braced_init_obstack);
8759 return;
8761 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8762 && (!constructor_incremental
8763 || field != constructor_unfilled_fields))
8765 /* We do this for records but not for unions. In a union,
8766 no matter which field is specified, it can be initialized
8767 right away since it starts at the beginning of the union. */
8768 if (constructor_incremental)
8770 if (!constructor_unfilled_fields)
8771 set_nonincremental_init (braced_init_obstack);
8772 else
8774 tree bitpos, unfillpos;
8776 bitpos = bit_position (field);
8777 unfillpos = bit_position (constructor_unfilled_fields);
8779 if (tree_int_cst_lt (bitpos, unfillpos))
8780 set_nonincremental_init (braced_init_obstack);
8784 add_pending_init (loc, field, value, origtype, implicit,
8785 braced_init_obstack);
8786 return;
8788 else if (TREE_CODE (constructor_type) == UNION_TYPE
8789 && !vec_safe_is_empty (constructor_elements))
8791 if (!implicit)
8793 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8794 warning_init (loc, 0,
8795 "initialized field with side-effects overwritten");
8796 else if (warn_override_init)
8797 warning_init (loc, OPT_Woverride_init,
8798 "initialized field overwritten");
8801 /* We can have just one union field set. */
8802 constructor_elements = NULL;
8805 /* Otherwise, output this element either to
8806 constructor_elements or to the assembler file. */
8808 constructor_elt celt = {field, value};
8809 vec_safe_push (constructor_elements, celt);
8811 /* Advance the variable that indicates sequential elements output. */
8812 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8813 constructor_unfilled_index
8814 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8815 bitsize_one_node);
8816 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8818 constructor_unfilled_fields
8819 = DECL_CHAIN (constructor_unfilled_fields);
8821 /* Skip any nameless bit fields. */
8822 while (constructor_unfilled_fields != 0
8823 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8824 && DECL_NAME (constructor_unfilled_fields) == 0)
8825 constructor_unfilled_fields =
8826 DECL_CHAIN (constructor_unfilled_fields);
8828 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8829 constructor_unfilled_fields = 0;
8831 /* Now output any pending elements which have become next. */
8832 if (pending)
8833 output_pending_init_elements (0, braced_init_obstack);
8836 /* Output any pending elements which have become next.
8837 As we output elements, constructor_unfilled_{fields,index}
8838 advances, which may cause other elements to become next;
8839 if so, they too are output.
8841 If ALL is 0, we return when there are
8842 no more pending elements to output now.
8844 If ALL is 1, we output space as necessary so that
8845 we can output all the pending elements. */
8846 static void
8847 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8849 struct init_node *elt = constructor_pending_elts;
8850 tree next;
8852 retry:
8854 /* Look through the whole pending tree.
8855 If we find an element that should be output now,
8856 output it. Otherwise, set NEXT to the element
8857 that comes first among those still pending. */
8859 next = 0;
8860 while (elt)
8862 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8864 if (tree_int_cst_equal (elt->purpose,
8865 constructor_unfilled_index))
8866 output_init_element (input_location, elt->value, elt->origtype,
8867 true, TREE_TYPE (constructor_type),
8868 constructor_unfilled_index, 0, false,
8869 braced_init_obstack);
8870 else if (tree_int_cst_lt (constructor_unfilled_index,
8871 elt->purpose))
8873 /* Advance to the next smaller node. */
8874 if (elt->left)
8875 elt = elt->left;
8876 else
8878 /* We have reached the smallest node bigger than the
8879 current unfilled index. Fill the space first. */
8880 next = elt->purpose;
8881 break;
8884 else
8886 /* Advance to the next bigger node. */
8887 if (elt->right)
8888 elt = elt->right;
8889 else
8891 /* We have reached the biggest node in a subtree. Find
8892 the parent of it, which is the next bigger node. */
8893 while (elt->parent && elt->parent->right == elt)
8894 elt = elt->parent;
8895 elt = elt->parent;
8896 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8897 elt->purpose))
8899 next = elt->purpose;
8900 break;
8905 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8906 || TREE_CODE (constructor_type) == UNION_TYPE)
8908 tree ctor_unfilled_bitpos, elt_bitpos;
8910 /* If the current record is complete we are done. */
8911 if (constructor_unfilled_fields == 0)
8912 break;
8914 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8915 elt_bitpos = bit_position (elt->purpose);
8916 /* We can't compare fields here because there might be empty
8917 fields in between. */
8918 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8920 constructor_unfilled_fields = elt->purpose;
8921 output_init_element (input_location, elt->value, elt->origtype,
8922 true, TREE_TYPE (elt->purpose),
8923 elt->purpose, 0, false,
8924 braced_init_obstack);
8926 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8928 /* Advance to the next smaller node. */
8929 if (elt->left)
8930 elt = elt->left;
8931 else
8933 /* We have reached the smallest node bigger than the
8934 current unfilled field. Fill the space first. */
8935 next = elt->purpose;
8936 break;
8939 else
8941 /* Advance to the next bigger node. */
8942 if (elt->right)
8943 elt = elt->right;
8944 else
8946 /* We have reached the biggest node in a subtree. Find
8947 the parent of it, which is the next bigger node. */
8948 while (elt->parent && elt->parent->right == elt)
8949 elt = elt->parent;
8950 elt = elt->parent;
8951 if (elt
8952 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8953 bit_position (elt->purpose))))
8955 next = elt->purpose;
8956 break;
8963 /* Ordinarily return, but not if we want to output all
8964 and there are elements left. */
8965 if (!(all && next != 0))
8966 return;
8968 /* If it's not incremental, just skip over the gap, so that after
8969 jumping to retry we will output the next successive element. */
8970 if (TREE_CODE (constructor_type) == RECORD_TYPE
8971 || TREE_CODE (constructor_type) == UNION_TYPE)
8972 constructor_unfilled_fields = next;
8973 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8974 constructor_unfilled_index = next;
8976 /* ELT now points to the node in the pending tree with the next
8977 initializer to output. */
8978 goto retry;
8981 /* Add one non-braced element to the current constructor level.
8982 This adjusts the current position within the constructor's type.
8983 This may also start or terminate implicit levels
8984 to handle a partly-braced initializer.
8986 Once this has found the correct level for the new element,
8987 it calls output_init_element.
8989 IMPLICIT is true if value comes from pop_init_level (1),
8990 the new initializer has been merged with the existing one
8991 and thus no warnings should be emitted about overriding an
8992 existing initializer. */
8994 void
8995 process_init_element (location_t loc, struct c_expr value, bool implicit,
8996 struct obstack * braced_init_obstack)
8998 tree orig_value = value.value;
8999 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9000 bool strict_string = value.original_code == STRING_CST;
9001 bool was_designated = designator_depth != 0;
9003 designator_depth = 0;
9004 designator_erroneous = 0;
9006 if (!implicit && value.value && !integer_zerop (value.value))
9007 constructor_zeroinit = 0;
9009 /* Handle superfluous braces around string cst as in
9010 char x[] = {"foo"}; */
9011 if (string_flag
9012 && constructor_type
9013 && !was_designated
9014 && TREE_CODE (constructor_type) == ARRAY_TYPE
9015 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9016 && integer_zerop (constructor_unfilled_index))
9018 if (constructor_stack->replacement_value.value)
9019 error_init (loc, "excess elements in char array initializer");
9020 constructor_stack->replacement_value = value;
9021 return;
9024 if (constructor_stack->replacement_value.value != 0)
9026 error_init (loc, "excess elements in struct initializer");
9027 return;
9030 /* Ignore elements of a brace group if it is entirely superfluous
9031 and has already been diagnosed. */
9032 if (constructor_type == 0)
9033 return;
9035 if (!implicit && warn_designated_init && !was_designated
9036 && TREE_CODE (constructor_type) == RECORD_TYPE
9037 && lookup_attribute ("designated_init",
9038 TYPE_ATTRIBUTES (constructor_type)))
9039 warning_init (loc,
9040 OPT_Wdesignated_init,
9041 "positional initialization of field "
9042 "in %<struct%> declared with %<designated_init%> attribute");
9044 /* If we've exhausted any levels that didn't have braces,
9045 pop them now. */
9046 while (constructor_stack->implicit)
9048 if ((TREE_CODE (constructor_type) == RECORD_TYPE
9049 || TREE_CODE (constructor_type) == UNION_TYPE)
9050 && constructor_fields == 0)
9051 process_init_element (loc,
9052 pop_init_level (loc, 1, braced_init_obstack),
9053 true, braced_init_obstack);
9054 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9055 || TREE_CODE (constructor_type) == VECTOR_TYPE)
9056 && constructor_max_index
9057 && tree_int_cst_lt (constructor_max_index,
9058 constructor_index))
9059 process_init_element (loc,
9060 pop_init_level (loc, 1, braced_init_obstack),
9061 true, braced_init_obstack);
9062 else
9063 break;
9066 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9067 if (constructor_range_stack)
9069 /* If value is a compound literal and we'll be just using its
9070 content, don't put it into a SAVE_EXPR. */
9071 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9072 || !require_constant_value)
9074 tree semantic_type = NULL_TREE;
9075 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9077 semantic_type = TREE_TYPE (value.value);
9078 value.value = TREE_OPERAND (value.value, 0);
9080 value.value = c_save_expr (value.value);
9081 if (semantic_type)
9082 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9083 value.value);
9087 while (1)
9089 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9091 tree fieldtype;
9092 enum tree_code fieldcode;
9094 if (constructor_fields == 0)
9096 pedwarn_init (loc, 0, "excess elements in struct initializer");
9097 break;
9100 fieldtype = TREE_TYPE (constructor_fields);
9101 if (fieldtype != error_mark_node)
9102 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9103 fieldcode = TREE_CODE (fieldtype);
9105 /* Error for non-static initialization of a flexible array member. */
9106 if (fieldcode == ARRAY_TYPE
9107 && !require_constant_value
9108 && TYPE_SIZE (fieldtype) == NULL_TREE
9109 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9111 error_init (loc, "non-static initialization of a flexible "
9112 "array member");
9113 break;
9116 /* Error for initialization of a flexible array member with
9117 a string constant if the structure is in an array. E.g.:
9118 struct S { int x; char y[]; };
9119 struct S s[] = { { 1, "foo" } };
9120 is invalid. */
9121 if (string_flag
9122 && fieldcode == ARRAY_TYPE
9123 && constructor_depth > 1
9124 && TYPE_SIZE (fieldtype) == NULL_TREE
9125 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9127 bool in_array_p = false;
9128 for (struct constructor_stack *p = constructor_stack;
9129 p && p->type; p = p->next)
9130 if (TREE_CODE (p->type) == ARRAY_TYPE)
9132 in_array_p = true;
9133 break;
9135 if (in_array_p)
9137 error_init (loc, "initialization of flexible array "
9138 "member in a nested context");
9139 break;
9143 /* Accept a string constant to initialize a subarray. */
9144 if (value.value != 0
9145 && fieldcode == ARRAY_TYPE
9146 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9147 && string_flag)
9148 value.value = orig_value;
9149 /* Otherwise, if we have come to a subaggregate,
9150 and we don't have an element of its type, push into it. */
9151 else if (value.value != 0
9152 && value.value != error_mark_node
9153 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9154 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9155 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9157 push_init_level (loc, 1, braced_init_obstack);
9158 continue;
9161 if (value.value)
9163 push_member_name (constructor_fields);
9164 output_init_element (loc, value.value, value.original_type,
9165 strict_string, fieldtype,
9166 constructor_fields, 1, implicit,
9167 braced_init_obstack);
9168 RESTORE_SPELLING_DEPTH (constructor_depth);
9170 else
9171 /* Do the bookkeeping for an element that was
9172 directly output as a constructor. */
9174 /* For a record, keep track of end position of last field. */
9175 if (DECL_SIZE (constructor_fields))
9176 constructor_bit_index
9177 = size_binop_loc (input_location, PLUS_EXPR,
9178 bit_position (constructor_fields),
9179 DECL_SIZE (constructor_fields));
9181 /* If the current field was the first one not yet written out,
9182 it isn't now, so update. */
9183 if (constructor_unfilled_fields == constructor_fields)
9185 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9186 /* Skip any nameless bit fields. */
9187 while (constructor_unfilled_fields != 0
9188 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9189 && DECL_NAME (constructor_unfilled_fields) == 0)
9190 constructor_unfilled_fields =
9191 DECL_CHAIN (constructor_unfilled_fields);
9195 constructor_fields = DECL_CHAIN (constructor_fields);
9196 /* Skip any nameless bit fields at the beginning. */
9197 while (constructor_fields != 0
9198 && DECL_C_BIT_FIELD (constructor_fields)
9199 && DECL_NAME (constructor_fields) == 0)
9200 constructor_fields = DECL_CHAIN (constructor_fields);
9202 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9204 tree fieldtype;
9205 enum tree_code fieldcode;
9207 if (constructor_fields == 0)
9209 pedwarn_init (loc, 0,
9210 "excess elements in union initializer");
9211 break;
9214 fieldtype = TREE_TYPE (constructor_fields);
9215 if (fieldtype != error_mark_node)
9216 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9217 fieldcode = TREE_CODE (fieldtype);
9219 /* Warn that traditional C rejects initialization of unions.
9220 We skip the warning if the value is zero. This is done
9221 under the assumption that the zero initializer in user
9222 code appears conditioned on e.g. __STDC__ to avoid
9223 "missing initializer" warnings and relies on default
9224 initialization to zero in the traditional C case.
9225 We also skip the warning if the initializer is designated,
9226 again on the assumption that this must be conditional on
9227 __STDC__ anyway (and we've already complained about the
9228 member-designator already). */
9229 if (!in_system_header_at (input_location) && !constructor_designated
9230 && !(value.value && (integer_zerop (value.value)
9231 || real_zerop (value.value))))
9232 warning (OPT_Wtraditional, "traditional C rejects initialization "
9233 "of unions");
9235 /* Accept a string constant to initialize a subarray. */
9236 if (value.value != 0
9237 && fieldcode == ARRAY_TYPE
9238 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9239 && string_flag)
9240 value.value = orig_value;
9241 /* Otherwise, if we have come to a subaggregate,
9242 and we don't have an element of its type, push into it. */
9243 else if (value.value != 0
9244 && value.value != error_mark_node
9245 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9246 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9247 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9249 push_init_level (loc, 1, braced_init_obstack);
9250 continue;
9253 if (value.value)
9255 push_member_name (constructor_fields);
9256 output_init_element (loc, value.value, value.original_type,
9257 strict_string, fieldtype,
9258 constructor_fields, 1, implicit,
9259 braced_init_obstack);
9260 RESTORE_SPELLING_DEPTH (constructor_depth);
9262 else
9263 /* Do the bookkeeping for an element that was
9264 directly output as a constructor. */
9266 constructor_bit_index = DECL_SIZE (constructor_fields);
9267 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9270 constructor_fields = 0;
9272 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9274 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9275 enum tree_code eltcode = TREE_CODE (elttype);
9277 /* Accept a string constant to initialize a subarray. */
9278 if (value.value != 0
9279 && eltcode == ARRAY_TYPE
9280 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9281 && string_flag)
9282 value.value = orig_value;
9283 /* Otherwise, if we have come to a subaggregate,
9284 and we don't have an element of its type, push into it. */
9285 else if (value.value != 0
9286 && value.value != error_mark_node
9287 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9288 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9289 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9291 push_init_level (loc, 1, braced_init_obstack);
9292 continue;
9295 if (constructor_max_index != 0
9296 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9297 || integer_all_onesp (constructor_max_index)))
9299 pedwarn_init (loc, 0,
9300 "excess elements in array initializer");
9301 break;
9304 /* Now output the actual element. */
9305 if (value.value)
9307 push_array_bounds (tree_to_uhwi (constructor_index));
9308 output_init_element (loc, value.value, value.original_type,
9309 strict_string, elttype,
9310 constructor_index, 1, implicit,
9311 braced_init_obstack);
9312 RESTORE_SPELLING_DEPTH (constructor_depth);
9315 constructor_index
9316 = size_binop_loc (input_location, PLUS_EXPR,
9317 constructor_index, bitsize_one_node);
9319 if (!value.value)
9320 /* If we are doing the bookkeeping for an element that was
9321 directly output as a constructor, we must update
9322 constructor_unfilled_index. */
9323 constructor_unfilled_index = constructor_index;
9325 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9327 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9329 /* Do a basic check of initializer size. Note that vectors
9330 always have a fixed size derived from their type. */
9331 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9333 pedwarn_init (loc, 0,
9334 "excess elements in vector initializer");
9335 break;
9338 /* Now output the actual element. */
9339 if (value.value)
9341 if (TREE_CODE (value.value) == VECTOR_CST)
9342 elttype = TYPE_MAIN_VARIANT (constructor_type);
9343 output_init_element (loc, value.value, value.original_type,
9344 strict_string, elttype,
9345 constructor_index, 1, implicit,
9346 braced_init_obstack);
9349 constructor_index
9350 = size_binop_loc (input_location,
9351 PLUS_EXPR, constructor_index, bitsize_one_node);
9353 if (!value.value)
9354 /* If we are doing the bookkeeping for an element that was
9355 directly output as a constructor, we must update
9356 constructor_unfilled_index. */
9357 constructor_unfilled_index = constructor_index;
9360 /* Handle the sole element allowed in a braced initializer
9361 for a scalar variable. */
9362 else if (constructor_type != error_mark_node
9363 && constructor_fields == 0)
9365 pedwarn_init (loc, 0,
9366 "excess elements in scalar initializer");
9367 break;
9369 else
9371 if (value.value)
9372 output_init_element (loc, value.value, value.original_type,
9373 strict_string, constructor_type,
9374 NULL_TREE, 1, implicit,
9375 braced_init_obstack);
9376 constructor_fields = 0;
9379 /* Handle range initializers either at this level or anywhere higher
9380 in the designator stack. */
9381 if (constructor_range_stack)
9383 struct constructor_range_stack *p, *range_stack;
9384 int finish = 0;
9386 range_stack = constructor_range_stack;
9387 constructor_range_stack = 0;
9388 while (constructor_stack != range_stack->stack)
9390 gcc_assert (constructor_stack->implicit);
9391 process_init_element (loc,
9392 pop_init_level (loc, 1,
9393 braced_init_obstack),
9394 true, braced_init_obstack);
9396 for (p = range_stack;
9397 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9398 p = p->prev)
9400 gcc_assert (constructor_stack->implicit);
9401 process_init_element (loc,
9402 pop_init_level (loc, 1,
9403 braced_init_obstack),
9404 true, braced_init_obstack);
9407 p->index = size_binop_loc (input_location,
9408 PLUS_EXPR, p->index, bitsize_one_node);
9409 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9410 finish = 1;
9412 while (1)
9414 constructor_index = p->index;
9415 constructor_fields = p->fields;
9416 if (finish && p->range_end && p->index == p->range_start)
9418 finish = 0;
9419 p->prev = 0;
9421 p = p->next;
9422 if (!p)
9423 break;
9424 push_init_level (loc, 2, braced_init_obstack);
9425 p->stack = constructor_stack;
9426 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9427 p->index = p->range_start;
9430 if (!finish)
9431 constructor_range_stack = range_stack;
9432 continue;
9435 break;
9438 constructor_range_stack = 0;
9441 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9442 (guaranteed to be 'volatile' or null) and ARGS (represented using
9443 an ASM_EXPR node). */
9444 tree
9445 build_asm_stmt (tree cv_qualifier, tree args)
9447 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9448 ASM_VOLATILE_P (args) = 1;
9449 return add_stmt (args);
9452 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9453 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9454 SIMPLE indicates whether there was anything at all after the
9455 string in the asm expression -- asm("blah") and asm("blah" : )
9456 are subtly different. We use a ASM_EXPR node to represent this. */
9457 tree
9458 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9459 tree clobbers, tree labels, bool simple)
9461 tree tail;
9462 tree args;
9463 int i;
9464 const char *constraint;
9465 const char **oconstraints;
9466 bool allows_mem, allows_reg, is_inout;
9467 int ninputs, noutputs;
9469 ninputs = list_length (inputs);
9470 noutputs = list_length (outputs);
9471 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9473 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9475 /* Remove output conversions that change the type but not the mode. */
9476 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9478 tree output = TREE_VALUE (tail);
9480 output = c_fully_fold (output, false, NULL);
9482 /* ??? Really, this should not be here. Users should be using a
9483 proper lvalue, dammit. But there's a long history of using casts
9484 in the output operands. In cases like longlong.h, this becomes a
9485 primitive form of typechecking -- if the cast can be removed, then
9486 the output operand had a type of the proper width; otherwise we'll
9487 get an error. Gross, but ... */
9488 STRIP_NOPS (output);
9490 if (!lvalue_or_else (loc, output, lv_asm))
9491 output = error_mark_node;
9493 if (output != error_mark_node
9494 && (TREE_READONLY (output)
9495 || TYPE_READONLY (TREE_TYPE (output))
9496 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9497 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9498 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9499 readonly_error (loc, output, lv_asm);
9501 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9502 oconstraints[i] = constraint;
9504 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9505 &allows_mem, &allows_reg, &is_inout))
9507 /* If the operand is going to end up in memory,
9508 mark it addressable. */
9509 if (!allows_reg && !c_mark_addressable (output))
9510 output = error_mark_node;
9511 if (!(!allows_reg && allows_mem)
9512 && output != error_mark_node
9513 && VOID_TYPE_P (TREE_TYPE (output)))
9515 error_at (loc, "invalid use of void expression");
9516 output = error_mark_node;
9519 else
9520 output = error_mark_node;
9522 TREE_VALUE (tail) = output;
9525 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9527 tree input;
9529 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9530 input = TREE_VALUE (tail);
9532 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9533 oconstraints, &allows_mem, &allows_reg))
9535 /* If the operand is going to end up in memory,
9536 mark it addressable. */
9537 if (!allows_reg && allows_mem)
9539 input = c_fully_fold (input, false, NULL);
9541 /* Strip the nops as we allow this case. FIXME, this really
9542 should be rejected or made deprecated. */
9543 STRIP_NOPS (input);
9544 if (!c_mark_addressable (input))
9545 input = error_mark_node;
9547 else
9549 struct c_expr expr;
9550 memset (&expr, 0, sizeof (expr));
9551 expr.value = input;
9552 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9553 input = c_fully_fold (expr.value, false, NULL);
9555 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9557 error_at (loc, "invalid use of void expression");
9558 input = error_mark_node;
9562 else
9563 input = error_mark_node;
9565 TREE_VALUE (tail) = input;
9568 /* ASMs with labels cannot have outputs. This should have been
9569 enforced by the parser. */
9570 gcc_assert (outputs == NULL || labels == NULL);
9572 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9574 /* asm statements without outputs, including simple ones, are treated
9575 as volatile. */
9576 ASM_INPUT_P (args) = simple;
9577 ASM_VOLATILE_P (args) = (noutputs == 0);
9579 return args;
9582 /* Generate a goto statement to LABEL. LOC is the location of the
9583 GOTO. */
9585 tree
9586 c_finish_goto_label (location_t loc, tree label)
9588 tree decl = lookup_label_for_goto (loc, label);
9589 if (!decl)
9590 return NULL_TREE;
9591 TREE_USED (decl) = 1;
9593 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9594 SET_EXPR_LOCATION (t, loc);
9595 return add_stmt (t);
9599 /* Generate a computed goto statement to EXPR. LOC is the location of
9600 the GOTO. */
9602 tree
9603 c_finish_goto_ptr (location_t loc, tree expr)
9605 tree t;
9606 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9607 expr = c_fully_fold (expr, false, NULL);
9608 expr = convert (ptr_type_node, expr);
9609 t = build1 (GOTO_EXPR, void_type_node, expr);
9610 SET_EXPR_LOCATION (t, loc);
9611 return add_stmt (t);
9614 /* Generate a C `return' statement. RETVAL is the expression for what
9615 to return, or a null pointer for `return;' with no value. LOC is
9616 the location of the return statement, or the location of the expression,
9617 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9618 is the original type of RETVAL. */
9620 tree
9621 c_finish_return (location_t loc, tree retval, tree origtype)
9623 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9624 bool no_warning = false;
9625 bool npc = false;
9626 size_t rank = 0;
9628 if (TREE_THIS_VOLATILE (current_function_decl))
9629 warning_at (loc, 0,
9630 "function declared %<noreturn%> has a %<return%> statement");
9632 if (flag_cilkplus && contains_array_notation_expr (retval))
9634 /* Array notations are allowed in a return statement if it is inside a
9635 built-in array notation reduction function. */
9636 if (!find_rank (loc, retval, retval, false, &rank))
9637 return error_mark_node;
9638 if (rank >= 1)
9640 error_at (loc, "array notation expression cannot be used as a "
9641 "return value");
9642 return error_mark_node;
9645 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9647 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9648 "allowed");
9649 return error_mark_node;
9651 if (retval)
9653 tree semantic_type = NULL_TREE;
9654 npc = null_pointer_constant_p (retval);
9655 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9657 semantic_type = TREE_TYPE (retval);
9658 retval = TREE_OPERAND (retval, 0);
9660 retval = c_fully_fold (retval, false, NULL);
9661 if (semantic_type)
9662 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9665 if (!retval)
9667 current_function_returns_null = 1;
9668 if ((warn_return_type || flag_isoc99)
9669 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9671 if (flag_isoc99)
9672 pedwarn (loc, 0, "%<return%> with no value, in "
9673 "function returning non-void");
9674 else
9675 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9676 "in function returning non-void");
9677 no_warning = true;
9680 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9682 current_function_returns_null = 1;
9683 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9684 pedwarn (loc, 0,
9685 "%<return%> with a value, in function returning void");
9686 else
9687 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9688 "%<return%> with expression, in function returning void");
9690 else
9692 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9693 retval, origtype, ic_return,
9694 npc, NULL_TREE, NULL_TREE, 0);
9695 tree res = DECL_RESULT (current_function_decl);
9696 tree inner;
9697 bool save;
9699 current_function_returns_value = 1;
9700 if (t == error_mark_node)
9701 return NULL_TREE;
9703 save = in_late_binary_op;
9704 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9705 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9706 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9707 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9708 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9709 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9710 in_late_binary_op = true;
9711 inner = t = convert (TREE_TYPE (res), t);
9712 in_late_binary_op = save;
9714 /* Strip any conversions, additions, and subtractions, and see if
9715 we are returning the address of a local variable. Warn if so. */
9716 while (1)
9718 switch (TREE_CODE (inner))
9720 CASE_CONVERT:
9721 case NON_LVALUE_EXPR:
9722 case PLUS_EXPR:
9723 case POINTER_PLUS_EXPR:
9724 inner = TREE_OPERAND (inner, 0);
9725 continue;
9727 case MINUS_EXPR:
9728 /* If the second operand of the MINUS_EXPR has a pointer
9729 type (or is converted from it), this may be valid, so
9730 don't give a warning. */
9732 tree op1 = TREE_OPERAND (inner, 1);
9734 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9735 && (CONVERT_EXPR_P (op1)
9736 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9737 op1 = TREE_OPERAND (op1, 0);
9739 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9740 break;
9742 inner = TREE_OPERAND (inner, 0);
9743 continue;
9746 case ADDR_EXPR:
9747 inner = TREE_OPERAND (inner, 0);
9749 while (REFERENCE_CLASS_P (inner)
9750 && TREE_CODE (inner) != INDIRECT_REF)
9751 inner = TREE_OPERAND (inner, 0);
9753 if (DECL_P (inner)
9754 && !DECL_EXTERNAL (inner)
9755 && !TREE_STATIC (inner)
9756 && DECL_CONTEXT (inner) == current_function_decl)
9758 if (TREE_CODE (inner) == LABEL_DECL)
9759 warning_at (loc, OPT_Wreturn_local_addr,
9760 "function returns address of label");
9761 else
9763 warning_at (loc, OPT_Wreturn_local_addr,
9764 "function returns address of local variable");
9765 tree zero = build_zero_cst (TREE_TYPE (res));
9766 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9769 break;
9771 default:
9772 break;
9775 break;
9778 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9779 SET_EXPR_LOCATION (retval, loc);
9781 if (warn_sequence_point)
9782 verify_sequence_points (retval);
9785 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9786 TREE_NO_WARNING (ret_stmt) |= no_warning;
9787 return add_stmt (ret_stmt);
9790 /* Convert EXPR to TYPE if the type of EXPR is
9791 assignment compatible with TYPE.
9792 Otherwise, issue an error (or warning) as appropriate. */
9794 tree
9795 c_cvt_expr_for_assign (location_t loc, tree type, tree expr)
9797 if (expr == NULL_TREE || expr == error_mark_node)
9798 return expr;
9799 return convert_for_assignment (loc, UNKNOWN_LOCATION, type,
9800 expr, TREE_TYPE (expr),
9801 ic_assign, false, NULL_TREE, NULL_TREE, 0);
9804 struct c_switch {
9805 /* The SWITCH_EXPR being built. */
9806 tree switch_expr;
9808 /* The original type of the testing expression, i.e. before the
9809 default conversion is applied. */
9810 tree orig_type;
9812 /* A splay-tree mapping the low element of a case range to the high
9813 element, or NULL_TREE if there is no high element. Used to
9814 determine whether or not a new case label duplicates an old case
9815 label. We need a tree, rather than simply a hash table, because
9816 of the GNU case range extension. */
9817 splay_tree cases;
9819 /* The bindings at the point of the switch. This is used for
9820 warnings crossing decls when branching to a case label. */
9821 struct c_spot_bindings *bindings;
9823 /* The next node on the stack. */
9824 struct c_switch *next;
9827 /* A stack of the currently active switch statements. The innermost
9828 switch statement is on the top of the stack. There is no need to
9829 mark the stack for garbage collection because it is only active
9830 during the processing of the body of a function, and we never
9831 collect at that point. */
9833 struct c_switch *c_switch_stack;
9835 /* Start a C switch statement, testing expression EXP. Return the new
9836 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9837 SWITCH_COND_LOC is the location of the switch's condition.
9838 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9840 tree
9841 c_start_case (location_t switch_loc,
9842 location_t switch_cond_loc,
9843 tree exp, bool explicit_cast_p)
9845 tree orig_type = error_mark_node;
9846 struct c_switch *cs;
9848 if (exp != error_mark_node)
9850 orig_type = TREE_TYPE (exp);
9852 if (!INTEGRAL_TYPE_P (orig_type))
9854 if (orig_type != error_mark_node)
9856 error_at (switch_cond_loc, "switch quantity not an integer");
9857 orig_type = error_mark_node;
9859 exp = integer_zero_node;
9861 else
9863 tree type = TYPE_MAIN_VARIANT (orig_type);
9864 tree e = exp;
9866 /* Warn if the condition has boolean value. */
9867 while (TREE_CODE (e) == COMPOUND_EXPR)
9868 e = TREE_OPERAND (e, 1);
9870 if ((TREE_CODE (type) == BOOLEAN_TYPE
9871 || truth_value_p (TREE_CODE (e)))
9872 /* Explicit cast to int suppresses this warning. */
9873 && !(TREE_CODE (type) == INTEGER_TYPE
9874 && explicit_cast_p))
9875 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9876 "switch condition has boolean value");
9878 if (!in_system_header_at (input_location)
9879 && (type == long_integer_type_node
9880 || type == long_unsigned_type_node))
9881 warning_at (switch_cond_loc,
9882 OPT_Wtraditional, "%<long%> switch expression not "
9883 "converted to %<int%> in ISO C");
9885 exp = c_fully_fold (exp, false, NULL);
9886 exp = default_conversion (exp);
9888 if (warn_sequence_point)
9889 verify_sequence_points (exp);
9893 /* Add this new SWITCH_EXPR to the stack. */
9894 cs = XNEW (struct c_switch);
9895 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9896 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9897 cs->orig_type = orig_type;
9898 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9899 cs->bindings = c_get_switch_bindings ();
9900 cs->next = c_switch_stack;
9901 c_switch_stack = cs;
9903 return add_stmt (cs->switch_expr);
9906 /* Process a case label at location LOC. */
9908 tree
9909 do_case (location_t loc, tree low_value, tree high_value)
9911 tree label = NULL_TREE;
9913 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9915 low_value = c_fully_fold (low_value, false, NULL);
9916 if (TREE_CODE (low_value) == INTEGER_CST)
9917 pedwarn (loc, OPT_Wpedantic,
9918 "case label is not an integer constant expression");
9921 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9923 high_value = c_fully_fold (high_value, false, NULL);
9924 if (TREE_CODE (high_value) == INTEGER_CST)
9925 pedwarn (input_location, OPT_Wpedantic,
9926 "case label is not an integer constant expression");
9929 if (c_switch_stack == NULL)
9931 if (low_value)
9932 error_at (loc, "case label not within a switch statement");
9933 else
9934 error_at (loc, "%<default%> label not within a switch statement");
9935 return NULL_TREE;
9938 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9939 EXPR_LOCATION (c_switch_stack->switch_expr),
9940 loc))
9941 return NULL_TREE;
9943 label = c_add_case_label (loc, c_switch_stack->cases,
9944 SWITCH_COND (c_switch_stack->switch_expr),
9945 c_switch_stack->orig_type,
9946 low_value, high_value);
9947 if (label == error_mark_node)
9948 label = NULL_TREE;
9949 return label;
9952 /* Finish the switch statement. TYPE is the original type of the
9953 controlling expression of the switch, or NULL_TREE. */
9955 void
9956 c_finish_case (tree body, tree type)
9958 struct c_switch *cs = c_switch_stack;
9959 location_t switch_location;
9961 SWITCH_BODY (cs->switch_expr) = body;
9963 /* Emit warnings as needed. */
9964 switch_location = EXPR_LOCATION (cs->switch_expr);
9965 c_do_switch_warnings (cs->cases, switch_location,
9966 type ? type : TREE_TYPE (cs->switch_expr),
9967 SWITCH_COND (cs->switch_expr));
9969 /* Pop the stack. */
9970 c_switch_stack = cs->next;
9971 splay_tree_delete (cs->cases);
9972 c_release_switch_bindings (cs->bindings);
9973 XDELETE (cs);
9976 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9977 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9978 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9979 statement, and was not surrounded with parenthesis. */
9981 void
9982 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9983 tree else_block, bool nested_if)
9985 tree stmt;
9987 /* If the condition has array notations, then the rank of the then_block and
9988 else_block must be either 0 or be equal to the rank of the condition. If
9989 the condition does not have array notations then break them up as it is
9990 broken up in a normal expression. */
9991 if (flag_cilkplus && contains_array_notation_expr (cond))
9993 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9994 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9995 return;
9996 if (then_block
9997 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9998 return;
9999 if (else_block
10000 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10001 return;
10002 if (cond_rank != then_rank && then_rank != 0)
10004 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10005 " and the then-block");
10006 return;
10008 else if (cond_rank != else_rank && else_rank != 0)
10010 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10011 " and the else-block");
10012 return;
10015 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
10016 if (warn_parentheses && nested_if && else_block == NULL)
10018 tree inner_if = then_block;
10020 /* We know from the grammar productions that there is an IF nested
10021 within THEN_BLOCK. Due to labels and c99 conditional declarations,
10022 it might not be exactly THEN_BLOCK, but should be the last
10023 non-container statement within. */
10024 while (1)
10025 switch (TREE_CODE (inner_if))
10027 case COND_EXPR:
10028 goto found;
10029 case BIND_EXPR:
10030 inner_if = BIND_EXPR_BODY (inner_if);
10031 break;
10032 case STATEMENT_LIST:
10033 inner_if = expr_last (then_block);
10034 break;
10035 case TRY_FINALLY_EXPR:
10036 case TRY_CATCH_EXPR:
10037 inner_if = TREE_OPERAND (inner_if, 0);
10038 break;
10039 default:
10040 gcc_unreachable ();
10042 found:
10044 if (COND_EXPR_ELSE (inner_if))
10045 warning_at (if_locus, OPT_Wparentheses,
10046 "suggest explicit braces to avoid ambiguous %<else%>");
10049 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10050 SET_EXPR_LOCATION (stmt, if_locus);
10051 add_stmt (stmt);
10054 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10055 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10056 is false for DO loops. INCR is the FOR increment expression. BODY is
10057 the statement controlled by the loop. BLAB is the break label. CLAB is
10058 the continue label. Everything is allowed to be NULL. */
10060 void
10061 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10062 tree blab, tree clab, bool cond_is_first)
10064 tree entry = NULL, exit = NULL, t;
10066 /* In theory could forbid cilk spawn for loop increment expression,
10067 but it should work just fine. */
10069 /* If the condition is zero don't generate a loop construct. */
10070 if (cond && integer_zerop (cond))
10072 if (cond_is_first)
10074 t = build_and_jump (&blab);
10075 SET_EXPR_LOCATION (t, start_locus);
10076 add_stmt (t);
10079 else
10081 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10083 /* If we have an exit condition, then we build an IF with gotos either
10084 out of the loop, or to the top of it. If there's no exit condition,
10085 then we just build a jump back to the top. */
10086 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10088 if (cond && !integer_nonzerop (cond))
10090 /* Canonicalize the loop condition to the end. This means
10091 generating a branch to the loop condition. Reuse the
10092 continue label, if possible. */
10093 if (cond_is_first)
10095 if (incr || !clab)
10097 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10098 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10100 else
10101 t = build1 (GOTO_EXPR, void_type_node, clab);
10102 SET_EXPR_LOCATION (t, start_locus);
10103 add_stmt (t);
10106 t = build_and_jump (&blab);
10107 if (cond_is_first)
10108 exit = fold_build3_loc (start_locus,
10109 COND_EXPR, void_type_node, cond, exit, t);
10110 else
10111 exit = fold_build3_loc (input_location,
10112 COND_EXPR, void_type_node, cond, exit, t);
10115 add_stmt (top);
10118 if (body)
10119 add_stmt (body);
10120 if (clab)
10121 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10122 if (incr)
10123 add_stmt (incr);
10124 if (entry)
10125 add_stmt (entry);
10126 if (exit)
10127 add_stmt (exit);
10128 if (blab)
10129 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10132 tree
10133 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10135 bool skip;
10136 tree label = *label_p;
10138 /* In switch statements break is sometimes stylistically used after
10139 a return statement. This can lead to spurious warnings about
10140 control reaching the end of a non-void function when it is
10141 inlined. Note that we are calling block_may_fallthru with
10142 language specific tree nodes; this works because
10143 block_may_fallthru returns true when given something it does not
10144 understand. */
10145 skip = !block_may_fallthru (cur_stmt_list);
10147 if (!label)
10149 if (!skip)
10150 *label_p = label = create_artificial_label (loc);
10152 else if (TREE_CODE (label) == LABEL_DECL)
10154 else switch (TREE_INT_CST_LOW (label))
10156 case 0:
10157 if (is_break)
10158 error_at (loc, "break statement not within loop or switch");
10159 else
10160 error_at (loc, "continue statement not within a loop");
10161 return NULL_TREE;
10163 case 1:
10164 gcc_assert (is_break);
10165 error_at (loc, "break statement used with OpenMP for loop");
10166 return NULL_TREE;
10168 case 2:
10169 if (is_break)
10170 error ("break statement within %<#pragma simd%> loop body");
10171 else
10172 error ("continue statement within %<#pragma simd%> loop body");
10173 return NULL_TREE;
10175 default:
10176 gcc_unreachable ();
10179 if (skip)
10180 return NULL_TREE;
10182 if (!is_break)
10183 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10185 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10188 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10190 static void
10191 emit_side_effect_warnings (location_t loc, tree expr)
10193 if (expr == error_mark_node)
10195 else if (!TREE_SIDE_EFFECTS (expr))
10197 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10198 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10200 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10202 tree r = expr;
10203 location_t cloc = loc;
10204 while (TREE_CODE (r) == COMPOUND_EXPR)
10206 if (EXPR_HAS_LOCATION (r))
10207 cloc = EXPR_LOCATION (r);
10208 r = TREE_OPERAND (r, 1);
10210 if (!TREE_SIDE_EFFECTS (r)
10211 && !VOID_TYPE_P (TREE_TYPE (r))
10212 && !CONVERT_EXPR_P (r)
10213 && !TREE_NO_WARNING (r)
10214 && !TREE_NO_WARNING (expr))
10215 warning_at (cloc, OPT_Wunused_value,
10216 "right-hand operand of comma expression has no effect");
10218 else
10219 warn_if_unused_value (expr, loc);
10222 /* Process an expression as if it were a complete statement. Emit
10223 diagnostics, but do not call ADD_STMT. LOC is the location of the
10224 statement. */
10226 tree
10227 c_process_expr_stmt (location_t loc, tree expr)
10229 tree exprv;
10231 if (!expr)
10232 return NULL_TREE;
10234 expr = c_fully_fold (expr, false, NULL);
10236 if (warn_sequence_point)
10237 verify_sequence_points (expr);
10239 if (TREE_TYPE (expr) != error_mark_node
10240 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10241 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10242 error_at (loc, "expression statement has incomplete type");
10244 /* If we're not processing a statement expression, warn about unused values.
10245 Warnings for statement expressions will be emitted later, once we figure
10246 out which is the result. */
10247 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10248 && warn_unused_value)
10249 emit_side_effect_warnings (loc, expr);
10251 exprv = expr;
10252 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10253 exprv = TREE_OPERAND (exprv, 1);
10254 while (CONVERT_EXPR_P (exprv))
10255 exprv = TREE_OPERAND (exprv, 0);
10256 if (DECL_P (exprv)
10257 || handled_component_p (exprv)
10258 || TREE_CODE (exprv) == ADDR_EXPR)
10259 mark_exp_read (exprv);
10261 /* If the expression is not of a type to which we cannot assign a line
10262 number, wrap the thing in a no-op NOP_EXPR. */
10263 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10265 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10266 SET_EXPR_LOCATION (expr, loc);
10269 return expr;
10272 /* Emit an expression as a statement. LOC is the location of the
10273 expression. */
10275 tree
10276 c_finish_expr_stmt (location_t loc, tree expr)
10278 if (expr)
10279 return add_stmt (c_process_expr_stmt (loc, expr));
10280 else
10281 return NULL;
10284 /* Do the opposite and emit a statement as an expression. To begin,
10285 create a new binding level and return it. */
10287 tree
10288 c_begin_stmt_expr (void)
10290 tree ret;
10292 /* We must force a BLOCK for this level so that, if it is not expanded
10293 later, there is a way to turn off the entire subtree of blocks that
10294 are contained in it. */
10295 keep_next_level ();
10296 ret = c_begin_compound_stmt (true);
10298 c_bindings_start_stmt_expr (c_switch_stack == NULL
10299 ? NULL
10300 : c_switch_stack->bindings);
10302 /* Mark the current statement list as belonging to a statement list. */
10303 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10305 return ret;
10308 /* LOC is the location of the compound statement to which this body
10309 belongs. */
10311 tree
10312 c_finish_stmt_expr (location_t loc, tree body)
10314 tree last, type, tmp, val;
10315 tree *last_p;
10317 body = c_end_compound_stmt (loc, body, true);
10319 c_bindings_end_stmt_expr (c_switch_stack == NULL
10320 ? NULL
10321 : c_switch_stack->bindings);
10323 /* Locate the last statement in BODY. See c_end_compound_stmt
10324 about always returning a BIND_EXPR. */
10325 last_p = &BIND_EXPR_BODY (body);
10326 last = BIND_EXPR_BODY (body);
10328 continue_searching:
10329 if (TREE_CODE (last) == STATEMENT_LIST)
10331 tree_stmt_iterator i;
10333 /* This can happen with degenerate cases like ({ }). No value. */
10334 if (!TREE_SIDE_EFFECTS (last))
10335 return body;
10337 /* If we're supposed to generate side effects warnings, process
10338 all of the statements except the last. */
10339 if (warn_unused_value)
10341 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10343 location_t tloc;
10344 tree t = tsi_stmt (i);
10346 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10347 emit_side_effect_warnings (tloc, t);
10350 else
10351 i = tsi_last (last);
10352 last_p = tsi_stmt_ptr (i);
10353 last = *last_p;
10356 /* If the end of the list is exception related, then the list was split
10357 by a call to push_cleanup. Continue searching. */
10358 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10359 || TREE_CODE (last) == TRY_CATCH_EXPR)
10361 last_p = &TREE_OPERAND (last, 0);
10362 last = *last_p;
10363 goto continue_searching;
10366 if (last == error_mark_node)
10367 return last;
10369 /* In the case that the BIND_EXPR is not necessary, return the
10370 expression out from inside it. */
10371 if (last == BIND_EXPR_BODY (body)
10372 && BIND_EXPR_VARS (body) == NULL)
10374 /* Even if this looks constant, do not allow it in a constant
10375 expression. */
10376 last = c_wrap_maybe_const (last, true);
10377 /* Do not warn if the return value of a statement expression is
10378 unused. */
10379 TREE_NO_WARNING (last) = 1;
10380 return last;
10383 /* Extract the type of said expression. */
10384 type = TREE_TYPE (last);
10386 /* If we're not returning a value at all, then the BIND_EXPR that
10387 we already have is a fine expression to return. */
10388 if (!type || VOID_TYPE_P (type))
10389 return body;
10391 /* Now that we've located the expression containing the value, it seems
10392 silly to make voidify_wrapper_expr repeat the process. Create a
10393 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10394 tmp = create_tmp_var_raw (type);
10396 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10397 tree_expr_nonnegative_p giving up immediately. */
10398 val = last;
10399 if (TREE_CODE (val) == NOP_EXPR
10400 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10401 val = TREE_OPERAND (val, 0);
10403 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10404 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10407 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10408 SET_EXPR_LOCATION (t, loc);
10409 return t;
10413 /* Begin and end compound statements. This is as simple as pushing
10414 and popping new statement lists from the tree. */
10416 tree
10417 c_begin_compound_stmt (bool do_scope)
10419 tree stmt = push_stmt_list ();
10420 if (do_scope)
10421 push_scope ();
10422 return stmt;
10425 /* End a compound statement. STMT is the statement. LOC is the
10426 location of the compound statement-- this is usually the location
10427 of the opening brace. */
10429 tree
10430 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10432 tree block = NULL;
10434 if (do_scope)
10436 if (c_dialect_objc ())
10437 objc_clear_super_receiver ();
10438 block = pop_scope ();
10441 stmt = pop_stmt_list (stmt);
10442 stmt = c_build_bind_expr (loc, block, stmt);
10444 /* If this compound statement is nested immediately inside a statement
10445 expression, then force a BIND_EXPR to be created. Otherwise we'll
10446 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10447 STATEMENT_LISTs merge, and thus we can lose track of what statement
10448 was really last. */
10449 if (building_stmt_list_p ()
10450 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10451 && TREE_CODE (stmt) != BIND_EXPR)
10453 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10454 TREE_SIDE_EFFECTS (stmt) = 1;
10455 SET_EXPR_LOCATION (stmt, loc);
10458 return stmt;
10461 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10462 when the current scope is exited. EH_ONLY is true when this is not
10463 meant to apply to normal control flow transfer. */
10465 void
10466 push_cleanup (tree decl, tree cleanup, bool eh_only)
10468 enum tree_code code;
10469 tree stmt, list;
10470 bool stmt_expr;
10472 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10473 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10474 add_stmt (stmt);
10475 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10476 list = push_stmt_list ();
10477 TREE_OPERAND (stmt, 0) = list;
10478 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10481 /* Build a binary-operation expression without default conversions.
10482 CODE is the kind of expression to build.
10483 LOCATION is the operator's location.
10484 This function differs from `build' in several ways:
10485 the data type of the result is computed and recorded in it,
10486 warnings are generated if arg data types are invalid,
10487 special handling for addition and subtraction of pointers is known,
10488 and some optimization is done (operations on narrow ints
10489 are done in the narrower type when that gives the same result).
10490 Constant folding is also done before the result is returned.
10492 Note that the operands will never have enumeral types, or function
10493 or array types, because either they will have the default conversions
10494 performed or they have both just been converted to some other type in which
10495 the arithmetic is to be done. */
10497 tree
10498 build_binary_op (location_t location, enum tree_code code,
10499 tree orig_op0, tree orig_op1, int convert_p)
10501 tree type0, type1, orig_type0, orig_type1;
10502 tree eptype;
10503 enum tree_code code0, code1;
10504 tree op0, op1;
10505 tree ret = error_mark_node;
10506 const char *invalid_op_diag;
10507 bool op0_int_operands, op1_int_operands;
10508 bool int_const, int_const_or_overflow, int_operands;
10510 /* Expression code to give to the expression when it is built.
10511 Normally this is CODE, which is what the caller asked for,
10512 but in some special cases we change it. */
10513 enum tree_code resultcode = code;
10515 /* Data type in which the computation is to be performed.
10516 In the simplest cases this is the common type of the arguments. */
10517 tree result_type = NULL;
10519 /* When the computation is in excess precision, the type of the
10520 final EXCESS_PRECISION_EXPR. */
10521 tree semantic_result_type = NULL;
10523 /* Nonzero means operands have already been type-converted
10524 in whatever way is necessary.
10525 Zero means they need to be converted to RESULT_TYPE. */
10526 int converted = 0;
10528 /* Nonzero means create the expression with this type, rather than
10529 RESULT_TYPE. */
10530 tree build_type = 0;
10532 /* Nonzero means after finally constructing the expression
10533 convert it to this type. */
10534 tree final_type = 0;
10536 /* Nonzero if this is an operation like MIN or MAX which can
10537 safely be computed in short if both args are promoted shorts.
10538 Also implies COMMON.
10539 -1 indicates a bitwise operation; this makes a difference
10540 in the exact conditions for when it is safe to do the operation
10541 in a narrower mode. */
10542 int shorten = 0;
10544 /* Nonzero if this is a comparison operation;
10545 if both args are promoted shorts, compare the original shorts.
10546 Also implies COMMON. */
10547 int short_compare = 0;
10549 /* Nonzero if this is a right-shift operation, which can be computed on the
10550 original short and then promoted if the operand is a promoted short. */
10551 int short_shift = 0;
10553 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10554 int common = 0;
10556 /* True means types are compatible as far as ObjC is concerned. */
10557 bool objc_ok;
10559 /* True means this is an arithmetic operation that may need excess
10560 precision. */
10561 bool may_need_excess_precision;
10563 /* True means this is a boolean operation that converts both its
10564 operands to truth-values. */
10565 bool boolean_op = false;
10567 /* Remember whether we're doing / or %. */
10568 bool doing_div_or_mod = false;
10570 /* Remember whether we're doing << or >>. */
10571 bool doing_shift = false;
10573 /* Tree holding instrumentation expression. */
10574 tree instrument_expr = NULL;
10576 if (location == UNKNOWN_LOCATION)
10577 location = input_location;
10579 op0 = orig_op0;
10580 op1 = orig_op1;
10582 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10583 if (op0_int_operands)
10584 op0 = remove_c_maybe_const_expr (op0);
10585 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10586 if (op1_int_operands)
10587 op1 = remove_c_maybe_const_expr (op1);
10588 int_operands = (op0_int_operands && op1_int_operands);
10589 if (int_operands)
10591 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10592 && TREE_CODE (orig_op1) == INTEGER_CST);
10593 int_const = (int_const_or_overflow
10594 && !TREE_OVERFLOW (orig_op0)
10595 && !TREE_OVERFLOW (orig_op1));
10597 else
10598 int_const = int_const_or_overflow = false;
10600 /* Do not apply default conversion in mixed vector/scalar expression. */
10601 if (convert_p
10602 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10603 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10605 op0 = default_conversion (op0);
10606 op1 = default_conversion (op1);
10609 /* When Cilk Plus is enabled and there are array notations inside op0, then
10610 we check to see if there are builtin array notation functions. If
10611 so, then we take on the type of the array notation inside it. */
10612 if (flag_cilkplus && contains_array_notation_expr (op0))
10613 orig_type0 = type0 = find_correct_array_notation_type (op0);
10614 else
10615 orig_type0 = type0 = TREE_TYPE (op0);
10617 if (flag_cilkplus && contains_array_notation_expr (op1))
10618 orig_type1 = type1 = find_correct_array_notation_type (op1);
10619 else
10620 orig_type1 = type1 = TREE_TYPE (op1);
10622 /* The expression codes of the data types of the arguments tell us
10623 whether the arguments are integers, floating, pointers, etc. */
10624 code0 = TREE_CODE (type0);
10625 code1 = TREE_CODE (type1);
10627 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10628 STRIP_TYPE_NOPS (op0);
10629 STRIP_TYPE_NOPS (op1);
10631 /* If an error was already reported for one of the arguments,
10632 avoid reporting another error. */
10634 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10635 return error_mark_node;
10637 if ((invalid_op_diag
10638 = targetm.invalid_binary_op (code, type0, type1)))
10640 error_at (location, invalid_op_diag);
10641 return error_mark_node;
10644 switch (code)
10646 case PLUS_EXPR:
10647 case MINUS_EXPR:
10648 case MULT_EXPR:
10649 case TRUNC_DIV_EXPR:
10650 case CEIL_DIV_EXPR:
10651 case FLOOR_DIV_EXPR:
10652 case ROUND_DIV_EXPR:
10653 case EXACT_DIV_EXPR:
10654 may_need_excess_precision = true;
10655 break;
10656 default:
10657 may_need_excess_precision = false;
10658 break;
10660 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10662 op0 = TREE_OPERAND (op0, 0);
10663 type0 = TREE_TYPE (op0);
10665 else if (may_need_excess_precision
10666 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10668 type0 = eptype;
10669 op0 = convert (eptype, op0);
10671 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10673 op1 = TREE_OPERAND (op1, 0);
10674 type1 = TREE_TYPE (op1);
10676 else if (may_need_excess_precision
10677 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10679 type1 = eptype;
10680 op1 = convert (eptype, op1);
10683 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10685 /* In case when one of the operands of the binary operation is
10686 a vector and another is a scalar -- convert scalar to vector. */
10687 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10689 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10690 true);
10692 switch (convert_flag)
10694 case stv_error:
10695 return error_mark_node;
10696 case stv_firstarg:
10698 bool maybe_const = true;
10699 tree sc;
10700 sc = c_fully_fold (op0, false, &maybe_const);
10701 sc = save_expr (sc);
10702 sc = convert (TREE_TYPE (type1), sc);
10703 op0 = build_vector_from_val (type1, sc);
10704 if (!maybe_const)
10705 op0 = c_wrap_maybe_const (op0, true);
10706 orig_type0 = type0 = TREE_TYPE (op0);
10707 code0 = TREE_CODE (type0);
10708 converted = 1;
10709 break;
10711 case stv_secondarg:
10713 bool maybe_const = true;
10714 tree sc;
10715 sc = c_fully_fold (op1, false, &maybe_const);
10716 sc = save_expr (sc);
10717 sc = convert (TREE_TYPE (type0), sc);
10718 op1 = build_vector_from_val (type0, sc);
10719 if (!maybe_const)
10720 op1 = c_wrap_maybe_const (op1, true);
10721 orig_type1 = type1 = TREE_TYPE (op1);
10722 code1 = TREE_CODE (type1);
10723 converted = 1;
10724 break;
10726 default:
10727 break;
10731 switch (code)
10733 case PLUS_EXPR:
10734 /* Handle the pointer + int case. */
10735 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10737 ret = c_pointer_int_sum (location, PLUS_EXPR, op0, op1);
10738 goto return_build_binary_op;
10740 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10742 ret = c_pointer_int_sum (location, PLUS_EXPR, op1, op0);
10743 goto return_build_binary_op;
10745 else
10746 common = 1;
10747 break;
10749 case MINUS_EXPR:
10750 /* Subtraction of two similar pointers.
10751 We must subtract them as integers, then divide by object size. */
10752 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10753 && comp_target_types (location, type0, type1))
10755 ret = pointer_diff (location, op0, op1);
10756 goto return_build_binary_op;
10758 /* Handle pointer minus int. Just like pointer plus int. */
10759 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10761 ret = c_pointer_int_sum (location, MINUS_EXPR, op0, op1);
10762 goto return_build_binary_op;
10764 else
10765 common = 1;
10766 break;
10768 case MULT_EXPR:
10769 common = 1;
10770 break;
10772 case TRUNC_DIV_EXPR:
10773 case CEIL_DIV_EXPR:
10774 case FLOOR_DIV_EXPR:
10775 case ROUND_DIV_EXPR:
10776 case EXACT_DIV_EXPR:
10777 doing_div_or_mod = true;
10778 warn_for_div_by_zero (location, op1);
10780 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10781 || code0 == FIXED_POINT_TYPE
10782 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10783 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10784 || code1 == FIXED_POINT_TYPE
10785 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10787 enum tree_code tcode0 = code0, tcode1 = code1;
10789 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10790 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10791 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10792 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10794 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10795 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10796 resultcode = RDIV_EXPR;
10797 else
10798 /* Although it would be tempting to shorten always here, that
10799 loses on some targets, since the modulo instruction is
10800 undefined if the quotient can't be represented in the
10801 computation mode. We shorten only if unsigned or if
10802 dividing by something we know != -1. */
10803 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10804 || (TREE_CODE (op1) == INTEGER_CST
10805 && !integer_all_onesp (op1)));
10806 common = 1;
10808 break;
10810 case BIT_AND_EXPR:
10811 case BIT_IOR_EXPR:
10812 case BIT_XOR_EXPR:
10813 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10814 shorten = -1;
10815 /* Allow vector types which are not floating point types. */
10816 else if (code0 == VECTOR_TYPE
10817 && code1 == VECTOR_TYPE
10818 && !VECTOR_FLOAT_TYPE_P (type0)
10819 && !VECTOR_FLOAT_TYPE_P (type1))
10820 common = 1;
10821 break;
10823 case TRUNC_MOD_EXPR:
10824 case FLOOR_MOD_EXPR:
10825 doing_div_or_mod = true;
10826 warn_for_div_by_zero (location, op1);
10828 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10829 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10830 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10831 common = 1;
10832 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10834 /* Although it would be tempting to shorten always here, that loses
10835 on some targets, since the modulo instruction is undefined if the
10836 quotient can't be represented in the computation mode. We shorten
10837 only if unsigned or if dividing by something we know != -1. */
10838 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10839 || (TREE_CODE (op1) == INTEGER_CST
10840 && !integer_all_onesp (op1)));
10841 common = 1;
10843 break;
10845 case TRUTH_ANDIF_EXPR:
10846 case TRUTH_ORIF_EXPR:
10847 case TRUTH_AND_EXPR:
10848 case TRUTH_OR_EXPR:
10849 case TRUTH_XOR_EXPR:
10850 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10851 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10852 || code0 == FIXED_POINT_TYPE)
10853 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10854 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10855 || code1 == FIXED_POINT_TYPE))
10857 /* Result of these operations is always an int,
10858 but that does not mean the operands should be
10859 converted to ints! */
10860 result_type = integer_type_node;
10861 if (op0_int_operands)
10863 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10864 op0 = remove_c_maybe_const_expr (op0);
10866 else
10867 op0 = c_objc_common_truthvalue_conversion (location, op0);
10868 if (op1_int_operands)
10870 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10871 op1 = remove_c_maybe_const_expr (op1);
10873 else
10874 op1 = c_objc_common_truthvalue_conversion (location, op1);
10875 converted = 1;
10876 boolean_op = true;
10878 if (code == TRUTH_ANDIF_EXPR)
10880 int_const_or_overflow = (int_operands
10881 && TREE_CODE (orig_op0) == INTEGER_CST
10882 && (op0 == truthvalue_false_node
10883 || TREE_CODE (orig_op1) == INTEGER_CST));
10884 int_const = (int_const_or_overflow
10885 && !TREE_OVERFLOW (orig_op0)
10886 && (op0 == truthvalue_false_node
10887 || !TREE_OVERFLOW (orig_op1)));
10889 else if (code == TRUTH_ORIF_EXPR)
10891 int_const_or_overflow = (int_operands
10892 && TREE_CODE (orig_op0) == INTEGER_CST
10893 && (op0 == truthvalue_true_node
10894 || TREE_CODE (orig_op1) == INTEGER_CST));
10895 int_const = (int_const_or_overflow
10896 && !TREE_OVERFLOW (orig_op0)
10897 && (op0 == truthvalue_true_node
10898 || !TREE_OVERFLOW (orig_op1)));
10900 break;
10902 /* Shift operations: result has same type as first operand;
10903 always convert second operand to int.
10904 Also set SHORT_SHIFT if shifting rightward. */
10906 case RSHIFT_EXPR:
10907 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10908 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10910 result_type = type0;
10911 converted = 1;
10913 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10914 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10915 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10916 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10918 result_type = type0;
10919 converted = 1;
10921 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10922 && code1 == INTEGER_TYPE)
10924 doing_shift = true;
10925 if (TREE_CODE (op1) == INTEGER_CST)
10927 if (tree_int_cst_sgn (op1) < 0)
10929 int_const = false;
10930 if (c_inhibit_evaluation_warnings == 0)
10931 warning_at (location, OPT_Wshift_count_negative,
10932 "right shift count is negative");
10934 else
10936 if (!integer_zerop (op1))
10937 short_shift = 1;
10939 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10941 int_const = false;
10942 if (c_inhibit_evaluation_warnings == 0)
10943 warning_at (location, OPT_Wshift_count_overflow,
10944 "right shift count >= width of type");
10949 /* Use the type of the value to be shifted. */
10950 result_type = type0;
10951 /* Avoid converting op1 to result_type later. */
10952 converted = 1;
10954 break;
10956 case LSHIFT_EXPR:
10957 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10958 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10960 result_type = type0;
10961 converted = 1;
10963 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10964 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10965 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10966 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10968 result_type = type0;
10969 converted = 1;
10971 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10972 && code1 == INTEGER_TYPE)
10974 doing_shift = true;
10975 if (TREE_CODE (op1) == INTEGER_CST)
10977 if (tree_int_cst_sgn (op1) < 0)
10979 int_const = false;
10980 if (c_inhibit_evaluation_warnings == 0)
10981 warning_at (location, OPT_Wshift_count_negative,
10982 "left shift count is negative");
10985 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10987 int_const = false;
10988 if (c_inhibit_evaluation_warnings == 0)
10989 warning_at (location, OPT_Wshift_count_overflow,
10990 "left shift count >= width of type");
10994 /* Use the type of the value to be shifted. */
10995 result_type = type0;
10996 /* Avoid converting op1 to result_type later. */
10997 converted = 1;
10999 break;
11001 case EQ_EXPR:
11002 case NE_EXPR:
11003 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11005 tree intt;
11006 if (!vector_types_compatible_elements_p (type0, type1))
11008 error_at (location, "comparing vectors with different "
11009 "element types");
11010 return error_mark_node;
11013 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11015 error_at (location, "comparing vectors with different "
11016 "number of elements");
11017 return error_mark_node;
11020 /* Always construct signed integer vector type. */
11021 intt = c_common_type_for_size (GET_MODE_BITSIZE
11022 (TYPE_MODE (TREE_TYPE (type0))), 0);
11023 result_type = build_opaque_vector_type (intt,
11024 TYPE_VECTOR_SUBPARTS (type0));
11025 converted = 1;
11026 break;
11028 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11029 warning_at (location,
11030 OPT_Wfloat_equal,
11031 "comparing floating point with == or != is unsafe");
11032 /* Result of comparison is always int,
11033 but don't convert the args to int! */
11034 build_type = integer_type_node;
11035 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11036 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11037 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11038 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11039 short_compare = 1;
11040 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11042 if (TREE_CODE (op0) == ADDR_EXPR
11043 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
11045 if (code == EQ_EXPR)
11046 warning_at (location,
11047 OPT_Waddress,
11048 "the comparison will always evaluate as %<false%> "
11049 "for the address of %qD will never be NULL",
11050 TREE_OPERAND (op0, 0));
11051 else
11052 warning_at (location,
11053 OPT_Waddress,
11054 "the comparison will always evaluate as %<true%> "
11055 "for the address of %qD will never be NULL",
11056 TREE_OPERAND (op0, 0));
11058 result_type = type0;
11060 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11062 if (TREE_CODE (op1) == ADDR_EXPR
11063 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
11065 if (code == EQ_EXPR)
11066 warning_at (location,
11067 OPT_Waddress,
11068 "the comparison will always evaluate as %<false%> "
11069 "for the address of %qD will never be NULL",
11070 TREE_OPERAND (op1, 0));
11071 else
11072 warning_at (location,
11073 OPT_Waddress,
11074 "the comparison will always evaluate as %<true%> "
11075 "for the address of %qD will never be NULL",
11076 TREE_OPERAND (op1, 0));
11078 result_type = type1;
11080 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11082 tree tt0 = TREE_TYPE (type0);
11083 tree tt1 = TREE_TYPE (type1);
11084 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11085 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11086 addr_space_t as_common = ADDR_SPACE_GENERIC;
11088 if ((upc_shared_type_p (tt0)
11089 && !(upc_shared_type_p (tt1) || integer_zerop(op1)))
11090 || (upc_shared_type_p (tt1)
11091 && !(upc_shared_type_p (tt0) || integer_zerop(op0))))
11093 error_at (location, "UPC does not allow comparisons "
11094 "between pointers to shared and "
11095 "local pointers");
11096 return error_mark_node;
11098 if (upc_shared_type_p (tt0)
11099 && upc_shared_type_p (tt1) && (tt0 != tt1)
11100 && !(VOID_TYPE_P (tt0) || VOID_TYPE_P (tt1)))
11102 const tree bs_0 = upc_get_block_factor (tt0);
11103 const tree bs_1 = upc_get_block_factor (tt1);
11104 /* Both source and destination are non-void pointers to shared,
11105 whose target types are not equal.
11106 UPC dictates that their blocking factors must be equal. */
11107 if (!tree_int_cst_equal (bs_0, bs_1))
11109 error_at (location, "UPC does not allow comparison "
11110 "between pointers to shared with "
11111 "differing block sizes without a cast");
11112 return error_mark_node;
11115 /* Anything compares with void *. void * compares with anything.
11116 Otherwise, the targets must be compatible
11117 and both must be object or both incomplete. */
11118 if (comp_target_types (location, type0, type1))
11119 result_type = common_pointer_type (type0, type1);
11120 else if (!addr_space_superset (as0, as1, &as_common))
11122 error_at (location, "comparison of pointers to "
11123 "disjoint address spaces");
11124 return error_mark_node;
11126 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11128 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11129 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11130 "comparison of %<void *%> with function pointer");
11132 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11134 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11135 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11136 "comparison of %<void *%> with function pointer");
11138 else
11139 /* Avoid warning about the volatile ObjC EH puts on decls. */
11140 if (!objc_ok)
11141 pedwarn (location, 0,
11142 "comparison of distinct pointer types lacks a cast");
11144 if (result_type == NULL_TREE)
11146 if (upc_shared_type_p(tt0) || upc_shared_type_p(tt1))
11148 result_type = upc_pts_type_node;
11150 else
11152 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11153 result_type = build_pointer_type
11154 (build_qualified_type (void_type_node,
11155 qual));
11159 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11161 result_type = type0;
11162 pedwarn (location, 0, "comparison between pointer and integer");
11164 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11166 result_type = type1;
11167 pedwarn (location, 0, "comparison between pointer and integer");
11169 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11170 || truth_value_p (TREE_CODE (orig_op0)))
11171 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11172 || truth_value_p (TREE_CODE (orig_op1))))
11173 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11174 break;
11176 case LE_EXPR:
11177 case GE_EXPR:
11178 case LT_EXPR:
11179 case GT_EXPR:
11180 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11182 tree intt;
11183 if (!vector_types_compatible_elements_p (type0, type1))
11185 error_at (location, "comparing vectors with different "
11186 "element types");
11187 return error_mark_node;
11190 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11192 error_at (location, "comparing vectors with different "
11193 "number of elements");
11194 return error_mark_node;
11197 /* Always construct signed integer vector type. */
11198 intt = c_common_type_for_size (GET_MODE_BITSIZE
11199 (TYPE_MODE (TREE_TYPE (type0))), 0);
11200 result_type = build_opaque_vector_type (intt,
11201 TYPE_VECTOR_SUBPARTS (type0));
11202 converted = 1;
11203 break;
11205 build_type = integer_type_node;
11206 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11207 || code0 == FIXED_POINT_TYPE)
11208 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11209 || code1 == FIXED_POINT_TYPE))
11210 short_compare = 1;
11211 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11213 const tree tt0 = TREE_TYPE (type0);
11214 const tree tt1 = TREE_TYPE (type1);
11215 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11216 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11217 addr_space_t as_common;
11219 if (upc_shared_type_p (tt0) != upc_shared_type_p (tt1))
11221 error_at (location, "UPC does not allow comparisons between "
11222 "pointers to shared and local pointers");
11223 return error_mark_node;
11225 if (upc_shared_type_p (tt0)
11226 && upc_shared_type_p (tt1) && (tt0 != tt1)
11227 && !(VOID_TYPE_P (tt0) || VOID_TYPE_P (tt1)))
11229 const tree bs_0 = upc_get_block_factor (tt0);
11230 const tree bs_1 = upc_get_block_factor (tt1);
11231 /* Both source and destination are non-void pointers to shared,
11232 whose target types are not equal.
11233 UPC dictates that their blocking factors must be equal. */
11234 if (!tree_int_cst_equal (bs_0, bs_1))
11236 error_at (location, "UPC does not allow comparison "
11237 "between pointers to shared with "
11238 "differing block sizes without a cast");
11239 return error_mark_node;
11242 if (comp_target_types (location, type0, type1))
11244 result_type = common_pointer_type (type0, type1);
11245 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11246 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11247 pedwarn (location, 0,
11248 "comparison of complete and incomplete pointers");
11249 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11250 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11251 "ordered comparisons of pointers to functions");
11252 else if (null_pointer_constant_p (orig_op0)
11253 || null_pointer_constant_p (orig_op1))
11254 warning_at (location, OPT_Wextra,
11255 "ordered comparison of pointer with null pointer");
11258 else if (!addr_space_superset (as0, as1, &as_common))
11260 error_at (location, "comparison of pointers to "
11261 "disjoint address spaces");
11262 return error_mark_node;
11264 else if (upc_shared_type_p (TREE_TYPE (type0))
11265 || upc_shared_type_p (TREE_TYPE (type1)))
11267 result_type = upc_pts_type_node;
11269 else
11271 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11272 result_type = build_pointer_type
11273 (build_qualified_type (void_type_node, qual));
11274 pedwarn (location, 0,
11275 "comparison of distinct pointer types lacks a cast");
11278 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11280 result_type = type0;
11281 if (pedantic)
11282 pedwarn (location, OPT_Wpedantic,
11283 "ordered comparison of pointer with integer zero");
11284 else if (extra_warnings)
11285 warning_at (location, OPT_Wextra,
11286 "ordered comparison of pointer with integer zero");
11288 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11290 result_type = type1;
11291 if (pedantic)
11292 pedwarn (location, OPT_Wpedantic,
11293 "ordered comparison of pointer with integer zero");
11294 else if (extra_warnings)
11295 warning_at (location, OPT_Wextra,
11296 "ordered comparison of pointer with integer zero");
11298 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11300 result_type = type0;
11301 pedwarn (location, 0, "comparison between pointer and integer");
11303 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11305 result_type = type1;
11306 pedwarn (location, 0, "comparison between pointer and integer");
11308 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11309 || truth_value_p (TREE_CODE (orig_op0)))
11310 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11311 || truth_value_p (TREE_CODE (orig_op1))))
11312 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11313 break;
11315 default:
11316 gcc_unreachable ();
11319 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11320 return error_mark_node;
11322 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11323 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11324 || !vector_types_compatible_elements_p (type0, type1)))
11326 binary_op_error (location, code, type0, type1);
11327 return error_mark_node;
11330 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11331 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11333 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11334 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11336 bool first_complex = (code0 == COMPLEX_TYPE);
11337 bool second_complex = (code1 == COMPLEX_TYPE);
11338 int none_complex = (!first_complex && !second_complex);
11340 if (shorten || common || short_compare)
11342 result_type = c_common_type (type0, type1);
11343 do_warn_double_promotion (result_type, type0, type1,
11344 "implicit conversion from %qT to %qT "
11345 "to match other operand of binary "
11346 "expression",
11347 location);
11348 if (result_type == error_mark_node)
11349 return error_mark_node;
11352 if (first_complex != second_complex
11353 && (code == PLUS_EXPR
11354 || code == MINUS_EXPR
11355 || code == MULT_EXPR
11356 || (code == TRUNC_DIV_EXPR && first_complex))
11357 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11358 && flag_signed_zeros)
11360 /* An operation on mixed real/complex operands must be
11361 handled specially, but the language-independent code can
11362 more easily optimize the plain complex arithmetic if
11363 -fno-signed-zeros. */
11364 tree real_type = TREE_TYPE (result_type);
11365 tree real, imag;
11366 if (type0 != orig_type0 || type1 != orig_type1)
11368 gcc_assert (may_need_excess_precision && common);
11369 semantic_result_type = c_common_type (orig_type0, orig_type1);
11371 if (first_complex)
11373 if (TREE_TYPE (op0) != result_type)
11374 op0 = convert_and_check (location, result_type, op0);
11375 if (TREE_TYPE (op1) != real_type)
11376 op1 = convert_and_check (location, real_type, op1);
11378 else
11380 if (TREE_TYPE (op0) != real_type)
11381 op0 = convert_and_check (location, real_type, op0);
11382 if (TREE_TYPE (op1) != result_type)
11383 op1 = convert_and_check (location, result_type, op1);
11385 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11386 return error_mark_node;
11387 if (first_complex)
11389 op0 = c_save_expr (op0);
11390 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11391 op0, 1);
11392 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11393 op0, 1);
11394 switch (code)
11396 case MULT_EXPR:
11397 case TRUNC_DIV_EXPR:
11398 op1 = c_save_expr (op1);
11399 imag = build2 (resultcode, real_type, imag, op1);
11400 /* Fall through. */
11401 case PLUS_EXPR:
11402 case MINUS_EXPR:
11403 real = build2 (resultcode, real_type, real, op1);
11404 break;
11405 default:
11406 gcc_unreachable();
11409 else
11411 op1 = c_save_expr (op1);
11412 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11413 op1, 1);
11414 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11415 op1, 1);
11416 switch (code)
11418 case MULT_EXPR:
11419 op0 = c_save_expr (op0);
11420 imag = build2 (resultcode, real_type, op0, imag);
11421 /* Fall through. */
11422 case PLUS_EXPR:
11423 real = build2 (resultcode, real_type, op0, real);
11424 break;
11425 case MINUS_EXPR:
11426 real = build2 (resultcode, real_type, op0, real);
11427 imag = build1 (NEGATE_EXPR, real_type, imag);
11428 break;
11429 default:
11430 gcc_unreachable();
11433 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11434 goto return_build_binary_op;
11437 /* For certain operations (which identify themselves by shorten != 0)
11438 if both args were extended from the same smaller type,
11439 do the arithmetic in that type and then extend.
11441 shorten !=0 and !=1 indicates a bitwise operation.
11442 For them, this optimization is safe only if
11443 both args are zero-extended or both are sign-extended.
11444 Otherwise, we might change the result.
11445 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11446 but calculated in (unsigned short) it would be (unsigned short)-1. */
11448 if (shorten && none_complex)
11450 final_type = result_type;
11451 result_type = shorten_binary_op (result_type, op0, op1,
11452 shorten == -1);
11455 /* Shifts can be shortened if shifting right. */
11457 if (short_shift)
11459 int unsigned_arg;
11460 tree arg0 = get_narrower (op0, &unsigned_arg);
11462 final_type = result_type;
11464 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11465 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11467 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11468 && tree_int_cst_sgn (op1) > 0
11469 /* We can shorten only if the shift count is less than the
11470 number of bits in the smaller type size. */
11471 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11472 /* We cannot drop an unsigned shift after sign-extension. */
11473 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11475 /* Do an unsigned shift if the operand was zero-extended. */
11476 result_type
11477 = c_common_signed_or_unsigned_type (unsigned_arg,
11478 TREE_TYPE (arg0));
11479 /* Convert value-to-be-shifted to that type. */
11480 if (TREE_TYPE (op0) != result_type)
11481 op0 = convert (result_type, op0);
11482 converted = 1;
11486 /* Comparison operations are shortened too but differently.
11487 They identify themselves by setting short_compare = 1. */
11489 if (short_compare)
11491 /* Don't write &op0, etc., because that would prevent op0
11492 from being kept in a register.
11493 Instead, make copies of the our local variables and
11494 pass the copies by reference, then copy them back afterward. */
11495 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11496 enum tree_code xresultcode = resultcode;
11497 tree val
11498 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11499 &xresultcode);
11501 if (val != 0)
11503 ret = val;
11504 goto return_build_binary_op;
11507 op0 = xop0, op1 = xop1;
11508 converted = 1;
11509 resultcode = xresultcode;
11511 if (c_inhibit_evaluation_warnings == 0)
11513 bool op0_maybe_const = true;
11514 bool op1_maybe_const = true;
11515 tree orig_op0_folded, orig_op1_folded;
11517 if (in_late_binary_op)
11519 orig_op0_folded = orig_op0;
11520 orig_op1_folded = orig_op1;
11522 else
11524 /* Fold for the sake of possible warnings, as in
11525 build_conditional_expr. This requires the
11526 "original" values to be folded, not just op0 and
11527 op1. */
11528 c_inhibit_evaluation_warnings++;
11529 op0 = c_fully_fold (op0, require_constant_value,
11530 &op0_maybe_const);
11531 op1 = c_fully_fold (op1, require_constant_value,
11532 &op1_maybe_const);
11533 c_inhibit_evaluation_warnings--;
11534 orig_op0_folded = c_fully_fold (orig_op0,
11535 require_constant_value,
11536 NULL);
11537 orig_op1_folded = c_fully_fold (orig_op1,
11538 require_constant_value,
11539 NULL);
11542 if (warn_sign_compare)
11543 warn_for_sign_compare (location, orig_op0_folded,
11544 orig_op1_folded, op0, op1,
11545 result_type, resultcode);
11546 if (!in_late_binary_op && !int_operands)
11548 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11549 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11550 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11551 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11557 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11558 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11559 Then the expression will be built.
11560 It will be given type FINAL_TYPE if that is nonzero;
11561 otherwise, it will be given type RESULT_TYPE. */
11563 if (!result_type)
11565 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11566 return error_mark_node;
11569 if (build_type == NULL_TREE)
11571 build_type = result_type;
11572 if ((type0 != orig_type0 || type1 != orig_type1)
11573 && !boolean_op)
11575 gcc_assert (may_need_excess_precision && common);
11576 semantic_result_type = c_common_type (orig_type0, orig_type1);
11580 if (!converted)
11582 op0 = ep_convert_and_check (location, result_type, op0,
11583 semantic_result_type);
11584 op1 = ep_convert_and_check (location, result_type, op1,
11585 semantic_result_type);
11587 /* This can happen if one operand has a vector type, and the other
11588 has a different type. */
11589 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11590 return error_mark_node;
11593 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11594 | SANITIZE_FLOAT_DIVIDE))
11595 && do_ubsan_in_current_function ()
11596 && (doing_div_or_mod || doing_shift))
11598 /* OP0 and/or OP1 might have side-effects. */
11599 op0 = c_save_expr (op0);
11600 op1 = c_save_expr (op1);
11601 op0 = c_fully_fold (op0, false, NULL);
11602 op1 = c_fully_fold (op1, false, NULL);
11603 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11604 | SANITIZE_FLOAT_DIVIDE)))
11605 instrument_expr = ubsan_instrument_division (location, op0, op1);
11606 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11607 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11610 /* Treat expressions in initializers specially as they can't trap. */
11611 if (int_const_or_overflow)
11612 ret = (require_constant_value
11613 ? fold_build2_initializer_loc (location, resultcode, build_type,
11614 op0, op1)
11615 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11616 else
11617 ret = build2 (resultcode, build_type, op0, op1);
11618 if (final_type != 0)
11619 ret = convert (final_type, ret);
11621 return_build_binary_op:
11622 gcc_assert (ret != error_mark_node);
11623 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11624 ret = (int_operands
11625 ? note_integer_operands (ret)
11626 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11627 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11628 && !in_late_binary_op)
11629 ret = note_integer_operands (ret);
11630 if (semantic_result_type)
11631 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11632 protected_set_expr_location (ret, location);
11634 if (instrument_expr != NULL)
11635 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11636 instrument_expr, ret);
11638 return ret;
11641 /* Convert EXPR to be a truth-value, validating its type for this
11642 purpose. LOCATION is the source location for the expression. */
11644 tree
11645 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11647 bool int_const, int_operands;
11649 switch (TREE_CODE (TREE_TYPE (expr)))
11651 case ARRAY_TYPE:
11652 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11653 return error_mark_node;
11655 case RECORD_TYPE:
11656 error_at (location, "used struct type value where scalar is required");
11657 return error_mark_node;
11659 case UNION_TYPE:
11660 error_at (location, "used union type value where scalar is required");
11661 return error_mark_node;
11663 case VOID_TYPE:
11664 error_at (location, "void value not ignored as it ought to be");
11665 return error_mark_node;
11667 case FUNCTION_TYPE:
11668 gcc_unreachable ();
11670 case VECTOR_TYPE:
11671 error_at (location, "used vector type where scalar is required");
11672 return error_mark_node;
11674 default:
11675 break;
11678 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11679 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11680 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11682 expr = remove_c_maybe_const_expr (expr);
11683 expr = build2 (NE_EXPR, integer_type_node, expr,
11684 convert (TREE_TYPE (expr), integer_zero_node));
11685 expr = note_integer_operands (expr);
11687 else
11688 /* ??? Should we also give an error for vectors rather than leaving
11689 those to give errors later? */
11690 expr = c_common_truthvalue_conversion (location, expr);
11692 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11694 if (TREE_OVERFLOW (expr))
11695 return expr;
11696 else
11697 return note_integer_operands (expr);
11699 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11700 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11701 return expr;
11705 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11706 required. */
11708 tree
11709 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11711 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11713 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11714 /* Executing a compound literal inside a function reinitializes
11715 it. */
11716 if (!TREE_STATIC (decl))
11717 *se = true;
11718 return decl;
11720 else
11721 return expr;
11724 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11725 statement. LOC is the location of the OACC_PARALLEL. */
11727 tree
11728 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11730 tree stmt;
11732 block = c_end_compound_stmt (loc, block, true);
11734 stmt = make_node (OACC_PARALLEL);
11735 TREE_TYPE (stmt) = void_type_node;
11736 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11737 OACC_PARALLEL_BODY (stmt) = block;
11738 SET_EXPR_LOCATION (stmt, loc);
11740 return add_stmt (stmt);
11743 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11744 statement. LOC is the location of the OACC_KERNELS. */
11746 tree
11747 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11749 tree stmt;
11751 block = c_end_compound_stmt (loc, block, true);
11753 stmt = make_node (OACC_KERNELS);
11754 TREE_TYPE (stmt) = void_type_node;
11755 OACC_KERNELS_CLAUSES (stmt) = clauses;
11756 OACC_KERNELS_BODY (stmt) = block;
11757 SET_EXPR_LOCATION (stmt, loc);
11759 return add_stmt (stmt);
11762 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11763 statement. LOC is the location of the OACC_DATA. */
11765 tree
11766 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11768 tree stmt;
11770 block = c_end_compound_stmt (loc, block, true);
11772 stmt = make_node (OACC_DATA);
11773 TREE_TYPE (stmt) = void_type_node;
11774 OACC_DATA_CLAUSES (stmt) = clauses;
11775 OACC_DATA_BODY (stmt) = block;
11776 SET_EXPR_LOCATION (stmt, loc);
11778 return add_stmt (stmt);
11781 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11783 tree
11784 c_begin_omp_parallel (void)
11786 tree block;
11788 keep_next_level ();
11789 block = c_begin_compound_stmt (true);
11791 return block;
11794 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11795 statement. LOC is the location of the OMP_PARALLEL. */
11797 tree
11798 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11800 tree stmt;
11802 block = c_end_compound_stmt (loc, block, true);
11804 stmt = make_node (OMP_PARALLEL);
11805 TREE_TYPE (stmt) = void_type_node;
11806 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11807 OMP_PARALLEL_BODY (stmt) = block;
11808 SET_EXPR_LOCATION (stmt, loc);
11810 return add_stmt (stmt);
11813 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11815 tree
11816 c_begin_omp_task (void)
11818 tree block;
11820 keep_next_level ();
11821 block = c_begin_compound_stmt (true);
11823 return block;
11826 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11827 statement. LOC is the location of the #pragma. */
11829 tree
11830 c_finish_omp_task (location_t loc, tree clauses, tree block)
11832 tree stmt;
11834 block = c_end_compound_stmt (loc, block, true);
11836 stmt = make_node (OMP_TASK);
11837 TREE_TYPE (stmt) = void_type_node;
11838 OMP_TASK_CLAUSES (stmt) = clauses;
11839 OMP_TASK_BODY (stmt) = block;
11840 SET_EXPR_LOCATION (stmt, loc);
11842 return add_stmt (stmt);
11845 /* Generate GOMP_cancel call for #pragma omp cancel. */
11847 void
11848 c_finish_omp_cancel (location_t loc, tree clauses)
11850 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11851 int mask = 0;
11852 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11853 mask = 1;
11854 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11855 mask = 2;
11856 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11857 mask = 4;
11858 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11859 mask = 8;
11860 else
11862 error_at (loc, "%<#pragma omp cancel must specify one of "
11863 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11864 "clauses");
11865 return;
11867 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11868 if (ifc != NULL_TREE)
11870 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11871 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11872 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11873 build_zero_cst (type));
11875 else
11876 ifc = boolean_true_node;
11877 tree stmt = build_call_expr_loc (loc, fn, 2,
11878 build_int_cst (integer_type_node, mask),
11879 ifc);
11880 add_stmt (stmt);
11883 /* Generate GOMP_cancellation_point call for
11884 #pragma omp cancellation point. */
11886 void
11887 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11889 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11890 int mask = 0;
11891 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11892 mask = 1;
11893 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11894 mask = 2;
11895 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11896 mask = 4;
11897 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11898 mask = 8;
11899 else
11901 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11902 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11903 "clauses");
11904 return;
11906 tree stmt = build_call_expr_loc (loc, fn, 1,
11907 build_int_cst (integer_type_node, mask));
11908 add_stmt (stmt);
11911 /* Helper function for handle_omp_array_sections. Called recursively
11912 to handle multiple array-section-subscripts. C is the clause,
11913 T current expression (initially OMP_CLAUSE_DECL), which is either
11914 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11915 expression if specified, TREE_VALUE length expression if specified,
11916 TREE_CHAIN is what it has been specified after, or some decl.
11917 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11918 set to true if any of the array-section-subscript could have length
11919 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11920 first array-section-subscript which is known not to have length
11921 of one. Given say:
11922 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11923 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11924 all are or may have length of 1, array-section-subscript [:2] is the
11925 first one knonwn not to have length 1. For array-section-subscript
11926 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11927 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11928 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11929 case though, as some lengths could be zero. */
11931 static tree
11932 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11933 bool &maybe_zero_len, unsigned int &first_non_one)
11935 tree ret, low_bound, length, type;
11936 if (TREE_CODE (t) != TREE_LIST)
11938 if (error_operand_p (t))
11939 return error_mark_node;
11940 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11942 if (DECL_P (t))
11943 error_at (OMP_CLAUSE_LOCATION (c),
11944 "%qD is not a variable in %qs clause", t,
11945 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11946 else
11947 error_at (OMP_CLAUSE_LOCATION (c),
11948 "%qE is not a variable in %qs clause", t,
11949 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11950 return error_mark_node;
11952 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11953 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11955 error_at (OMP_CLAUSE_LOCATION (c),
11956 "%qD is threadprivate variable in %qs clause", t,
11957 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11958 return error_mark_node;
11960 return t;
11963 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11964 maybe_zero_len, first_non_one);
11965 if (ret == error_mark_node || ret == NULL_TREE)
11966 return ret;
11968 type = TREE_TYPE (ret);
11969 low_bound = TREE_PURPOSE (t);
11970 length = TREE_VALUE (t);
11972 if (low_bound == error_mark_node || length == error_mark_node)
11973 return error_mark_node;
11975 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11977 error_at (OMP_CLAUSE_LOCATION (c),
11978 "low bound %qE of array section does not have integral type",
11979 low_bound);
11980 return error_mark_node;
11982 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11984 error_at (OMP_CLAUSE_LOCATION (c),
11985 "length %qE of array section does not have integral type",
11986 length);
11987 return error_mark_node;
11989 if (low_bound
11990 && TREE_CODE (low_bound) == INTEGER_CST
11991 && TYPE_PRECISION (TREE_TYPE (low_bound))
11992 > TYPE_PRECISION (sizetype))
11993 low_bound = fold_convert (sizetype, low_bound);
11994 if (length
11995 && TREE_CODE (length) == INTEGER_CST
11996 && TYPE_PRECISION (TREE_TYPE (length))
11997 > TYPE_PRECISION (sizetype))
11998 length = fold_convert (sizetype, length);
11999 if (low_bound == NULL_TREE)
12000 low_bound = integer_zero_node;
12002 if (length != NULL_TREE)
12004 if (!integer_nonzerop (length))
12005 maybe_zero_len = true;
12006 if (first_non_one == types.length ()
12007 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12008 first_non_one++;
12010 if (TREE_CODE (type) == ARRAY_TYPE)
12012 if (length == NULL_TREE
12013 && (TYPE_DOMAIN (type) == NULL_TREE
12014 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12016 error_at (OMP_CLAUSE_LOCATION (c),
12017 "for unknown bound array type length expression must "
12018 "be specified");
12019 return error_mark_node;
12021 if (TREE_CODE (low_bound) == INTEGER_CST
12022 && tree_int_cst_sgn (low_bound) == -1)
12024 error_at (OMP_CLAUSE_LOCATION (c),
12025 "negative low bound in array section in %qs clause",
12026 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12027 return error_mark_node;
12029 if (length != NULL_TREE
12030 && TREE_CODE (length) == INTEGER_CST
12031 && tree_int_cst_sgn (length) == -1)
12033 error_at (OMP_CLAUSE_LOCATION (c),
12034 "negative length in array section in %qs clause",
12035 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12036 return error_mark_node;
12038 if (TYPE_DOMAIN (type)
12039 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12040 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12041 == INTEGER_CST)
12043 tree size = size_binop (PLUS_EXPR,
12044 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12045 size_one_node);
12046 if (TREE_CODE (low_bound) == INTEGER_CST)
12048 if (tree_int_cst_lt (size, low_bound))
12050 error_at (OMP_CLAUSE_LOCATION (c),
12051 "low bound %qE above array section size "
12052 "in %qs clause", low_bound,
12053 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12054 return error_mark_node;
12056 if (tree_int_cst_equal (size, low_bound))
12057 maybe_zero_len = true;
12058 else if (length == NULL_TREE
12059 && first_non_one == types.length ()
12060 && tree_int_cst_equal
12061 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12062 low_bound))
12063 first_non_one++;
12065 else if (length == NULL_TREE)
12067 maybe_zero_len = true;
12068 if (first_non_one == types.length ())
12069 first_non_one++;
12071 if (length && TREE_CODE (length) == INTEGER_CST)
12073 if (tree_int_cst_lt (size, length))
12075 error_at (OMP_CLAUSE_LOCATION (c),
12076 "length %qE above array section size "
12077 "in %qs clause", length,
12078 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12079 return error_mark_node;
12081 if (TREE_CODE (low_bound) == INTEGER_CST)
12083 tree lbpluslen
12084 = size_binop (PLUS_EXPR,
12085 fold_convert (sizetype, low_bound),
12086 fold_convert (sizetype, length));
12087 if (TREE_CODE (lbpluslen) == INTEGER_CST
12088 && tree_int_cst_lt (size, lbpluslen))
12090 error_at (OMP_CLAUSE_LOCATION (c),
12091 "high bound %qE above array section size "
12092 "in %qs clause", lbpluslen,
12093 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12094 return error_mark_node;
12099 else if (length == NULL_TREE)
12101 maybe_zero_len = true;
12102 if (first_non_one == types.length ())
12103 first_non_one++;
12106 /* For [lb:] we will need to evaluate lb more than once. */
12107 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12109 tree lb = c_save_expr (low_bound);
12110 if (lb != low_bound)
12112 TREE_PURPOSE (t) = lb;
12113 low_bound = lb;
12117 else if (TREE_CODE (type) == POINTER_TYPE)
12119 if (length == NULL_TREE)
12121 error_at (OMP_CLAUSE_LOCATION (c),
12122 "for pointer type length expression must be specified");
12123 return error_mark_node;
12125 /* If there is a pointer type anywhere but in the very first
12126 array-section-subscript, the array section can't be contiguous. */
12127 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12128 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12130 error_at (OMP_CLAUSE_LOCATION (c),
12131 "array section is not contiguous in %qs clause",
12132 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12133 return error_mark_node;
12136 else
12138 error_at (OMP_CLAUSE_LOCATION (c),
12139 "%qE does not have pointer or array type", ret);
12140 return error_mark_node;
12142 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12143 types.safe_push (TREE_TYPE (ret));
12144 /* We will need to evaluate lb more than once. */
12145 tree lb = c_save_expr (low_bound);
12146 if (lb != low_bound)
12148 TREE_PURPOSE (t) = lb;
12149 low_bound = lb;
12151 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12152 return ret;
12155 /* Handle array sections for clause C. */
12157 static bool
12158 handle_omp_array_sections (tree c)
12160 bool maybe_zero_len = false;
12161 unsigned int first_non_one = 0;
12162 vec<tree> types = vNULL;
12163 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12164 maybe_zero_len, first_non_one);
12165 if (first == error_mark_node)
12167 types.release ();
12168 return true;
12170 if (first == NULL_TREE)
12172 types.release ();
12173 return false;
12175 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12177 tree t = OMP_CLAUSE_DECL (c);
12178 tree tem = NULL_TREE;
12179 types.release ();
12180 /* Need to evaluate side effects in the length expressions
12181 if any. */
12182 while (TREE_CODE (t) == TREE_LIST)
12184 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12186 if (tem == NULL_TREE)
12187 tem = TREE_VALUE (t);
12188 else
12189 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12190 TREE_VALUE (t), tem);
12192 t = TREE_CHAIN (t);
12194 if (tem)
12195 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12196 first = c_fully_fold (first, false, NULL);
12197 OMP_CLAUSE_DECL (c) = first;
12199 else
12201 unsigned int num = types.length (), i;
12202 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12203 tree condition = NULL_TREE;
12205 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12206 maybe_zero_len = true;
12208 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12209 t = TREE_CHAIN (t))
12211 tree low_bound = TREE_PURPOSE (t);
12212 tree length = TREE_VALUE (t);
12214 i--;
12215 if (low_bound
12216 && TREE_CODE (low_bound) == INTEGER_CST
12217 && TYPE_PRECISION (TREE_TYPE (low_bound))
12218 > TYPE_PRECISION (sizetype))
12219 low_bound = fold_convert (sizetype, low_bound);
12220 if (length
12221 && TREE_CODE (length) == INTEGER_CST
12222 && TYPE_PRECISION (TREE_TYPE (length))
12223 > TYPE_PRECISION (sizetype))
12224 length = fold_convert (sizetype, length);
12225 if (low_bound == NULL_TREE)
12226 low_bound = integer_zero_node;
12227 if (!maybe_zero_len && i > first_non_one)
12229 if (integer_nonzerop (low_bound))
12230 goto do_warn_noncontiguous;
12231 if (length != NULL_TREE
12232 && TREE_CODE (length) == INTEGER_CST
12233 && TYPE_DOMAIN (types[i])
12234 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12235 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12236 == INTEGER_CST)
12238 tree size;
12239 size = size_binop (PLUS_EXPR,
12240 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12241 size_one_node);
12242 if (!tree_int_cst_equal (length, size))
12244 do_warn_noncontiguous:
12245 error_at (OMP_CLAUSE_LOCATION (c),
12246 "array section is not contiguous in %qs "
12247 "clause",
12248 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12249 types.release ();
12250 return true;
12253 if (length != NULL_TREE
12254 && TREE_SIDE_EFFECTS (length))
12256 if (side_effects == NULL_TREE)
12257 side_effects = length;
12258 else
12259 side_effects = build2 (COMPOUND_EXPR,
12260 TREE_TYPE (side_effects),
12261 length, side_effects);
12264 else
12266 tree l;
12268 if (i > first_non_one && length && integer_nonzerop (length))
12269 continue;
12270 if (length)
12271 l = fold_convert (sizetype, length);
12272 else
12274 l = size_binop (PLUS_EXPR,
12275 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12276 size_one_node);
12277 l = size_binop (MINUS_EXPR, l,
12278 fold_convert (sizetype, low_bound));
12280 if (i > first_non_one)
12282 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12283 size_zero_node);
12284 if (condition == NULL_TREE)
12285 condition = l;
12286 else
12287 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12288 l, condition);
12290 else if (size == NULL_TREE)
12292 size = size_in_bytes (TREE_TYPE (types[i]));
12293 size = size_binop (MULT_EXPR, size, l);
12294 if (condition)
12295 size = fold_build3 (COND_EXPR, sizetype, condition,
12296 size, size_zero_node);
12298 else
12299 size = size_binop (MULT_EXPR, size, l);
12302 types.release ();
12303 if (side_effects)
12304 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12305 first = c_fully_fold (first, false, NULL);
12306 OMP_CLAUSE_DECL (c) = first;
12307 if (size)
12308 size = c_fully_fold (size, false, NULL);
12309 OMP_CLAUSE_SIZE (c) = size;
12310 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12311 return false;
12312 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12313 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12314 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12315 if (!c_mark_addressable (t))
12316 return false;
12317 OMP_CLAUSE_DECL (c2) = t;
12318 t = build_fold_addr_expr (first);
12319 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12320 tree ptr = OMP_CLAUSE_DECL (c2);
12321 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12322 ptr = build_fold_addr_expr (ptr);
12323 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12324 ptrdiff_type_node, t,
12325 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12326 ptrdiff_type_node, ptr));
12327 t = c_fully_fold (t, false, NULL);
12328 OMP_CLAUSE_SIZE (c2) = t;
12329 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12330 OMP_CLAUSE_CHAIN (c) = c2;
12332 return false;
12335 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12336 an inline call. But, remap
12337 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12338 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12340 static tree
12341 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12342 tree decl, tree placeholder)
12344 copy_body_data id;
12345 hash_map<tree, tree> decl_map;
12347 decl_map.put (omp_decl1, placeholder);
12348 decl_map.put (omp_decl2, decl);
12349 memset (&id, 0, sizeof (id));
12350 id.src_fn = DECL_CONTEXT (omp_decl1);
12351 id.dst_fn = current_function_decl;
12352 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12353 id.decl_map = &decl_map;
12355 id.copy_decl = copy_decl_no_change;
12356 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12357 id.transform_new_cfg = true;
12358 id.transform_return_to_modify = false;
12359 id.transform_lang_insert_block = NULL;
12360 id.eh_lp_nr = 0;
12361 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12362 return stmt;
12365 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12366 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12368 static tree
12369 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12371 if (*tp == (tree) data)
12372 return *tp;
12373 return NULL_TREE;
12376 /* For all elements of CLAUSES, validate them against their constraints.
12377 Remove any elements from the list that are invalid. */
12379 tree
12380 c_finish_omp_clauses (tree clauses)
12382 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12383 bitmap_head aligned_head;
12384 tree c, t, *pc;
12385 bool branch_seen = false;
12386 bool copyprivate_seen = false;
12387 tree *nowait_clause = NULL;
12389 bitmap_obstack_initialize (NULL);
12390 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12391 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12392 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12393 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12395 for (pc = &clauses, c = clauses; c ; c = *pc)
12397 bool remove = false;
12398 bool need_complete = false;
12399 bool need_implicitly_determined = false;
12401 switch (OMP_CLAUSE_CODE (c))
12403 case OMP_CLAUSE_SHARED:
12404 need_implicitly_determined = true;
12405 goto check_dup_generic;
12407 case OMP_CLAUSE_PRIVATE:
12408 need_complete = true;
12409 need_implicitly_determined = true;
12410 goto check_dup_generic;
12412 case OMP_CLAUSE_REDUCTION:
12413 need_implicitly_determined = true;
12414 t = OMP_CLAUSE_DECL (c);
12415 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12416 && (FLOAT_TYPE_P (TREE_TYPE (t))
12417 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12419 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12420 const char *r_name = NULL;
12422 switch (r_code)
12424 case PLUS_EXPR:
12425 case MULT_EXPR:
12426 case MINUS_EXPR:
12427 break;
12428 case MIN_EXPR:
12429 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12430 r_name = "min";
12431 break;
12432 case MAX_EXPR:
12433 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12434 r_name = "max";
12435 break;
12436 case BIT_AND_EXPR:
12437 r_name = "&";
12438 break;
12439 case BIT_XOR_EXPR:
12440 r_name = "^";
12441 break;
12442 case BIT_IOR_EXPR:
12443 r_name = "|";
12444 break;
12445 case TRUTH_ANDIF_EXPR:
12446 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12447 r_name = "&&";
12448 break;
12449 case TRUTH_ORIF_EXPR:
12450 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12451 r_name = "||";
12452 break;
12453 default:
12454 gcc_unreachable ();
12456 if (r_name)
12458 error_at (OMP_CLAUSE_LOCATION (c),
12459 "%qE has invalid type for %<reduction(%s)%>",
12460 t, r_name);
12461 remove = true;
12462 break;
12465 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12467 error_at (OMP_CLAUSE_LOCATION (c),
12468 "user defined reduction not found for %qD", t);
12469 remove = true;
12470 break;
12472 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12474 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12475 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12476 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12477 VAR_DECL, NULL_TREE, type);
12478 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12479 DECL_ARTIFICIAL (placeholder) = 1;
12480 DECL_IGNORED_P (placeholder) = 1;
12481 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12482 c_mark_addressable (placeholder);
12483 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12484 c_mark_addressable (OMP_CLAUSE_DECL (c));
12485 OMP_CLAUSE_REDUCTION_MERGE (c)
12486 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12487 TREE_VEC_ELT (list, 0),
12488 TREE_VEC_ELT (list, 1),
12489 OMP_CLAUSE_DECL (c), placeholder);
12490 OMP_CLAUSE_REDUCTION_MERGE (c)
12491 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12492 void_type_node, NULL_TREE,
12493 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12494 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12495 if (TREE_VEC_LENGTH (list) == 6)
12497 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12498 c_mark_addressable (OMP_CLAUSE_DECL (c));
12499 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12500 c_mark_addressable (placeholder);
12501 tree init = TREE_VEC_ELT (list, 5);
12502 if (init == error_mark_node)
12503 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12504 OMP_CLAUSE_REDUCTION_INIT (c)
12505 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12506 TREE_VEC_ELT (list, 3),
12507 OMP_CLAUSE_DECL (c), placeholder);
12508 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12509 OMP_CLAUSE_REDUCTION_INIT (c)
12510 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12511 OMP_CLAUSE_REDUCTION_INIT (c));
12512 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12513 c_find_omp_placeholder_r,
12514 placeholder, NULL))
12515 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12517 else
12519 tree init;
12520 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12521 init = build_constructor (TREE_TYPE (t), NULL);
12522 else
12523 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12524 OMP_CLAUSE_REDUCTION_INIT (c)
12525 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12527 OMP_CLAUSE_REDUCTION_INIT (c)
12528 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12529 void_type_node, NULL_TREE,
12530 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12531 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12533 goto check_dup_generic;
12535 case OMP_CLAUSE_COPYPRIVATE:
12536 copyprivate_seen = true;
12537 if (nowait_clause)
12539 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12540 "%<nowait%> clause must not be used together "
12541 "with %<copyprivate%>");
12542 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12543 nowait_clause = NULL;
12545 goto check_dup_generic;
12547 case OMP_CLAUSE_COPYIN:
12548 t = OMP_CLAUSE_DECL (c);
12549 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12551 error_at (OMP_CLAUSE_LOCATION (c),
12552 "%qE must be %<threadprivate%> for %<copyin%>", t);
12553 remove = true;
12554 break;
12556 goto check_dup_generic;
12558 case OMP_CLAUSE_LINEAR:
12559 t = OMP_CLAUSE_DECL (c);
12560 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12561 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12563 error_at (OMP_CLAUSE_LOCATION (c),
12564 "linear clause applied to non-integral non-pointer "
12565 "variable with type %qT", TREE_TYPE (t));
12566 remove = true;
12567 break;
12569 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12571 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12572 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12573 OMP_CLAUSE_DECL (c), s);
12574 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12575 sizetype, s, OMP_CLAUSE_DECL (c));
12576 if (s == error_mark_node)
12577 s = size_one_node;
12578 OMP_CLAUSE_LINEAR_STEP (c) = s;
12580 else
12581 OMP_CLAUSE_LINEAR_STEP (c)
12582 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12583 goto check_dup_generic;
12585 check_dup_generic:
12586 t = OMP_CLAUSE_DECL (c);
12587 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12589 error_at (OMP_CLAUSE_LOCATION (c),
12590 "%qE is not a variable in clause %qs", t,
12591 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12592 remove = true;
12594 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12595 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12596 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12598 error_at (OMP_CLAUSE_LOCATION (c),
12599 "%qE appears more than once in data clauses", t);
12600 remove = true;
12602 else
12603 bitmap_set_bit (&generic_head, DECL_UID (t));
12604 break;
12606 case OMP_CLAUSE_FIRSTPRIVATE:
12607 t = OMP_CLAUSE_DECL (c);
12608 need_complete = true;
12609 need_implicitly_determined = true;
12610 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12612 error_at (OMP_CLAUSE_LOCATION (c),
12613 "%qE is not a variable in clause %<firstprivate%>", t);
12614 remove = true;
12616 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12617 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12619 error_at (OMP_CLAUSE_LOCATION (c),
12620 "%qE appears more than once in data clauses", t);
12621 remove = true;
12623 else
12624 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12625 break;
12627 case OMP_CLAUSE_LASTPRIVATE:
12628 t = OMP_CLAUSE_DECL (c);
12629 need_complete = true;
12630 need_implicitly_determined = true;
12631 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12633 error_at (OMP_CLAUSE_LOCATION (c),
12634 "%qE is not a variable in clause %<lastprivate%>", t);
12635 remove = true;
12637 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12638 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12640 error_at (OMP_CLAUSE_LOCATION (c),
12641 "%qE appears more than once in data clauses", t);
12642 remove = true;
12644 else
12645 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12646 break;
12648 case OMP_CLAUSE_ALIGNED:
12649 t = OMP_CLAUSE_DECL (c);
12650 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12652 error_at (OMP_CLAUSE_LOCATION (c),
12653 "%qE is not a variable in %<aligned%> clause", t);
12654 remove = true;
12656 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12657 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12659 error_at (OMP_CLAUSE_LOCATION (c),
12660 "%qE in %<aligned%> clause is neither a pointer nor "
12661 "an array", t);
12662 remove = true;
12664 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12666 error_at (OMP_CLAUSE_LOCATION (c),
12667 "%qE appears more than once in %<aligned%> clauses",
12669 remove = true;
12671 else
12672 bitmap_set_bit (&aligned_head, DECL_UID (t));
12673 break;
12675 case OMP_CLAUSE_DEPEND:
12676 t = OMP_CLAUSE_DECL (c);
12677 if (TREE_CODE (t) == TREE_LIST)
12679 if (handle_omp_array_sections (c))
12680 remove = true;
12681 break;
12683 if (t == error_mark_node)
12684 remove = true;
12685 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12687 error_at (OMP_CLAUSE_LOCATION (c),
12688 "%qE is not a variable in %<depend%> clause", t);
12689 remove = true;
12691 else if (!c_mark_addressable (t))
12692 remove = true;
12693 break;
12695 case OMP_CLAUSE_MAP:
12696 case OMP_CLAUSE_TO:
12697 case OMP_CLAUSE_FROM:
12698 case OMP_CLAUSE__CACHE_:
12699 t = OMP_CLAUSE_DECL (c);
12700 if (TREE_CODE (t) == TREE_LIST)
12702 if (handle_omp_array_sections (c))
12703 remove = true;
12704 else
12706 t = OMP_CLAUSE_DECL (c);
12707 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12709 error_at (OMP_CLAUSE_LOCATION (c),
12710 "array section does not have mappable type "
12711 "in %qs clause",
12712 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12713 remove = true;
12716 break;
12718 if (t == error_mark_node)
12719 remove = true;
12720 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12722 error_at (OMP_CLAUSE_LOCATION (c),
12723 "%qE is not a variable in %qs clause", t,
12724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12725 remove = true;
12727 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12729 error_at (OMP_CLAUSE_LOCATION (c),
12730 "%qD is threadprivate variable in %qs clause", t,
12731 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12732 remove = true;
12734 else if (!c_mark_addressable (t))
12735 remove = true;
12736 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12737 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12738 || (OMP_CLAUSE_MAP_KIND (c)
12739 == GOMP_MAP_FORCE_DEVICEPTR)))
12740 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12742 error_at (OMP_CLAUSE_LOCATION (c),
12743 "%qD does not have a mappable type in %qs clause", t,
12744 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12745 remove = true;
12747 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12749 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12750 error ("%qD appears more than once in motion clauses", t);
12751 else
12752 error ("%qD appears more than once in map clauses", t);
12753 remove = true;
12755 else
12756 bitmap_set_bit (&generic_head, DECL_UID (t));
12757 break;
12759 case OMP_CLAUSE_UNIFORM:
12760 t = OMP_CLAUSE_DECL (c);
12761 if (TREE_CODE (t) != PARM_DECL)
12763 if (DECL_P (t))
12764 error_at (OMP_CLAUSE_LOCATION (c),
12765 "%qD is not an argument in %<uniform%> clause", t);
12766 else
12767 error_at (OMP_CLAUSE_LOCATION (c),
12768 "%qE is not an argument in %<uniform%> clause", t);
12769 remove = true;
12770 break;
12772 goto check_dup_generic;
12774 case OMP_CLAUSE_NOWAIT:
12775 if (copyprivate_seen)
12777 error_at (OMP_CLAUSE_LOCATION (c),
12778 "%<nowait%> clause must not be used together "
12779 "with %<copyprivate%>");
12780 remove = true;
12781 break;
12783 nowait_clause = pc;
12784 pc = &OMP_CLAUSE_CHAIN (c);
12785 continue;
12787 case OMP_CLAUSE_IF:
12788 case OMP_CLAUSE_NUM_THREADS:
12789 case OMP_CLAUSE_NUM_TEAMS:
12790 case OMP_CLAUSE_THREAD_LIMIT:
12791 case OMP_CLAUSE_SCHEDULE:
12792 case OMP_CLAUSE_ORDERED:
12793 case OMP_CLAUSE_DEFAULT:
12794 case OMP_CLAUSE_UNTIED:
12795 case OMP_CLAUSE_COLLAPSE:
12796 case OMP_CLAUSE_FINAL:
12797 case OMP_CLAUSE_MERGEABLE:
12798 case OMP_CLAUSE_SAFELEN:
12799 case OMP_CLAUSE_SIMDLEN:
12800 case OMP_CLAUSE_DEVICE:
12801 case OMP_CLAUSE_DIST_SCHEDULE:
12802 case OMP_CLAUSE_PARALLEL:
12803 case OMP_CLAUSE_FOR:
12804 case OMP_CLAUSE_SECTIONS:
12805 case OMP_CLAUSE_TASKGROUP:
12806 case OMP_CLAUSE_PROC_BIND:
12807 case OMP_CLAUSE__CILK_FOR_COUNT_:
12808 case OMP_CLAUSE_NUM_GANGS:
12809 case OMP_CLAUSE_NUM_WORKERS:
12810 case OMP_CLAUSE_VECTOR_LENGTH:
12811 case OMP_CLAUSE_ASYNC:
12812 case OMP_CLAUSE_WAIT:
12813 case OMP_CLAUSE_AUTO:
12814 case OMP_CLAUSE_SEQ:
12815 case OMP_CLAUSE_GANG:
12816 case OMP_CLAUSE_WORKER:
12817 case OMP_CLAUSE_VECTOR:
12818 pc = &OMP_CLAUSE_CHAIN (c);
12819 continue;
12821 case OMP_CLAUSE_INBRANCH:
12822 case OMP_CLAUSE_NOTINBRANCH:
12823 if (branch_seen)
12825 error_at (OMP_CLAUSE_LOCATION (c),
12826 "%<inbranch%> clause is incompatible with "
12827 "%<notinbranch%>");
12828 remove = true;
12829 break;
12831 branch_seen = true;
12832 pc = &OMP_CLAUSE_CHAIN (c);
12833 continue;
12835 default:
12836 gcc_unreachable ();
12839 if (!remove)
12841 t = OMP_CLAUSE_DECL (c);
12843 if (need_complete)
12845 t = require_complete_type (t);
12846 if (t == error_mark_node)
12847 remove = true;
12850 if (need_implicitly_determined)
12852 const char *share_name = NULL;
12854 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12855 share_name = "threadprivate";
12856 else switch (c_omp_predetermined_sharing (t))
12858 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12859 break;
12860 case OMP_CLAUSE_DEFAULT_SHARED:
12861 /* const vars may be specified in firstprivate clause. */
12862 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12863 && TREE_READONLY (t))
12864 break;
12865 share_name = "shared";
12866 break;
12867 case OMP_CLAUSE_DEFAULT_PRIVATE:
12868 share_name = "private";
12869 break;
12870 default:
12871 gcc_unreachable ();
12873 if (share_name)
12875 error_at (OMP_CLAUSE_LOCATION (c),
12876 "%qE is predetermined %qs for %qs",
12877 t, share_name,
12878 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12879 remove = true;
12884 if (remove)
12885 *pc = OMP_CLAUSE_CHAIN (c);
12886 else
12887 pc = &OMP_CLAUSE_CHAIN (c);
12890 bitmap_obstack_release (NULL);
12891 return clauses;
12894 /* Create a transaction node. */
12896 tree
12897 c_finish_transaction (location_t loc, tree block, int flags)
12899 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12900 if (flags & TM_STMT_ATTR_OUTER)
12901 TRANSACTION_EXPR_OUTER (stmt) = 1;
12902 if (flags & TM_STMT_ATTR_RELAXED)
12903 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12904 return add_stmt (stmt);
12907 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12908 down to the element type of an array. */
12910 tree
12911 c_build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
12913 if (type == error_mark_node)
12914 return type;
12916 if (TREE_CODE (type) == ARRAY_TYPE)
12918 tree t;
12919 tree element_type = c_build_qualified_type_1 (TREE_TYPE (type),
12920 type_quals,
12921 layout_qualifier);
12923 /* See if we already have an identically qualified type. */
12924 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12926 const tree t_elem_type = strip_array_types (t);
12927 tree t_elem_block_factor = TYPE_UPC_BLOCK_FACTOR (t_elem_type);
12928 if (TYPE_QUALS (t_elem_type) == type_quals
12929 && t_elem_block_factor == layout_qualifier
12930 && TYPE_NAME (t) == TYPE_NAME (type)
12931 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12932 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12933 TYPE_ATTRIBUTES (type)))
12934 break;
12936 if (!t)
12938 tree domain = TYPE_DOMAIN (type);
12940 t = build_variant_type_copy (type);
12941 TREE_TYPE (t) = element_type;
12943 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12944 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12945 SET_TYPE_STRUCTURAL_EQUALITY (t);
12946 else if (TYPE_CANONICAL (element_type) != element_type
12947 || (domain && TYPE_CANONICAL (domain) != domain))
12949 tree unqualified_canon
12950 = build_array_type (TYPE_CANONICAL (element_type),
12951 domain? TYPE_CANONICAL (domain)
12952 : NULL_TREE);
12953 TYPE_CANONICAL (t)
12954 = c_build_qualified_type_1 (unqualified_canon, type_quals,
12955 layout_qualifier);
12957 else
12958 TYPE_CANONICAL (t) = t;
12960 return t;
12963 /* A restrict-qualified pointer type must be a pointer to object or
12964 incomplete type. Note that the use of POINTER_TYPE_P also allows
12965 REFERENCE_TYPEs, which is appropriate for C++. */
12966 if ((type_quals & TYPE_QUAL_RESTRICT)
12967 && (!POINTER_TYPE_P (type)
12968 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12970 error ("invalid use of %<restrict%>");
12971 type_quals &= ~TYPE_QUAL_RESTRICT;
12974 return build_qualified_type_1 (type, type_quals, layout_qualifier);
12977 /* Build a VA_ARG_EXPR for the C parser. */
12979 tree
12980 c_build_va_arg (location_t loc, tree expr, tree type)
12982 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12983 warning_at (loc, OPT_Wc___compat,
12984 "C++ requires promoted type, not enum type, in %<va_arg%>");
12985 return build_va_arg (loc, expr, type);
12988 /* Return truthvalue of whether T1 is the same tree structure as T2.
12989 Return 1 if they are the same. Return 0 if they are different. */
12991 bool
12992 c_tree_equal (tree t1, tree t2)
12994 enum tree_code code1, code2;
12996 if (t1 == t2)
12997 return true;
12998 if (!t1 || !t2)
12999 return false;
13001 for (code1 = TREE_CODE (t1);
13002 CONVERT_EXPR_CODE_P (code1)
13003 || code1 == NON_LVALUE_EXPR;
13004 code1 = TREE_CODE (t1))
13005 t1 = TREE_OPERAND (t1, 0);
13006 for (code2 = TREE_CODE (t2);
13007 CONVERT_EXPR_CODE_P (code2)
13008 || code2 == NON_LVALUE_EXPR;
13009 code2 = TREE_CODE (t2))
13010 t2 = TREE_OPERAND (t2, 0);
13012 /* They might have become equal now. */
13013 if (t1 == t2)
13014 return true;
13016 if (code1 != code2)
13017 return false;
13019 switch (code1)
13021 case INTEGER_CST:
13022 return wi::eq_p (t1, t2);
13024 case REAL_CST:
13025 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
13027 case STRING_CST:
13028 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13029 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13030 TREE_STRING_LENGTH (t1));
13032 case FIXED_CST:
13033 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13034 TREE_FIXED_CST (t2));
13036 case COMPLEX_CST:
13037 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13038 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13040 case VECTOR_CST:
13041 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13043 case CONSTRUCTOR:
13044 /* We need to do this when determining whether or not two
13045 non-type pointer to member function template arguments
13046 are the same. */
13047 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13048 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13049 return false;
13051 tree field, value;
13052 unsigned int i;
13053 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13055 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13056 if (!c_tree_equal (field, elt2->index)
13057 || !c_tree_equal (value, elt2->value))
13058 return false;
13061 return true;
13063 case TREE_LIST:
13064 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13065 return false;
13066 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13067 return false;
13068 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13070 case SAVE_EXPR:
13071 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13073 case CALL_EXPR:
13075 tree arg1, arg2;
13076 call_expr_arg_iterator iter1, iter2;
13077 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13078 return false;
13079 for (arg1 = first_call_expr_arg (t1, &iter1),
13080 arg2 = first_call_expr_arg (t2, &iter2);
13081 arg1 && arg2;
13082 arg1 = next_call_expr_arg (&iter1),
13083 arg2 = next_call_expr_arg (&iter2))
13084 if (!c_tree_equal (arg1, arg2))
13085 return false;
13086 if (arg1 || arg2)
13087 return false;
13088 return true;
13091 case TARGET_EXPR:
13093 tree o1 = TREE_OPERAND (t1, 0);
13094 tree o2 = TREE_OPERAND (t2, 0);
13096 /* Special case: if either target is an unallocated VAR_DECL,
13097 it means that it's going to be unified with whatever the
13098 TARGET_EXPR is really supposed to initialize, so treat it
13099 as being equivalent to anything. */
13100 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
13101 && !DECL_RTL_SET_P (o1))
13102 /*Nop*/;
13103 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
13104 && !DECL_RTL_SET_P (o2))
13105 /*Nop*/;
13106 else if (!c_tree_equal (o1, o2))
13107 return false;
13109 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13112 case COMPONENT_REF:
13113 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13114 return false;
13115 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13117 case PARM_DECL:
13118 case VAR_DECL:
13119 case CONST_DECL:
13120 case FIELD_DECL:
13121 case FUNCTION_DECL:
13122 case IDENTIFIER_NODE:
13123 case SSA_NAME:
13124 return false;
13126 case TREE_VEC:
13128 unsigned ix;
13129 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13130 return false;
13131 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13132 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13133 TREE_VEC_ELT (t2, ix)))
13134 return false;
13135 return true;
13138 default:
13139 break;
13142 switch (TREE_CODE_CLASS (code1))
13144 case tcc_unary:
13145 case tcc_binary:
13146 case tcc_comparison:
13147 case tcc_expression:
13148 case tcc_vl_exp:
13149 case tcc_reference:
13150 case tcc_statement:
13152 int i, n = TREE_OPERAND_LENGTH (t1);
13154 switch (code1)
13156 case PREINCREMENT_EXPR:
13157 case PREDECREMENT_EXPR:
13158 case POSTINCREMENT_EXPR:
13159 case POSTDECREMENT_EXPR:
13160 n = 1;
13161 break;
13162 case ARRAY_REF:
13163 n = 2;
13164 break;
13165 default:
13166 break;
13169 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13170 && n != TREE_OPERAND_LENGTH (t2))
13171 return false;
13173 for (i = 0; i < n; ++i)
13174 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13175 return false;
13177 return true;
13180 case tcc_type:
13181 return comptypes (t1, t2);
13182 default:
13183 gcc_unreachable ();
13185 /* We can get here with --disable-checking. */
13186 return false;
13189 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13190 spawn-helper and BODY is the newly created body for FNDECL. */
13192 void
13193 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13195 tree list = alloc_stmt_list ();
13196 tree frame = make_cilk_frame (fndecl);
13197 tree dtor = create_cilk_function_exit (frame, false, true);
13198 add_local_decl (cfun, frame);
13200 DECL_SAVED_TREE (fndecl) = list;
13201 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13202 frame);
13203 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13204 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13206 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13207 append_to_statement_list (detach_expr, &body_list);
13209 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13210 body = fold_build_cleanup_point_expr (void_type_node, body);
13212 append_to_statement_list (body, &body_list);
13213 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13214 body_list, dtor), &list);