Rebase.
[official-gcc.git] / gcc / c / c-typeck.c
blob1b664bd0258b5ebd37655095f5a47bcd6c646942
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 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 "tree.h"
31 #include "stor-layout.h"
32 #include "trans-mem.h"
33 #include "varasm.h"
34 #include "stmt.h"
35 #include "langhooks.h"
36 #include "c-tree.h"
37 #include "c-lang.h"
38 #include "flags.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "pointer-set.h"
44 #include "basic-block.h"
45 #include "gimple-expr.h"
46 #include "gimplify.h"
47 #include "tree-inline.h"
48 #include "omp-low.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-common.h"
51 #include "c-family/c-ubsan.h"
52 #include "cilk.h"
53 #include "wide-int.h"
55 /* Possible cases of implicit bad conversions. Used to select
56 diagnostic messages in convert_for_assignment. */
57 enum impl_conv {
58 ic_argpass,
59 ic_assign,
60 ic_init,
61 ic_return
64 /* The level of nesting inside "__alignof__". */
65 int in_alignof;
67 /* The level of nesting inside "sizeof". */
68 int in_sizeof;
70 /* The level of nesting inside "typeof". */
71 int in_typeof;
73 /* The argument of last parsed sizeof expression, only to be tested
74 if expr.original_code == SIZEOF_EXPR. */
75 tree c_last_sizeof_arg;
77 /* Nonzero if we might need to print a "missing braces around
78 initializer" message within this initializer. */
79 static int found_missing_braces;
81 static int require_constant_value;
82 static int require_constant_elements;
84 static bool null_pointer_constant_p (const_tree);
85 static tree qualify_type (tree, tree);
86 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
87 bool *);
88 static int comp_target_types (location_t, tree, tree);
89 static int function_types_compatible_p (const_tree, const_tree, bool *,
90 bool *);
91 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
92 static tree lookup_field (tree, tree);
93 static int convert_arguments (location_t, vec<location_t>, tree,
94 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
95 tree);
96 static tree pointer_diff (location_t, tree, tree);
97 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
98 enum impl_conv, bool, tree, tree, int);
99 static tree valid_compound_expr_initializer (tree, tree);
100 static void push_string (const char *);
101 static void push_member_name (tree);
102 static int spelling_length (void);
103 static char *print_spelling (char *);
104 static void warning_init (location_t, int, const char *);
105 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
106 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
107 bool, struct obstack *);
108 static void output_pending_init_elements (int, struct obstack *);
109 static int set_designator (location_t, int, struct obstack *);
110 static void push_range_stack (tree, struct obstack *);
111 static void add_pending_init (location_t, tree, tree, tree, bool,
112 struct obstack *);
113 static void set_nonincremental_init (struct obstack *);
114 static void set_nonincremental_init_from_string (tree, struct obstack *);
115 static tree find_init_member (tree, struct obstack *);
116 static void readonly_warning (tree, enum lvalue_use);
117 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
118 static void record_maybe_used_decl (tree);
119 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
121 /* Return true if EXP is a null pointer constant, false otherwise. */
123 static bool
124 null_pointer_constant_p (const_tree expr)
126 /* This should really operate on c_expr structures, but they aren't
127 yet available everywhere required. */
128 tree type = TREE_TYPE (expr);
129 return (TREE_CODE (expr) == INTEGER_CST
130 && !TREE_OVERFLOW (expr)
131 && integer_zerop (expr)
132 && (INTEGRAL_TYPE_P (type)
133 || (TREE_CODE (type) == POINTER_TYPE
134 && VOID_TYPE_P (TREE_TYPE (type))
135 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
138 /* EXPR may appear in an unevaluated part of an integer constant
139 expression, but not in an evaluated part. Wrap it in a
140 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
141 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
143 static tree
144 note_integer_operands (tree expr)
146 tree ret;
147 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
149 ret = copy_node (expr);
150 TREE_OVERFLOW (ret) = 1;
152 else
154 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
155 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
157 return ret;
160 /* Having checked whether EXPR may appear in an unevaluated part of an
161 integer constant expression and found that it may, remove any
162 C_MAYBE_CONST_EXPR noting this fact and return the resulting
163 expression. */
165 static inline tree
166 remove_c_maybe_const_expr (tree expr)
168 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
169 return C_MAYBE_CONST_EXPR_EXPR (expr);
170 else
171 return expr;
174 \f/* This is a cache to hold if two types are compatible or not. */
176 struct tagged_tu_seen_cache {
177 const struct tagged_tu_seen_cache * next;
178 const_tree t1;
179 const_tree t2;
180 /* The return value of tagged_types_tu_compatible_p if we had seen
181 these two types already. */
182 int val;
185 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
186 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
188 /* Do `exp = require_complete_type (exp);' to make sure exp
189 does not have an incomplete type. (That includes void types.) */
191 tree
192 require_complete_type (tree value)
194 tree type = TREE_TYPE (value);
196 if (value == error_mark_node || type == error_mark_node)
197 return error_mark_node;
199 /* First, detect a valid value with a complete type. */
200 if (COMPLETE_TYPE_P (type))
201 return value;
203 c_incomplete_type_error (value, type);
204 return error_mark_node;
207 /* Print an error message for invalid use of an incomplete type.
208 VALUE is the expression that was used (or 0 if that isn't known)
209 and TYPE is the type that was invalid. */
211 void
212 c_incomplete_type_error (const_tree value, const_tree type)
214 const char *type_code_string;
216 /* Avoid duplicate error message. */
217 if (TREE_CODE (type) == ERROR_MARK)
218 return;
220 if (value != 0 && (TREE_CODE (value) == VAR_DECL
221 || TREE_CODE (value) == PARM_DECL))
222 error ("%qD has an incomplete type", value);
223 else
225 retry:
226 /* We must print an error message. Be clever about what it says. */
228 switch (TREE_CODE (type))
230 case RECORD_TYPE:
231 type_code_string = "struct";
232 break;
234 case UNION_TYPE:
235 type_code_string = "union";
236 break;
238 case ENUMERAL_TYPE:
239 type_code_string = "enum";
240 break;
242 case VOID_TYPE:
243 error ("invalid use of void expression");
244 return;
246 case ARRAY_TYPE:
247 if (TYPE_DOMAIN (type))
249 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
251 error ("invalid use of flexible array member");
252 return;
254 type = TREE_TYPE (type);
255 goto retry;
257 error ("invalid use of array with unspecified bounds");
258 return;
260 default:
261 gcc_unreachable ();
264 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
265 error ("invalid use of undefined type %<%s %E%>",
266 type_code_string, TYPE_NAME (type));
267 else
268 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
269 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
273 /* Given a type, apply default promotions wrt unnamed function
274 arguments and return the new type. */
276 tree
277 c_type_promotes_to (tree type)
279 tree ret = NULL_TREE;
281 if (TYPE_MAIN_VARIANT (type) == float_type_node)
282 ret = double_type_node;
283 else if (c_promoting_integer_type_p (type))
285 /* Preserve unsignedness if not really getting any wider. */
286 if (TYPE_UNSIGNED (type)
287 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
288 ret = unsigned_type_node;
289 else
290 ret = integer_type_node;
293 if (ret != NULL_TREE)
294 return (TYPE_ATOMIC (type)
295 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
296 : ret);
298 return type;
301 /* Return true if between two named address spaces, whether there is a superset
302 named address space that encompasses both address spaces. If there is a
303 superset, return which address space is the superset. */
305 static bool
306 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
308 if (as1 == as2)
310 *common = as1;
311 return true;
313 else if (targetm.addr_space.subset_p (as1, as2))
315 *common = as2;
316 return true;
318 else if (targetm.addr_space.subset_p (as2, as1))
320 *common = as1;
321 return true;
323 else
324 return false;
327 /* Return a variant of TYPE which has all the type qualifiers of LIKE
328 as well as those of TYPE. */
330 static tree
331 qualify_type (tree type, tree like)
333 addr_space_t as_type = TYPE_ADDR_SPACE (type);
334 addr_space_t as_like = TYPE_ADDR_SPACE (like);
335 addr_space_t as_common;
337 /* If the two named address spaces are different, determine the common
338 superset address space. If there isn't one, raise an error. */
339 if (!addr_space_superset (as_type, as_like, &as_common))
341 as_common = as_type;
342 error ("%qT and %qT are in disjoint named address spaces",
343 type, like);
346 return c_build_qualified_type (type,
347 TYPE_QUALS_NO_ADDR_SPACE (type)
348 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
349 | ENCODE_QUAL_ADDR_SPACE (as_common));
352 /* Return true iff the given tree T is a variable length array. */
354 bool
355 c_vla_type_p (const_tree t)
357 if (TREE_CODE (t) == ARRAY_TYPE
358 && C_TYPE_VARIABLE_SIZE (t))
359 return true;
360 return false;
363 /* Return the composite type of two compatible types.
365 We assume that comptypes has already been done and returned
366 nonzero; if that isn't so, this may crash. In particular, we
367 assume that qualifiers match. */
369 tree
370 composite_type (tree t1, tree t2)
372 enum tree_code code1;
373 enum tree_code code2;
374 tree attributes;
376 /* Save time if the two types are the same. */
378 if (t1 == t2) return t1;
380 /* If one type is nonsense, use the other. */
381 if (t1 == error_mark_node)
382 return t2;
383 if (t2 == error_mark_node)
384 return t1;
386 code1 = TREE_CODE (t1);
387 code2 = TREE_CODE (t2);
389 /* Merge the attributes. */
390 attributes = targetm.merge_type_attributes (t1, t2);
392 /* If one is an enumerated type and the other is the compatible
393 integer type, the composite type might be either of the two
394 (DR#013 question 3). For consistency, use the enumerated type as
395 the composite type. */
397 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
398 return t1;
399 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
400 return t2;
402 gcc_assert (code1 == code2);
404 switch (code1)
406 case POINTER_TYPE:
407 /* For two pointers, do this recursively on the target type. */
409 tree pointed_to_1 = TREE_TYPE (t1);
410 tree pointed_to_2 = TREE_TYPE (t2);
411 tree target = composite_type (pointed_to_1, pointed_to_2);
412 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
413 t1 = build_type_attribute_variant (t1, attributes);
414 return qualify_type (t1, t2);
417 case ARRAY_TYPE:
419 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
420 int quals;
421 tree unqual_elt;
422 tree d1 = TYPE_DOMAIN (t1);
423 tree d2 = TYPE_DOMAIN (t2);
424 bool d1_variable, d2_variable;
425 bool d1_zero, d2_zero;
426 bool t1_complete, t2_complete;
428 /* We should not have any type quals on arrays at all. */
429 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
430 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
432 t1_complete = COMPLETE_TYPE_P (t1);
433 t2_complete = COMPLETE_TYPE_P (t2);
435 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
436 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
438 d1_variable = (!d1_zero
439 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
440 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
441 d2_variable = (!d2_zero
442 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
443 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
444 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
445 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
447 /* Save space: see if the result is identical to one of the args. */
448 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
449 && (d2_variable || d2_zero || !d1_variable))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
452 && (d1_variable || d1_zero || !d2_variable))
453 return build_type_attribute_variant (t2, attributes);
455 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
456 return build_type_attribute_variant (t1, attributes);
457 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
458 return build_type_attribute_variant (t2, attributes);
460 /* Merge the element types, and have a size if either arg has
461 one. We may have qualifiers on the element types. To set
462 up TYPE_MAIN_VARIANT correctly, we need to form the
463 composite of the unqualified types and add the qualifiers
464 back at the end. */
465 quals = TYPE_QUALS (strip_array_types (elt));
466 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
467 t1 = build_array_type (unqual_elt,
468 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
469 && (d2_variable
470 || d2_zero
471 || !d1_variable))
472 ? t1
473 : t2));
474 /* Ensure a composite type involving a zero-length array type
475 is a zero-length type not an incomplete type. */
476 if (d1_zero && d2_zero
477 && (t1_complete || t2_complete)
478 && !COMPLETE_TYPE_P (t1))
480 TYPE_SIZE (t1) = bitsize_zero_node;
481 TYPE_SIZE_UNIT (t1) = size_zero_node;
483 t1 = c_build_qualified_type (t1, quals);
484 return build_type_attribute_variant (t1, attributes);
487 case ENUMERAL_TYPE:
488 case RECORD_TYPE:
489 case UNION_TYPE:
490 if (attributes != NULL)
492 /* Try harder not to create a new aggregate type. */
493 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
494 return t1;
495 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
496 return t2;
498 return build_type_attribute_variant (t1, attributes);
500 case FUNCTION_TYPE:
501 /* Function types: prefer the one that specified arg types.
502 If both do, merge the arg types. Also merge the return types. */
504 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
505 tree p1 = TYPE_ARG_TYPES (t1);
506 tree p2 = TYPE_ARG_TYPES (t2);
507 int len;
508 tree newargs, n;
509 int i;
511 /* Save space: see if the result is identical to one of the args. */
512 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
513 return build_type_attribute_variant (t1, attributes);
514 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
515 return build_type_attribute_variant (t2, attributes);
517 /* Simple way if one arg fails to specify argument types. */
518 if (TYPE_ARG_TYPES (t1) == 0)
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
524 if (TYPE_ARG_TYPES (t2) == 0)
526 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
527 t1 = build_type_attribute_variant (t1, attributes);
528 return qualify_type (t1, t2);
531 /* If both args specify argument types, we must merge the two
532 lists, argument by argument. */
534 len = list_length (p1);
535 newargs = 0;
537 for (i = 0; i < len; i++)
538 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
540 n = newargs;
542 for (; p1;
543 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
545 /* A null type means arg type is not specified.
546 Take whatever the other function type has. */
547 if (TREE_VALUE (p1) == 0)
549 TREE_VALUE (n) = TREE_VALUE (p2);
550 goto parm_done;
552 if (TREE_VALUE (p2) == 0)
554 TREE_VALUE (n) = TREE_VALUE (p1);
555 goto parm_done;
558 /* Given wait (union {union wait *u; int *i} *)
559 and wait (union wait *),
560 prefer union wait * as type of parm. */
561 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
562 && TREE_VALUE (p1) != TREE_VALUE (p2))
564 tree memb;
565 tree mv2 = TREE_VALUE (p2);
566 if (mv2 && mv2 != error_mark_node
567 && TREE_CODE (mv2) != ARRAY_TYPE)
568 mv2 = TYPE_MAIN_VARIANT (mv2);
569 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
570 memb; memb = DECL_CHAIN (memb))
572 tree mv3 = TREE_TYPE (memb);
573 if (mv3 && mv3 != error_mark_node
574 && TREE_CODE (mv3) != ARRAY_TYPE)
575 mv3 = TYPE_MAIN_VARIANT (mv3);
576 if (comptypes (mv3, mv2))
578 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
579 TREE_VALUE (p2));
580 pedwarn (input_location, OPT_Wpedantic,
581 "function types not truly compatible in ISO C");
582 goto parm_done;
586 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
587 && TREE_VALUE (p2) != TREE_VALUE (p1))
589 tree memb;
590 tree mv1 = TREE_VALUE (p1);
591 if (mv1 && mv1 != error_mark_node
592 && TREE_CODE (mv1) != ARRAY_TYPE)
593 mv1 = TYPE_MAIN_VARIANT (mv1);
594 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
595 memb; memb = DECL_CHAIN (memb))
597 tree mv3 = TREE_TYPE (memb);
598 if (mv3 && mv3 != error_mark_node
599 && TREE_CODE (mv3) != ARRAY_TYPE)
600 mv3 = TYPE_MAIN_VARIANT (mv3);
601 if (comptypes (mv3, mv1))
603 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
604 TREE_VALUE (p1));
605 pedwarn (input_location, OPT_Wpedantic,
606 "function types not truly compatible in ISO C");
607 goto parm_done;
611 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
612 parm_done: ;
615 t1 = build_function_type (valtype, newargs);
616 t1 = qualify_type (t1, t2);
617 /* ... falls through ... */
620 default:
621 return build_type_attribute_variant (t1, attributes);
626 /* Return the type of a conditional expression between pointers to
627 possibly differently qualified versions of compatible types.
629 We assume that comp_target_types has already been done and returned
630 nonzero; if that isn't so, this may crash. */
632 static tree
633 common_pointer_type (tree t1, tree t2)
635 tree attributes;
636 tree pointed_to_1, mv1;
637 tree pointed_to_2, mv2;
638 tree target;
639 unsigned target_quals;
640 addr_space_t as1, as2, as_common;
641 int quals1, quals2;
643 /* Save time if the two types are the same. */
645 if (t1 == t2) return t1;
647 /* If one type is nonsense, use the other. */
648 if (t1 == error_mark_node)
649 return t2;
650 if (t2 == error_mark_node)
651 return t1;
653 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
654 && TREE_CODE (t2) == POINTER_TYPE);
656 /* Merge the attributes. */
657 attributes = targetm.merge_type_attributes (t1, t2);
659 /* Find the composite type of the target types, and combine the
660 qualifiers of the two types' targets. Do not lose qualifiers on
661 array element types by taking the TYPE_MAIN_VARIANT. */
662 mv1 = pointed_to_1 = TREE_TYPE (t1);
663 mv2 = pointed_to_2 = TREE_TYPE (t2);
664 if (TREE_CODE (mv1) != ARRAY_TYPE)
665 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
666 if (TREE_CODE (mv2) != ARRAY_TYPE)
667 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
668 target = composite_type (mv1, mv2);
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
674 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
676 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
677 target_quals = (quals1 & quals2);
678 else
679 target_quals = (quals1 | quals2);
681 /* If the two named address spaces are different, determine the common
682 superset address space. This is guaranteed to exist due to the
683 assumption that comp_target_type returned non-zero. */
684 as1 = TYPE_ADDR_SPACE (pointed_to_1);
685 as2 = TYPE_ADDR_SPACE (pointed_to_2);
686 if (!addr_space_superset (as1, as2, &as_common))
687 gcc_unreachable ();
689 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
691 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
692 return build_type_attribute_variant (t1, attributes);
695 /* Return the common type for two arithmetic types under the usual
696 arithmetic conversions. The default conversions have already been
697 applied, and enumerated types converted to their compatible integer
698 types. The resulting type is unqualified and has no attributes.
700 This is the type for the result of most arithmetic operations
701 if the operands have the given two types. */
703 static tree
704 c_common_type (tree t1, tree t2)
706 enum tree_code code1;
707 enum tree_code code2;
709 /* If one type is nonsense, use the other. */
710 if (t1 == error_mark_node)
711 return t2;
712 if (t2 == error_mark_node)
713 return t1;
715 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
716 t1 = TYPE_MAIN_VARIANT (t1);
718 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
719 t2 = TYPE_MAIN_VARIANT (t2);
721 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
722 t1 = build_type_attribute_variant (t1, NULL_TREE);
724 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
725 t2 = build_type_attribute_variant (t2, NULL_TREE);
727 /* Save time if the two types are the same. */
729 if (t1 == t2) return t1;
731 code1 = TREE_CODE (t1);
732 code2 = TREE_CODE (t2);
734 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
735 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
736 || code1 == INTEGER_TYPE);
737 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
738 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
739 || code2 == INTEGER_TYPE);
741 /* When one operand is a decimal float type, the other operand cannot be
742 a generic float type or a complex type. We also disallow vector types
743 here. */
744 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
745 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
747 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
749 error ("can%'t mix operands of decimal float and vector types");
750 return error_mark_node;
752 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
754 error ("can%'t mix operands of decimal float and complex types");
755 return error_mark_node;
757 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
759 error ("can%'t mix operands of decimal float and other float types");
760 return error_mark_node;
764 /* If one type is a vector type, return that type. (How the usual
765 arithmetic conversions apply to the vector types extension is not
766 precisely specified.) */
767 if (code1 == VECTOR_TYPE)
768 return t1;
770 if (code2 == VECTOR_TYPE)
771 return t2;
773 /* If one type is complex, form the common type of the non-complex
774 components, then make that complex. Use T1 or T2 if it is the
775 required type. */
776 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
778 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
779 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
780 tree subtype = c_common_type (subtype1, subtype2);
782 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
783 return t1;
784 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
785 return t2;
786 else
787 return build_complex_type (subtype);
790 /* If only one is real, use it as the result. */
792 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
793 return t1;
795 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
796 return t2;
798 /* If both are real and either are decimal floating point types, use
799 the decimal floating point type with the greater precision. */
801 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
803 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
805 return dfloat128_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
808 return dfloat64_type_node;
809 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
810 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
811 return dfloat32_type_node;
814 /* Deal with fixed-point types. */
815 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
817 unsigned int unsignedp = 0, satp = 0;
818 enum machine_mode m1, m2;
819 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
821 m1 = TYPE_MODE (t1);
822 m2 = TYPE_MODE (t2);
824 /* If one input type is saturating, the result type is saturating. */
825 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
826 satp = 1;
828 /* If both fixed-point types are unsigned, the result type is unsigned.
829 When mixing fixed-point and integer types, follow the sign of the
830 fixed-point type.
831 Otherwise, the result type is signed. */
832 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
833 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
834 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t1))
836 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
837 && TYPE_UNSIGNED (t2)))
838 unsignedp = 1;
840 /* The result type is signed. */
841 if (unsignedp == 0)
843 /* If the input type is unsigned, we need to convert to the
844 signed type. */
845 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
847 enum mode_class mclass = (enum mode_class) 0;
848 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
849 mclass = MODE_FRACT;
850 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
851 mclass = MODE_ACCUM;
852 else
853 gcc_unreachable ();
854 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
856 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
858 enum mode_class mclass = (enum mode_class) 0;
859 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
860 mclass = MODE_FRACT;
861 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
862 mclass = MODE_ACCUM;
863 else
864 gcc_unreachable ();
865 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
869 if (code1 == FIXED_POINT_TYPE)
871 fbit1 = GET_MODE_FBIT (m1);
872 ibit1 = GET_MODE_IBIT (m1);
874 else
876 fbit1 = 0;
877 /* Signed integers need to subtract one sign bit. */
878 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
881 if (code2 == FIXED_POINT_TYPE)
883 fbit2 = GET_MODE_FBIT (m2);
884 ibit2 = GET_MODE_IBIT (m2);
886 else
888 fbit2 = 0;
889 /* Signed integers need to subtract one sign bit. */
890 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
893 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
894 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
895 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
896 satp);
899 /* Both real or both integers; use the one with greater precision. */
901 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
902 return t1;
903 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
904 return t2;
906 /* Same precision. Prefer long longs to longs to ints when the
907 same precision, following the C99 rules on integer type rank
908 (which are equivalent to the C90 rules for C90 types). */
910 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
911 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
912 return long_long_unsigned_type_node;
914 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_long_unsigned_type_node;
919 else
920 return long_long_integer_type_node;
923 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
924 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
925 return long_unsigned_type_node;
927 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
928 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
930 /* But preserve unsignedness from the other type,
931 since long cannot hold all the values of an unsigned int. */
932 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
933 return long_unsigned_type_node;
934 else
935 return long_integer_type_node;
938 /* Likewise, prefer long double to double even if same size. */
939 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
940 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
941 return long_double_type_node;
943 /* Likewise, prefer double to float even if same size.
944 We got a couple of embedded targets with 32 bit doubles, and the
945 pdp11 might have 64 bit floats. */
946 if (TYPE_MAIN_VARIANT (t1) == double_type_node
947 || TYPE_MAIN_VARIANT (t2) == double_type_node)
948 return double_type_node;
950 /* Otherwise prefer the unsigned one. */
952 if (TYPE_UNSIGNED (t1))
953 return t1;
954 else
955 return t2;
958 /* Wrapper around c_common_type that is used by c-common.c and other
959 front end optimizations that remove promotions. ENUMERAL_TYPEs
960 are allowed here and are converted to their compatible integer types.
961 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
962 preferably a non-Boolean type as the common type. */
963 tree
964 common_type (tree t1, tree t2)
966 if (TREE_CODE (t1) == ENUMERAL_TYPE)
967 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
968 if (TREE_CODE (t2) == ENUMERAL_TYPE)
969 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
971 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
972 if (TREE_CODE (t1) == BOOLEAN_TYPE
973 && TREE_CODE (t2) == BOOLEAN_TYPE)
974 return boolean_type_node;
976 /* If either type is BOOLEAN_TYPE, then return the other. */
977 if (TREE_CODE (t1) == BOOLEAN_TYPE)
978 return t2;
979 if (TREE_CODE (t2) == BOOLEAN_TYPE)
980 return t1;
982 return c_common_type (t1, t2);
985 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
986 or various other operations. Return 2 if they are compatible
987 but a warning may be needed if you use them together. */
990 comptypes (tree type1, tree type2)
992 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
993 int val;
995 val = comptypes_internal (type1, type2, NULL, NULL);
996 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
998 return val;
1001 /* Like comptypes, but if it returns non-zero because enum and int are
1002 compatible, it sets *ENUM_AND_INT_P to true. */
1004 static int
1005 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1007 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1008 int val;
1010 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1011 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1013 return val;
1016 /* Like comptypes, but if it returns nonzero for different types, it
1017 sets *DIFFERENT_TYPES_P to true. */
1020 comptypes_check_different_types (tree type1, tree type2,
1021 bool *different_types_p)
1023 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1024 int val;
1026 val = comptypes_internal (type1, type2, NULL, different_types_p);
1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1029 return val;
1032 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1033 or various other operations. Return 2 if they are compatible
1034 but a warning may be needed if you use them together. If
1035 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1036 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1037 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1038 NULL, and the types are compatible but different enough not to be
1039 permitted in C11 typedef redeclarations, then this sets
1040 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1041 false, but may or may not be set if the types are incompatible.
1042 This differs from comptypes, in that we don't free the seen
1043 types. */
1045 static int
1046 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1047 bool *different_types_p)
1049 const_tree t1 = type1;
1050 const_tree t2 = type2;
1051 int attrval, val;
1053 /* Suppress errors caused by previously reported errors. */
1055 if (t1 == t2 || !t1 || !t2
1056 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1057 return 1;
1059 /* Enumerated types are compatible with integer types, but this is
1060 not transitive: two enumerated types in the same translation unit
1061 are compatible with each other only if they are the same type. */
1063 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1065 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1066 if (TREE_CODE (t2) != VOID_TYPE)
1068 if (enum_and_int_p != NULL)
1069 *enum_and_int_p = true;
1070 if (different_types_p != NULL)
1071 *different_types_p = true;
1074 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1076 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1077 if (TREE_CODE (t1) != VOID_TYPE)
1079 if (enum_and_int_p != NULL)
1080 *enum_and_int_p = true;
1081 if (different_types_p != NULL)
1082 *different_types_p = true;
1086 if (t1 == t2)
1087 return 1;
1089 /* Different classes of types can't be compatible. */
1091 if (TREE_CODE (t1) != TREE_CODE (t2))
1092 return 0;
1094 /* Qualifiers must match. C99 6.7.3p9 */
1096 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1097 return 0;
1099 /* Allow for two different type nodes which have essentially the same
1100 definition. Note that we already checked for equality of the type
1101 qualifiers (just above). */
1103 if (TREE_CODE (t1) != ARRAY_TYPE
1104 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1105 return 1;
1107 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1108 if (!(attrval = comp_type_attributes (t1, t2)))
1109 return 0;
1111 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1112 val = 0;
1114 switch (TREE_CODE (t1))
1116 case POINTER_TYPE:
1117 /* Do not remove mode or aliasing information. */
1118 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1119 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1120 break;
1121 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1122 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1123 enum_and_int_p, different_types_p));
1124 break;
1126 case FUNCTION_TYPE:
1127 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1128 different_types_p);
1129 break;
1131 case ARRAY_TYPE:
1133 tree d1 = TYPE_DOMAIN (t1);
1134 tree d2 = TYPE_DOMAIN (t2);
1135 bool d1_variable, d2_variable;
1136 bool d1_zero, d2_zero;
1137 val = 1;
1139 /* Target types must match incl. qualifiers. */
1140 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1141 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1142 enum_and_int_p,
1143 different_types_p)))
1144 return 0;
1146 if (different_types_p != NULL
1147 && (d1 == 0) != (d2 == 0))
1148 *different_types_p = true;
1149 /* Sizes must match unless one is missing or variable. */
1150 if (d1 == 0 || d2 == 0 || d1 == d2)
1151 break;
1153 d1_zero = !TYPE_MAX_VALUE (d1);
1154 d2_zero = !TYPE_MAX_VALUE (d2);
1156 d1_variable = (!d1_zero
1157 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1158 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1159 d2_variable = (!d2_zero
1160 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1161 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1162 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1163 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1165 if (different_types_p != NULL
1166 && d1_variable != d2_variable)
1167 *different_types_p = true;
1168 if (d1_variable || d2_variable)
1169 break;
1170 if (d1_zero && d2_zero)
1171 break;
1172 if (d1_zero || d2_zero
1173 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1174 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1175 val = 0;
1177 break;
1180 case ENUMERAL_TYPE:
1181 case RECORD_TYPE:
1182 case UNION_TYPE:
1183 if (val != 1 && !same_translation_unit_p (t1, t2))
1185 tree a1 = TYPE_ATTRIBUTES (t1);
1186 tree a2 = TYPE_ATTRIBUTES (t2);
1188 if (! attribute_list_contained (a1, a2)
1189 && ! attribute_list_contained (a2, a1))
1190 break;
1192 if (attrval != 2)
1193 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 different_types_p);
1195 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1196 different_types_p);
1198 break;
1200 case VECTOR_TYPE:
1201 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1202 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1203 enum_and_int_p, different_types_p));
1204 break;
1206 default:
1207 break;
1209 return attrval == 2 && val == 1 ? 2 : val;
1212 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1213 their qualifiers, except for named address spaces. If the pointers point to
1214 different named addresses, then we must determine if one address space is a
1215 subset of the other. */
1217 static int
1218 comp_target_types (location_t location, tree ttl, tree ttr)
1220 int val;
1221 tree mvl = TREE_TYPE (ttl);
1222 tree mvr = TREE_TYPE (ttr);
1223 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1224 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1225 addr_space_t as_common;
1226 bool enum_and_int_p;
1228 /* Fail if pointers point to incompatible address spaces. */
1229 if (!addr_space_superset (asl, asr, &as_common))
1230 return 0;
1232 /* Do not lose qualifiers on element types of array types that are
1233 pointer targets by taking their TYPE_MAIN_VARIANT. */
1234 if (TREE_CODE (mvl) != ARRAY_TYPE)
1235 mvl = (TYPE_ATOMIC (mvl)
1236 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1237 : TYPE_MAIN_VARIANT (mvl));
1238 if (TREE_CODE (mvr) != ARRAY_TYPE)
1239 mvr = (TYPE_ATOMIC (mvr)
1240 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1241 : TYPE_MAIN_VARIANT (mvr));
1242 enum_and_int_p = false;
1243 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1245 if (val == 2)
1246 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1248 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1249 warning_at (location, OPT_Wc___compat,
1250 "pointer target types incompatible in C++");
1252 return val;
1255 /* Subroutines of `comptypes'. */
1257 /* Determine whether two trees derive from the same translation unit.
1258 If the CONTEXT chain ends in a null, that tree's context is still
1259 being parsed, so if two trees have context chains ending in null,
1260 they're in the same translation unit. */
1262 same_translation_unit_p (const_tree t1, const_tree t2)
1264 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1265 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1267 case tcc_declaration:
1268 t1 = DECL_CONTEXT (t1); break;
1269 case tcc_type:
1270 t1 = TYPE_CONTEXT (t1); break;
1271 case tcc_exceptional:
1272 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1273 default: gcc_unreachable ();
1276 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1277 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1279 case tcc_declaration:
1280 t2 = DECL_CONTEXT (t2); break;
1281 case tcc_type:
1282 t2 = TYPE_CONTEXT (t2); break;
1283 case tcc_exceptional:
1284 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1285 default: gcc_unreachable ();
1288 return t1 == t2;
1291 /* Allocate the seen two types, assuming that they are compatible. */
1293 static struct tagged_tu_seen_cache *
1294 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1296 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1297 tu->next = tagged_tu_seen_base;
1298 tu->t1 = t1;
1299 tu->t2 = t2;
1301 tagged_tu_seen_base = tu;
1303 /* The C standard says that two structures in different translation
1304 units are compatible with each other only if the types of their
1305 fields are compatible (among other things). We assume that they
1306 are compatible until proven otherwise when building the cache.
1307 An example where this can occur is:
1308 struct a
1310 struct a *next;
1312 If we are comparing this against a similar struct in another TU,
1313 and did not assume they were compatible, we end up with an infinite
1314 loop. */
1315 tu->val = 1;
1316 return tu;
1319 /* Free the seen types until we get to TU_TIL. */
1321 static void
1322 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1324 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1325 while (tu != tu_til)
1327 const struct tagged_tu_seen_cache *const tu1
1328 = (const struct tagged_tu_seen_cache *) tu;
1329 tu = tu1->next;
1330 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1332 tagged_tu_seen_base = tu_til;
1335 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1336 compatible. If the two types are not the same (which has been
1337 checked earlier), this can only happen when multiple translation
1338 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1339 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1340 comptypes_internal. */
1342 static int
1343 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1344 bool *enum_and_int_p, bool *different_types_p)
1346 tree s1, s2;
1347 bool needs_warning = false;
1349 /* We have to verify that the tags of the types are the same. This
1350 is harder than it looks because this may be a typedef, so we have
1351 to go look at the original type. It may even be a typedef of a
1352 typedef...
1353 In the case of compiler-created builtin structs the TYPE_DECL
1354 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1355 while (TYPE_NAME (t1)
1356 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1357 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1358 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1360 while (TYPE_NAME (t2)
1361 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1362 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1363 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1365 /* C90 didn't have the requirement that the two tags be the same. */
1366 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1367 return 0;
1369 /* C90 didn't say what happened if one or both of the types were
1370 incomplete; we choose to follow C99 rules here, which is that they
1371 are compatible. */
1372 if (TYPE_SIZE (t1) == NULL
1373 || TYPE_SIZE (t2) == NULL)
1374 return 1;
1377 const struct tagged_tu_seen_cache * tts_i;
1378 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1379 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1380 return tts_i->val;
1383 switch (TREE_CODE (t1))
1385 case ENUMERAL_TYPE:
1387 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1388 /* Speed up the case where the type values are in the same order. */
1389 tree tv1 = TYPE_VALUES (t1);
1390 tree tv2 = TYPE_VALUES (t2);
1392 if (tv1 == tv2)
1394 return 1;
1397 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1399 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1400 break;
1401 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1403 tu->val = 0;
1404 return 0;
1408 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1410 return 1;
1412 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1414 tu->val = 0;
1415 return 0;
1418 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1420 tu->val = 0;
1421 return 0;
1424 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1426 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1427 if (s2 == NULL
1428 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1430 tu->val = 0;
1431 return 0;
1434 return 1;
1437 case UNION_TYPE:
1439 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1440 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1442 tu->val = 0;
1443 return 0;
1446 /* Speed up the common case where the fields are in the same order. */
1447 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1448 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1450 int result;
1452 if (DECL_NAME (s1) != DECL_NAME (s2))
1453 break;
1454 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1455 enum_and_int_p, different_types_p);
1457 if (result != 1 && !DECL_NAME (s1))
1458 break;
1459 if (result == 0)
1461 tu->val = 0;
1462 return 0;
1464 if (result == 2)
1465 needs_warning = true;
1467 if (TREE_CODE (s1) == FIELD_DECL
1468 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1469 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1471 tu->val = 0;
1472 return 0;
1475 if (!s1 && !s2)
1477 tu->val = needs_warning ? 2 : 1;
1478 return tu->val;
1481 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1483 bool ok = false;
1485 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1486 if (DECL_NAME (s1) == DECL_NAME (s2))
1488 int result;
1490 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1491 enum_and_int_p,
1492 different_types_p);
1494 if (result != 1 && !DECL_NAME (s1))
1495 continue;
1496 if (result == 0)
1498 tu->val = 0;
1499 return 0;
1501 if (result == 2)
1502 needs_warning = true;
1504 if (TREE_CODE (s1) == FIELD_DECL
1505 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1506 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1507 break;
1509 ok = true;
1510 break;
1512 if (!ok)
1514 tu->val = 0;
1515 return 0;
1518 tu->val = needs_warning ? 2 : 10;
1519 return tu->val;
1522 case RECORD_TYPE:
1524 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1526 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1527 s1 && s2;
1528 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1530 int result;
1531 if (TREE_CODE (s1) != TREE_CODE (s2)
1532 || DECL_NAME (s1) != DECL_NAME (s2))
1533 break;
1534 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1535 enum_and_int_p, different_types_p);
1536 if (result == 0)
1537 break;
1538 if (result == 2)
1539 needs_warning = true;
1541 if (TREE_CODE (s1) == FIELD_DECL
1542 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1543 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1544 break;
1546 if (s1 && s2)
1547 tu->val = 0;
1548 else
1549 tu->val = needs_warning ? 2 : 1;
1550 return tu->val;
1553 default:
1554 gcc_unreachable ();
1558 /* Return 1 if two function types F1 and F2 are compatible.
1559 If either type specifies no argument types,
1560 the other must specify a fixed number of self-promoting arg types.
1561 Otherwise, if one type specifies only the number of arguments,
1562 the other must specify that number of self-promoting arg types.
1563 Otherwise, the argument types must match.
1564 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1566 static int
1567 function_types_compatible_p (const_tree f1, const_tree f2,
1568 bool *enum_and_int_p, bool *different_types_p)
1570 tree args1, args2;
1571 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1572 int val = 1;
1573 int val1;
1574 tree ret1, ret2;
1576 ret1 = TREE_TYPE (f1);
1577 ret2 = TREE_TYPE (f2);
1579 /* 'volatile' qualifiers on a function's return type used to mean
1580 the function is noreturn. */
1581 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1582 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1583 if (TYPE_VOLATILE (ret1))
1584 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1585 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1586 if (TYPE_VOLATILE (ret2))
1587 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1588 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1589 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1590 if (val == 0)
1591 return 0;
1593 args1 = TYPE_ARG_TYPES (f1);
1594 args2 = TYPE_ARG_TYPES (f2);
1596 if (different_types_p != NULL
1597 && (args1 == 0) != (args2 == 0))
1598 *different_types_p = true;
1600 /* An unspecified parmlist matches any specified parmlist
1601 whose argument types don't need default promotions. */
1603 if (args1 == 0)
1605 if (!self_promoting_args_p (args2))
1606 return 0;
1607 /* If one of these types comes from a non-prototype fn definition,
1608 compare that with the other type's arglist.
1609 If they don't match, ask for a warning (but no error). */
1610 if (TYPE_ACTUAL_ARG_TYPES (f1)
1611 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1612 enum_and_int_p, different_types_p))
1613 val = 2;
1614 return val;
1616 if (args2 == 0)
1618 if (!self_promoting_args_p (args1))
1619 return 0;
1620 if (TYPE_ACTUAL_ARG_TYPES (f2)
1621 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1622 enum_and_int_p, different_types_p))
1623 val = 2;
1624 return val;
1627 /* Both types have argument lists: compare them and propagate results. */
1628 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1629 different_types_p);
1630 return val1 != 1 ? val1 : val;
1633 /* Check two lists of types for compatibility, returning 0 for
1634 incompatible, 1 for compatible, or 2 for compatible with
1635 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1636 comptypes_internal. */
1638 static int
1639 type_lists_compatible_p (const_tree args1, const_tree args2,
1640 bool *enum_and_int_p, bool *different_types_p)
1642 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1643 int val = 1;
1644 int newval = 0;
1646 while (1)
1648 tree a1, mv1, a2, mv2;
1649 if (args1 == 0 && args2 == 0)
1650 return val;
1651 /* If one list is shorter than the other,
1652 they fail to match. */
1653 if (args1 == 0 || args2 == 0)
1654 return 0;
1655 mv1 = a1 = TREE_VALUE (args1);
1656 mv2 = a2 = TREE_VALUE (args2);
1657 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1658 mv1 = (TYPE_ATOMIC (mv1)
1659 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1660 TYPE_QUAL_ATOMIC)
1661 : TYPE_MAIN_VARIANT (mv1));
1662 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1663 mv2 = (TYPE_ATOMIC (mv2)
1664 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1665 TYPE_QUAL_ATOMIC)
1666 : TYPE_MAIN_VARIANT (mv2));
1667 /* A null pointer instead of a type
1668 means there is supposed to be an argument
1669 but nothing is specified about what type it has.
1670 So match anything that self-promotes. */
1671 if (different_types_p != NULL
1672 && (a1 == 0) != (a2 == 0))
1673 *different_types_p = true;
1674 if (a1 == 0)
1676 if (c_type_promotes_to (a2) != a2)
1677 return 0;
1679 else if (a2 == 0)
1681 if (c_type_promotes_to (a1) != a1)
1682 return 0;
1684 /* If one of the lists has an error marker, ignore this arg. */
1685 else if (TREE_CODE (a1) == ERROR_MARK
1686 || TREE_CODE (a2) == ERROR_MARK)
1688 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1689 different_types_p)))
1691 if (different_types_p != NULL)
1692 *different_types_p = true;
1693 /* Allow wait (union {union wait *u; int *i} *)
1694 and wait (union wait *) to be compatible. */
1695 if (TREE_CODE (a1) == UNION_TYPE
1696 && (TYPE_NAME (a1) == 0
1697 || TYPE_TRANSPARENT_AGGR (a1))
1698 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1699 && tree_int_cst_equal (TYPE_SIZE (a1),
1700 TYPE_SIZE (a2)))
1702 tree memb;
1703 for (memb = TYPE_FIELDS (a1);
1704 memb; memb = DECL_CHAIN (memb))
1706 tree mv3 = TREE_TYPE (memb);
1707 if (mv3 && mv3 != error_mark_node
1708 && TREE_CODE (mv3) != ARRAY_TYPE)
1709 mv3 = (TYPE_ATOMIC (mv3)
1710 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1711 TYPE_QUAL_ATOMIC)
1712 : TYPE_MAIN_VARIANT (mv3));
1713 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1714 different_types_p))
1715 break;
1717 if (memb == 0)
1718 return 0;
1720 else if (TREE_CODE (a2) == UNION_TYPE
1721 && (TYPE_NAME (a2) == 0
1722 || TYPE_TRANSPARENT_AGGR (a2))
1723 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1724 && tree_int_cst_equal (TYPE_SIZE (a2),
1725 TYPE_SIZE (a1)))
1727 tree memb;
1728 for (memb = TYPE_FIELDS (a2);
1729 memb; memb = DECL_CHAIN (memb))
1731 tree mv3 = TREE_TYPE (memb);
1732 if (mv3 && mv3 != error_mark_node
1733 && TREE_CODE (mv3) != ARRAY_TYPE)
1734 mv3 = (TYPE_ATOMIC (mv3)
1735 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1736 TYPE_QUAL_ATOMIC)
1737 : TYPE_MAIN_VARIANT (mv3));
1738 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1739 different_types_p))
1740 break;
1742 if (memb == 0)
1743 return 0;
1745 else
1746 return 0;
1749 /* comptypes said ok, but record if it said to warn. */
1750 if (newval > val)
1751 val = newval;
1753 args1 = TREE_CHAIN (args1);
1754 args2 = TREE_CHAIN (args2);
1758 /* Compute the size to increment a pointer by. When a function type or void
1759 type or incomplete type is passed, size_one_node is returned.
1760 This function does not emit any diagnostics; the caller is responsible
1761 for that. */
1763 static tree
1764 c_size_in_bytes (const_tree type)
1766 enum tree_code code = TREE_CODE (type);
1768 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1769 || !COMPLETE_TYPE_P (type))
1770 return size_one_node;
1772 /* Convert in case a char is more than one unit. */
1773 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1774 size_int (TYPE_PRECISION (char_type_node)
1775 / BITS_PER_UNIT));
1778 /* Return either DECL or its known constant value (if it has one). */
1780 tree
1781 decl_constant_value (tree decl)
1783 if (/* Don't change a variable array bound or initial value to a constant
1784 in a place where a variable is invalid. Note that DECL_INITIAL
1785 isn't valid for a PARM_DECL. */
1786 current_function_decl != 0
1787 && TREE_CODE (decl) != PARM_DECL
1788 && !TREE_THIS_VOLATILE (decl)
1789 && TREE_READONLY (decl)
1790 && DECL_INITIAL (decl) != 0
1791 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1792 /* This is invalid if initial value is not constant.
1793 If it has either a function call, a memory reference,
1794 or a variable, then re-evaluating it could give different results. */
1795 && TREE_CONSTANT (DECL_INITIAL (decl))
1796 /* Check for cases where this is sub-optimal, even though valid. */
1797 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1798 return DECL_INITIAL (decl);
1799 return decl;
1802 /* Convert the array expression EXP to a pointer. */
1803 static tree
1804 array_to_pointer_conversion (location_t loc, tree exp)
1806 tree orig_exp = exp;
1807 tree type = TREE_TYPE (exp);
1808 tree adr;
1809 tree restype = TREE_TYPE (type);
1810 tree ptrtype;
1812 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1814 STRIP_TYPE_NOPS (exp);
1816 if (TREE_NO_WARNING (orig_exp))
1817 TREE_NO_WARNING (exp) = 1;
1819 ptrtype = build_pointer_type (restype);
1821 if (TREE_CODE (exp) == INDIRECT_REF)
1822 return convert (ptrtype, TREE_OPERAND (exp, 0));
1824 /* In C++ array compound literals are temporary objects unless they are
1825 const or appear in namespace scope, so they are destroyed too soon
1826 to use them for much of anything (c++/53220). */
1827 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1829 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1830 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1831 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1832 "converting an array compound literal to a pointer "
1833 "is ill-formed in C++");
1836 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1837 return convert (ptrtype, adr);
1840 /* Convert the function expression EXP to a pointer. */
1841 static tree
1842 function_to_pointer_conversion (location_t loc, tree exp)
1844 tree orig_exp = exp;
1846 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1848 STRIP_TYPE_NOPS (exp);
1850 if (TREE_NO_WARNING (orig_exp))
1851 TREE_NO_WARNING (exp) = 1;
1853 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1856 /* Mark EXP as read, not just set, for set but not used -Wunused
1857 warning purposes. */
1859 void
1860 mark_exp_read (tree exp)
1862 switch (TREE_CODE (exp))
1864 case VAR_DECL:
1865 case PARM_DECL:
1866 DECL_READ_P (exp) = 1;
1867 break;
1868 case ARRAY_REF:
1869 case COMPONENT_REF:
1870 case MODIFY_EXPR:
1871 case REALPART_EXPR:
1872 case IMAGPART_EXPR:
1873 CASE_CONVERT:
1874 case ADDR_EXPR:
1875 mark_exp_read (TREE_OPERAND (exp, 0));
1876 break;
1877 case COMPOUND_EXPR:
1878 case C_MAYBE_CONST_EXPR:
1879 mark_exp_read (TREE_OPERAND (exp, 1));
1880 break;
1881 default:
1882 break;
1886 /* Perform the default conversion of arrays and functions to pointers.
1887 Return the result of converting EXP. For any other expression, just
1888 return EXP.
1890 LOC is the location of the expression. */
1892 struct c_expr
1893 default_function_array_conversion (location_t loc, struct c_expr exp)
1895 tree orig_exp = exp.value;
1896 tree type = TREE_TYPE (exp.value);
1897 enum tree_code code = TREE_CODE (type);
1899 switch (code)
1901 case ARRAY_TYPE:
1903 bool not_lvalue = false;
1904 bool lvalue_array_p;
1906 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1907 || CONVERT_EXPR_P (exp.value))
1908 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1910 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1911 not_lvalue = true;
1912 exp.value = TREE_OPERAND (exp.value, 0);
1915 if (TREE_NO_WARNING (orig_exp))
1916 TREE_NO_WARNING (exp.value) = 1;
1918 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1919 if (!flag_isoc99 && !lvalue_array_p)
1921 /* Before C99, non-lvalue arrays do not decay to pointers.
1922 Normally, using such an array would be invalid; but it can
1923 be used correctly inside sizeof or as a statement expression.
1924 Thus, do not give an error here; an error will result later. */
1925 return exp;
1928 exp.value = array_to_pointer_conversion (loc, exp.value);
1930 break;
1931 case FUNCTION_TYPE:
1932 exp.value = function_to_pointer_conversion (loc, exp.value);
1933 break;
1934 default:
1935 break;
1938 return exp;
1941 struct c_expr
1942 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1944 mark_exp_read (exp.value);
1945 return default_function_array_conversion (loc, exp);
1948 /* Return whether EXPR should be treated as an atomic lvalue for the
1949 purposes of load and store handling. */
1951 static bool
1952 really_atomic_lvalue (tree expr)
1954 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1955 return false;
1956 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1957 return false;
1958 if (!lvalue_p (expr))
1959 return false;
1961 /* Ignore _Atomic on register variables, since their addresses can't
1962 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1963 sequences wouldn't work. Ignore _Atomic on structures containing
1964 bit-fields, since accessing elements of atomic structures or
1965 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1966 it's undefined at translation time or execution time, and the
1967 normal atomic sequences again wouldn't work. */
1968 while (handled_component_p (expr))
1970 if (TREE_CODE (expr) == COMPONENT_REF
1971 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1972 return false;
1973 expr = TREE_OPERAND (expr, 0);
1975 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1976 return false;
1977 return true;
1980 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1981 including converting functions and arrays to pointers if CONVERT_P.
1982 If READ_P, also mark the expression as having been read. */
1984 struct c_expr
1985 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1986 bool convert_p, bool read_p)
1988 if (read_p)
1989 mark_exp_read (exp.value);
1990 if (convert_p)
1991 exp = default_function_array_conversion (loc, exp);
1992 if (really_atomic_lvalue (exp.value))
1994 vec<tree, va_gc> *params;
1995 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1996 tree expr_type = TREE_TYPE (exp.value);
1997 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1998 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2000 gcc_assert (TYPE_ATOMIC (expr_type));
2002 /* Expansion of a generic atomic load may require an addition
2003 element, so allocate enough to prevent a resize. */
2004 vec_alloc (params, 4);
2006 /* Remove the qualifiers for the rest of the expressions and
2007 create the VAL temp variable to hold the RHS. */
2008 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2009 tmp = create_tmp_var (nonatomic_type, NULL);
2010 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2011 TREE_ADDRESSABLE (tmp) = 1;
2012 TREE_NO_WARNING (tmp) = 1;
2014 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2015 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2016 params->quick_push (expr_addr);
2017 params->quick_push (tmp_addr);
2018 params->quick_push (seq_cst);
2019 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2021 /* EXPR is always read. */
2022 mark_exp_read (exp.value);
2024 /* Return tmp which contains the value loaded. */
2025 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2027 return exp;
2030 /* EXP is an expression of integer type. Apply the integer promotions
2031 to it and return the promoted value. */
2033 tree
2034 perform_integral_promotions (tree exp)
2036 tree type = TREE_TYPE (exp);
2037 enum tree_code code = TREE_CODE (type);
2039 gcc_assert (INTEGRAL_TYPE_P (type));
2041 /* Normally convert enums to int,
2042 but convert wide enums to something wider. */
2043 if (code == ENUMERAL_TYPE)
2045 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2046 TYPE_PRECISION (integer_type_node)),
2047 ((TYPE_PRECISION (type)
2048 >= TYPE_PRECISION (integer_type_node))
2049 && TYPE_UNSIGNED (type)));
2051 return convert (type, exp);
2054 /* ??? This should no longer be needed now bit-fields have their
2055 proper types. */
2056 if (TREE_CODE (exp) == COMPONENT_REF
2057 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2058 /* If it's thinner than an int, promote it like a
2059 c_promoting_integer_type_p, otherwise leave it alone. */
2060 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2061 TYPE_PRECISION (integer_type_node)))
2062 return convert (integer_type_node, exp);
2064 if (c_promoting_integer_type_p (type))
2066 /* Preserve unsignedness if not really getting any wider. */
2067 if (TYPE_UNSIGNED (type)
2068 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2069 return convert (unsigned_type_node, exp);
2071 return convert (integer_type_node, exp);
2074 return exp;
2078 /* Perform default promotions for C data used in expressions.
2079 Enumeral types or short or char are converted to int.
2080 In addition, manifest constants symbols are replaced by their values. */
2082 tree
2083 default_conversion (tree exp)
2085 tree orig_exp;
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
2088 tree promoted_type;
2090 mark_exp_read (exp);
2092 /* Functions and arrays have been converted during parsing. */
2093 gcc_assert (code != FUNCTION_TYPE);
2094 if (code == ARRAY_TYPE)
2095 return exp;
2097 /* Constants can be used directly unless they're not loadable. */
2098 if (TREE_CODE (exp) == CONST_DECL)
2099 exp = DECL_INITIAL (exp);
2101 /* Strip no-op conversions. */
2102 orig_exp = exp;
2103 STRIP_TYPE_NOPS (exp);
2105 if (TREE_NO_WARNING (orig_exp))
2106 TREE_NO_WARNING (exp) = 1;
2108 if (code == VOID_TYPE)
2110 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2111 "void value not ignored as it ought to be");
2112 return error_mark_node;
2115 exp = require_complete_type (exp);
2116 if (exp == error_mark_node)
2117 return error_mark_node;
2119 promoted_type = targetm.promoted_type (type);
2120 if (promoted_type)
2121 return convert (promoted_type, exp);
2123 if (INTEGRAL_TYPE_P (type))
2124 return perform_integral_promotions (exp);
2126 return exp;
2129 /* Look up COMPONENT in a structure or union TYPE.
2131 If the component name is not found, returns NULL_TREE. Otherwise,
2132 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2133 stepping down the chain to the component, which is in the last
2134 TREE_VALUE of the list. Normally the list is of length one, but if
2135 the component is embedded within (nested) anonymous structures or
2136 unions, the list steps down the chain to the component. */
2138 static tree
2139 lookup_field (tree type, tree component)
2141 tree field;
2143 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2144 to the field elements. Use a binary search on this array to quickly
2145 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2146 will always be set for structures which have many elements. */
2148 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2150 int bot, top, half;
2151 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2153 field = TYPE_FIELDS (type);
2154 bot = 0;
2155 top = TYPE_LANG_SPECIFIC (type)->s->len;
2156 while (top - bot > 1)
2158 half = (top - bot + 1) >> 1;
2159 field = field_array[bot+half];
2161 if (DECL_NAME (field) == NULL_TREE)
2163 /* Step through all anon unions in linear fashion. */
2164 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2166 field = field_array[bot++];
2167 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2168 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2170 tree anon = lookup_field (TREE_TYPE (field), component);
2172 if (anon)
2173 return tree_cons (NULL_TREE, field, anon);
2175 /* The Plan 9 compiler permits referring
2176 directly to an anonymous struct/union field
2177 using a typedef name. */
2178 if (flag_plan9_extensions
2179 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2180 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2181 == TYPE_DECL)
2182 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2183 == component))
2184 break;
2188 /* Entire record is only anon unions. */
2189 if (bot > top)
2190 return NULL_TREE;
2192 /* Restart the binary search, with new lower bound. */
2193 continue;
2196 if (DECL_NAME (field) == component)
2197 break;
2198 if (DECL_NAME (field) < component)
2199 bot += half;
2200 else
2201 top = bot + half;
2204 if (DECL_NAME (field_array[bot]) == component)
2205 field = field_array[bot];
2206 else if (DECL_NAME (field) != component)
2207 return NULL_TREE;
2209 else
2211 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2213 if (DECL_NAME (field) == NULL_TREE
2214 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2215 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2217 tree anon = lookup_field (TREE_TYPE (field), component);
2219 if (anon)
2220 return tree_cons (NULL_TREE, field, anon);
2222 /* The Plan 9 compiler permits referring directly to an
2223 anonymous struct/union field using a typedef
2224 name. */
2225 if (flag_plan9_extensions
2226 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2227 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2228 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2229 == component))
2230 break;
2233 if (DECL_NAME (field) == component)
2234 break;
2237 if (field == NULL_TREE)
2238 return NULL_TREE;
2241 return tree_cons (NULL_TREE, field, NULL_TREE);
2244 /* Make an expression to refer to the COMPONENT field of structure or
2245 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2246 location of the COMPONENT_REF. */
2248 tree
2249 build_component_ref (location_t loc, tree datum, tree component)
2251 tree type = TREE_TYPE (datum);
2252 enum tree_code code = TREE_CODE (type);
2253 tree field = NULL;
2254 tree ref;
2255 bool datum_lvalue = lvalue_p (datum);
2257 if (!objc_is_public (datum, component))
2258 return error_mark_node;
2260 /* Detect Objective-C property syntax object.property. */
2261 if (c_dialect_objc ()
2262 && (ref = objc_maybe_build_component_ref (datum, component)))
2263 return ref;
2265 /* See if there is a field or component with name COMPONENT. */
2267 if (code == RECORD_TYPE || code == UNION_TYPE)
2269 if (!COMPLETE_TYPE_P (type))
2271 c_incomplete_type_error (NULL_TREE, type);
2272 return error_mark_node;
2275 field = lookup_field (type, component);
2277 if (!field)
2279 error_at (loc, "%qT has no member named %qE", type, component);
2280 return error_mark_node;
2283 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2284 This might be better solved in future the way the C++ front
2285 end does it - by giving the anonymous entities each a
2286 separate name and type, and then have build_component_ref
2287 recursively call itself. We can't do that here. */
2290 tree subdatum = TREE_VALUE (field);
2291 int quals;
2292 tree subtype;
2293 bool use_datum_quals;
2295 if (TREE_TYPE (subdatum) == error_mark_node)
2296 return error_mark_node;
2298 /* If this is an rvalue, it does not have qualifiers in C
2299 standard terms and we must avoid propagating such
2300 qualifiers down to a non-lvalue array that is then
2301 converted to a pointer. */
2302 use_datum_quals = (datum_lvalue
2303 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2305 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2306 if (use_datum_quals)
2307 quals |= TYPE_QUALS (TREE_TYPE (datum));
2308 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2310 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2311 NULL_TREE);
2312 SET_EXPR_LOCATION (ref, loc);
2313 if (TREE_READONLY (subdatum)
2314 || (use_datum_quals && TREE_READONLY (datum)))
2315 TREE_READONLY (ref) = 1;
2316 if (TREE_THIS_VOLATILE (subdatum)
2317 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2318 TREE_THIS_VOLATILE (ref) = 1;
2320 if (TREE_DEPRECATED (subdatum))
2321 warn_deprecated_use (subdatum, NULL_TREE);
2323 datum = ref;
2325 field = TREE_CHAIN (field);
2327 while (field);
2329 return ref;
2331 else if (code != ERROR_MARK)
2332 error_at (loc,
2333 "request for member %qE in something not a structure or union",
2334 component);
2336 return error_mark_node;
2339 /* Given an expression PTR for a pointer, return an expression
2340 for the value pointed to.
2341 ERRORSTRING is the name of the operator to appear in error messages.
2343 LOC is the location to use for the generated tree. */
2345 tree
2346 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2348 tree pointer = default_conversion (ptr);
2349 tree type = TREE_TYPE (pointer);
2350 tree ref;
2352 if (TREE_CODE (type) == POINTER_TYPE)
2354 if (CONVERT_EXPR_P (pointer)
2355 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2357 /* If a warning is issued, mark it to avoid duplicates from
2358 the backend. This only needs to be done at
2359 warn_strict_aliasing > 2. */
2360 if (warn_strict_aliasing > 2)
2361 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2362 type, TREE_OPERAND (pointer, 0)))
2363 TREE_NO_WARNING (pointer) = 1;
2366 if (TREE_CODE (pointer) == ADDR_EXPR
2367 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2368 == TREE_TYPE (type)))
2370 ref = TREE_OPERAND (pointer, 0);
2371 protected_set_expr_location (ref, loc);
2372 return ref;
2374 else
2376 tree t = TREE_TYPE (type);
2378 ref = build1 (INDIRECT_REF, t, pointer);
2380 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2382 error_at (loc, "dereferencing pointer to incomplete type");
2383 return error_mark_node;
2385 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2386 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2388 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2389 so that we get the proper error message if the result is used
2390 to assign to. Also, &* is supposed to be a no-op.
2391 And ANSI C seems to specify that the type of the result
2392 should be the const type. */
2393 /* A de-reference of a pointer to const is not a const. It is valid
2394 to change it via some other pointer. */
2395 TREE_READONLY (ref) = TYPE_READONLY (t);
2396 TREE_SIDE_EFFECTS (ref)
2397 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2398 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2399 protected_set_expr_location (ref, loc);
2400 return ref;
2403 else if (TREE_CODE (pointer) != ERROR_MARK)
2404 invalid_indirection_error (loc, type, errstring);
2406 return error_mark_node;
2409 /* This handles expressions of the form "a[i]", which denotes
2410 an array reference.
2412 This is logically equivalent in C to *(a+i), but we may do it differently.
2413 If A is a variable or a member, we generate a primitive ARRAY_REF.
2414 This avoids forcing the array out of registers, and can work on
2415 arrays that are not lvalues (for example, members of structures returned
2416 by functions).
2418 For vector types, allow vector[i] but not i[vector], and create
2419 *(((type*)&vectortype) + i) for the expression.
2421 LOC is the location to use for the returned expression. */
2423 tree
2424 build_array_ref (location_t loc, tree array, tree index)
2426 tree ret;
2427 bool swapped = false;
2428 if (TREE_TYPE (array) == error_mark_node
2429 || TREE_TYPE (index) == error_mark_node)
2430 return error_mark_node;
2432 if (flag_cilkplus && contains_array_notation_expr (index))
2434 size_t rank = 0;
2435 if (!find_rank (loc, index, index, true, &rank))
2436 return error_mark_node;
2437 if (rank > 1)
2439 error_at (loc, "rank of the array's index is greater than 1");
2440 return error_mark_node;
2443 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2444 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2445 /* Allow vector[index] but not index[vector]. */
2446 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2448 tree temp;
2449 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2450 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2452 error_at (loc,
2453 "subscripted value is neither array nor pointer nor vector");
2455 return error_mark_node;
2457 temp = array;
2458 array = index;
2459 index = temp;
2460 swapped = true;
2463 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2465 error_at (loc, "array subscript is not an integer");
2466 return error_mark_node;
2469 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2471 error_at (loc, "subscripted value is pointer to function");
2472 return error_mark_node;
2475 /* ??? Existing practice has been to warn only when the char
2476 index is syntactically the index, not for char[array]. */
2477 if (!swapped)
2478 warn_array_subscript_with_type_char (index);
2480 /* Apply default promotions *after* noticing character types. */
2481 index = default_conversion (index);
2483 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2485 convert_vector_to_pointer_for_subscript (loc, &array, index);
2487 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2489 tree rval, type;
2491 /* An array that is indexed by a non-constant
2492 cannot be stored in a register; we must be able to do
2493 address arithmetic on its address.
2494 Likewise an array of elements of variable size. */
2495 if (TREE_CODE (index) != INTEGER_CST
2496 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2497 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2499 if (!c_mark_addressable (array))
2500 return error_mark_node;
2502 /* An array that is indexed by a constant value which is not within
2503 the array bounds cannot be stored in a register either; because we
2504 would get a crash in store_bit_field/extract_bit_field when trying
2505 to access a non-existent part of the register. */
2506 if (TREE_CODE (index) == INTEGER_CST
2507 && TYPE_DOMAIN (TREE_TYPE (array))
2508 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2510 if (!c_mark_addressable (array))
2511 return error_mark_node;
2514 if (pedantic)
2516 tree foo = array;
2517 while (TREE_CODE (foo) == COMPONENT_REF)
2518 foo = TREE_OPERAND (foo, 0);
2519 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2520 pedwarn (loc, OPT_Wpedantic,
2521 "ISO C forbids subscripting %<register%> array");
2522 else if (!flag_isoc99 && !lvalue_p (foo))
2523 pedwarn (loc, OPT_Wpedantic,
2524 "ISO C90 forbids subscripting non-lvalue array");
2527 type = TREE_TYPE (TREE_TYPE (array));
2528 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2529 /* Array ref is const/volatile if the array elements are
2530 or if the array is. */
2531 TREE_READONLY (rval)
2532 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2533 | TREE_READONLY (array));
2534 TREE_SIDE_EFFECTS (rval)
2535 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2536 | TREE_SIDE_EFFECTS (array));
2537 TREE_THIS_VOLATILE (rval)
2538 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2539 /* This was added by rms on 16 Nov 91.
2540 It fixes vol struct foo *a; a->elts[1]
2541 in an inline function.
2542 Hope it doesn't break something else. */
2543 | TREE_THIS_VOLATILE (array));
2544 ret = require_complete_type (rval);
2545 protected_set_expr_location (ret, loc);
2546 return ret;
2548 else
2550 tree ar = default_conversion (array);
2552 if (ar == error_mark_node)
2553 return ar;
2555 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2556 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2558 return build_indirect_ref
2559 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2560 RO_ARRAY_INDEXING);
2564 /* Build an external reference to identifier ID. FUN indicates
2565 whether this will be used for a function call. LOC is the source
2566 location of the identifier. This sets *TYPE to the type of the
2567 identifier, which is not the same as the type of the returned value
2568 for CONST_DECLs defined as enum constants. If the type of the
2569 identifier is not available, *TYPE is set to NULL. */
2570 tree
2571 build_external_ref (location_t loc, tree id, int fun, tree *type)
2573 tree ref;
2574 tree decl = lookup_name (id);
2576 /* In Objective-C, an instance variable (ivar) may be preferred to
2577 whatever lookup_name() found. */
2578 decl = objc_lookup_ivar (decl, id);
2580 *type = NULL;
2581 if (decl && decl != error_mark_node)
2583 ref = decl;
2584 *type = TREE_TYPE (ref);
2586 else if (fun)
2587 /* Implicit function declaration. */
2588 ref = implicitly_declare (loc, id);
2589 else if (decl == error_mark_node)
2590 /* Don't complain about something that's already been
2591 complained about. */
2592 return error_mark_node;
2593 else
2595 undeclared_variable (loc, id);
2596 return error_mark_node;
2599 if (TREE_TYPE (ref) == error_mark_node)
2600 return error_mark_node;
2602 if (TREE_DEPRECATED (ref))
2603 warn_deprecated_use (ref, NULL_TREE);
2605 /* Recursive call does not count as usage. */
2606 if (ref != current_function_decl)
2608 TREE_USED (ref) = 1;
2611 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2613 if (!in_sizeof && !in_typeof)
2614 C_DECL_USED (ref) = 1;
2615 else if (DECL_INITIAL (ref) == 0
2616 && DECL_EXTERNAL (ref)
2617 && !TREE_PUBLIC (ref))
2618 record_maybe_used_decl (ref);
2621 if (TREE_CODE (ref) == CONST_DECL)
2623 used_types_insert (TREE_TYPE (ref));
2625 if (warn_cxx_compat
2626 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2627 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2629 warning_at (loc, OPT_Wc___compat,
2630 ("enum constant defined in struct or union "
2631 "is not visible in C++"));
2632 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2635 ref = DECL_INITIAL (ref);
2636 TREE_CONSTANT (ref) = 1;
2638 else if (current_function_decl != 0
2639 && !DECL_FILE_SCOPE_P (current_function_decl)
2640 && (TREE_CODE (ref) == VAR_DECL
2641 || TREE_CODE (ref) == PARM_DECL
2642 || TREE_CODE (ref) == FUNCTION_DECL))
2644 tree context = decl_function_context (ref);
2646 if (context != 0 && context != current_function_decl)
2647 DECL_NONLOCAL (ref) = 1;
2649 /* C99 6.7.4p3: An inline definition of a function with external
2650 linkage ... shall not contain a reference to an identifier with
2651 internal linkage. */
2652 else if (current_function_decl != 0
2653 && DECL_DECLARED_INLINE_P (current_function_decl)
2654 && DECL_EXTERNAL (current_function_decl)
2655 && VAR_OR_FUNCTION_DECL_P (ref)
2656 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2657 && ! TREE_PUBLIC (ref)
2658 && DECL_CONTEXT (ref) != current_function_decl)
2659 record_inline_static (loc, current_function_decl, ref,
2660 csi_internal);
2662 return ref;
2665 /* Record details of decls possibly used inside sizeof or typeof. */
2666 struct maybe_used_decl
2668 /* The decl. */
2669 tree decl;
2670 /* The level seen at (in_sizeof + in_typeof). */
2671 int level;
2672 /* The next one at this level or above, or NULL. */
2673 struct maybe_used_decl *next;
2676 static struct maybe_used_decl *maybe_used_decls;
2678 /* Record that DECL, an undefined static function reference seen
2679 inside sizeof or typeof, might be used if the operand of sizeof is
2680 a VLA type or the operand of typeof is a variably modified
2681 type. */
2683 static void
2684 record_maybe_used_decl (tree decl)
2686 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2687 t->decl = decl;
2688 t->level = in_sizeof + in_typeof;
2689 t->next = maybe_used_decls;
2690 maybe_used_decls = t;
2693 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2694 USED is false, just discard them. If it is true, mark them used
2695 (if no longer inside sizeof or typeof) or move them to the next
2696 level up (if still inside sizeof or typeof). */
2698 void
2699 pop_maybe_used (bool used)
2701 struct maybe_used_decl *p = maybe_used_decls;
2702 int cur_level = in_sizeof + in_typeof;
2703 while (p && p->level > cur_level)
2705 if (used)
2707 if (cur_level == 0)
2708 C_DECL_USED (p->decl) = 1;
2709 else
2710 p->level = cur_level;
2712 p = p->next;
2714 if (!used || cur_level == 0)
2715 maybe_used_decls = p;
2718 /* Return the result of sizeof applied to EXPR. */
2720 struct c_expr
2721 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2723 struct c_expr ret;
2724 if (expr.value == error_mark_node)
2726 ret.value = error_mark_node;
2727 ret.original_code = ERROR_MARK;
2728 ret.original_type = NULL;
2729 pop_maybe_used (false);
2731 else
2733 bool expr_const_operands = true;
2735 if (TREE_CODE (expr.value) == PARM_DECL
2736 && C_ARRAY_PARAMETER (expr.value))
2738 if (warning_at (loc, OPT_Wsizeof_array_argument,
2739 "%<sizeof%> on array function parameter %qE will "
2740 "return size of %qT", expr.value,
2741 expr.original_type))
2742 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2744 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2745 &expr_const_operands);
2746 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2747 c_last_sizeof_arg = expr.value;
2748 ret.original_code = SIZEOF_EXPR;
2749 ret.original_type = NULL;
2750 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2752 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2753 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2754 folded_expr, ret.value);
2755 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2756 SET_EXPR_LOCATION (ret.value, loc);
2758 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2760 return ret;
2763 /* Return the result of sizeof applied to T, a structure for the type
2764 name passed to sizeof (rather than the type itself). LOC is the
2765 location of the original expression. */
2767 struct c_expr
2768 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2770 tree type;
2771 struct c_expr ret;
2772 tree type_expr = NULL_TREE;
2773 bool type_expr_const = true;
2774 type = groktypename (t, &type_expr, &type_expr_const);
2775 ret.value = c_sizeof (loc, type);
2776 c_last_sizeof_arg = type;
2777 ret.original_code = SIZEOF_EXPR;
2778 ret.original_type = NULL;
2779 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2780 && c_vla_type_p (type))
2782 /* If the type is a [*] array, it is a VLA but is represented as
2783 having a size of zero. In such a case we must ensure that
2784 the result of sizeof does not get folded to a constant by
2785 c_fully_fold, because if the size is evaluated the result is
2786 not constant and so constraints on zero or negative size
2787 arrays must not be applied when this sizeof call is inside
2788 another array declarator. */
2789 if (!type_expr)
2790 type_expr = integer_zero_node;
2791 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2792 type_expr, ret.value);
2793 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2795 pop_maybe_used (type != error_mark_node
2796 ? C_TYPE_VARIABLE_SIZE (type) : false);
2797 return ret;
2800 /* Build a function call to function FUNCTION with parameters PARAMS.
2801 The function call is at LOC.
2802 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2803 TREE_VALUE of each node is a parameter-expression.
2804 FUNCTION's data type may be a function type or a pointer-to-function. */
2806 tree
2807 build_function_call (location_t loc, tree function, tree params)
2809 vec<tree, va_gc> *v;
2810 tree ret;
2812 vec_alloc (v, list_length (params));
2813 for (; params; params = TREE_CHAIN (params))
2814 v->quick_push (TREE_VALUE (params));
2815 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2816 vec_free (v);
2817 return ret;
2820 /* Give a note about the location of the declaration of DECL. */
2822 static void inform_declaration (tree decl)
2824 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2825 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2828 /* Build a function call to function FUNCTION with parameters PARAMS.
2829 ORIGTYPES, if not NULL, is a vector of types; each element is
2830 either NULL or the original type of the corresponding element in
2831 PARAMS. The original type may differ from TREE_TYPE of the
2832 parameter for enums. FUNCTION's data type may be a function type
2833 or pointer-to-function. This function changes the elements of
2834 PARAMS. */
2836 tree
2837 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2838 tree function, vec<tree, va_gc> *params,
2839 vec<tree, va_gc> *origtypes)
2841 tree fntype, fundecl = 0;
2842 tree name = NULL_TREE, result;
2843 tree tem;
2844 int nargs;
2845 tree *argarray;
2848 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2849 STRIP_TYPE_NOPS (function);
2851 /* Convert anything with function type to a pointer-to-function. */
2852 if (TREE_CODE (function) == FUNCTION_DECL)
2854 name = DECL_NAME (function);
2856 if (flag_tm)
2857 tm_malloc_replacement (function);
2858 fundecl = function;
2859 /* Atomic functions have type checking/casting already done. They are
2860 often rewritten and don't match the original parameter list. */
2861 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2862 origtypes = NULL;
2864 if (flag_cilkplus
2865 && is_cilkplus_reduce_builtin (function))
2866 origtypes = NULL;
2868 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2869 function = function_to_pointer_conversion (loc, function);
2871 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2872 expressions, like those used for ObjC messenger dispatches. */
2873 if (params && !params->is_empty ())
2874 function = objc_rewrite_function_call (function, (*params)[0]);
2876 function = c_fully_fold (function, false, NULL);
2878 fntype = TREE_TYPE (function);
2880 if (TREE_CODE (fntype) == ERROR_MARK)
2881 return error_mark_node;
2883 if (!(TREE_CODE (fntype) == POINTER_TYPE
2884 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2886 if (!flag_diagnostics_show_caret)
2887 error_at (loc,
2888 "called object %qE is not a function or function pointer",
2889 function);
2890 else if (DECL_P (function))
2892 error_at (loc,
2893 "called object %qD is not a function or function pointer",
2894 function);
2895 inform_declaration (function);
2897 else
2898 error_at (loc,
2899 "called object is not a function or function pointer");
2900 return error_mark_node;
2903 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2904 current_function_returns_abnormally = 1;
2906 /* fntype now gets the type of function pointed to. */
2907 fntype = TREE_TYPE (fntype);
2909 /* Convert the parameters to the types declared in the
2910 function prototype, or apply default promotions. */
2912 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2913 origtypes, function, fundecl);
2914 if (nargs < 0)
2915 return error_mark_node;
2917 /* Check that the function is called through a compatible prototype.
2918 If it is not, warn. */
2919 if (CONVERT_EXPR_P (function)
2920 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2921 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2922 && !comptypes (fntype, TREE_TYPE (tem)))
2924 tree return_type = TREE_TYPE (fntype);
2926 /* This situation leads to run-time undefined behavior. We can't,
2927 therefore, simply error unless we can prove that all possible
2928 executions of the program must execute the code. */
2929 warning_at (loc, 0, "function called through a non-compatible type");
2931 if (VOID_TYPE_P (return_type)
2932 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2933 pedwarn (loc, 0,
2934 "function with qualified void return type called");
2937 argarray = vec_safe_address (params);
2939 /* Check that arguments to builtin functions match the expectations. */
2940 if (fundecl
2941 && DECL_BUILT_IN (fundecl)
2942 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2943 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2944 return error_mark_node;
2946 /* Check that the arguments to the function are valid. */
2947 check_function_arguments (fntype, nargs, argarray);
2949 if (name != NULL_TREE
2950 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2952 if (require_constant_value)
2953 result =
2954 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2955 function, nargs, argarray);
2956 else
2957 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2958 function, nargs, argarray);
2959 if (TREE_CODE (result) == NOP_EXPR
2960 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2961 STRIP_TYPE_NOPS (result);
2963 else
2964 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2965 function, nargs, argarray);
2967 if (VOID_TYPE_P (TREE_TYPE (result)))
2969 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2970 pedwarn (loc, 0,
2971 "function with qualified void return type called");
2972 return result;
2974 return require_complete_type (result);
2977 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2979 tree
2980 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2981 tree function, vec<tree, va_gc> *params,
2982 vec<tree, va_gc> *origtypes)
2984 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2985 STRIP_TYPE_NOPS (function);
2987 /* Convert anything with function type to a pointer-to-function. */
2988 if (TREE_CODE (function) == FUNCTION_DECL)
2990 /* Implement type-directed function overloading for builtins.
2991 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2992 handle all the type checking. The result is a complete expression
2993 that implements this function call. */
2994 tree tem = resolve_overloaded_builtin (loc, function, params);
2995 if (tem)
2996 return tem;
2998 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3001 /* Convert the argument expressions in the vector VALUES
3002 to the types in the list TYPELIST.
3004 If TYPELIST is exhausted, or when an element has NULL as its type,
3005 perform the default conversions.
3007 ORIGTYPES is the original types of the expressions in VALUES. This
3008 holds the type of enum values which have been converted to integral
3009 types. It may be NULL.
3011 FUNCTION is a tree for the called function. It is used only for
3012 error messages, where it is formatted with %qE.
3014 This is also where warnings about wrong number of args are generated.
3016 ARG_LOC are locations of function arguments (if any).
3018 Returns the actual number of arguments processed (which may be less
3019 than the length of VALUES in some error situations), or -1 on
3020 failure. */
3022 static int
3023 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3024 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3025 tree function, tree fundecl)
3027 tree typetail, val;
3028 unsigned int parmnum;
3029 bool error_args = false;
3030 const bool type_generic = fundecl
3031 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3032 bool type_generic_remove_excess_precision = false;
3033 tree selector;
3035 /* Change pointer to function to the function itself for
3036 diagnostics. */
3037 if (TREE_CODE (function) == ADDR_EXPR
3038 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3039 function = TREE_OPERAND (function, 0);
3041 /* Handle an ObjC selector specially for diagnostics. */
3042 selector = objc_message_selector ();
3044 /* For type-generic built-in functions, determine whether excess
3045 precision should be removed (classification) or not
3046 (comparison). */
3047 if (type_generic
3048 && DECL_BUILT_IN (fundecl)
3049 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3051 switch (DECL_FUNCTION_CODE (fundecl))
3053 case BUILT_IN_ISFINITE:
3054 case BUILT_IN_ISINF:
3055 case BUILT_IN_ISINF_SIGN:
3056 case BUILT_IN_ISNAN:
3057 case BUILT_IN_ISNORMAL:
3058 case BUILT_IN_FPCLASSIFY:
3059 type_generic_remove_excess_precision = true;
3060 break;
3062 default:
3063 type_generic_remove_excess_precision = false;
3064 break;
3067 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3068 return vec_safe_length (values);
3070 /* Scan the given expressions and types, producing individual
3071 converted arguments. */
3073 for (typetail = typelist, parmnum = 0;
3074 values && values->iterate (parmnum, &val);
3075 ++parmnum)
3077 tree type = typetail ? TREE_VALUE (typetail) : 0;
3078 tree valtype = TREE_TYPE (val);
3079 tree rname = function;
3080 int argnum = parmnum + 1;
3081 const char *invalid_func_diag;
3082 bool excess_precision = false;
3083 bool npc;
3084 tree parmval;
3085 /* Some __atomic_* builtins have additional hidden argument at
3086 position 0. */
3087 location_t ploc
3088 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3089 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3090 : input_location;
3092 if (type == void_type_node)
3094 if (selector)
3095 error_at (loc, "too many arguments to method %qE", selector);
3096 else
3097 error_at (loc, "too many arguments to function %qE", function);
3098 inform_declaration (fundecl);
3099 return parmnum;
3102 if (selector && argnum > 2)
3104 rname = selector;
3105 argnum -= 2;
3108 npc = null_pointer_constant_p (val);
3110 /* If there is excess precision and a prototype, convert once to
3111 the required type rather than converting via the semantic
3112 type. Likewise without a prototype a float value represented
3113 as long double should be converted once to double. But for
3114 type-generic classification functions excess precision must
3115 be removed here. */
3116 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3117 && (type || !type_generic || !type_generic_remove_excess_precision))
3119 val = TREE_OPERAND (val, 0);
3120 excess_precision = true;
3122 val = c_fully_fold (val, false, NULL);
3123 STRIP_TYPE_NOPS (val);
3125 val = require_complete_type (val);
3127 if (type != 0)
3129 /* Formal parm type is specified by a function prototype. */
3131 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3133 error_at (ploc, "type of formal parameter %d is incomplete",
3134 parmnum + 1);
3135 parmval = val;
3137 else
3139 tree origtype;
3141 /* Optionally warn about conversions that
3142 differ from the default conversions. */
3143 if (warn_traditional_conversion || warn_traditional)
3145 unsigned int formal_prec = TYPE_PRECISION (type);
3147 if (INTEGRAL_TYPE_P (type)
3148 && TREE_CODE (valtype) == REAL_TYPE)
3149 warning_at (ploc, OPT_Wtraditional_conversion,
3150 "passing argument %d of %qE as integer rather "
3151 "than floating due to prototype",
3152 argnum, rname);
3153 if (INTEGRAL_TYPE_P (type)
3154 && TREE_CODE (valtype) == COMPLEX_TYPE)
3155 warning_at (ploc, OPT_Wtraditional_conversion,
3156 "passing argument %d of %qE as integer rather "
3157 "than complex due to prototype",
3158 argnum, rname);
3159 else if (TREE_CODE (type) == COMPLEX_TYPE
3160 && TREE_CODE (valtype) == REAL_TYPE)
3161 warning_at (ploc, OPT_Wtraditional_conversion,
3162 "passing argument %d of %qE as complex rather "
3163 "than floating due to prototype",
3164 argnum, rname);
3165 else if (TREE_CODE (type) == REAL_TYPE
3166 && INTEGRAL_TYPE_P (valtype))
3167 warning_at (ploc, OPT_Wtraditional_conversion,
3168 "passing argument %d of %qE as floating rather "
3169 "than integer due to prototype",
3170 argnum, rname);
3171 else if (TREE_CODE (type) == COMPLEX_TYPE
3172 && INTEGRAL_TYPE_P (valtype))
3173 warning_at (ploc, OPT_Wtraditional_conversion,
3174 "passing argument %d of %qE as complex rather "
3175 "than integer due to prototype",
3176 argnum, rname);
3177 else if (TREE_CODE (type) == REAL_TYPE
3178 && TREE_CODE (valtype) == COMPLEX_TYPE)
3179 warning_at (ploc, OPT_Wtraditional_conversion,
3180 "passing argument %d of %qE as floating rather "
3181 "than complex due to prototype",
3182 argnum, rname);
3183 /* ??? At some point, messages should be written about
3184 conversions between complex types, but that's too messy
3185 to do now. */
3186 else if (TREE_CODE (type) == REAL_TYPE
3187 && TREE_CODE (valtype) == REAL_TYPE)
3189 /* Warn if any argument is passed as `float',
3190 since without a prototype it would be `double'. */
3191 if (formal_prec == TYPE_PRECISION (float_type_node)
3192 && type != dfloat32_type_node)
3193 warning_at (ploc, 0,
3194 "passing argument %d of %qE as %<float%> "
3195 "rather than %<double%> due to prototype",
3196 argnum, rname);
3198 /* Warn if mismatch between argument and prototype
3199 for decimal float types. Warn of conversions with
3200 binary float types and of precision narrowing due to
3201 prototype. */
3202 else if (type != valtype
3203 && (type == dfloat32_type_node
3204 || type == dfloat64_type_node
3205 || type == dfloat128_type_node
3206 || valtype == dfloat32_type_node
3207 || valtype == dfloat64_type_node
3208 || valtype == dfloat128_type_node)
3209 && (formal_prec
3210 <= TYPE_PRECISION (valtype)
3211 || (type == dfloat128_type_node
3212 && (valtype
3213 != dfloat64_type_node
3214 && (valtype
3215 != dfloat32_type_node)))
3216 || (type == dfloat64_type_node
3217 && (valtype
3218 != dfloat32_type_node))))
3219 warning_at (ploc, 0,
3220 "passing argument %d of %qE as %qT "
3221 "rather than %qT due to prototype",
3222 argnum, rname, type, valtype);
3225 /* Detect integer changing in width or signedness.
3226 These warnings are only activated with
3227 -Wtraditional-conversion, not with -Wtraditional. */
3228 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3229 && INTEGRAL_TYPE_P (valtype))
3231 tree would_have_been = default_conversion (val);
3232 tree type1 = TREE_TYPE (would_have_been);
3234 if (TREE_CODE (type) == ENUMERAL_TYPE
3235 && (TYPE_MAIN_VARIANT (type)
3236 == TYPE_MAIN_VARIANT (valtype)))
3237 /* No warning if function asks for enum
3238 and the actual arg is that enum type. */
3240 else if (formal_prec != TYPE_PRECISION (type1))
3241 warning_at (ploc, OPT_Wtraditional_conversion,
3242 "passing argument %d of %qE "
3243 "with different width due to prototype",
3244 argnum, rname);
3245 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3247 /* Don't complain if the formal parameter type
3248 is an enum, because we can't tell now whether
3249 the value was an enum--even the same enum. */
3250 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3252 else if (TREE_CODE (val) == INTEGER_CST
3253 && int_fits_type_p (val, type))
3254 /* Change in signedness doesn't matter
3255 if a constant value is unaffected. */
3257 /* If the value is extended from a narrower
3258 unsigned type, it doesn't matter whether we
3259 pass it as signed or unsigned; the value
3260 certainly is the same either way. */
3261 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3262 && TYPE_UNSIGNED (valtype))
3264 else if (TYPE_UNSIGNED (type))
3265 warning_at (ploc, OPT_Wtraditional_conversion,
3266 "passing argument %d of %qE "
3267 "as unsigned due to prototype",
3268 argnum, rname);
3269 else
3270 warning_at (ploc, OPT_Wtraditional_conversion,
3271 "passing argument %d of %qE "
3272 "as signed due to prototype",
3273 argnum, rname);
3277 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3278 sake of better warnings from convert_and_check. */
3279 if (excess_precision)
3280 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3281 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3282 parmval = convert_for_assignment (loc, ploc, type,
3283 val, origtype, ic_argpass,
3284 npc, fundecl, function,
3285 parmnum + 1);
3287 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3288 && INTEGRAL_TYPE_P (type)
3289 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3290 parmval = default_conversion (parmval);
3293 else if (TREE_CODE (valtype) == REAL_TYPE
3294 && (TYPE_PRECISION (valtype)
3295 <= TYPE_PRECISION (double_type_node))
3296 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3297 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3298 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3300 if (type_generic)
3301 parmval = val;
3302 else
3304 /* Convert `float' to `double'. */
3305 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3306 warning_at (ploc, OPT_Wdouble_promotion,
3307 "implicit conversion from %qT to %qT when passing "
3308 "argument to function",
3309 valtype, double_type_node);
3310 parmval = convert (double_type_node, val);
3313 else if (excess_precision && !type_generic)
3314 /* A "double" argument with excess precision being passed
3315 without a prototype or in variable arguments. */
3316 parmval = convert (valtype, val);
3317 else if ((invalid_func_diag =
3318 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3320 error (invalid_func_diag);
3321 return -1;
3323 else
3324 /* Convert `short' and `char' to full-size `int'. */
3325 parmval = default_conversion (val);
3327 (*values)[parmnum] = parmval;
3328 if (parmval == error_mark_node)
3329 error_args = true;
3331 if (typetail)
3332 typetail = TREE_CHAIN (typetail);
3335 gcc_assert (parmnum == vec_safe_length (values));
3337 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3339 error_at (loc, "too few arguments to function %qE", function);
3340 inform_declaration (fundecl);
3341 return -1;
3344 return error_args ? -1 : (int) parmnum;
3347 /* This is the entry point used by the parser to build unary operators
3348 in the input. CODE, a tree_code, specifies the unary operator, and
3349 ARG is the operand. For unary plus, the C parser currently uses
3350 CONVERT_EXPR for code.
3352 LOC is the location to use for the tree generated.
3355 struct c_expr
3356 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3358 struct c_expr result;
3360 result.value = build_unary_op (loc, code, arg.value, 0);
3361 result.original_code = code;
3362 result.original_type = NULL;
3364 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3365 overflow_warning (loc, result.value);
3367 return result;
3370 /* This is the entry point used by the parser to build binary operators
3371 in the input. CODE, a tree_code, specifies the binary operator, and
3372 ARG1 and ARG2 are the operands. In addition to constructing the
3373 expression, we check for operands that were written with other binary
3374 operators in a way that is likely to confuse the user.
3376 LOCATION is the location of the binary operator. */
3378 struct c_expr
3379 parser_build_binary_op (location_t location, enum tree_code code,
3380 struct c_expr arg1, struct c_expr arg2)
3382 struct c_expr result;
3384 enum tree_code code1 = arg1.original_code;
3385 enum tree_code code2 = arg2.original_code;
3386 tree type1 = (arg1.original_type
3387 ? arg1.original_type
3388 : TREE_TYPE (arg1.value));
3389 tree type2 = (arg2.original_type
3390 ? arg2.original_type
3391 : TREE_TYPE (arg2.value));
3393 result.value = build_binary_op (location, code,
3394 arg1.value, arg2.value, 1);
3395 result.original_code = code;
3396 result.original_type = NULL;
3398 if (TREE_CODE (result.value) == ERROR_MARK)
3399 return result;
3401 if (location != UNKNOWN_LOCATION)
3402 protected_set_expr_location (result.value, location);
3404 /* Check for cases such as x+y<<z which users are likely
3405 to misinterpret. */
3406 if (warn_parentheses)
3407 warn_about_parentheses (location, code, code1, arg1.value, code2,
3408 arg2.value);
3410 if (warn_logical_op)
3411 warn_logical_operator (location, code, TREE_TYPE (result.value),
3412 code1, arg1.value, code2, arg2.value);
3414 if (warn_logical_not_paren
3415 && code1 == TRUTH_NOT_EXPR
3416 && code2 != TRUTH_NOT_EXPR)
3417 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3419 /* Warn about comparisons against string literals, with the exception
3420 of testing for equality or inequality of a string literal with NULL. */
3421 if (code == EQ_EXPR || code == NE_EXPR)
3423 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3424 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3425 warning_at (location, OPT_Waddress,
3426 "comparison with string literal results in unspecified behavior");
3428 else if (TREE_CODE_CLASS (code) == tcc_comparison
3429 && (code1 == STRING_CST || code2 == STRING_CST))
3430 warning_at (location, OPT_Waddress,
3431 "comparison with string literal results in unspecified behavior");
3433 if (TREE_OVERFLOW_P (result.value)
3434 && !TREE_OVERFLOW_P (arg1.value)
3435 && !TREE_OVERFLOW_P (arg2.value))
3436 overflow_warning (location, result.value);
3438 /* Warn about comparisons of different enum types. */
3439 if (warn_enum_compare
3440 && TREE_CODE_CLASS (code) == tcc_comparison
3441 && TREE_CODE (type1) == ENUMERAL_TYPE
3442 && TREE_CODE (type2) == ENUMERAL_TYPE
3443 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3444 warning_at (location, OPT_Wenum_compare,
3445 "comparison between %qT and %qT",
3446 type1, type2);
3448 return result;
3451 /* Return a tree for the difference of pointers OP0 and OP1.
3452 The resulting tree has type int. */
3454 static tree
3455 pointer_diff (location_t loc, tree op0, tree op1)
3457 tree restype = ptrdiff_type_node;
3458 tree result, inttype;
3460 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3461 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3462 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3463 tree con0, con1, lit0, lit1;
3464 tree orig_op1 = op1;
3466 /* If the operands point into different address spaces, we need to
3467 explicitly convert them to pointers into the common address space
3468 before we can subtract the numerical address values. */
3469 if (as0 != as1)
3471 addr_space_t as_common;
3472 tree common_type;
3474 /* Determine the common superset address space. This is guaranteed
3475 to exist because the caller verified that comp_target_types
3476 returned non-zero. */
3477 if (!addr_space_superset (as0, as1, &as_common))
3478 gcc_unreachable ();
3480 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3481 op0 = convert (common_type, op0);
3482 op1 = convert (common_type, op1);
3485 /* Determine integer type to perform computations in. This will usually
3486 be the same as the result type (ptrdiff_t), but may need to be a wider
3487 type if pointers for the address space are wider than ptrdiff_t. */
3488 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3489 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3490 else
3491 inttype = restype;
3494 if (TREE_CODE (target_type) == VOID_TYPE)
3495 pedwarn (loc, OPT_Wpointer_arith,
3496 "pointer of type %<void *%> used in subtraction");
3497 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3498 pedwarn (loc, OPT_Wpointer_arith,
3499 "pointer to a function used in subtraction");
3501 /* If the conversion to ptrdiff_type does anything like widening or
3502 converting a partial to an integral mode, we get a convert_expression
3503 that is in the way to do any simplifications.
3504 (fold-const.c doesn't know that the extra bits won't be needed.
3505 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3506 different mode in place.)
3507 So first try to find a common term here 'by hand'; we want to cover
3508 at least the cases that occur in legal static initializers. */
3509 if (CONVERT_EXPR_P (op0)
3510 && (TYPE_PRECISION (TREE_TYPE (op0))
3511 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3512 con0 = TREE_OPERAND (op0, 0);
3513 else
3514 con0 = op0;
3515 if (CONVERT_EXPR_P (op1)
3516 && (TYPE_PRECISION (TREE_TYPE (op1))
3517 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3518 con1 = TREE_OPERAND (op1, 0);
3519 else
3520 con1 = op1;
3522 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3524 lit0 = TREE_OPERAND (con0, 1);
3525 con0 = TREE_OPERAND (con0, 0);
3527 else
3528 lit0 = integer_zero_node;
3530 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3532 lit1 = TREE_OPERAND (con1, 1);
3533 con1 = TREE_OPERAND (con1, 0);
3535 else
3536 lit1 = integer_zero_node;
3538 if (operand_equal_p (con0, con1, 0))
3540 op0 = lit0;
3541 op1 = lit1;
3545 /* First do the subtraction as integers;
3546 then drop through to build the divide operator.
3547 Do not do default conversions on the minus operator
3548 in case restype is a short type. */
3550 op0 = build_binary_op (loc,
3551 MINUS_EXPR, convert (inttype, op0),
3552 convert (inttype, op1), 0);
3553 /* This generates an error if op1 is pointer to incomplete type. */
3554 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3555 error_at (loc, "arithmetic on pointer to an incomplete type");
3557 op1 = c_size_in_bytes (target_type);
3559 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3560 error_at (loc, "arithmetic on pointer to an empty aggregate");
3562 /* Divide by the size, in easiest possible way. */
3563 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3564 op0, convert (inttype, op1));
3566 /* Convert to final result type if necessary. */
3567 return convert (restype, result);
3570 /* Expand atomic compound assignments into an approriate sequence as
3571 specified by the C11 standard section 6.5.16.2.
3572 given
3573 _Atomic T1 E1
3574 T2 E2
3575 E1 op= E2
3577 This sequence is used for all types for which these operations are
3578 supported.
3580 In addition, built-in versions of the 'fe' prefixed routines may
3581 need to be invoked for floating point (real, complex or vector) when
3582 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3584 T1 newval;
3585 T1 old;
3586 T1 *addr
3587 T2 val
3588 fenv_t fenv
3590 addr = &E1;
3591 val = (E2);
3592 __atomic_load (addr, &old, SEQ_CST);
3593 feholdexcept (&fenv);
3594 loop:
3595 newval = old op val;
3596 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3597 SEQ_CST))
3598 goto done;
3599 feclearexcept (FE_ALL_EXCEPT);
3600 goto loop:
3601 done:
3602 feupdateenv (&fenv);
3604 Also note that the compiler is simply issuing the generic form of
3605 the atomic operations. This requires temp(s) and has their address
3606 taken. The atomic processing is smart enough to figure out when the
3607 size of an object can utilize a lock-free version, and convert the
3608 built-in call to the appropriate lock-free routine. The optimizers
3609 will then dispose of any temps that are no longer required, and
3610 lock-free implementations are utilized as long as there is target
3611 support for the required size.
3613 If the operator is NOP_EXPR, then this is a simple assignment, and
3614 an __atomic_store is issued to perform the assignment rather than
3615 the above loop.
3619 /* Build an atomic assignment at LOC, expanding into the proper
3620 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3621 the result of the operation, unless RETURN_OLD_P in which case
3622 return the old value of LHS (this is only for postincrement and
3623 postdecrement). */
3624 static tree
3625 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3626 tree rhs, bool return_old_p)
3628 tree fndecl, func_call;
3629 vec<tree, va_gc> *params;
3630 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3631 tree old, old_addr;
3632 tree compound_stmt;
3633 tree stmt, goto_stmt;
3634 tree loop_label, loop_decl, done_label, done_decl;
3636 tree lhs_type = TREE_TYPE (lhs);
3637 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3638 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3639 tree rhs_type = TREE_TYPE (rhs);
3641 gcc_assert (TYPE_ATOMIC (lhs_type));
3643 if (return_old_p)
3644 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3646 /* Allocate enough vector items for a compare_exchange. */
3647 vec_alloc (params, 6);
3649 /* Create a compound statement to hold the sequence of statements
3650 with a loop. */
3651 compound_stmt = c_begin_compound_stmt (false);
3653 /* Fold the RHS if it hasn't already been folded. */
3654 if (modifycode != NOP_EXPR)
3655 rhs = c_fully_fold (rhs, false, NULL);
3657 /* Remove the qualifiers for the rest of the expressions and create
3658 the VAL temp variable to hold the RHS. */
3659 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3660 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3661 val = create_tmp_var (nonatomic_rhs_type, NULL);
3662 TREE_ADDRESSABLE (val) = 1;
3663 TREE_NO_WARNING (val) = 1;
3664 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3665 SET_EXPR_LOCATION (rhs, loc);
3666 add_stmt (rhs);
3668 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3669 an atomic_store. */
3670 if (modifycode == NOP_EXPR)
3672 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3673 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3674 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3675 params->quick_push (lhs_addr);
3676 params->quick_push (rhs);
3677 params->quick_push (seq_cst);
3678 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3679 add_stmt (func_call);
3681 /* Finish the compound statement. */
3682 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3684 /* VAL is the value which was stored, return a COMPOUND_STMT of
3685 the statement and that value. */
3686 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3689 /* Create the variables and labels required for the op= form. */
3690 old = create_tmp_var (nonatomic_lhs_type, NULL);
3691 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3692 TREE_ADDRESSABLE (old) = 1;
3693 TREE_NO_WARNING (old) = 1;
3695 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3696 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3697 TREE_ADDRESSABLE (newval) = 1;
3699 loop_decl = create_artificial_label (loc);
3700 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3702 done_decl = create_artificial_label (loc);
3703 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3705 /* __atomic_load (addr, &old, SEQ_CST). */
3706 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3707 params->quick_push (lhs_addr);
3708 params->quick_push (old_addr);
3709 params->quick_push (seq_cst);
3710 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3711 add_stmt (func_call);
3712 params->truncate (0);
3714 /* Create the expressions for floating-point environment
3715 manipulation, if required. */
3716 bool need_fenv = (flag_trapping_math
3717 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3718 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3719 if (need_fenv)
3720 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3722 if (hold_call)
3723 add_stmt (hold_call);
3725 /* loop: */
3726 add_stmt (loop_label);
3728 /* newval = old + val; */
3729 rhs = build_binary_op (loc, modifycode, old, val, 1);
3730 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3731 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3732 NULL_TREE, 0);
3733 if (rhs != error_mark_node)
3735 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3736 SET_EXPR_LOCATION (rhs, loc);
3737 add_stmt (rhs);
3740 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3741 goto done; */
3742 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3743 params->quick_push (lhs_addr);
3744 params->quick_push (old_addr);
3745 params->quick_push (newval_addr);
3746 params->quick_push (integer_zero_node);
3747 params->quick_push (seq_cst);
3748 params->quick_push (seq_cst);
3749 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3751 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3752 SET_EXPR_LOCATION (goto_stmt, loc);
3754 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3755 SET_EXPR_LOCATION (stmt, loc);
3756 add_stmt (stmt);
3758 if (clear_call)
3759 add_stmt (clear_call);
3761 /* goto loop; */
3762 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3763 SET_EXPR_LOCATION (goto_stmt, loc);
3764 add_stmt (goto_stmt);
3766 /* done: */
3767 add_stmt (done_label);
3769 if (update_call)
3770 add_stmt (update_call);
3772 /* Finish the compound statement. */
3773 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3775 /* NEWVAL is the value that was successfully stored, return a
3776 COMPOUND_EXPR of the statement and the appropriate value. */
3777 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3778 return_old_p ? old : newval);
3781 /* Construct and perhaps optimize a tree representation
3782 for a unary operation. CODE, a tree_code, specifies the operation
3783 and XARG is the operand.
3784 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3785 the default promotions (such as from short to int).
3786 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3787 allows non-lvalues; this is only used to handle conversion of non-lvalue
3788 arrays to pointers in C99.
3790 LOCATION is the location of the operator. */
3792 tree
3793 build_unary_op (location_t location,
3794 enum tree_code code, tree xarg, int flag)
3796 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3797 tree arg = xarg;
3798 tree argtype = 0;
3799 enum tree_code typecode;
3800 tree val;
3801 tree ret = error_mark_node;
3802 tree eptype = NULL_TREE;
3803 int noconvert = flag;
3804 const char *invalid_op_diag;
3805 bool int_operands;
3807 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3808 if (int_operands)
3809 arg = remove_c_maybe_const_expr (arg);
3811 if (code != ADDR_EXPR)
3812 arg = require_complete_type (arg);
3814 typecode = TREE_CODE (TREE_TYPE (arg));
3815 if (typecode == ERROR_MARK)
3816 return error_mark_node;
3817 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3818 typecode = INTEGER_TYPE;
3820 if ((invalid_op_diag
3821 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3823 error_at (location, invalid_op_diag);
3824 return error_mark_node;
3827 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3829 eptype = TREE_TYPE (arg);
3830 arg = TREE_OPERAND (arg, 0);
3833 switch (code)
3835 case CONVERT_EXPR:
3836 /* This is used for unary plus, because a CONVERT_EXPR
3837 is enough to prevent anybody from looking inside for
3838 associativity, but won't generate any code. */
3839 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3840 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3841 || typecode == VECTOR_TYPE))
3843 error_at (location, "wrong type argument to unary plus");
3844 return error_mark_node;
3846 else if (!noconvert)
3847 arg = default_conversion (arg);
3848 arg = non_lvalue_loc (location, arg);
3849 break;
3851 case NEGATE_EXPR:
3852 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3853 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3854 || typecode == VECTOR_TYPE))
3856 error_at (location, "wrong type argument to unary minus");
3857 return error_mark_node;
3859 else if (!noconvert)
3860 arg = default_conversion (arg);
3861 break;
3863 case BIT_NOT_EXPR:
3864 /* ~ works on integer types and non float vectors. */
3865 if (typecode == INTEGER_TYPE
3866 || (typecode == VECTOR_TYPE
3867 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3869 if (!noconvert)
3870 arg = default_conversion (arg);
3872 else if (typecode == COMPLEX_TYPE)
3874 code = CONJ_EXPR;
3875 pedwarn (location, OPT_Wpedantic,
3876 "ISO C does not support %<~%> for complex conjugation");
3877 if (!noconvert)
3878 arg = default_conversion (arg);
3880 else
3882 error_at (location, "wrong type argument to bit-complement");
3883 return error_mark_node;
3885 break;
3887 case ABS_EXPR:
3888 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3890 error_at (location, "wrong type argument to abs");
3891 return error_mark_node;
3893 else if (!noconvert)
3894 arg = default_conversion (arg);
3895 break;
3897 case CONJ_EXPR:
3898 /* Conjugating a real value is a no-op, but allow it anyway. */
3899 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3900 || typecode == COMPLEX_TYPE))
3902 error_at (location, "wrong type argument to conjugation");
3903 return error_mark_node;
3905 else if (!noconvert)
3906 arg = default_conversion (arg);
3907 break;
3909 case TRUTH_NOT_EXPR:
3910 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3911 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3912 && typecode != COMPLEX_TYPE)
3914 error_at (location,
3915 "wrong type argument to unary exclamation mark");
3916 return error_mark_node;
3918 if (int_operands)
3920 arg = c_objc_common_truthvalue_conversion (location, xarg);
3921 arg = remove_c_maybe_const_expr (arg);
3923 else
3924 arg = c_objc_common_truthvalue_conversion (location, arg);
3925 ret = invert_truthvalue_loc (location, arg);
3926 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3927 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3928 location = EXPR_LOCATION (ret);
3929 goto return_build_unary_op;
3931 case REALPART_EXPR:
3932 case IMAGPART_EXPR:
3933 ret = build_real_imag_expr (location, code, arg);
3934 if (ret == error_mark_node)
3935 return error_mark_node;
3936 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3937 eptype = TREE_TYPE (eptype);
3938 goto return_build_unary_op;
3940 case PREINCREMENT_EXPR:
3941 case POSTINCREMENT_EXPR:
3942 case PREDECREMENT_EXPR:
3943 case POSTDECREMENT_EXPR:
3945 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3947 tree inner = build_unary_op (location, code,
3948 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3949 if (inner == error_mark_node)
3950 return error_mark_node;
3951 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3952 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3953 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3954 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3955 goto return_build_unary_op;
3958 /* Complain about anything that is not a true lvalue. In
3959 Objective-C, skip this check for property_refs. */
3960 if (!objc_is_property_ref (arg)
3961 && !lvalue_or_else (location,
3962 arg, ((code == PREINCREMENT_EXPR
3963 || code == POSTINCREMENT_EXPR)
3964 ? lv_increment
3965 : lv_decrement)))
3966 return error_mark_node;
3968 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3970 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3971 warning_at (location, OPT_Wc___compat,
3972 "increment of enumeration value is invalid in C++");
3973 else
3974 warning_at (location, OPT_Wc___compat,
3975 "decrement of enumeration value is invalid in C++");
3978 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3979 arg = c_fully_fold (arg, false, NULL);
3981 bool atomic_op;
3982 atomic_op = really_atomic_lvalue (arg);
3984 /* Increment or decrement the real part of the value,
3985 and don't change the imaginary part. */
3986 if (typecode == COMPLEX_TYPE)
3988 tree real, imag;
3990 pedwarn (location, OPT_Wpedantic,
3991 "ISO C does not support %<++%> and %<--%> on complex types");
3993 if (!atomic_op)
3995 arg = stabilize_reference (arg);
3996 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3997 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3998 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3999 if (real == error_mark_node || imag == error_mark_node)
4000 return error_mark_node;
4001 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4002 real, imag);
4003 goto return_build_unary_op;
4007 /* Report invalid types. */
4009 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4010 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4011 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4013 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4014 error_at (location, "wrong type argument to increment");
4015 else
4016 error_at (location, "wrong type argument to decrement");
4018 return error_mark_node;
4022 tree inc;
4024 argtype = TREE_TYPE (arg);
4026 /* Compute the increment. */
4028 if (typecode == POINTER_TYPE)
4030 /* If pointer target is an incomplete type,
4031 we just cannot know how to do the arithmetic. */
4032 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4034 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4035 error_at (location,
4036 "increment of pointer to an incomplete type %qT",
4037 TREE_TYPE (argtype));
4038 else
4039 error_at (location,
4040 "decrement of pointer to an incomplete type %qT",
4041 TREE_TYPE (argtype));
4043 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4044 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4046 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4047 pedwarn (location, OPT_Wpointer_arith,
4048 "wrong type argument to increment");
4049 else
4050 pedwarn (location, OPT_Wpointer_arith,
4051 "wrong type argument to decrement");
4054 inc = c_size_in_bytes (TREE_TYPE (argtype));
4055 inc = convert_to_ptrofftype_loc (location, inc);
4057 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4059 /* For signed fract types, we invert ++ to -- or
4060 -- to ++, and change inc from 1 to -1, because
4061 it is not possible to represent 1 in signed fract constants.
4062 For unsigned fract types, the result always overflows and
4063 we get an undefined (original) or the maximum value. */
4064 if (code == PREINCREMENT_EXPR)
4065 code = PREDECREMENT_EXPR;
4066 else if (code == PREDECREMENT_EXPR)
4067 code = PREINCREMENT_EXPR;
4068 else if (code == POSTINCREMENT_EXPR)
4069 code = POSTDECREMENT_EXPR;
4070 else /* code == POSTDECREMENT_EXPR */
4071 code = POSTINCREMENT_EXPR;
4073 inc = integer_minus_one_node;
4074 inc = convert (argtype, inc);
4076 else
4078 inc = VECTOR_TYPE_P (argtype)
4079 ? build_one_cst (argtype)
4080 : integer_one_node;
4081 inc = convert (argtype, inc);
4084 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4085 need to ask Objective-C to build the increment or decrement
4086 expression for it. */
4087 if (objc_is_property_ref (arg))
4088 return objc_build_incr_expr_for_property_ref (location, code,
4089 arg, inc);
4091 /* Report a read-only lvalue. */
4092 if (TYPE_READONLY (argtype))
4094 readonly_error (location, arg,
4095 ((code == PREINCREMENT_EXPR
4096 || code == POSTINCREMENT_EXPR)
4097 ? lv_increment : lv_decrement));
4098 return error_mark_node;
4100 else if (TREE_READONLY (arg))
4101 readonly_warning (arg,
4102 ((code == PREINCREMENT_EXPR
4103 || code == POSTINCREMENT_EXPR)
4104 ? lv_increment : lv_decrement));
4106 /* If the argument is atomic, use the special code sequences for
4107 atomic compound assignment. */
4108 if (atomic_op)
4110 arg = stabilize_reference (arg);
4111 ret = build_atomic_assign (location, arg,
4112 ((code == PREINCREMENT_EXPR
4113 || code == POSTINCREMENT_EXPR)
4114 ? PLUS_EXPR
4115 : MINUS_EXPR),
4116 (FRACT_MODE_P (TYPE_MODE (argtype))
4117 ? inc
4118 : integer_one_node),
4119 (code == POSTINCREMENT_EXPR
4120 || code == POSTDECREMENT_EXPR));
4121 goto return_build_unary_op;
4124 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4125 val = boolean_increment (code, arg);
4126 else
4127 val = build2 (code, TREE_TYPE (arg), arg, inc);
4128 TREE_SIDE_EFFECTS (val) = 1;
4129 if (TREE_CODE (val) != code)
4130 TREE_NO_WARNING (val) = 1;
4131 ret = val;
4132 goto return_build_unary_op;
4135 case ADDR_EXPR:
4136 /* Note that this operation never does default_conversion. */
4138 /* The operand of unary '&' must be an lvalue (which excludes
4139 expressions of type void), or, in C99, the result of a [] or
4140 unary '*' operator. */
4141 if (VOID_TYPE_P (TREE_TYPE (arg))
4142 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4143 && (TREE_CODE (arg) != INDIRECT_REF
4144 || !flag_isoc99))
4145 pedwarn (location, 0, "taking address of expression of type %<void%>");
4147 /* Let &* cancel out to simplify resulting code. */
4148 if (TREE_CODE (arg) == INDIRECT_REF)
4150 /* Don't let this be an lvalue. */
4151 if (lvalue_p (TREE_OPERAND (arg, 0)))
4152 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4153 ret = TREE_OPERAND (arg, 0);
4154 goto return_build_unary_op;
4157 /* For &x[y], return x+y */
4158 if (TREE_CODE (arg) == ARRAY_REF)
4160 tree op0 = TREE_OPERAND (arg, 0);
4161 if (!c_mark_addressable (op0))
4162 return error_mark_node;
4165 /* Anything not already handled and not a true memory reference
4166 or a non-lvalue array is an error. */
4167 else if (typecode != FUNCTION_TYPE && !flag
4168 && !lvalue_or_else (location, arg, lv_addressof))
4169 return error_mark_node;
4171 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4172 folding later. */
4173 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4175 tree inner = build_unary_op (location, code,
4176 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4178 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4179 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4180 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4181 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4182 goto return_build_unary_op;
4185 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4186 argtype = TREE_TYPE (arg);
4188 /* If the lvalue is const or volatile, merge that into the type
4189 to which the address will point. This is only needed
4190 for function types. */
4191 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4192 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4193 && TREE_CODE (argtype) == FUNCTION_TYPE)
4195 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4196 int quals = orig_quals;
4198 if (TREE_READONLY (arg))
4199 quals |= TYPE_QUAL_CONST;
4200 if (TREE_THIS_VOLATILE (arg))
4201 quals |= TYPE_QUAL_VOLATILE;
4203 argtype = c_build_qualified_type (argtype, quals);
4206 if (!c_mark_addressable (arg))
4207 return error_mark_node;
4209 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4210 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4212 argtype = build_pointer_type (argtype);
4214 /* ??? Cope with user tricks that amount to offsetof. Delete this
4215 when we have proper support for integer constant expressions. */
4216 val = get_base_address (arg);
4217 if (val && TREE_CODE (val) == INDIRECT_REF
4218 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4220 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4221 goto return_build_unary_op;
4224 val = build1 (ADDR_EXPR, argtype, arg);
4226 ret = val;
4227 goto return_build_unary_op;
4229 default:
4230 gcc_unreachable ();
4233 if (argtype == 0)
4234 argtype = TREE_TYPE (arg);
4235 if (TREE_CODE (arg) == INTEGER_CST)
4236 ret = (require_constant_value
4237 ? fold_build1_initializer_loc (location, code, argtype, arg)
4238 : fold_build1_loc (location, code, argtype, arg));
4239 else
4240 ret = build1 (code, argtype, arg);
4241 return_build_unary_op:
4242 gcc_assert (ret != error_mark_node);
4243 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4244 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4245 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4246 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4247 ret = note_integer_operands (ret);
4248 if (eptype)
4249 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4250 protected_set_expr_location (ret, location);
4251 return ret;
4254 /* Return nonzero if REF is an lvalue valid for this language.
4255 Lvalues can be assigned, unless their type has TYPE_READONLY.
4256 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4258 bool
4259 lvalue_p (const_tree ref)
4261 const enum tree_code code = TREE_CODE (ref);
4263 switch (code)
4265 case REALPART_EXPR:
4266 case IMAGPART_EXPR:
4267 case COMPONENT_REF:
4268 return lvalue_p (TREE_OPERAND (ref, 0));
4270 case C_MAYBE_CONST_EXPR:
4271 return lvalue_p (TREE_OPERAND (ref, 1));
4273 case COMPOUND_LITERAL_EXPR:
4274 case STRING_CST:
4275 return 1;
4277 case INDIRECT_REF:
4278 case ARRAY_REF:
4279 case ARRAY_NOTATION_REF:
4280 case VAR_DECL:
4281 case PARM_DECL:
4282 case RESULT_DECL:
4283 case ERROR_MARK:
4284 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4285 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4287 case BIND_EXPR:
4288 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4290 default:
4291 return 0;
4295 /* Give a warning for storing in something that is read-only in GCC
4296 terms but not const in ISO C terms. */
4298 static void
4299 readonly_warning (tree arg, enum lvalue_use use)
4301 switch (use)
4303 case lv_assign:
4304 warning (0, "assignment of read-only location %qE", arg);
4305 break;
4306 case lv_increment:
4307 warning (0, "increment of read-only location %qE", arg);
4308 break;
4309 case lv_decrement:
4310 warning (0, "decrement of read-only location %qE", arg);
4311 break;
4312 default:
4313 gcc_unreachable ();
4315 return;
4319 /* Return nonzero if REF is an lvalue valid for this language;
4320 otherwise, print an error message and return zero. USE says
4321 how the lvalue is being used and so selects the error message.
4322 LOCATION is the location at which any error should be reported. */
4324 static int
4325 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4327 int win = lvalue_p (ref);
4329 if (!win)
4330 lvalue_error (loc, use);
4332 return win;
4335 /* Mark EXP saying that we need to be able to take the
4336 address of it; it should not be allocated in a register.
4337 Returns true if successful. */
4339 bool
4340 c_mark_addressable (tree exp)
4342 tree x = exp;
4344 while (1)
4345 switch (TREE_CODE (x))
4347 case COMPONENT_REF:
4348 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4350 error
4351 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4352 return false;
4355 /* ... fall through ... */
4357 case ADDR_EXPR:
4358 case ARRAY_REF:
4359 case REALPART_EXPR:
4360 case IMAGPART_EXPR:
4361 x = TREE_OPERAND (x, 0);
4362 break;
4364 case COMPOUND_LITERAL_EXPR:
4365 case CONSTRUCTOR:
4366 TREE_ADDRESSABLE (x) = 1;
4367 return true;
4369 case VAR_DECL:
4370 case CONST_DECL:
4371 case PARM_DECL:
4372 case RESULT_DECL:
4373 if (C_DECL_REGISTER (x)
4374 && DECL_NONLOCAL (x))
4376 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4378 error
4379 ("global register variable %qD used in nested function", x);
4380 return false;
4382 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4384 else if (C_DECL_REGISTER (x))
4386 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4387 error ("address of global register variable %qD requested", x);
4388 else
4389 error ("address of register variable %qD requested", x);
4390 return false;
4393 /* drops in */
4394 case FUNCTION_DECL:
4395 TREE_ADDRESSABLE (x) = 1;
4396 /* drops out */
4397 default:
4398 return true;
4402 /* Convert EXPR to TYPE, warning about conversion problems with
4403 constants. SEMANTIC_TYPE is the type this conversion would use
4404 without excess precision. If SEMANTIC_TYPE is NULL, this function
4405 is equivalent to convert_and_check. This function is a wrapper that
4406 handles conversions that may be different than
4407 the usual ones because of excess precision. */
4409 static tree
4410 ep_convert_and_check (location_t loc, tree type, tree expr,
4411 tree semantic_type)
4413 if (TREE_TYPE (expr) == type)
4414 return expr;
4416 if (!semantic_type)
4417 return convert_and_check (loc, type, expr);
4419 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4420 && TREE_TYPE (expr) != semantic_type)
4422 /* For integers, we need to check the real conversion, not
4423 the conversion to the excess precision type. */
4424 expr = convert_and_check (loc, semantic_type, expr);
4426 /* Result type is the excess precision type, which should be
4427 large enough, so do not check. */
4428 return convert (type, expr);
4431 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4432 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4433 if folded to an integer constant then the unselected half may
4434 contain arbitrary operations not normally permitted in constant
4435 expressions. Set the location of the expression to LOC. */
4437 tree
4438 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4439 tree op1, tree op1_original_type, tree op2,
4440 tree op2_original_type)
4442 tree type1;
4443 tree type2;
4444 enum tree_code code1;
4445 enum tree_code code2;
4446 tree result_type = NULL;
4447 tree semantic_result_type = NULL;
4448 tree orig_op1 = op1, orig_op2 = op2;
4449 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4450 bool ifexp_int_operands;
4451 tree ret;
4453 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4454 if (op1_int_operands)
4455 op1 = remove_c_maybe_const_expr (op1);
4456 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4457 if (op2_int_operands)
4458 op2 = remove_c_maybe_const_expr (op2);
4459 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4460 if (ifexp_int_operands)
4461 ifexp = remove_c_maybe_const_expr (ifexp);
4463 /* Promote both alternatives. */
4465 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4466 op1 = default_conversion (op1);
4467 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4468 op2 = default_conversion (op2);
4470 if (TREE_CODE (ifexp) == ERROR_MARK
4471 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4472 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4473 return error_mark_node;
4475 type1 = TREE_TYPE (op1);
4476 code1 = TREE_CODE (type1);
4477 type2 = TREE_TYPE (op2);
4478 code2 = TREE_CODE (type2);
4480 /* C90 does not permit non-lvalue arrays in conditional expressions.
4481 In C99 they will be pointers by now. */
4482 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4484 error_at (colon_loc, "non-lvalue array in conditional expression");
4485 return error_mark_node;
4488 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4489 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4490 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4491 || code1 == COMPLEX_TYPE)
4492 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4493 || code2 == COMPLEX_TYPE))
4495 semantic_result_type = c_common_type (type1, type2);
4496 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4498 op1 = TREE_OPERAND (op1, 0);
4499 type1 = TREE_TYPE (op1);
4500 gcc_assert (TREE_CODE (type1) == code1);
4502 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4504 op2 = TREE_OPERAND (op2, 0);
4505 type2 = TREE_TYPE (op2);
4506 gcc_assert (TREE_CODE (type2) == code2);
4510 if (warn_cxx_compat)
4512 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4513 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4515 if (TREE_CODE (t1) == ENUMERAL_TYPE
4516 && TREE_CODE (t2) == ENUMERAL_TYPE
4517 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4518 warning_at (colon_loc, OPT_Wc___compat,
4519 ("different enum types in conditional is "
4520 "invalid in C++: %qT vs %qT"),
4521 t1, t2);
4524 /* Quickly detect the usual case where op1 and op2 have the same type
4525 after promotion. */
4526 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4528 if (type1 == type2)
4529 result_type = type1;
4530 else
4531 result_type = TYPE_MAIN_VARIANT (type1);
4533 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4534 || code1 == COMPLEX_TYPE)
4535 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4536 || code2 == COMPLEX_TYPE))
4538 result_type = c_common_type (type1, type2);
4539 do_warn_double_promotion (result_type, type1, type2,
4540 "implicit conversion from %qT to %qT to "
4541 "match other result of conditional",
4542 colon_loc);
4544 /* If -Wsign-compare, warn here if type1 and type2 have
4545 different signedness. We'll promote the signed to unsigned
4546 and later code won't know it used to be different.
4547 Do this check on the original types, so that explicit casts
4548 will be considered, but default promotions won't. */
4549 if (c_inhibit_evaluation_warnings == 0)
4551 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4552 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4554 if (unsigned_op1 ^ unsigned_op2)
4556 bool ovf;
4558 /* Do not warn if the result type is signed, since the
4559 signed type will only be chosen if it can represent
4560 all the values of the unsigned type. */
4561 if (!TYPE_UNSIGNED (result_type))
4562 /* OK */;
4563 else
4565 bool op1_maybe_const = true;
4566 bool op2_maybe_const = true;
4568 /* Do not warn if the signed quantity is an
4569 unsuffixed integer literal (or some static
4570 constant expression involving such literals) and
4571 it is non-negative. This warning requires the
4572 operands to be folded for best results, so do
4573 that folding in this case even without
4574 warn_sign_compare to avoid warning options
4575 possibly affecting code generation. */
4576 c_inhibit_evaluation_warnings
4577 += (ifexp == truthvalue_false_node);
4578 op1 = c_fully_fold (op1, require_constant_value,
4579 &op1_maybe_const);
4580 c_inhibit_evaluation_warnings
4581 -= (ifexp == truthvalue_false_node);
4583 c_inhibit_evaluation_warnings
4584 += (ifexp == truthvalue_true_node);
4585 op2 = c_fully_fold (op2, require_constant_value,
4586 &op2_maybe_const);
4587 c_inhibit_evaluation_warnings
4588 -= (ifexp == truthvalue_true_node);
4590 if (warn_sign_compare)
4592 if ((unsigned_op2
4593 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4594 || (unsigned_op1
4595 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4596 /* OK */;
4597 else
4598 warning_at (colon_loc, OPT_Wsign_compare,
4599 ("signed and unsigned type in "
4600 "conditional expression"));
4602 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4603 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4604 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4605 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4610 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4612 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4613 pedwarn (colon_loc, OPT_Wpedantic,
4614 "ISO C forbids conditional expr with only one void side");
4615 result_type = void_type_node;
4617 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4619 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4620 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4621 addr_space_t as_common;
4623 if (comp_target_types (colon_loc, type1, type2))
4624 result_type = common_pointer_type (type1, type2);
4625 else if (null_pointer_constant_p (orig_op1))
4626 result_type = type2;
4627 else if (null_pointer_constant_p (orig_op2))
4628 result_type = type1;
4629 else if (!addr_space_superset (as1, as2, &as_common))
4631 error_at (colon_loc, "pointers to disjoint address spaces "
4632 "used in conditional expression");
4633 return error_mark_node;
4635 else if (VOID_TYPE_P (TREE_TYPE (type1))
4636 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4638 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4639 pedwarn (colon_loc, OPT_Wpedantic,
4640 "ISO C forbids conditional expr between "
4641 "%<void *%> and function pointer");
4642 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4643 TREE_TYPE (type2)));
4645 else if (VOID_TYPE_P (TREE_TYPE (type2))
4646 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4648 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4649 pedwarn (colon_loc, OPT_Wpedantic,
4650 "ISO C forbids conditional expr between "
4651 "%<void *%> and function pointer");
4652 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4653 TREE_TYPE (type1)));
4655 /* Objective-C pointer comparisons are a bit more lenient. */
4656 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4657 result_type = objc_common_type (type1, type2);
4658 else
4660 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4662 pedwarn (colon_loc, 0,
4663 "pointer type mismatch in conditional expression");
4664 result_type = build_pointer_type
4665 (build_qualified_type (void_type_node, qual));
4668 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4670 if (!null_pointer_constant_p (orig_op2))
4671 pedwarn (colon_loc, 0,
4672 "pointer/integer type mismatch in conditional expression");
4673 else
4675 op2 = null_pointer_node;
4677 result_type = type1;
4679 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4681 if (!null_pointer_constant_p (orig_op1))
4682 pedwarn (colon_loc, 0,
4683 "pointer/integer type mismatch in conditional expression");
4684 else
4686 op1 = null_pointer_node;
4688 result_type = type2;
4691 if (!result_type)
4693 if (flag_cond_mismatch)
4694 result_type = void_type_node;
4695 else
4697 error_at (colon_loc, "type mismatch in conditional expression");
4698 return error_mark_node;
4702 /* Merge const and volatile flags of the incoming types. */
4703 result_type
4704 = build_type_variant (result_type,
4705 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4706 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4708 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4709 semantic_result_type);
4710 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4711 semantic_result_type);
4713 if (ifexp_bcp && ifexp == truthvalue_true_node)
4715 op2_int_operands = true;
4716 op1 = c_fully_fold (op1, require_constant_value, NULL);
4718 if (ifexp_bcp && ifexp == truthvalue_false_node)
4720 op1_int_operands = true;
4721 op2 = c_fully_fold (op2, require_constant_value, NULL);
4723 int_const = int_operands = (ifexp_int_operands
4724 && op1_int_operands
4725 && op2_int_operands);
4726 if (int_operands)
4728 int_const = ((ifexp == truthvalue_true_node
4729 && TREE_CODE (orig_op1) == INTEGER_CST
4730 && !TREE_OVERFLOW (orig_op1))
4731 || (ifexp == truthvalue_false_node
4732 && TREE_CODE (orig_op2) == INTEGER_CST
4733 && !TREE_OVERFLOW (orig_op2)));
4735 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4736 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4737 else
4739 if (int_operands)
4741 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4742 nested inside of the expression. */
4743 op1 = c_fully_fold (op1, false, NULL);
4744 op2 = c_fully_fold (op2, false, NULL);
4746 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4747 if (int_operands)
4748 ret = note_integer_operands (ret);
4750 if (semantic_result_type)
4751 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4753 protected_set_expr_location (ret, colon_loc);
4754 return ret;
4757 /* Return a compound expression that performs two expressions and
4758 returns the value of the second of them.
4760 LOC is the location of the COMPOUND_EXPR. */
4762 tree
4763 build_compound_expr (location_t loc, tree expr1, tree expr2)
4765 bool expr1_int_operands, expr2_int_operands;
4766 tree eptype = NULL_TREE;
4767 tree ret;
4769 if (flag_cilkplus
4770 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4771 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4773 error_at (loc,
4774 "spawned function call cannot be part of a comma expression");
4775 return error_mark_node;
4777 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4778 if (expr1_int_operands)
4779 expr1 = remove_c_maybe_const_expr (expr1);
4780 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4781 if (expr2_int_operands)
4782 expr2 = remove_c_maybe_const_expr (expr2);
4784 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4785 expr1 = TREE_OPERAND (expr1, 0);
4786 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4788 eptype = TREE_TYPE (expr2);
4789 expr2 = TREE_OPERAND (expr2, 0);
4792 if (!TREE_SIDE_EFFECTS (expr1))
4794 /* The left-hand operand of a comma expression is like an expression
4795 statement: with -Wunused, we should warn if it doesn't have
4796 any side-effects, unless it was explicitly cast to (void). */
4797 if (warn_unused_value)
4799 if (VOID_TYPE_P (TREE_TYPE (expr1))
4800 && CONVERT_EXPR_P (expr1))
4801 ; /* (void) a, b */
4802 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4803 && TREE_CODE (expr1) == COMPOUND_EXPR
4804 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4805 ; /* (void) a, (void) b, c */
4806 else
4807 warning_at (loc, OPT_Wunused_value,
4808 "left-hand operand of comma expression has no effect");
4811 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4812 && warn_unused_value)
4814 tree r = expr1;
4815 location_t cloc = loc;
4816 while (TREE_CODE (r) == COMPOUND_EXPR)
4818 if (EXPR_HAS_LOCATION (r))
4819 cloc = EXPR_LOCATION (r);
4820 r = TREE_OPERAND (r, 1);
4822 if (!TREE_SIDE_EFFECTS (r)
4823 && !VOID_TYPE_P (TREE_TYPE (r))
4824 && !CONVERT_EXPR_P (r))
4825 warning_at (cloc, OPT_Wunused_value,
4826 "right-hand operand of comma expression has no effect");
4829 /* With -Wunused, we should also warn if the left-hand operand does have
4830 side-effects, but computes a value which is not used. For example, in
4831 `foo() + bar(), baz()' the result of the `+' operator is not used,
4832 so we should issue a warning. */
4833 else if (warn_unused_value)
4834 warn_if_unused_value (expr1, loc);
4836 if (expr2 == error_mark_node)
4837 return error_mark_node;
4839 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4841 if (flag_isoc99
4842 && expr1_int_operands
4843 && expr2_int_operands)
4844 ret = note_integer_operands (ret);
4846 if (eptype)
4847 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4849 protected_set_expr_location (ret, loc);
4850 return ret;
4853 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4854 which we are casting. OTYPE is the type of the expression being
4855 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4856 of the cast. -Wcast-qual appeared on the command line. Named
4857 address space qualifiers are not handled here, because they result
4858 in different warnings. */
4860 static void
4861 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4863 tree in_type = type;
4864 tree in_otype = otype;
4865 int added = 0;
4866 int discarded = 0;
4867 bool is_const;
4869 /* Check that the qualifiers on IN_TYPE are a superset of the
4870 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4871 nodes is uninteresting and we stop as soon as we hit a
4872 non-POINTER_TYPE node on either type. */
4875 in_otype = TREE_TYPE (in_otype);
4876 in_type = TREE_TYPE (in_type);
4878 /* GNU C allows cv-qualified function types. 'const' means the
4879 function is very pure, 'volatile' means it can't return. We
4880 need to warn when such qualifiers are added, not when they're
4881 taken away. */
4882 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4883 && TREE_CODE (in_type) == FUNCTION_TYPE)
4884 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4885 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4886 else
4887 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4888 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4890 while (TREE_CODE (in_type) == POINTER_TYPE
4891 && TREE_CODE (in_otype) == POINTER_TYPE);
4893 if (added)
4894 warning_at (loc, OPT_Wcast_qual,
4895 "cast adds %q#v qualifier to function type", added);
4897 if (discarded)
4898 /* There are qualifiers present in IN_OTYPE that are not present
4899 in IN_TYPE. */
4900 warning_at (loc, OPT_Wcast_qual,
4901 "cast discards %qv qualifier from pointer target type",
4902 discarded);
4904 if (added || discarded)
4905 return;
4907 /* A cast from **T to const **T is unsafe, because it can cause a
4908 const value to be changed with no additional warning. We only
4909 issue this warning if T is the same on both sides, and we only
4910 issue the warning if there are the same number of pointers on
4911 both sides, as otherwise the cast is clearly unsafe anyhow. A
4912 cast is unsafe when a qualifier is added at one level and const
4913 is not present at all outer levels.
4915 To issue this warning, we check at each level whether the cast
4916 adds new qualifiers not already seen. We don't need to special
4917 case function types, as they won't have the same
4918 TYPE_MAIN_VARIANT. */
4920 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4921 return;
4922 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4923 return;
4925 in_type = type;
4926 in_otype = otype;
4927 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4930 in_type = TREE_TYPE (in_type);
4931 in_otype = TREE_TYPE (in_otype);
4932 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4933 && !is_const)
4935 warning_at (loc, OPT_Wcast_qual,
4936 "to be safe all intermediate pointers in cast from "
4937 "%qT to %qT must be %<const%> qualified",
4938 otype, type);
4939 break;
4941 if (is_const)
4942 is_const = TYPE_READONLY (in_type);
4944 while (TREE_CODE (in_type) == POINTER_TYPE);
4947 /* Build an expression representing a cast to type TYPE of expression EXPR.
4948 LOC is the location of the cast-- typically the open paren of the cast. */
4950 tree
4951 build_c_cast (location_t loc, tree type, tree expr)
4953 tree value;
4955 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4956 expr = TREE_OPERAND (expr, 0);
4958 value = expr;
4960 if (type == error_mark_node || expr == error_mark_node)
4961 return error_mark_node;
4963 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4964 only in <protocol> qualifications. But when constructing cast expressions,
4965 the protocols do matter and must be kept around. */
4966 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4967 return build1 (NOP_EXPR, type, expr);
4969 type = TYPE_MAIN_VARIANT (type);
4971 if (TREE_CODE (type) == ARRAY_TYPE)
4973 error_at (loc, "cast specifies array type");
4974 return error_mark_node;
4977 if (TREE_CODE (type) == FUNCTION_TYPE)
4979 error_at (loc, "cast specifies function type");
4980 return error_mark_node;
4983 if (!VOID_TYPE_P (type))
4985 value = require_complete_type (value);
4986 if (value == error_mark_node)
4987 return error_mark_node;
4990 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4992 if (TREE_CODE (type) == RECORD_TYPE
4993 || TREE_CODE (type) == UNION_TYPE)
4994 pedwarn (loc, OPT_Wpedantic,
4995 "ISO C forbids casting nonscalar to the same type");
4997 else if (TREE_CODE (type) == UNION_TYPE)
4999 tree field;
5001 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5002 if (TREE_TYPE (field) != error_mark_node
5003 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5004 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5005 break;
5007 if (field)
5009 tree t;
5010 bool maybe_const = true;
5012 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5013 t = c_fully_fold (value, false, &maybe_const);
5014 t = build_constructor_single (type, field, t);
5015 if (!maybe_const)
5016 t = c_wrap_maybe_const (t, true);
5017 t = digest_init (loc, type, t,
5018 NULL_TREE, false, true, 0);
5019 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5020 return t;
5022 error_at (loc, "cast to union type from type not present in union");
5023 return error_mark_node;
5025 else
5027 tree otype, ovalue;
5029 if (type == void_type_node)
5031 tree t = build1 (CONVERT_EXPR, type, value);
5032 SET_EXPR_LOCATION (t, loc);
5033 return t;
5036 otype = TREE_TYPE (value);
5038 /* Optionally warn about potentially worrisome casts. */
5039 if (warn_cast_qual
5040 && TREE_CODE (type) == POINTER_TYPE
5041 && TREE_CODE (otype) == POINTER_TYPE)
5042 handle_warn_cast_qual (loc, type, otype);
5044 /* Warn about conversions between pointers to disjoint
5045 address spaces. */
5046 if (TREE_CODE (type) == POINTER_TYPE
5047 && TREE_CODE (otype) == POINTER_TYPE
5048 && !null_pointer_constant_p (value))
5050 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5051 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5052 addr_space_t as_common;
5054 if (!addr_space_superset (as_to, as_from, &as_common))
5056 if (ADDR_SPACE_GENERIC_P (as_from))
5057 warning_at (loc, 0, "cast to %s address space pointer "
5058 "from disjoint generic address space pointer",
5059 c_addr_space_name (as_to));
5061 else if (ADDR_SPACE_GENERIC_P (as_to))
5062 warning_at (loc, 0, "cast to generic address space pointer "
5063 "from disjoint %s address space pointer",
5064 c_addr_space_name (as_from));
5066 else
5067 warning_at (loc, 0, "cast to %s address space pointer "
5068 "from disjoint %s address space pointer",
5069 c_addr_space_name (as_to),
5070 c_addr_space_name (as_from));
5074 /* Warn about possible alignment problems. */
5075 if (STRICT_ALIGNMENT
5076 && TREE_CODE (type) == POINTER_TYPE
5077 && TREE_CODE (otype) == POINTER_TYPE
5078 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5079 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5080 /* Don't warn about opaque types, where the actual alignment
5081 restriction is unknown. */
5082 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5083 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5084 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5085 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5086 warning_at (loc, OPT_Wcast_align,
5087 "cast increases required alignment of target type");
5089 if (TREE_CODE (type) == INTEGER_TYPE
5090 && TREE_CODE (otype) == POINTER_TYPE
5091 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5092 /* Unlike conversion of integers to pointers, where the
5093 warning is disabled for converting constants because
5094 of cases such as SIG_*, warn about converting constant
5095 pointers to integers. In some cases it may cause unwanted
5096 sign extension, and a warning is appropriate. */
5097 warning_at (loc, OPT_Wpointer_to_int_cast,
5098 "cast from pointer to integer of different size");
5100 if (TREE_CODE (value) == CALL_EXPR
5101 && TREE_CODE (type) != TREE_CODE (otype))
5102 warning_at (loc, OPT_Wbad_function_cast,
5103 "cast from function call of type %qT "
5104 "to non-matching type %qT", otype, type);
5106 if (TREE_CODE (type) == POINTER_TYPE
5107 && TREE_CODE (otype) == INTEGER_TYPE
5108 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5109 /* Don't warn about converting any constant. */
5110 && !TREE_CONSTANT (value))
5111 warning_at (loc,
5112 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5113 "of different size");
5115 if (warn_strict_aliasing <= 2)
5116 strict_aliasing_warning (otype, type, expr);
5118 /* If pedantic, warn for conversions between function and object
5119 pointer types, except for converting a null pointer constant
5120 to function pointer type. */
5121 if (pedantic
5122 && TREE_CODE (type) == POINTER_TYPE
5123 && TREE_CODE (otype) == POINTER_TYPE
5124 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5125 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5126 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5127 "conversion of function pointer to object pointer type");
5129 if (pedantic
5130 && TREE_CODE (type) == POINTER_TYPE
5131 && TREE_CODE (otype) == POINTER_TYPE
5132 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5133 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5134 && !null_pointer_constant_p (value))
5135 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5136 "conversion of object pointer to function pointer type");
5138 ovalue = value;
5139 value = convert (type, value);
5141 /* Ignore any integer overflow caused by the cast. */
5142 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5144 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5146 if (!TREE_OVERFLOW (value))
5148 /* Avoid clobbering a shared constant. */
5149 value = copy_node (value);
5150 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5153 else if (TREE_OVERFLOW (value))
5154 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5155 value = wide_int_to_tree (TREE_TYPE (value), value);
5159 /* Don't let a cast be an lvalue. */
5160 if (value == expr)
5161 value = non_lvalue_loc (loc, value);
5163 /* Don't allow the results of casting to floating-point or complex
5164 types be confused with actual constants, or casts involving
5165 integer and pointer types other than direct integer-to-integer
5166 and integer-to-pointer be confused with integer constant
5167 expressions and null pointer constants. */
5168 if (TREE_CODE (value) == REAL_CST
5169 || TREE_CODE (value) == COMPLEX_CST
5170 || (TREE_CODE (value) == INTEGER_CST
5171 && !((TREE_CODE (expr) == INTEGER_CST
5172 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5173 || TREE_CODE (expr) == REAL_CST
5174 || TREE_CODE (expr) == COMPLEX_CST)))
5175 value = build1 (NOP_EXPR, type, value);
5177 if (CAN_HAVE_LOCATION_P (value))
5178 SET_EXPR_LOCATION (value, loc);
5179 return value;
5182 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5183 location of the open paren of the cast, or the position of the cast
5184 expr. */
5185 tree
5186 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5188 tree type;
5189 tree type_expr = NULL_TREE;
5190 bool type_expr_const = true;
5191 tree ret;
5192 int saved_wsp = warn_strict_prototypes;
5194 /* This avoids warnings about unprototyped casts on
5195 integers. E.g. "#define SIG_DFL (void(*)())0". */
5196 if (TREE_CODE (expr) == INTEGER_CST)
5197 warn_strict_prototypes = 0;
5198 type = groktypename (type_name, &type_expr, &type_expr_const);
5199 warn_strict_prototypes = saved_wsp;
5201 ret = build_c_cast (loc, type, expr);
5202 if (type_expr)
5204 bool inner_expr_const = true;
5205 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5206 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5207 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5208 && inner_expr_const);
5209 SET_EXPR_LOCATION (ret, loc);
5212 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5213 SET_EXPR_LOCATION (ret, loc);
5215 /* C++ does not permits types to be defined in a cast, but it
5216 allows references to incomplete types. */
5217 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5218 warning_at (loc, OPT_Wc___compat,
5219 "defining a type in a cast is invalid in C++");
5221 return ret;
5224 /* Build an assignment expression of lvalue LHS from value RHS.
5225 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5226 may differ from TREE_TYPE (LHS) for an enum bitfield.
5227 MODIFYCODE is the code for a binary operator that we use
5228 to combine the old value of LHS with RHS to get the new value.
5229 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5230 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5231 which may differ from TREE_TYPE (RHS) for an enum value.
5233 LOCATION is the location of the MODIFYCODE operator.
5234 RHS_LOC is the location of the RHS. */
5236 tree
5237 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5238 enum tree_code modifycode,
5239 location_t rhs_loc, tree rhs, tree rhs_origtype)
5241 tree result;
5242 tree newrhs;
5243 tree rhseval = NULL_TREE;
5244 tree rhs_semantic_type = NULL_TREE;
5245 tree lhstype = TREE_TYPE (lhs);
5246 tree olhstype = lhstype;
5247 bool npc;
5248 bool is_atomic_op;
5250 /* Types that aren't fully specified cannot be used in assignments. */
5251 lhs = require_complete_type (lhs);
5253 /* Avoid duplicate error messages from operands that had errors. */
5254 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5255 return error_mark_node;
5257 /* Ensure an error for assigning a non-lvalue array to an array in
5258 C90. */
5259 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5261 error_at (location, "assignment to expression with array type");
5262 return error_mark_node;
5265 /* For ObjC properties, defer this check. */
5266 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5267 return error_mark_node;
5269 is_atomic_op = really_atomic_lvalue (lhs);
5271 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5273 rhs_semantic_type = TREE_TYPE (rhs);
5274 rhs = TREE_OPERAND (rhs, 0);
5277 newrhs = rhs;
5279 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5281 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5282 lhs_origtype, modifycode, rhs_loc, rhs,
5283 rhs_origtype);
5284 if (inner == error_mark_node)
5285 return error_mark_node;
5286 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5287 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5288 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5289 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5290 protected_set_expr_location (result, location);
5291 return result;
5294 /* If a binary op has been requested, combine the old LHS value with the RHS
5295 producing the value we should actually store into the LHS. */
5297 if (modifycode != NOP_EXPR)
5299 lhs = c_fully_fold (lhs, false, NULL);
5300 lhs = stabilize_reference (lhs);
5302 /* Construct the RHS for any non-atomic compound assignemnt. */
5303 if (!is_atomic_op)
5305 /* If in LHS op= RHS the RHS has side-effects, ensure they
5306 are preevaluated before the rest of the assignment expression's
5307 side-effects, because RHS could contain e.g. function calls
5308 that modify LHS. */
5309 if (TREE_SIDE_EFFECTS (rhs))
5311 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5312 rhseval = newrhs;
5314 newrhs = build_binary_op (location,
5315 modifycode, lhs, newrhs, 1);
5317 /* The original type of the right hand side is no longer
5318 meaningful. */
5319 rhs_origtype = NULL_TREE;
5323 if (c_dialect_objc ())
5325 /* Check if we are modifying an Objective-C property reference;
5326 if so, we need to generate setter calls. */
5327 result = objc_maybe_build_modify_expr (lhs, newrhs);
5328 if (result)
5329 goto return_result;
5331 /* Else, do the check that we postponed for Objective-C. */
5332 if (!lvalue_or_else (location, lhs, lv_assign))
5333 return error_mark_node;
5336 /* Give an error for storing in something that is 'const'. */
5338 if (TYPE_READONLY (lhstype)
5339 || ((TREE_CODE (lhstype) == RECORD_TYPE
5340 || TREE_CODE (lhstype) == UNION_TYPE)
5341 && C_TYPE_FIELDS_READONLY (lhstype)))
5343 readonly_error (location, lhs, lv_assign);
5344 return error_mark_node;
5346 else if (TREE_READONLY (lhs))
5347 readonly_warning (lhs, lv_assign);
5349 /* If storing into a structure or union member,
5350 it has probably been given type `int'.
5351 Compute the type that would go with
5352 the actual amount of storage the member occupies. */
5354 if (TREE_CODE (lhs) == COMPONENT_REF
5355 && (TREE_CODE (lhstype) == INTEGER_TYPE
5356 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5357 || TREE_CODE (lhstype) == REAL_TYPE
5358 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5359 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5361 /* If storing in a field that is in actuality a short or narrower than one,
5362 we must store in the field in its actual type. */
5364 if (lhstype != TREE_TYPE (lhs))
5366 lhs = copy_node (lhs);
5367 TREE_TYPE (lhs) = lhstype;
5370 /* Issue -Wc++-compat warnings about an assignment to an enum type
5371 when LHS does not have its original type. This happens for,
5372 e.g., an enum bitfield in a struct. */
5373 if (warn_cxx_compat
5374 && lhs_origtype != NULL_TREE
5375 && lhs_origtype != lhstype
5376 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5378 tree checktype = (rhs_origtype != NULL_TREE
5379 ? rhs_origtype
5380 : TREE_TYPE (rhs));
5381 if (checktype != error_mark_node
5382 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5383 || (is_atomic_op && modifycode != NOP_EXPR)))
5384 warning_at (location, OPT_Wc___compat,
5385 "enum conversion in assignment is invalid in C++");
5388 /* If the lhs is atomic, remove that qualifier. */
5389 if (is_atomic_op)
5391 lhstype = build_qualified_type (lhstype,
5392 (TYPE_QUALS (lhstype)
5393 & ~TYPE_QUAL_ATOMIC));
5394 olhstype = build_qualified_type (olhstype,
5395 (TYPE_QUALS (lhstype)
5396 & ~TYPE_QUAL_ATOMIC));
5399 /* Convert new value to destination type. Fold it first, then
5400 restore any excess precision information, for the sake of
5401 conversion warnings. */
5403 if (!(is_atomic_op && modifycode != NOP_EXPR))
5405 npc = null_pointer_constant_p (newrhs);
5406 newrhs = c_fully_fold (newrhs, false, NULL);
5407 if (rhs_semantic_type)
5408 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5409 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5410 rhs_origtype, ic_assign, npc,
5411 NULL_TREE, NULL_TREE, 0);
5412 if (TREE_CODE (newrhs) == ERROR_MARK)
5413 return error_mark_node;
5416 /* Emit ObjC write barrier, if necessary. */
5417 if (c_dialect_objc () && flag_objc_gc)
5419 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5420 if (result)
5422 protected_set_expr_location (result, location);
5423 goto return_result;
5427 /* Scan operands. */
5429 if (is_atomic_op)
5430 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5431 else
5433 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5434 TREE_SIDE_EFFECTS (result) = 1;
5435 protected_set_expr_location (result, location);
5438 /* If we got the LHS in a different type for storing in,
5439 convert the result back to the nominal type of LHS
5440 so that the value we return always has the same type
5441 as the LHS argument. */
5443 if (olhstype == TREE_TYPE (result))
5444 goto return_result;
5446 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5447 rhs_origtype, ic_assign, false, NULL_TREE,
5448 NULL_TREE, 0);
5449 protected_set_expr_location (result, location);
5451 return_result:
5452 if (rhseval)
5453 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5454 return result;
5457 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5458 This is used to implement -fplan9-extensions. */
5460 static bool
5461 find_anonymous_field_with_type (tree struct_type, tree type)
5463 tree field;
5464 bool found;
5466 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5467 || TREE_CODE (struct_type) == UNION_TYPE);
5468 found = false;
5469 for (field = TYPE_FIELDS (struct_type);
5470 field != NULL_TREE;
5471 field = TREE_CHAIN (field))
5473 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5474 ? c_build_qualified_type (TREE_TYPE (field),
5475 TYPE_QUAL_ATOMIC)
5476 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5477 if (DECL_NAME (field) == NULL
5478 && comptypes (type, fieldtype))
5480 if (found)
5481 return false;
5482 found = true;
5484 else if (DECL_NAME (field) == NULL
5485 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5486 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5487 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5489 if (found)
5490 return false;
5491 found = true;
5494 return found;
5497 /* RHS is an expression whose type is pointer to struct. If there is
5498 an anonymous field in RHS with type TYPE, then return a pointer to
5499 that field in RHS. This is used with -fplan9-extensions. This
5500 returns NULL if no conversion could be found. */
5502 static tree
5503 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5505 tree rhs_struct_type, lhs_main_type;
5506 tree field, found_field;
5507 bool found_sub_field;
5508 tree ret;
5510 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5511 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5512 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5513 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5515 gcc_assert (POINTER_TYPE_P (type));
5516 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5517 ? c_build_qualified_type (TREE_TYPE (type),
5518 TYPE_QUAL_ATOMIC)
5519 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5521 found_field = NULL_TREE;
5522 found_sub_field = false;
5523 for (field = TYPE_FIELDS (rhs_struct_type);
5524 field != NULL_TREE;
5525 field = TREE_CHAIN (field))
5527 if (DECL_NAME (field) != NULL_TREE
5528 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5529 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5530 continue;
5531 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5532 ? c_build_qualified_type (TREE_TYPE (field),
5533 TYPE_QUAL_ATOMIC)
5534 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5535 if (comptypes (lhs_main_type, fieldtype))
5537 if (found_field != NULL_TREE)
5538 return NULL_TREE;
5539 found_field = field;
5541 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5542 lhs_main_type))
5544 if (found_field != NULL_TREE)
5545 return NULL_TREE;
5546 found_field = field;
5547 found_sub_field = true;
5551 if (found_field == NULL_TREE)
5552 return NULL_TREE;
5554 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5555 build_fold_indirect_ref (rhs), found_field,
5556 NULL_TREE);
5557 ret = build_fold_addr_expr_loc (location, ret);
5559 if (found_sub_field)
5561 ret = convert_to_anonymous_field (location, type, ret);
5562 gcc_assert (ret != NULL_TREE);
5565 return ret;
5568 /* Issue an error message for a bad initializer component.
5569 GMSGID identifies the message.
5570 The component name is taken from the spelling stack. */
5572 static void
5573 error_init (location_t loc, const char *gmsgid)
5575 char *ofwhat;
5577 /* The gmsgid may be a format string with %< and %>. */
5578 error_at (loc, gmsgid);
5579 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5580 if (*ofwhat)
5581 inform (loc, "(near initialization for %qs)", ofwhat);
5584 /* Issue a pedantic warning for a bad initializer component. OPT is
5585 the option OPT_* (from options.h) controlling this warning or 0 if
5586 it is unconditionally given. GMSGID identifies the message. The
5587 component name is taken from the spelling stack. */
5589 static void
5590 pedwarn_init (location_t location, int opt, const char *gmsgid)
5592 char *ofwhat;
5593 bool warned;
5595 /* The gmsgid may be a format string with %< and %>. */
5596 warned = pedwarn (location, opt, gmsgid);
5597 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5598 if (*ofwhat && warned)
5599 inform (location, "(near initialization for %qs)", ofwhat);
5602 /* Issue a warning for a bad initializer component.
5604 OPT is the OPT_W* value corresponding to the warning option that
5605 controls this warning. GMSGID identifies the message. The
5606 component name is taken from the spelling stack. */
5608 static void
5609 warning_init (location_t loc, int opt, const char *gmsgid)
5611 char *ofwhat;
5612 bool warned;
5614 /* The gmsgid may be a format string with %< and %>. */
5615 warned = warning_at (loc, opt, gmsgid);
5616 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5617 if (*ofwhat && warned)
5618 inform (loc, "(near initialization for %qs)", ofwhat);
5621 /* If TYPE is an array type and EXPR is a parenthesized string
5622 constant, warn if pedantic that EXPR is being used to initialize an
5623 object of type TYPE. */
5625 void
5626 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5628 if (pedantic
5629 && TREE_CODE (type) == ARRAY_TYPE
5630 && TREE_CODE (expr.value) == STRING_CST
5631 && expr.original_code != STRING_CST)
5632 pedwarn_init (loc, OPT_Wpedantic,
5633 "array initialized from parenthesized string constant");
5636 /* Convert value RHS to type TYPE as preparation for an assignment to
5637 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5638 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5639 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5640 constant before any folding.
5641 The real work of conversion is done by `convert'.
5642 The purpose of this function is to generate error messages
5643 for assignments that are not allowed in C.
5644 ERRTYPE says whether it is argument passing, assignment,
5645 initialization or return.
5647 LOCATION is the location of the assignment, EXPR_LOC is the location of
5648 the RHS or, for a function, location of an argument.
5649 FUNCTION is a tree for the function being called.
5650 PARMNUM is the number of the argument, for printing in error messages. */
5652 static tree
5653 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5654 tree rhs, tree origtype, enum impl_conv errtype,
5655 bool null_pointer_constant, tree fundecl,
5656 tree function, int parmnum)
5658 enum tree_code codel = TREE_CODE (type);
5659 tree orig_rhs = rhs;
5660 tree rhstype;
5661 enum tree_code coder;
5662 tree rname = NULL_TREE;
5663 bool objc_ok = false;
5665 if (errtype == ic_argpass)
5667 tree selector;
5668 /* Change pointer to function to the function itself for
5669 diagnostics. */
5670 if (TREE_CODE (function) == ADDR_EXPR
5671 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5672 function = TREE_OPERAND (function, 0);
5674 /* Handle an ObjC selector specially for diagnostics. */
5675 selector = objc_message_selector ();
5676 rname = function;
5677 if (selector && parmnum > 2)
5679 rname = selector;
5680 parmnum -= 2;
5684 /* This macro is used to emit diagnostics to ensure that all format
5685 strings are complete sentences, visible to gettext and checked at
5686 compile time. */
5687 #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5688 do { \
5689 switch (errtype) \
5691 case ic_argpass: \
5692 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5693 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5694 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5695 "expected %qT but argument is of type %qT", \
5696 type, rhstype); \
5697 break; \
5698 case ic_assign: \
5699 pedwarn (LOCATION, OPT, AS); \
5700 break; \
5701 case ic_init: \
5702 pedwarn_init (LOCATION, OPT, IN); \
5703 break; \
5704 case ic_return: \
5705 pedwarn (LOCATION, OPT, RE); \
5706 break; \
5707 default: \
5708 gcc_unreachable (); \
5710 } while (0)
5712 /* This macro is used to emit diagnostics to ensure that all format
5713 strings are complete sentences, visible to gettext and checked at
5714 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5715 extra parameter to enumerate qualifiers. */
5717 #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5718 do { \
5719 switch (errtype) \
5721 case ic_argpass: \
5722 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5723 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5724 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5725 "expected %qT but argument is of type %qT", \
5726 type, rhstype); \
5727 break; \
5728 case ic_assign: \
5729 pedwarn (LOCATION, OPT, AS, QUALS); \
5730 break; \
5731 case ic_init: \
5732 pedwarn (LOCATION, OPT, IN, QUALS); \
5733 break; \
5734 case ic_return: \
5735 pedwarn (LOCATION, OPT, RE, QUALS); \
5736 break; \
5737 default: \
5738 gcc_unreachable (); \
5740 } while (0)
5742 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5743 rhs = TREE_OPERAND (rhs, 0);
5745 rhstype = TREE_TYPE (rhs);
5746 coder = TREE_CODE (rhstype);
5748 if (coder == ERROR_MARK)
5749 return error_mark_node;
5751 if (c_dialect_objc ())
5753 int parmno;
5755 switch (errtype)
5757 case ic_return:
5758 parmno = 0;
5759 break;
5761 case ic_assign:
5762 parmno = -1;
5763 break;
5765 case ic_init:
5766 parmno = -2;
5767 break;
5769 default:
5770 parmno = parmnum;
5771 break;
5774 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5777 if (warn_cxx_compat)
5779 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5780 if (checktype != error_mark_node
5781 && TREE_CODE (type) == ENUMERAL_TYPE
5782 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5784 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5785 G_("enum conversion when passing argument "
5786 "%d of %qE is invalid in C++"),
5787 G_("enum conversion in assignment is "
5788 "invalid in C++"),
5789 G_("enum conversion in initialization is "
5790 "invalid in C++"),
5791 G_("enum conversion in return is "
5792 "invalid in C++"));
5796 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5797 return rhs;
5799 if (coder == VOID_TYPE)
5801 /* Except for passing an argument to an unprototyped function,
5802 this is a constraint violation. When passing an argument to
5803 an unprototyped function, it is compile-time undefined;
5804 making it a constraint in that case was rejected in
5805 DR#252. */
5806 error_at (location, "void value not ignored as it ought to be");
5807 return error_mark_node;
5809 rhs = require_complete_type (rhs);
5810 if (rhs == error_mark_node)
5811 return error_mark_node;
5812 /* A non-reference type can convert to a reference. This handles
5813 va_start, va_copy and possibly port built-ins. */
5814 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5816 if (!lvalue_p (rhs))
5818 error_at (location, "cannot pass rvalue to reference parameter");
5819 return error_mark_node;
5821 if (!c_mark_addressable (rhs))
5822 return error_mark_node;
5823 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5824 SET_EXPR_LOCATION (rhs, location);
5826 rhs = convert_for_assignment (location, expr_loc,
5827 build_pointer_type (TREE_TYPE (type)),
5828 rhs, origtype, errtype,
5829 null_pointer_constant, fundecl, function,
5830 parmnum);
5831 if (rhs == error_mark_node)
5832 return error_mark_node;
5834 rhs = build1 (NOP_EXPR, type, rhs);
5835 SET_EXPR_LOCATION (rhs, location);
5836 return rhs;
5838 /* Some types can interconvert without explicit casts. */
5839 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5840 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5841 return convert (type, rhs);
5842 /* Arithmetic types all interconvert, and enum is treated like int. */
5843 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5844 || codel == FIXED_POINT_TYPE
5845 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5846 || codel == BOOLEAN_TYPE)
5847 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5848 || coder == FIXED_POINT_TYPE
5849 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5850 || coder == BOOLEAN_TYPE))
5852 tree ret;
5853 bool save = in_late_binary_op;
5854 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5855 in_late_binary_op = true;
5856 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5857 ? expr_loc : location, type, orig_rhs);
5858 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5859 in_late_binary_op = save;
5860 return ret;
5863 /* Aggregates in different TUs might need conversion. */
5864 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5865 && codel == coder
5866 && comptypes (type, rhstype))
5867 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5868 ? expr_loc : location, type, rhs);
5870 /* Conversion to a transparent union or record from its member types.
5871 This applies only to function arguments. */
5872 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5873 && TYPE_TRANSPARENT_AGGR (type))
5874 && errtype == ic_argpass)
5876 tree memb, marginal_memb = NULL_TREE;
5878 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5880 tree memb_type = TREE_TYPE (memb);
5882 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5883 TYPE_MAIN_VARIANT (rhstype)))
5884 break;
5886 if (TREE_CODE (memb_type) != POINTER_TYPE)
5887 continue;
5889 if (coder == POINTER_TYPE)
5891 tree ttl = TREE_TYPE (memb_type);
5892 tree ttr = TREE_TYPE (rhstype);
5894 /* Any non-function converts to a [const][volatile] void *
5895 and vice versa; otherwise, targets must be the same.
5896 Meanwhile, the lhs target must have all the qualifiers of
5897 the rhs. */
5898 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5899 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5900 || comp_target_types (location, memb_type, rhstype))
5902 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5903 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5904 /* If this type won't generate any warnings, use it. */
5905 if (lquals == rquals
5906 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5907 && TREE_CODE (ttl) == FUNCTION_TYPE)
5908 ? ((lquals | rquals) == rquals)
5909 : ((lquals | rquals) == lquals)))
5910 break;
5912 /* Keep looking for a better type, but remember this one. */
5913 if (!marginal_memb)
5914 marginal_memb = memb;
5918 /* Can convert integer zero to any pointer type. */
5919 if (null_pointer_constant)
5921 rhs = null_pointer_node;
5922 break;
5926 if (memb || marginal_memb)
5928 if (!memb)
5930 /* We have only a marginally acceptable member type;
5931 it needs a warning. */
5932 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5933 tree ttr = TREE_TYPE (rhstype);
5935 /* Const and volatile mean something different for function
5936 types, so the usual warnings are not appropriate. */
5937 if (TREE_CODE (ttr) == FUNCTION_TYPE
5938 && TREE_CODE (ttl) == FUNCTION_TYPE)
5940 /* Because const and volatile on functions are
5941 restrictions that say the function will not do
5942 certain things, it is okay to use a const or volatile
5943 function where an ordinary one is wanted, but not
5944 vice-versa. */
5945 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5946 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5947 WARN_FOR_QUALIFIERS (location, expr_loc,
5948 OPT_Wdiscarded_qualifiers,
5949 G_("passing argument %d of %qE "
5950 "makes %q#v qualified function "
5951 "pointer from unqualified"),
5952 G_("assignment makes %q#v qualified "
5953 "function pointer from "
5954 "unqualified"),
5955 G_("initialization makes %q#v qualified "
5956 "function pointer from "
5957 "unqualified"),
5958 G_("return makes %q#v qualified function "
5959 "pointer from unqualified"),
5960 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5962 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5963 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5964 WARN_FOR_QUALIFIERS (location, expr_loc,
5965 OPT_Wdiscarded_qualifiers,
5966 G_("passing argument %d of %qE discards "
5967 "%qv qualifier from pointer target type"),
5968 G_("assignment discards %qv qualifier "
5969 "from pointer target type"),
5970 G_("initialization discards %qv qualifier "
5971 "from pointer target type"),
5972 G_("return discards %qv qualifier from "
5973 "pointer target type"),
5974 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5976 memb = marginal_memb;
5979 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5980 pedwarn (location, OPT_Wpedantic,
5981 "ISO C prohibits argument conversion to union type");
5983 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5984 return build_constructor_single (type, memb, rhs);
5988 /* Conversions among pointers */
5989 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5990 && (coder == codel))
5992 tree ttl = TREE_TYPE (type);
5993 tree ttr = TREE_TYPE (rhstype);
5994 tree mvl = ttl;
5995 tree mvr = ttr;
5996 bool is_opaque_pointer;
5997 int target_cmp = 0; /* Cache comp_target_types () result. */
5998 addr_space_t asl;
5999 addr_space_t asr;
6001 if (TREE_CODE (mvl) != ARRAY_TYPE)
6002 mvl = (TYPE_ATOMIC (mvl)
6003 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6004 TYPE_QUAL_ATOMIC)
6005 : TYPE_MAIN_VARIANT (mvl));
6006 if (TREE_CODE (mvr) != ARRAY_TYPE)
6007 mvr = (TYPE_ATOMIC (mvr)
6008 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6009 TYPE_QUAL_ATOMIC)
6010 : TYPE_MAIN_VARIANT (mvr));
6011 /* Opaque pointers are treated like void pointers. */
6012 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6014 /* The Plan 9 compiler permits a pointer to a struct to be
6015 automatically converted into a pointer to an anonymous field
6016 within the struct. */
6017 if (flag_plan9_extensions
6018 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6019 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6020 && mvl != mvr)
6022 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6023 if (new_rhs != NULL_TREE)
6025 rhs = new_rhs;
6026 rhstype = TREE_TYPE (rhs);
6027 coder = TREE_CODE (rhstype);
6028 ttr = TREE_TYPE (rhstype);
6029 mvr = TYPE_MAIN_VARIANT (ttr);
6033 /* C++ does not allow the implicit conversion void* -> T*. However,
6034 for the purpose of reducing the number of false positives, we
6035 tolerate the special case of
6037 int *p = NULL;
6039 where NULL is typically defined in C to be '(void *) 0'. */
6040 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6041 warning_at (errtype == ic_argpass ? expr_loc : location,
6042 OPT_Wc___compat,
6043 "request for implicit conversion "
6044 "from %qT to %qT not permitted in C++", rhstype, type);
6046 /* See if the pointers point to incompatible address spaces. */
6047 asl = TYPE_ADDR_SPACE (ttl);
6048 asr = TYPE_ADDR_SPACE (ttr);
6049 if (!null_pointer_constant_p (rhs)
6050 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6052 switch (errtype)
6054 case ic_argpass:
6055 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6056 "non-enclosed address space", parmnum, rname);
6057 break;
6058 case ic_assign:
6059 error_at (location, "assignment from pointer to "
6060 "non-enclosed address space");
6061 break;
6062 case ic_init:
6063 error_at (location, "initialization from pointer to "
6064 "non-enclosed address space");
6065 break;
6066 case ic_return:
6067 error_at (location, "return from pointer to "
6068 "non-enclosed address space");
6069 break;
6070 default:
6071 gcc_unreachable ();
6073 return error_mark_node;
6076 /* Check if the right-hand side has a format attribute but the
6077 left-hand side doesn't. */
6078 if (warn_suggest_attribute_format
6079 && check_missing_format_attribute (type, rhstype))
6081 switch (errtype)
6083 case ic_argpass:
6084 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6085 "argument %d of %qE might be "
6086 "a candidate for a format attribute",
6087 parmnum, rname);
6088 break;
6089 case ic_assign:
6090 warning_at (location, OPT_Wsuggest_attribute_format,
6091 "assignment left-hand side might be "
6092 "a candidate for a format attribute");
6093 break;
6094 case ic_init:
6095 warning_at (location, OPT_Wsuggest_attribute_format,
6096 "initialization left-hand side might be "
6097 "a candidate for a format attribute");
6098 break;
6099 case ic_return:
6100 warning_at (location, OPT_Wsuggest_attribute_format,
6101 "return type might be "
6102 "a candidate for a format attribute");
6103 break;
6104 default:
6105 gcc_unreachable ();
6109 /* Any non-function converts to a [const][volatile] void *
6110 and vice versa; otherwise, targets must be the same.
6111 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6112 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6113 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6114 || (target_cmp = comp_target_types (location, type, rhstype))
6115 || is_opaque_pointer
6116 || ((c_common_unsigned_type (mvl)
6117 == c_common_unsigned_type (mvr))
6118 && (c_common_signed_type (mvl)
6119 == c_common_signed_type (mvr))
6120 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6122 if (pedantic
6123 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6125 (VOID_TYPE_P (ttr)
6126 && !null_pointer_constant
6127 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6128 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6129 G_("ISO C forbids passing argument %d of "
6130 "%qE between function pointer "
6131 "and %<void *%>"),
6132 G_("ISO C forbids assignment between "
6133 "function pointer and %<void *%>"),
6134 G_("ISO C forbids initialization between "
6135 "function pointer and %<void *%>"),
6136 G_("ISO C forbids return between function "
6137 "pointer and %<void *%>"));
6138 /* Const and volatile mean something different for function types,
6139 so the usual warnings are not appropriate. */
6140 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6141 && TREE_CODE (ttl) != FUNCTION_TYPE)
6143 /* Assignments between atomic and non-atomic objects are OK. */
6144 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6145 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6147 WARN_FOR_QUALIFIERS (location, expr_loc,
6148 OPT_Wdiscarded_qualifiers,
6149 G_("passing argument %d of %qE discards "
6150 "%qv qualifier from pointer target type"),
6151 G_("assignment discards %qv qualifier "
6152 "from pointer target type"),
6153 G_("initialization discards %qv qualifier "
6154 "from pointer target type"),
6155 G_("return discards %qv qualifier from "
6156 "pointer target type"),
6157 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6159 /* If this is not a case of ignoring a mismatch in signedness,
6160 no warning. */
6161 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6162 || target_cmp)
6164 /* If there is a mismatch, do warn. */
6165 else if (warn_pointer_sign)
6166 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6167 G_("pointer targets in passing argument "
6168 "%d of %qE differ in signedness"),
6169 G_("pointer targets in assignment "
6170 "differ in signedness"),
6171 G_("pointer targets in initialization "
6172 "differ in signedness"),
6173 G_("pointer targets in return differ "
6174 "in signedness"));
6176 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6177 && TREE_CODE (ttr) == FUNCTION_TYPE)
6179 /* Because const and volatile on functions are restrictions
6180 that say the function will not do certain things,
6181 it is okay to use a const or volatile function
6182 where an ordinary one is wanted, but not vice-versa. */
6183 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6184 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6185 WARN_FOR_QUALIFIERS (location, expr_loc,
6186 OPT_Wdiscarded_qualifiers,
6187 G_("passing argument %d of %qE makes "
6188 "%q#v qualified function pointer "
6189 "from unqualified"),
6190 G_("assignment makes %q#v qualified function "
6191 "pointer from unqualified"),
6192 G_("initialization makes %q#v qualified "
6193 "function pointer from unqualified"),
6194 G_("return makes %q#v qualified function "
6195 "pointer from unqualified"),
6196 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6199 else
6200 /* Avoid warning about the volatile ObjC EH puts on decls. */
6201 if (!objc_ok)
6202 WARN_FOR_ASSIGNMENT (location, expr_loc,
6203 OPT_Wincompatible_pointer_types,
6204 G_("passing argument %d of %qE from "
6205 "incompatible pointer type"),
6206 G_("assignment from incompatible pointer type"),
6207 G_("initialization from incompatible "
6208 "pointer type"),
6209 G_("return from incompatible pointer type"));
6211 return convert (type, rhs);
6213 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6215 /* ??? This should not be an error when inlining calls to
6216 unprototyped functions. */
6217 error_at (location, "invalid use of non-lvalue array");
6218 return error_mark_node;
6220 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6222 /* An explicit constant 0 can convert to a pointer,
6223 or one that results from arithmetic, even including
6224 a cast to integer type. */
6225 if (!null_pointer_constant)
6226 WARN_FOR_ASSIGNMENT (location, expr_loc,
6227 OPT_Wint_conversion,
6228 G_("passing argument %d of %qE makes "
6229 "pointer from integer without a cast"),
6230 G_("assignment makes pointer from integer "
6231 "without a cast"),
6232 G_("initialization makes pointer from "
6233 "integer without a cast"),
6234 G_("return makes pointer from integer "
6235 "without a cast"));
6237 return convert (type, rhs);
6239 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6241 WARN_FOR_ASSIGNMENT (location, expr_loc,
6242 OPT_Wint_conversion,
6243 G_("passing argument %d of %qE makes integer "
6244 "from pointer without a cast"),
6245 G_("assignment makes integer from pointer "
6246 "without a cast"),
6247 G_("initialization makes integer from pointer "
6248 "without a cast"),
6249 G_("return makes integer from pointer "
6250 "without a cast"));
6251 return convert (type, rhs);
6253 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6255 tree ret;
6256 bool save = in_late_binary_op;
6257 in_late_binary_op = true;
6258 ret = convert (type, rhs);
6259 in_late_binary_op = save;
6260 return ret;
6263 switch (errtype)
6265 case ic_argpass:
6266 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6267 rname);
6268 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6269 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6270 "expected %qT but argument is of type %qT", type, rhstype);
6271 break;
6272 case ic_assign:
6273 error_at (location, "incompatible types when assigning to type %qT from "
6274 "type %qT", type, rhstype);
6275 break;
6276 case ic_init:
6277 error_at (location,
6278 "incompatible types when initializing type %qT using type %qT",
6279 type, rhstype);
6280 break;
6281 case ic_return:
6282 error_at (location,
6283 "incompatible types when returning type %qT but %qT was "
6284 "expected", rhstype, type);
6285 break;
6286 default:
6287 gcc_unreachable ();
6290 return error_mark_node;
6293 /* If VALUE is a compound expr all of whose expressions are constant, then
6294 return its value. Otherwise, return error_mark_node.
6296 This is for handling COMPOUND_EXPRs as initializer elements
6297 which is allowed with a warning when -pedantic is specified. */
6299 static tree
6300 valid_compound_expr_initializer (tree value, tree endtype)
6302 if (TREE_CODE (value) == COMPOUND_EXPR)
6304 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6305 == error_mark_node)
6306 return error_mark_node;
6307 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6308 endtype);
6310 else if (!initializer_constant_valid_p (value, endtype))
6311 return error_mark_node;
6312 else
6313 return value;
6316 /* Perform appropriate conversions on the initial value of a variable,
6317 store it in the declaration DECL,
6318 and print any error messages that are appropriate.
6319 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6320 If the init is invalid, store an ERROR_MARK.
6322 INIT_LOC is the location of the initial value. */
6324 void
6325 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6327 tree value, type;
6328 bool npc = false;
6330 /* If variable's type was invalidly declared, just ignore it. */
6332 type = TREE_TYPE (decl);
6333 if (TREE_CODE (type) == ERROR_MARK)
6334 return;
6336 /* Digest the specified initializer into an expression. */
6338 if (init)
6339 npc = null_pointer_constant_p (init);
6340 value = digest_init (init_loc, type, init, origtype, npc,
6341 true, TREE_STATIC (decl));
6343 /* Store the expression if valid; else report error. */
6345 if (!in_system_header_at (input_location)
6346 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6347 warning (OPT_Wtraditional, "traditional C rejects automatic "
6348 "aggregate initialization");
6350 DECL_INITIAL (decl) = value;
6352 /* ANSI wants warnings about out-of-range constant initializers. */
6353 STRIP_TYPE_NOPS (value);
6354 if (TREE_STATIC (decl))
6355 constant_expression_warning (value);
6357 /* Check if we need to set array size from compound literal size. */
6358 if (TREE_CODE (type) == ARRAY_TYPE
6359 && TYPE_DOMAIN (type) == 0
6360 && value != error_mark_node)
6362 tree inside_init = init;
6364 STRIP_TYPE_NOPS (inside_init);
6365 inside_init = fold (inside_init);
6367 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6369 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6371 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6373 /* For int foo[] = (int [3]){1}; we need to set array size
6374 now since later on array initializer will be just the
6375 brace enclosed list of the compound literal. */
6376 tree etype = strip_array_types (TREE_TYPE (decl));
6377 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6378 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6379 layout_type (type);
6380 layout_decl (cldecl, 0);
6381 TREE_TYPE (decl)
6382 = c_build_qualified_type (type, TYPE_QUALS (etype));
6388 /* Methods for storing and printing names for error messages. */
6390 /* Implement a spelling stack that allows components of a name to be pushed
6391 and popped. Each element on the stack is this structure. */
6393 struct spelling
6395 int kind;
6396 union
6398 unsigned HOST_WIDE_INT i;
6399 const char *s;
6400 } u;
6403 #define SPELLING_STRING 1
6404 #define SPELLING_MEMBER 2
6405 #define SPELLING_BOUNDS 3
6407 static struct spelling *spelling; /* Next stack element (unused). */
6408 static struct spelling *spelling_base; /* Spelling stack base. */
6409 static int spelling_size; /* Size of the spelling stack. */
6411 /* Macros to save and restore the spelling stack around push_... functions.
6412 Alternative to SAVE_SPELLING_STACK. */
6414 #define SPELLING_DEPTH() (spelling - spelling_base)
6415 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6417 /* Push an element on the spelling stack with type KIND and assign VALUE
6418 to MEMBER. */
6420 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6422 int depth = SPELLING_DEPTH (); \
6424 if (depth >= spelling_size) \
6426 spelling_size += 10; \
6427 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6428 spelling_size); \
6429 RESTORE_SPELLING_DEPTH (depth); \
6432 spelling->kind = (KIND); \
6433 spelling->MEMBER = (VALUE); \
6434 spelling++; \
6437 /* Push STRING on the stack. Printed literally. */
6439 static void
6440 push_string (const char *string)
6442 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6445 /* Push a member name on the stack. Printed as '.' STRING. */
6447 static void
6448 push_member_name (tree decl)
6450 const char *const string
6451 = (DECL_NAME (decl)
6452 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6453 : _("<anonymous>"));
6454 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6457 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6459 static void
6460 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6462 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6465 /* Compute the maximum size in bytes of the printed spelling. */
6467 static int
6468 spelling_length (void)
6470 int size = 0;
6471 struct spelling *p;
6473 for (p = spelling_base; p < spelling; p++)
6475 if (p->kind == SPELLING_BOUNDS)
6476 size += 25;
6477 else
6478 size += strlen (p->u.s) + 1;
6481 return size;
6484 /* Print the spelling to BUFFER and return it. */
6486 static char *
6487 print_spelling (char *buffer)
6489 char *d = buffer;
6490 struct spelling *p;
6492 for (p = spelling_base; p < spelling; p++)
6493 if (p->kind == SPELLING_BOUNDS)
6495 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6496 d += strlen (d);
6498 else
6500 const char *s;
6501 if (p->kind == SPELLING_MEMBER)
6502 *d++ = '.';
6503 for (s = p->u.s; (*d = *s++); d++)
6506 *d++ = '\0';
6507 return buffer;
6510 /* Digest the parser output INIT as an initializer for type TYPE.
6511 Return a C expression of type TYPE to represent the initial value.
6513 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6515 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6517 If INIT is a string constant, STRICT_STRING is true if it is
6518 unparenthesized or we should not warn here for it being parenthesized.
6519 For other types of INIT, STRICT_STRING is not used.
6521 INIT_LOC is the location of the INIT.
6523 REQUIRE_CONSTANT requests an error if non-constant initializers or
6524 elements are seen. */
6526 static tree
6527 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6528 bool null_pointer_constant, bool strict_string,
6529 int require_constant)
6531 enum tree_code code = TREE_CODE (type);
6532 tree inside_init = init;
6533 tree semantic_type = NULL_TREE;
6534 bool maybe_const = true;
6536 if (type == error_mark_node
6537 || !init
6538 || init == error_mark_node
6539 || TREE_TYPE (init) == error_mark_node)
6540 return error_mark_node;
6542 STRIP_TYPE_NOPS (inside_init);
6544 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6546 semantic_type = TREE_TYPE (inside_init);
6547 inside_init = TREE_OPERAND (inside_init, 0);
6549 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6550 inside_init = decl_constant_value_for_optimization (inside_init);
6552 /* Initialization of an array of chars from a string constant
6553 optionally enclosed in braces. */
6555 if (code == ARRAY_TYPE && inside_init
6556 && TREE_CODE (inside_init) == STRING_CST)
6558 tree typ1
6559 = (TYPE_ATOMIC (TREE_TYPE (type))
6560 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6561 TYPE_QUAL_ATOMIC)
6562 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6563 /* Note that an array could be both an array of character type
6564 and an array of wchar_t if wchar_t is signed char or unsigned
6565 char. */
6566 bool char_array = (typ1 == char_type_node
6567 || typ1 == signed_char_type_node
6568 || typ1 == unsigned_char_type_node);
6569 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6570 bool char16_array = !!comptypes (typ1, char16_type_node);
6571 bool char32_array = !!comptypes (typ1, char32_type_node);
6573 if (char_array || wchar_array || char16_array || char32_array)
6575 struct c_expr expr;
6576 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6577 expr.value = inside_init;
6578 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6579 expr.original_type = NULL;
6580 maybe_warn_string_init (init_loc, type, expr);
6582 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6583 pedwarn_init (init_loc, OPT_Wpedantic,
6584 "initialization of a flexible array member");
6586 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6587 TYPE_MAIN_VARIANT (type)))
6588 return inside_init;
6590 if (char_array)
6592 if (typ2 != char_type_node)
6594 error_init (init_loc, "char-array initialized from wide "
6595 "string");
6596 return error_mark_node;
6599 else
6601 if (typ2 == char_type_node)
6603 error_init (init_loc, "wide character array initialized "
6604 "from non-wide string");
6605 return error_mark_node;
6607 else if (!comptypes(typ1, typ2))
6609 error_init (init_loc, "wide character array initialized "
6610 "from incompatible wide string");
6611 return error_mark_node;
6615 TREE_TYPE (inside_init) = type;
6616 if (TYPE_DOMAIN (type) != 0
6617 && TYPE_SIZE (type) != 0
6618 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6620 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6622 /* Subtract the size of a single (possibly wide) character
6623 because it's ok to ignore the terminating null char
6624 that is counted in the length of the constant. */
6625 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6626 (len
6627 - (TYPE_PRECISION (typ1)
6628 / BITS_PER_UNIT))))
6629 pedwarn_init (init_loc, 0,
6630 ("initializer-string for array of chars "
6631 "is too long"));
6632 else if (warn_cxx_compat
6633 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6634 warning_at (init_loc, OPT_Wc___compat,
6635 ("initializer-string for array chars "
6636 "is too long for C++"));
6639 return inside_init;
6641 else if (INTEGRAL_TYPE_P (typ1))
6643 error_init (init_loc, "array of inappropriate type initialized "
6644 "from string constant");
6645 return error_mark_node;
6649 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6650 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6651 below and handle as a constructor. */
6652 if (code == VECTOR_TYPE
6653 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6654 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6655 && TREE_CONSTANT (inside_init))
6657 if (TREE_CODE (inside_init) == VECTOR_CST
6658 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6659 TYPE_MAIN_VARIANT (type)))
6660 return inside_init;
6662 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6664 unsigned HOST_WIDE_INT ix;
6665 tree value;
6666 bool constant_p = true;
6668 /* Iterate through elements and check if all constructor
6669 elements are *_CSTs. */
6670 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6671 if (!CONSTANT_CLASS_P (value))
6673 constant_p = false;
6674 break;
6677 if (constant_p)
6678 return build_vector_from_ctor (type,
6679 CONSTRUCTOR_ELTS (inside_init));
6683 if (warn_sequence_point)
6684 verify_sequence_points (inside_init);
6686 /* Any type can be initialized
6687 from an expression of the same type, optionally with braces. */
6689 if (inside_init && TREE_TYPE (inside_init) != 0
6690 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6691 TYPE_MAIN_VARIANT (type))
6692 || (code == ARRAY_TYPE
6693 && comptypes (TREE_TYPE (inside_init), type))
6694 || (code == VECTOR_TYPE
6695 && comptypes (TREE_TYPE (inside_init), type))
6696 || (code == POINTER_TYPE
6697 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6698 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6699 TREE_TYPE (type)))))
6701 if (code == POINTER_TYPE)
6703 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6705 if (TREE_CODE (inside_init) == STRING_CST
6706 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6707 inside_init = array_to_pointer_conversion
6708 (init_loc, inside_init);
6709 else
6711 error_init (init_loc, "invalid use of non-lvalue array");
6712 return error_mark_node;
6717 if (code == VECTOR_TYPE)
6718 /* Although the types are compatible, we may require a
6719 conversion. */
6720 inside_init = convert (type, inside_init);
6722 if (require_constant
6723 && (code == VECTOR_TYPE || !flag_isoc99)
6724 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6726 /* As an extension, allow initializing objects with static storage
6727 duration with compound literals (which are then treated just as
6728 the brace enclosed list they contain). Also allow this for
6729 vectors, as we can only assign them with compound literals. */
6730 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6731 inside_init = DECL_INITIAL (decl);
6734 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6735 && TREE_CODE (inside_init) != CONSTRUCTOR)
6737 error_init (init_loc, "array initialized from non-constant array "
6738 "expression");
6739 return error_mark_node;
6742 /* Compound expressions can only occur here if -Wpedantic or
6743 -pedantic-errors is specified. In the later case, we always want
6744 an error. In the former case, we simply want a warning. */
6745 if (require_constant && pedantic
6746 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6748 inside_init
6749 = valid_compound_expr_initializer (inside_init,
6750 TREE_TYPE (inside_init));
6751 if (inside_init == error_mark_node)
6752 error_init (init_loc, "initializer element is not constant");
6753 else
6754 pedwarn_init (init_loc, OPT_Wpedantic,
6755 "initializer element is not constant");
6756 if (flag_pedantic_errors)
6757 inside_init = error_mark_node;
6759 else if (require_constant
6760 && !initializer_constant_valid_p (inside_init,
6761 TREE_TYPE (inside_init)))
6763 error_init (init_loc, "initializer element is not constant");
6764 inside_init = error_mark_node;
6766 else if (require_constant && !maybe_const)
6767 pedwarn_init (init_loc, 0,
6768 "initializer element is not a constant expression");
6770 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6771 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6772 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6773 type, inside_init, origtype,
6774 ic_init, null_pointer_constant,
6775 NULL_TREE, NULL_TREE, 0);
6776 return inside_init;
6779 /* Handle scalar types, including conversions. */
6781 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6782 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6783 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6785 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6786 && (TREE_CODE (init) == STRING_CST
6787 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6788 inside_init = init = array_to_pointer_conversion (init_loc, init);
6789 if (semantic_type)
6790 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6791 inside_init);
6792 inside_init
6793 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6794 inside_init, origtype, ic_init,
6795 null_pointer_constant, NULL_TREE, NULL_TREE,
6798 /* Check to see if we have already given an error message. */
6799 if (inside_init == error_mark_node)
6801 else if (require_constant && !TREE_CONSTANT (inside_init))
6803 error_init (init_loc, "initializer element is not constant");
6804 inside_init = error_mark_node;
6806 else if (require_constant
6807 && !initializer_constant_valid_p (inside_init,
6808 TREE_TYPE (inside_init)))
6810 error_init (init_loc, "initializer element is not computable at "
6811 "load time");
6812 inside_init = error_mark_node;
6814 else if (require_constant && !maybe_const)
6815 pedwarn_init (init_loc, 0,
6816 "initializer element is not a constant expression");
6818 return inside_init;
6821 /* Come here only for records and arrays. */
6823 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6825 error_init (init_loc, "variable-sized object may not be initialized");
6826 return error_mark_node;
6829 error_init (init_loc, "invalid initializer");
6830 return error_mark_node;
6833 /* Handle initializers that use braces. */
6835 /* Type of object we are accumulating a constructor for.
6836 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6837 static tree constructor_type;
6839 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6840 left to fill. */
6841 static tree constructor_fields;
6843 /* For an ARRAY_TYPE, this is the specified index
6844 at which to store the next element we get. */
6845 static tree constructor_index;
6847 /* For an ARRAY_TYPE, this is the maximum index. */
6848 static tree constructor_max_index;
6850 /* For a RECORD_TYPE, this is the first field not yet written out. */
6851 static tree constructor_unfilled_fields;
6853 /* For an ARRAY_TYPE, this is the index of the first element
6854 not yet written out. */
6855 static tree constructor_unfilled_index;
6857 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6858 This is so we can generate gaps between fields, when appropriate. */
6859 static tree constructor_bit_index;
6861 /* If we are saving up the elements rather than allocating them,
6862 this is the list of elements so far (in reverse order,
6863 most recent first). */
6864 static vec<constructor_elt, va_gc> *constructor_elements;
6866 /* 1 if constructor should be incrementally stored into a constructor chain,
6867 0 if all the elements should be kept in AVL tree. */
6868 static int constructor_incremental;
6870 /* 1 if so far this constructor's elements are all compile-time constants. */
6871 static int constructor_constant;
6873 /* 1 if so far this constructor's elements are all valid address constants. */
6874 static int constructor_simple;
6876 /* 1 if this constructor has an element that cannot be part of a
6877 constant expression. */
6878 static int constructor_nonconst;
6880 /* 1 if this constructor is erroneous so far. */
6881 static int constructor_erroneous;
6883 /* 1 if this constructor is the universal zero initializer { 0 }. */
6884 static int constructor_zeroinit;
6886 /* Structure for managing pending initializer elements, organized as an
6887 AVL tree. */
6889 struct init_node
6891 struct init_node *left, *right;
6892 struct init_node *parent;
6893 int balance;
6894 tree purpose;
6895 tree value;
6896 tree origtype;
6899 /* Tree of pending elements at this constructor level.
6900 These are elements encountered out of order
6901 which belong at places we haven't reached yet in actually
6902 writing the output.
6903 Will never hold tree nodes across GC runs. */
6904 static struct init_node *constructor_pending_elts;
6906 /* The SPELLING_DEPTH of this constructor. */
6907 static int constructor_depth;
6909 /* DECL node for which an initializer is being read.
6910 0 means we are reading a constructor expression
6911 such as (struct foo) {...}. */
6912 static tree constructor_decl;
6914 /* Nonzero if this is an initializer for a top-level decl. */
6915 static int constructor_top_level;
6917 /* Nonzero if there were any member designators in this initializer. */
6918 static int constructor_designated;
6920 /* Nesting depth of designator list. */
6921 static int designator_depth;
6923 /* Nonzero if there were diagnosed errors in this designator list. */
6924 static int designator_erroneous;
6927 /* This stack has a level for each implicit or explicit level of
6928 structuring in the initializer, including the outermost one. It
6929 saves the values of most of the variables above. */
6931 struct constructor_range_stack;
6933 struct constructor_stack
6935 struct constructor_stack *next;
6936 tree type;
6937 tree fields;
6938 tree index;
6939 tree max_index;
6940 tree unfilled_index;
6941 tree unfilled_fields;
6942 tree bit_index;
6943 vec<constructor_elt, va_gc> *elements;
6944 struct init_node *pending_elts;
6945 int offset;
6946 int depth;
6947 /* If value nonzero, this value should replace the entire
6948 constructor at this level. */
6949 struct c_expr replacement_value;
6950 struct constructor_range_stack *range_stack;
6951 char constant;
6952 char simple;
6953 char nonconst;
6954 char implicit;
6955 char erroneous;
6956 char outer;
6957 char incremental;
6958 char designated;
6959 int designator_depth;
6962 static struct constructor_stack *constructor_stack;
6964 /* This stack represents designators from some range designator up to
6965 the last designator in the list. */
6967 struct constructor_range_stack
6969 struct constructor_range_stack *next, *prev;
6970 struct constructor_stack *stack;
6971 tree range_start;
6972 tree index;
6973 tree range_end;
6974 tree fields;
6977 static struct constructor_range_stack *constructor_range_stack;
6979 /* This stack records separate initializers that are nested.
6980 Nested initializers can't happen in ANSI C, but GNU C allows them
6981 in cases like { ... (struct foo) { ... } ... }. */
6983 struct initializer_stack
6985 struct initializer_stack *next;
6986 tree decl;
6987 struct constructor_stack *constructor_stack;
6988 struct constructor_range_stack *constructor_range_stack;
6989 vec<constructor_elt, va_gc> *elements;
6990 struct spelling *spelling;
6991 struct spelling *spelling_base;
6992 int spelling_size;
6993 char top_level;
6994 char require_constant_value;
6995 char require_constant_elements;
6998 static struct initializer_stack *initializer_stack;
7000 /* Prepare to parse and output the initializer for variable DECL. */
7002 void
7003 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7005 const char *locus;
7006 struct initializer_stack *p = XNEW (struct initializer_stack);
7008 p->decl = constructor_decl;
7009 p->require_constant_value = require_constant_value;
7010 p->require_constant_elements = require_constant_elements;
7011 p->constructor_stack = constructor_stack;
7012 p->constructor_range_stack = constructor_range_stack;
7013 p->elements = constructor_elements;
7014 p->spelling = spelling;
7015 p->spelling_base = spelling_base;
7016 p->spelling_size = spelling_size;
7017 p->top_level = constructor_top_level;
7018 p->next = initializer_stack;
7019 initializer_stack = p;
7021 constructor_decl = decl;
7022 constructor_designated = 0;
7023 constructor_top_level = top_level;
7025 if (decl != 0 && decl != error_mark_node)
7027 require_constant_value = TREE_STATIC (decl);
7028 require_constant_elements
7029 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7030 /* For a scalar, you can always use any value to initialize,
7031 even within braces. */
7032 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7033 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7034 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7035 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7036 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7038 else
7040 require_constant_value = 0;
7041 require_constant_elements = 0;
7042 locus = _("(anonymous)");
7045 constructor_stack = 0;
7046 constructor_range_stack = 0;
7048 found_missing_braces = 0;
7050 spelling_base = 0;
7051 spelling_size = 0;
7052 RESTORE_SPELLING_DEPTH (0);
7054 if (locus)
7055 push_string (locus);
7058 void
7059 finish_init (void)
7061 struct initializer_stack *p = initializer_stack;
7063 /* Free the whole constructor stack of this initializer. */
7064 while (constructor_stack)
7066 struct constructor_stack *q = constructor_stack;
7067 constructor_stack = q->next;
7068 free (q);
7071 gcc_assert (!constructor_range_stack);
7073 /* Pop back to the data of the outer initializer (if any). */
7074 free (spelling_base);
7076 constructor_decl = p->decl;
7077 require_constant_value = p->require_constant_value;
7078 require_constant_elements = p->require_constant_elements;
7079 constructor_stack = p->constructor_stack;
7080 constructor_range_stack = p->constructor_range_stack;
7081 constructor_elements = p->elements;
7082 spelling = p->spelling;
7083 spelling_base = p->spelling_base;
7084 spelling_size = p->spelling_size;
7085 constructor_top_level = p->top_level;
7086 initializer_stack = p->next;
7087 free (p);
7090 /* Call here when we see the initializer is surrounded by braces.
7091 This is instead of a call to push_init_level;
7092 it is matched by a call to pop_init_level.
7094 TYPE is the type to initialize, for a constructor expression.
7095 For an initializer for a decl, TYPE is zero. */
7097 void
7098 really_start_incremental_init (tree type)
7100 struct constructor_stack *p = XNEW (struct constructor_stack);
7102 if (type == 0)
7103 type = TREE_TYPE (constructor_decl);
7105 if (TREE_CODE (type) == VECTOR_TYPE
7106 && TYPE_VECTOR_OPAQUE (type))
7107 error ("opaque vector types cannot be initialized");
7109 p->type = constructor_type;
7110 p->fields = constructor_fields;
7111 p->index = constructor_index;
7112 p->max_index = constructor_max_index;
7113 p->unfilled_index = constructor_unfilled_index;
7114 p->unfilled_fields = constructor_unfilled_fields;
7115 p->bit_index = constructor_bit_index;
7116 p->elements = constructor_elements;
7117 p->constant = constructor_constant;
7118 p->simple = constructor_simple;
7119 p->nonconst = constructor_nonconst;
7120 p->erroneous = constructor_erroneous;
7121 p->pending_elts = constructor_pending_elts;
7122 p->depth = constructor_depth;
7123 p->replacement_value.value = 0;
7124 p->replacement_value.original_code = ERROR_MARK;
7125 p->replacement_value.original_type = NULL;
7126 p->implicit = 0;
7127 p->range_stack = 0;
7128 p->outer = 0;
7129 p->incremental = constructor_incremental;
7130 p->designated = constructor_designated;
7131 p->designator_depth = designator_depth;
7132 p->next = 0;
7133 constructor_stack = p;
7135 constructor_constant = 1;
7136 constructor_simple = 1;
7137 constructor_nonconst = 0;
7138 constructor_depth = SPELLING_DEPTH ();
7139 constructor_elements = NULL;
7140 constructor_pending_elts = 0;
7141 constructor_type = type;
7142 constructor_incremental = 1;
7143 constructor_designated = 0;
7144 constructor_zeroinit = 1;
7145 designator_depth = 0;
7146 designator_erroneous = 0;
7148 if (TREE_CODE (constructor_type) == RECORD_TYPE
7149 || TREE_CODE (constructor_type) == UNION_TYPE)
7151 constructor_fields = TYPE_FIELDS (constructor_type);
7152 /* Skip any nameless bit fields at the beginning. */
7153 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7154 && DECL_NAME (constructor_fields) == 0)
7155 constructor_fields = DECL_CHAIN (constructor_fields);
7157 constructor_unfilled_fields = constructor_fields;
7158 constructor_bit_index = bitsize_zero_node;
7160 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7162 if (TYPE_DOMAIN (constructor_type))
7164 constructor_max_index
7165 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7167 /* Detect non-empty initializations of zero-length arrays. */
7168 if (constructor_max_index == NULL_TREE
7169 && TYPE_SIZE (constructor_type))
7170 constructor_max_index = integer_minus_one_node;
7172 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7173 to initialize VLAs will cause a proper error; avoid tree
7174 checking errors as well by setting a safe value. */
7175 if (constructor_max_index
7176 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7177 constructor_max_index = integer_minus_one_node;
7179 constructor_index
7180 = convert (bitsizetype,
7181 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7183 else
7185 constructor_index = bitsize_zero_node;
7186 constructor_max_index = NULL_TREE;
7189 constructor_unfilled_index = constructor_index;
7191 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7193 /* Vectors are like simple fixed-size arrays. */
7194 constructor_max_index =
7195 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7196 constructor_index = bitsize_zero_node;
7197 constructor_unfilled_index = constructor_index;
7199 else
7201 /* Handle the case of int x = {5}; */
7202 constructor_fields = constructor_type;
7203 constructor_unfilled_fields = constructor_type;
7207 /* Push down into a subobject, for initialization.
7208 If this is for an explicit set of braces, IMPLICIT is 0.
7209 If it is because the next element belongs at a lower level,
7210 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7212 void
7213 push_init_level (location_t loc, int implicit,
7214 struct obstack *braced_init_obstack)
7216 struct constructor_stack *p;
7217 tree value = NULL_TREE;
7219 /* If we've exhausted any levels that didn't have braces,
7220 pop them now. If implicit == 1, this will have been done in
7221 process_init_element; do not repeat it here because in the case
7222 of excess initializers for an empty aggregate this leads to an
7223 infinite cycle of popping a level and immediately recreating
7224 it. */
7225 if (implicit != 1)
7227 while (constructor_stack->implicit)
7229 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7230 || TREE_CODE (constructor_type) == UNION_TYPE)
7231 && constructor_fields == 0)
7232 process_init_element (input_location,
7233 pop_init_level (loc, 1, braced_init_obstack),
7234 true, braced_init_obstack);
7235 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7236 && constructor_max_index
7237 && tree_int_cst_lt (constructor_max_index,
7238 constructor_index))
7239 process_init_element (input_location,
7240 pop_init_level (loc, 1, braced_init_obstack),
7241 true, braced_init_obstack);
7242 else
7243 break;
7247 /* Unless this is an explicit brace, we need to preserve previous
7248 content if any. */
7249 if (implicit)
7251 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7252 || TREE_CODE (constructor_type) == UNION_TYPE)
7253 && constructor_fields)
7254 value = find_init_member (constructor_fields, braced_init_obstack);
7255 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7256 value = find_init_member (constructor_index, braced_init_obstack);
7259 p = XNEW (struct constructor_stack);
7260 p->type = constructor_type;
7261 p->fields = constructor_fields;
7262 p->index = constructor_index;
7263 p->max_index = constructor_max_index;
7264 p->unfilled_index = constructor_unfilled_index;
7265 p->unfilled_fields = constructor_unfilled_fields;
7266 p->bit_index = constructor_bit_index;
7267 p->elements = constructor_elements;
7268 p->constant = constructor_constant;
7269 p->simple = constructor_simple;
7270 p->nonconst = constructor_nonconst;
7271 p->erroneous = constructor_erroneous;
7272 p->pending_elts = constructor_pending_elts;
7273 p->depth = constructor_depth;
7274 p->replacement_value.value = 0;
7275 p->replacement_value.original_code = ERROR_MARK;
7276 p->replacement_value.original_type = NULL;
7277 p->implicit = implicit;
7278 p->outer = 0;
7279 p->incremental = constructor_incremental;
7280 p->designated = constructor_designated;
7281 p->designator_depth = designator_depth;
7282 p->next = constructor_stack;
7283 p->range_stack = 0;
7284 constructor_stack = p;
7286 constructor_constant = 1;
7287 constructor_simple = 1;
7288 constructor_nonconst = 0;
7289 constructor_depth = SPELLING_DEPTH ();
7290 constructor_elements = NULL;
7291 constructor_incremental = 1;
7292 constructor_designated = 0;
7293 constructor_pending_elts = 0;
7294 if (!implicit)
7296 p->range_stack = constructor_range_stack;
7297 constructor_range_stack = 0;
7298 designator_depth = 0;
7299 designator_erroneous = 0;
7302 /* Don't die if an entire brace-pair level is superfluous
7303 in the containing level. */
7304 if (constructor_type == 0)
7306 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7307 || TREE_CODE (constructor_type) == UNION_TYPE)
7309 /* Don't die if there are extra init elts at the end. */
7310 if (constructor_fields == 0)
7311 constructor_type = 0;
7312 else
7314 constructor_type = TREE_TYPE (constructor_fields);
7315 push_member_name (constructor_fields);
7316 constructor_depth++;
7318 /* If upper initializer is designated, then mark this as
7319 designated too to prevent bogus warnings. */
7320 constructor_designated = p->designated;
7322 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7324 constructor_type = TREE_TYPE (constructor_type);
7325 push_array_bounds (tree_to_uhwi (constructor_index));
7326 constructor_depth++;
7329 if (constructor_type == 0)
7331 error_init (loc, "extra brace group at end of initializer");
7332 constructor_fields = 0;
7333 constructor_unfilled_fields = 0;
7334 return;
7337 if (value && TREE_CODE (value) == CONSTRUCTOR)
7339 constructor_constant = TREE_CONSTANT (value);
7340 constructor_simple = TREE_STATIC (value);
7341 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7342 constructor_elements = CONSTRUCTOR_ELTS (value);
7343 if (!vec_safe_is_empty (constructor_elements)
7344 && (TREE_CODE (constructor_type) == RECORD_TYPE
7345 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7346 set_nonincremental_init (braced_init_obstack);
7349 if (implicit == 1)
7350 found_missing_braces = 1;
7352 if (TREE_CODE (constructor_type) == RECORD_TYPE
7353 || TREE_CODE (constructor_type) == UNION_TYPE)
7355 constructor_fields = TYPE_FIELDS (constructor_type);
7356 /* Skip any nameless bit fields at the beginning. */
7357 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7358 && DECL_NAME (constructor_fields) == 0)
7359 constructor_fields = DECL_CHAIN (constructor_fields);
7361 constructor_unfilled_fields = constructor_fields;
7362 constructor_bit_index = bitsize_zero_node;
7364 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7366 /* Vectors are like simple fixed-size arrays. */
7367 constructor_max_index =
7368 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7369 constructor_index = bitsize_int (0);
7370 constructor_unfilled_index = constructor_index;
7372 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7374 if (TYPE_DOMAIN (constructor_type))
7376 constructor_max_index
7377 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7379 /* Detect non-empty initializations of zero-length arrays. */
7380 if (constructor_max_index == NULL_TREE
7381 && TYPE_SIZE (constructor_type))
7382 constructor_max_index = integer_minus_one_node;
7384 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7385 to initialize VLAs will cause a proper error; avoid tree
7386 checking errors as well by setting a safe value. */
7387 if (constructor_max_index
7388 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7389 constructor_max_index = integer_minus_one_node;
7391 constructor_index
7392 = convert (bitsizetype,
7393 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7395 else
7396 constructor_index = bitsize_zero_node;
7398 constructor_unfilled_index = constructor_index;
7399 if (value && TREE_CODE (value) == STRING_CST)
7401 /* We need to split the char/wchar array into individual
7402 characters, so that we don't have to special case it
7403 everywhere. */
7404 set_nonincremental_init_from_string (value, braced_init_obstack);
7407 else
7409 if (constructor_type != error_mark_node)
7410 warning_init (input_location, 0, "braces around scalar initializer");
7411 constructor_fields = constructor_type;
7412 constructor_unfilled_fields = constructor_type;
7416 /* At the end of an implicit or explicit brace level,
7417 finish up that level of constructor. If a single expression
7418 with redundant braces initialized that level, return the
7419 c_expr structure for that expression. Otherwise, the original_code
7420 element is set to ERROR_MARK.
7421 If we were outputting the elements as they are read, return 0 as the value
7422 from inner levels (process_init_element ignores that),
7423 but return error_mark_node as the value from the outermost level
7424 (that's what we want to put in DECL_INITIAL).
7425 Otherwise, return a CONSTRUCTOR expression as the value. */
7427 struct c_expr
7428 pop_init_level (location_t loc, int implicit,
7429 struct obstack *braced_init_obstack)
7431 struct constructor_stack *p;
7432 struct c_expr ret;
7433 ret.value = 0;
7434 ret.original_code = ERROR_MARK;
7435 ret.original_type = NULL;
7437 if (implicit == 0)
7439 /* When we come to an explicit close brace,
7440 pop any inner levels that didn't have explicit braces. */
7441 while (constructor_stack->implicit)
7442 process_init_element (input_location,
7443 pop_init_level (loc, 1, braced_init_obstack),
7444 true, braced_init_obstack);
7445 gcc_assert (!constructor_range_stack);
7448 /* Now output all pending elements. */
7449 constructor_incremental = 1;
7450 output_pending_init_elements (1, braced_init_obstack);
7452 p = constructor_stack;
7454 /* Error for initializing a flexible array member, or a zero-length
7455 array member in an inappropriate context. */
7456 if (constructor_type && constructor_fields
7457 && TREE_CODE (constructor_type) == ARRAY_TYPE
7458 && TYPE_DOMAIN (constructor_type)
7459 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7461 /* Silently discard empty initializations. The parser will
7462 already have pedwarned for empty brackets. */
7463 if (integer_zerop (constructor_unfilled_index))
7464 constructor_type = NULL_TREE;
7465 else
7467 gcc_assert (!TYPE_SIZE (constructor_type));
7469 if (constructor_depth > 2)
7470 error_init (loc, "initialization of flexible array member in a nested context");
7471 else
7472 pedwarn_init (loc, OPT_Wpedantic,
7473 "initialization of a flexible array member");
7475 /* We have already issued an error message for the existence
7476 of a flexible array member not at the end of the structure.
7477 Discard the initializer so that we do not die later. */
7478 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7479 constructor_type = NULL_TREE;
7483 if (vec_safe_length (constructor_elements) != 1)
7484 constructor_zeroinit = 0;
7486 /* Warn when some structs are initialized with direct aggregation. */
7487 if (!implicit && found_missing_braces && warn_missing_braces
7488 && !constructor_zeroinit)
7490 warning_init (loc, OPT_Wmissing_braces,
7491 "missing braces around initializer");
7494 /* Warn when some struct elements are implicitly initialized to zero. */
7495 if (warn_missing_field_initializers
7496 && constructor_type
7497 && TREE_CODE (constructor_type) == RECORD_TYPE
7498 && constructor_unfilled_fields)
7500 /* Do not warn for flexible array members or zero-length arrays. */
7501 while (constructor_unfilled_fields
7502 && (!DECL_SIZE (constructor_unfilled_fields)
7503 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7504 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7506 if (constructor_unfilled_fields
7507 /* Do not warn if this level of the initializer uses member
7508 designators; it is likely to be deliberate. */
7509 && !constructor_designated
7510 /* Do not warn about initializing with ` = {0}'. */
7511 && !constructor_zeroinit)
7513 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7514 "missing initializer for field %qD of %qT",
7515 constructor_unfilled_fields,
7516 constructor_type))
7517 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7518 "%qD declared here", constructor_unfilled_fields);
7522 /* Pad out the end of the structure. */
7523 if (p->replacement_value.value)
7524 /* If this closes a superfluous brace pair,
7525 just pass out the element between them. */
7526 ret = p->replacement_value;
7527 else if (constructor_type == 0)
7529 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7530 && TREE_CODE (constructor_type) != UNION_TYPE
7531 && TREE_CODE (constructor_type) != ARRAY_TYPE
7532 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7534 /* A nonincremental scalar initializer--just return
7535 the element, after verifying there is just one. */
7536 if (vec_safe_is_empty (constructor_elements))
7538 if (!constructor_erroneous)
7539 error_init (loc, "empty scalar initializer");
7540 ret.value = error_mark_node;
7542 else if (vec_safe_length (constructor_elements) != 1)
7544 error_init (loc, "extra elements in scalar initializer");
7545 ret.value = (*constructor_elements)[0].value;
7547 else
7548 ret.value = (*constructor_elements)[0].value;
7550 else
7552 if (constructor_erroneous)
7553 ret.value = error_mark_node;
7554 else
7556 ret.value = build_constructor (constructor_type,
7557 constructor_elements);
7558 if (constructor_constant)
7559 TREE_CONSTANT (ret.value) = 1;
7560 if (constructor_constant && constructor_simple)
7561 TREE_STATIC (ret.value) = 1;
7562 if (constructor_nonconst)
7563 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7567 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7569 if (constructor_nonconst)
7570 ret.original_code = C_MAYBE_CONST_EXPR;
7571 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7572 ret.original_code = ERROR_MARK;
7575 constructor_type = p->type;
7576 constructor_fields = p->fields;
7577 constructor_index = p->index;
7578 constructor_max_index = p->max_index;
7579 constructor_unfilled_index = p->unfilled_index;
7580 constructor_unfilled_fields = p->unfilled_fields;
7581 constructor_bit_index = p->bit_index;
7582 constructor_elements = p->elements;
7583 constructor_constant = p->constant;
7584 constructor_simple = p->simple;
7585 constructor_nonconst = p->nonconst;
7586 constructor_erroneous = p->erroneous;
7587 constructor_incremental = p->incremental;
7588 constructor_designated = p->designated;
7589 designator_depth = p->designator_depth;
7590 constructor_pending_elts = p->pending_elts;
7591 constructor_depth = p->depth;
7592 if (!p->implicit)
7593 constructor_range_stack = p->range_stack;
7594 RESTORE_SPELLING_DEPTH (constructor_depth);
7596 constructor_stack = p->next;
7597 free (p);
7599 if (ret.value == 0 && constructor_stack == 0)
7600 ret.value = error_mark_node;
7601 return ret;
7604 /* Common handling for both array range and field name designators.
7605 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7607 static int
7608 set_designator (location_t loc, int array,
7609 struct obstack *braced_init_obstack)
7611 tree subtype;
7612 enum tree_code subcode;
7614 /* Don't die if an entire brace-pair level is superfluous
7615 in the containing level. */
7616 if (constructor_type == 0)
7617 return 1;
7619 /* If there were errors in this designator list already, bail out
7620 silently. */
7621 if (designator_erroneous)
7622 return 1;
7624 if (!designator_depth)
7626 gcc_assert (!constructor_range_stack);
7628 /* Designator list starts at the level of closest explicit
7629 braces. */
7630 while (constructor_stack->implicit)
7631 process_init_element (input_location,
7632 pop_init_level (loc, 1, braced_init_obstack),
7633 true, braced_init_obstack);
7634 constructor_designated = 1;
7635 return 0;
7638 switch (TREE_CODE (constructor_type))
7640 case RECORD_TYPE:
7641 case UNION_TYPE:
7642 subtype = TREE_TYPE (constructor_fields);
7643 if (subtype != error_mark_node)
7644 subtype = TYPE_MAIN_VARIANT (subtype);
7645 break;
7646 case ARRAY_TYPE:
7647 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7648 break;
7649 default:
7650 gcc_unreachable ();
7653 subcode = TREE_CODE (subtype);
7654 if (array && subcode != ARRAY_TYPE)
7656 error_init (loc, "array index in non-array initializer");
7657 return 1;
7659 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7661 error_init (loc, "field name not in record or union initializer");
7662 return 1;
7665 constructor_designated = 1;
7666 push_init_level (loc, 2, braced_init_obstack);
7667 return 0;
7670 /* If there are range designators in designator list, push a new designator
7671 to constructor_range_stack. RANGE_END is end of such stack range or
7672 NULL_TREE if there is no range designator at this level. */
7674 static void
7675 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7677 struct constructor_range_stack *p;
7679 p = (struct constructor_range_stack *)
7680 obstack_alloc (braced_init_obstack,
7681 sizeof (struct constructor_range_stack));
7682 p->prev = constructor_range_stack;
7683 p->next = 0;
7684 p->fields = constructor_fields;
7685 p->range_start = constructor_index;
7686 p->index = constructor_index;
7687 p->stack = constructor_stack;
7688 p->range_end = range_end;
7689 if (constructor_range_stack)
7690 constructor_range_stack->next = p;
7691 constructor_range_stack = p;
7694 /* Within an array initializer, specify the next index to be initialized.
7695 FIRST is that index. If LAST is nonzero, then initialize a range
7696 of indices, running from FIRST through LAST. */
7698 void
7699 set_init_index (location_t loc, tree first, tree last,
7700 struct obstack *braced_init_obstack)
7702 if (set_designator (loc, 1, braced_init_obstack))
7703 return;
7705 designator_erroneous = 1;
7707 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7708 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7710 error_init (loc, "array index in initializer not of integer type");
7711 return;
7714 if (TREE_CODE (first) != INTEGER_CST)
7716 first = c_fully_fold (first, false, NULL);
7717 if (TREE_CODE (first) == INTEGER_CST)
7718 pedwarn_init (loc, OPT_Wpedantic,
7719 "array index in initializer is not "
7720 "an integer constant expression");
7723 if (last && TREE_CODE (last) != INTEGER_CST)
7725 last = c_fully_fold (last, false, NULL);
7726 if (TREE_CODE (last) == INTEGER_CST)
7727 pedwarn_init (loc, OPT_Wpedantic,
7728 "array index in initializer is not "
7729 "an integer constant expression");
7732 if (TREE_CODE (first) != INTEGER_CST)
7733 error_init (loc, "nonconstant array index in initializer");
7734 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7735 error_init (loc, "nonconstant array index in initializer");
7736 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7737 error_init (loc, "array index in non-array initializer");
7738 else if (tree_int_cst_sgn (first) == -1)
7739 error_init (loc, "array index in initializer exceeds array bounds");
7740 else if (constructor_max_index
7741 && tree_int_cst_lt (constructor_max_index, first))
7742 error_init (loc, "array index in initializer exceeds array bounds");
7743 else
7745 constant_expression_warning (first);
7746 if (last)
7747 constant_expression_warning (last);
7748 constructor_index = convert (bitsizetype, first);
7749 if (tree_int_cst_lt (constructor_index, first))
7751 constructor_index = copy_node (constructor_index);
7752 TREE_OVERFLOW (constructor_index) = 1;
7755 if (last)
7757 if (tree_int_cst_equal (first, last))
7758 last = 0;
7759 else if (tree_int_cst_lt (last, first))
7761 error_init (loc, "empty index range in initializer");
7762 last = 0;
7764 else
7766 last = convert (bitsizetype, last);
7767 if (constructor_max_index != 0
7768 && tree_int_cst_lt (constructor_max_index, last))
7770 error_init (loc, "array index range in initializer exceeds "
7771 "array bounds");
7772 last = 0;
7777 designator_depth++;
7778 designator_erroneous = 0;
7779 if (constructor_range_stack || last)
7780 push_range_stack (last, braced_init_obstack);
7784 /* Within a struct initializer, specify the next field to be initialized. */
7786 void
7787 set_init_label (location_t loc, tree fieldname,
7788 struct obstack *braced_init_obstack)
7790 tree field;
7792 if (set_designator (loc, 0, braced_init_obstack))
7793 return;
7795 designator_erroneous = 1;
7797 if (TREE_CODE (constructor_type) != RECORD_TYPE
7798 && TREE_CODE (constructor_type) != UNION_TYPE)
7800 error_init (loc, "field name not in record or union initializer");
7801 return;
7804 field = lookup_field (constructor_type, fieldname);
7806 if (field == 0)
7807 error ("unknown field %qE specified in initializer", fieldname);
7808 else
7811 constructor_fields = TREE_VALUE (field);
7812 designator_depth++;
7813 designator_erroneous = 0;
7814 if (constructor_range_stack)
7815 push_range_stack (NULL_TREE, braced_init_obstack);
7816 field = TREE_CHAIN (field);
7817 if (field)
7819 if (set_designator (loc, 0, braced_init_obstack))
7820 return;
7823 while (field != NULL_TREE);
7826 /* Add a new initializer to the tree of pending initializers. PURPOSE
7827 identifies the initializer, either array index or field in a structure.
7828 VALUE is the value of that index or field. If ORIGTYPE is not
7829 NULL_TREE, it is the original type of VALUE.
7831 IMPLICIT is true if value comes from pop_init_level (1),
7832 the new initializer has been merged with the existing one
7833 and thus no warnings should be emitted about overriding an
7834 existing initializer. */
7836 static void
7837 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7838 bool implicit, struct obstack *braced_init_obstack)
7840 struct init_node *p, **q, *r;
7842 q = &constructor_pending_elts;
7843 p = 0;
7845 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7847 while (*q != 0)
7849 p = *q;
7850 if (tree_int_cst_lt (purpose, p->purpose))
7851 q = &p->left;
7852 else if (tree_int_cst_lt (p->purpose, purpose))
7853 q = &p->right;
7854 else
7856 if (!implicit)
7858 if (TREE_SIDE_EFFECTS (p->value))
7859 warning_init (loc, 0,
7860 "initialized field with side-effects "
7861 "overwritten");
7862 else if (warn_override_init)
7863 warning_init (loc, OPT_Woverride_init,
7864 "initialized field overwritten");
7866 p->value = value;
7867 p->origtype = origtype;
7868 return;
7872 else
7874 tree bitpos;
7876 bitpos = bit_position (purpose);
7877 while (*q != NULL)
7879 p = *q;
7880 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7881 q = &p->left;
7882 else if (p->purpose != purpose)
7883 q = &p->right;
7884 else
7886 if (!implicit)
7888 if (TREE_SIDE_EFFECTS (p->value))
7889 warning_init (loc, 0,
7890 "initialized field with side-effects "
7891 "overwritten");
7892 else if (warn_override_init)
7893 warning_init (loc, OPT_Woverride_init,
7894 "initialized field overwritten");
7896 p->value = value;
7897 p->origtype = origtype;
7898 return;
7903 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7904 sizeof (struct init_node));
7905 r->purpose = purpose;
7906 r->value = value;
7907 r->origtype = origtype;
7909 *q = r;
7910 r->parent = p;
7911 r->left = 0;
7912 r->right = 0;
7913 r->balance = 0;
7915 while (p)
7917 struct init_node *s;
7919 if (r == p->left)
7921 if (p->balance == 0)
7922 p->balance = -1;
7923 else if (p->balance < 0)
7925 if (r->balance < 0)
7927 /* L rotation. */
7928 p->left = r->right;
7929 if (p->left)
7930 p->left->parent = p;
7931 r->right = p;
7933 p->balance = 0;
7934 r->balance = 0;
7936 s = p->parent;
7937 p->parent = r;
7938 r->parent = s;
7939 if (s)
7941 if (s->left == p)
7942 s->left = r;
7943 else
7944 s->right = r;
7946 else
7947 constructor_pending_elts = r;
7949 else
7951 /* LR rotation. */
7952 struct init_node *t = r->right;
7954 r->right = t->left;
7955 if (r->right)
7956 r->right->parent = r;
7957 t->left = r;
7959 p->left = t->right;
7960 if (p->left)
7961 p->left->parent = p;
7962 t->right = p;
7964 p->balance = t->balance < 0;
7965 r->balance = -(t->balance > 0);
7966 t->balance = 0;
7968 s = p->parent;
7969 p->parent = t;
7970 r->parent = t;
7971 t->parent = s;
7972 if (s)
7974 if (s->left == p)
7975 s->left = t;
7976 else
7977 s->right = t;
7979 else
7980 constructor_pending_elts = t;
7982 break;
7984 else
7986 /* p->balance == +1; growth of left side balances the node. */
7987 p->balance = 0;
7988 break;
7991 else /* r == p->right */
7993 if (p->balance == 0)
7994 /* Growth propagation from right side. */
7995 p->balance++;
7996 else if (p->balance > 0)
7998 if (r->balance > 0)
8000 /* R rotation. */
8001 p->right = r->left;
8002 if (p->right)
8003 p->right->parent = p;
8004 r->left = p;
8006 p->balance = 0;
8007 r->balance = 0;
8009 s = p->parent;
8010 p->parent = r;
8011 r->parent = s;
8012 if (s)
8014 if (s->left == p)
8015 s->left = r;
8016 else
8017 s->right = r;
8019 else
8020 constructor_pending_elts = r;
8022 else /* r->balance == -1 */
8024 /* RL rotation */
8025 struct init_node *t = r->left;
8027 r->left = t->right;
8028 if (r->left)
8029 r->left->parent = r;
8030 t->right = r;
8032 p->right = t->left;
8033 if (p->right)
8034 p->right->parent = p;
8035 t->left = p;
8037 r->balance = (t->balance < 0);
8038 p->balance = -(t->balance > 0);
8039 t->balance = 0;
8041 s = p->parent;
8042 p->parent = t;
8043 r->parent = t;
8044 t->parent = s;
8045 if (s)
8047 if (s->left == p)
8048 s->left = t;
8049 else
8050 s->right = t;
8052 else
8053 constructor_pending_elts = t;
8055 break;
8057 else
8059 /* p->balance == -1; growth of right side balances the node. */
8060 p->balance = 0;
8061 break;
8065 r = p;
8066 p = p->parent;
8070 /* Build AVL tree from a sorted chain. */
8072 static void
8073 set_nonincremental_init (struct obstack * braced_init_obstack)
8075 unsigned HOST_WIDE_INT ix;
8076 tree index, value;
8078 if (TREE_CODE (constructor_type) != RECORD_TYPE
8079 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8080 return;
8082 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8083 add_pending_init (input_location, index, value, NULL_TREE, true,
8084 braced_init_obstack);
8085 constructor_elements = NULL;
8086 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8088 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8089 /* Skip any nameless bit fields at the beginning. */
8090 while (constructor_unfilled_fields != 0
8091 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8092 && DECL_NAME (constructor_unfilled_fields) == 0)
8093 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8096 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8098 if (TYPE_DOMAIN (constructor_type))
8099 constructor_unfilled_index
8100 = convert (bitsizetype,
8101 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8102 else
8103 constructor_unfilled_index = bitsize_zero_node;
8105 constructor_incremental = 0;
8108 /* Build AVL tree from a string constant. */
8110 static void
8111 set_nonincremental_init_from_string (tree str,
8112 struct obstack * braced_init_obstack)
8114 tree value, purpose, type;
8115 HOST_WIDE_INT val[2];
8116 const char *p, *end;
8117 int byte, wchar_bytes, charwidth, bitpos;
8119 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8121 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8122 charwidth = TYPE_PRECISION (char_type_node);
8123 type = TREE_TYPE (constructor_type);
8124 p = TREE_STRING_POINTER (str);
8125 end = p + TREE_STRING_LENGTH (str);
8127 for (purpose = bitsize_zero_node;
8128 p < end
8129 && !(constructor_max_index
8130 && tree_int_cst_lt (constructor_max_index, purpose));
8131 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8133 if (wchar_bytes == 1)
8135 val[0] = (unsigned char) *p++;
8136 val[1] = 0;
8138 else
8140 val[1] = 0;
8141 val[0] = 0;
8142 for (byte = 0; byte < wchar_bytes; byte++)
8144 if (BYTES_BIG_ENDIAN)
8145 bitpos = (wchar_bytes - byte - 1) * charwidth;
8146 else
8147 bitpos = byte * charwidth;
8148 val[bitpos % HOST_BITS_PER_WIDE_INT]
8149 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8150 << (bitpos % HOST_BITS_PER_WIDE_INT);
8154 if (!TYPE_UNSIGNED (type))
8156 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8157 if (bitpos < HOST_BITS_PER_WIDE_INT)
8159 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8161 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8162 val[1] = -1;
8165 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8167 if (val[0] < 0)
8168 val[1] = -1;
8170 else if (val[1] & (((HOST_WIDE_INT) 1)
8171 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8172 val[1] |= ((HOST_WIDE_INT) -1)
8173 << (bitpos - HOST_BITS_PER_WIDE_INT);
8176 value = wide_int_to_tree (type,
8177 wide_int::from_array (val, 2,
8178 HOST_BITS_PER_WIDE_INT * 2));
8179 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8180 braced_init_obstack);
8183 constructor_incremental = 0;
8186 /* Return value of FIELD in pending initializer or zero if the field was
8187 not initialized yet. */
8189 static tree
8190 find_init_member (tree field, struct obstack * braced_init_obstack)
8192 struct init_node *p;
8194 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8196 if (constructor_incremental
8197 && tree_int_cst_lt (field, constructor_unfilled_index))
8198 set_nonincremental_init (braced_init_obstack);
8200 p = constructor_pending_elts;
8201 while (p)
8203 if (tree_int_cst_lt (field, p->purpose))
8204 p = p->left;
8205 else if (tree_int_cst_lt (p->purpose, field))
8206 p = p->right;
8207 else
8208 return p->value;
8211 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8213 tree bitpos = bit_position (field);
8215 if (constructor_incremental
8216 && (!constructor_unfilled_fields
8217 || tree_int_cst_lt (bitpos,
8218 bit_position (constructor_unfilled_fields))))
8219 set_nonincremental_init (braced_init_obstack);
8221 p = constructor_pending_elts;
8222 while (p)
8224 if (field == p->purpose)
8225 return p->value;
8226 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8227 p = p->left;
8228 else
8229 p = p->right;
8232 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8234 if (!vec_safe_is_empty (constructor_elements)
8235 && (constructor_elements->last ().index == field))
8236 return constructor_elements->last ().value;
8238 return 0;
8241 /* "Output" the next constructor element.
8242 At top level, really output it to assembler code now.
8243 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8244 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8245 TYPE is the data type that the containing data type wants here.
8246 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8247 If VALUE is a string constant, STRICT_STRING is true if it is
8248 unparenthesized or we should not warn here for it being parenthesized.
8249 For other types of VALUE, STRICT_STRING is not used.
8251 PENDING if non-nil means output pending elements that belong
8252 right after this element. (PENDING is normally 1;
8253 it is 0 while outputting pending elements, to avoid recursion.)
8255 IMPLICIT is true if value comes from pop_init_level (1),
8256 the new initializer has been merged with the existing one
8257 and thus no warnings should be emitted about overriding an
8258 existing initializer. */
8260 static void
8261 output_init_element (location_t loc, tree value, tree origtype,
8262 bool strict_string, tree type, tree field, int pending,
8263 bool implicit, struct obstack * braced_init_obstack)
8265 tree semantic_type = NULL_TREE;
8266 bool maybe_const = true;
8267 bool npc;
8269 if (type == error_mark_node || value == error_mark_node)
8271 constructor_erroneous = 1;
8272 return;
8274 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8275 && (TREE_CODE (value) == STRING_CST
8276 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8277 && !(TREE_CODE (value) == STRING_CST
8278 && TREE_CODE (type) == ARRAY_TYPE
8279 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8280 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8281 TYPE_MAIN_VARIANT (type)))
8282 value = array_to_pointer_conversion (input_location, value);
8284 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8285 && require_constant_value && !flag_isoc99 && pending)
8287 /* As an extension, allow initializing objects with static storage
8288 duration with compound literals (which are then treated just as
8289 the brace enclosed list they contain). */
8290 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8291 value = DECL_INITIAL (decl);
8294 npc = null_pointer_constant_p (value);
8295 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8297 semantic_type = TREE_TYPE (value);
8298 value = TREE_OPERAND (value, 0);
8300 value = c_fully_fold (value, require_constant_value, &maybe_const);
8302 if (value == error_mark_node)
8303 constructor_erroneous = 1;
8304 else if (!TREE_CONSTANT (value))
8305 constructor_constant = 0;
8306 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8307 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8308 || TREE_CODE (constructor_type) == UNION_TYPE)
8309 && DECL_C_BIT_FIELD (field)
8310 && TREE_CODE (value) != INTEGER_CST))
8311 constructor_simple = 0;
8312 if (!maybe_const)
8313 constructor_nonconst = 1;
8315 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8317 if (require_constant_value)
8319 error_init (loc, "initializer element is not constant");
8320 value = error_mark_node;
8322 else if (require_constant_elements)
8323 pedwarn (loc, OPT_Wpedantic,
8324 "initializer element is not computable at load time");
8326 else if (!maybe_const
8327 && (require_constant_value || require_constant_elements))
8328 pedwarn_init (loc, OPT_Wpedantic,
8329 "initializer element is not a constant expression");
8331 /* Issue -Wc++-compat warnings about initializing a bitfield with
8332 enum type. */
8333 if (warn_cxx_compat
8334 && field != NULL_TREE
8335 && TREE_CODE (field) == FIELD_DECL
8336 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8337 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8338 != TYPE_MAIN_VARIANT (type))
8339 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8341 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8342 if (checktype != error_mark_node
8343 && (TYPE_MAIN_VARIANT (checktype)
8344 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8345 warning_init (loc, OPT_Wc___compat,
8346 "enum conversion in initialization is invalid in C++");
8349 /* If this field is empty (and not at the end of structure),
8350 don't do anything other than checking the initializer. */
8351 if (field
8352 && (TREE_TYPE (field) == error_mark_node
8353 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8354 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8355 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8356 || DECL_CHAIN (field)))))
8357 return;
8359 if (semantic_type)
8360 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8361 value = digest_init (loc, type, value, origtype, npc, strict_string,
8362 require_constant_value);
8363 if (value == error_mark_node)
8365 constructor_erroneous = 1;
8366 return;
8368 if (require_constant_value || require_constant_elements)
8369 constant_expression_warning (value);
8371 /* If this element doesn't come next in sequence,
8372 put it on constructor_pending_elts. */
8373 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8374 && (!constructor_incremental
8375 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8377 if (constructor_incremental
8378 && tree_int_cst_lt (field, constructor_unfilled_index))
8379 set_nonincremental_init (braced_init_obstack);
8381 add_pending_init (loc, field, value, origtype, implicit,
8382 braced_init_obstack);
8383 return;
8385 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8386 && (!constructor_incremental
8387 || field != constructor_unfilled_fields))
8389 /* We do this for records but not for unions. In a union,
8390 no matter which field is specified, it can be initialized
8391 right away since it starts at the beginning of the union. */
8392 if (constructor_incremental)
8394 if (!constructor_unfilled_fields)
8395 set_nonincremental_init (braced_init_obstack);
8396 else
8398 tree bitpos, unfillpos;
8400 bitpos = bit_position (field);
8401 unfillpos = bit_position (constructor_unfilled_fields);
8403 if (tree_int_cst_lt (bitpos, unfillpos))
8404 set_nonincremental_init (braced_init_obstack);
8408 add_pending_init (loc, field, value, origtype, implicit,
8409 braced_init_obstack);
8410 return;
8412 else if (TREE_CODE (constructor_type) == UNION_TYPE
8413 && !vec_safe_is_empty (constructor_elements))
8415 if (!implicit)
8417 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8418 warning_init (loc, 0,
8419 "initialized field with side-effects overwritten");
8420 else if (warn_override_init)
8421 warning_init (loc, OPT_Woverride_init,
8422 "initialized field overwritten");
8425 /* We can have just one union field set. */
8426 constructor_elements = NULL;
8429 /* Otherwise, output this element either to
8430 constructor_elements or to the assembler file. */
8432 constructor_elt celt = {field, value};
8433 vec_safe_push (constructor_elements, celt);
8435 /* Advance the variable that indicates sequential elements output. */
8436 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8437 constructor_unfilled_index
8438 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8439 bitsize_one_node);
8440 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8442 constructor_unfilled_fields
8443 = DECL_CHAIN (constructor_unfilled_fields);
8445 /* Skip any nameless bit fields. */
8446 while (constructor_unfilled_fields != 0
8447 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8448 && DECL_NAME (constructor_unfilled_fields) == 0)
8449 constructor_unfilled_fields =
8450 DECL_CHAIN (constructor_unfilled_fields);
8452 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8453 constructor_unfilled_fields = 0;
8455 /* Now output any pending elements which have become next. */
8456 if (pending)
8457 output_pending_init_elements (0, braced_init_obstack);
8460 /* Output any pending elements which have become next.
8461 As we output elements, constructor_unfilled_{fields,index}
8462 advances, which may cause other elements to become next;
8463 if so, they too are output.
8465 If ALL is 0, we return when there are
8466 no more pending elements to output now.
8468 If ALL is 1, we output space as necessary so that
8469 we can output all the pending elements. */
8470 static void
8471 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8473 struct init_node *elt = constructor_pending_elts;
8474 tree next;
8476 retry:
8478 /* Look through the whole pending tree.
8479 If we find an element that should be output now,
8480 output it. Otherwise, set NEXT to the element
8481 that comes first among those still pending. */
8483 next = 0;
8484 while (elt)
8486 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8488 if (tree_int_cst_equal (elt->purpose,
8489 constructor_unfilled_index))
8490 output_init_element (input_location, elt->value, elt->origtype,
8491 true, TREE_TYPE (constructor_type),
8492 constructor_unfilled_index, 0, false,
8493 braced_init_obstack);
8494 else if (tree_int_cst_lt (constructor_unfilled_index,
8495 elt->purpose))
8497 /* Advance to the next smaller node. */
8498 if (elt->left)
8499 elt = elt->left;
8500 else
8502 /* We have reached the smallest node bigger than the
8503 current unfilled index. Fill the space first. */
8504 next = elt->purpose;
8505 break;
8508 else
8510 /* Advance to the next bigger node. */
8511 if (elt->right)
8512 elt = elt->right;
8513 else
8515 /* We have reached the biggest node in a subtree. Find
8516 the parent of it, which is the next bigger node. */
8517 while (elt->parent && elt->parent->right == elt)
8518 elt = elt->parent;
8519 elt = elt->parent;
8520 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8521 elt->purpose))
8523 next = elt->purpose;
8524 break;
8529 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8530 || TREE_CODE (constructor_type) == UNION_TYPE)
8532 tree ctor_unfilled_bitpos, elt_bitpos;
8534 /* If the current record is complete we are done. */
8535 if (constructor_unfilled_fields == 0)
8536 break;
8538 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8539 elt_bitpos = bit_position (elt->purpose);
8540 /* We can't compare fields here because there might be empty
8541 fields in between. */
8542 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8544 constructor_unfilled_fields = elt->purpose;
8545 output_init_element (input_location, elt->value, elt->origtype,
8546 true, TREE_TYPE (elt->purpose),
8547 elt->purpose, 0, false,
8548 braced_init_obstack);
8550 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8552 /* Advance to the next smaller node. */
8553 if (elt->left)
8554 elt = elt->left;
8555 else
8557 /* We have reached the smallest node bigger than the
8558 current unfilled field. Fill the space first. */
8559 next = elt->purpose;
8560 break;
8563 else
8565 /* Advance to the next bigger node. */
8566 if (elt->right)
8567 elt = elt->right;
8568 else
8570 /* We have reached the biggest node in a subtree. Find
8571 the parent of it, which is the next bigger node. */
8572 while (elt->parent && elt->parent->right == elt)
8573 elt = elt->parent;
8574 elt = elt->parent;
8575 if (elt
8576 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8577 bit_position (elt->purpose))))
8579 next = elt->purpose;
8580 break;
8587 /* Ordinarily return, but not if we want to output all
8588 and there are elements left. */
8589 if (!(all && next != 0))
8590 return;
8592 /* If it's not incremental, just skip over the gap, so that after
8593 jumping to retry we will output the next successive element. */
8594 if (TREE_CODE (constructor_type) == RECORD_TYPE
8595 || TREE_CODE (constructor_type) == UNION_TYPE)
8596 constructor_unfilled_fields = next;
8597 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8598 constructor_unfilled_index = next;
8600 /* ELT now points to the node in the pending tree with the next
8601 initializer to output. */
8602 goto retry;
8605 /* Add one non-braced element to the current constructor level.
8606 This adjusts the current position within the constructor's type.
8607 This may also start or terminate implicit levels
8608 to handle a partly-braced initializer.
8610 Once this has found the correct level for the new element,
8611 it calls output_init_element.
8613 IMPLICIT is true if value comes from pop_init_level (1),
8614 the new initializer has been merged with the existing one
8615 and thus no warnings should be emitted about overriding an
8616 existing initializer. */
8618 void
8619 process_init_element (location_t loc, struct c_expr value, bool implicit,
8620 struct obstack * braced_init_obstack)
8622 tree orig_value = value.value;
8623 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8624 bool strict_string = value.original_code == STRING_CST;
8625 bool was_designated = designator_depth != 0;
8627 designator_depth = 0;
8628 designator_erroneous = 0;
8630 if (!implicit && value.value && !integer_zerop (value.value))
8631 constructor_zeroinit = 0;
8633 /* Handle superfluous braces around string cst as in
8634 char x[] = {"foo"}; */
8635 if (string_flag
8636 && constructor_type
8637 && !was_designated
8638 && TREE_CODE (constructor_type) == ARRAY_TYPE
8639 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8640 && integer_zerop (constructor_unfilled_index))
8642 if (constructor_stack->replacement_value.value)
8643 error_init (loc, "excess elements in char array initializer");
8644 constructor_stack->replacement_value = value;
8645 return;
8648 if (constructor_stack->replacement_value.value != 0)
8650 error_init (loc, "excess elements in struct initializer");
8651 return;
8654 /* Ignore elements of a brace group if it is entirely superfluous
8655 and has already been diagnosed. */
8656 if (constructor_type == 0)
8657 return;
8659 if (!implicit && warn_designated_init && !was_designated
8660 && TREE_CODE (constructor_type) == RECORD_TYPE
8661 && lookup_attribute ("designated_init",
8662 TYPE_ATTRIBUTES (constructor_type)))
8663 warning_init (loc,
8664 OPT_Wdesignated_init,
8665 "positional initialization of field "
8666 "in %<struct%> declared with %<designated_init%> attribute");
8668 /* If we've exhausted any levels that didn't have braces,
8669 pop them now. */
8670 while (constructor_stack->implicit)
8672 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8673 || TREE_CODE (constructor_type) == UNION_TYPE)
8674 && constructor_fields == 0)
8675 process_init_element (loc,
8676 pop_init_level (loc, 1, braced_init_obstack),
8677 true, braced_init_obstack);
8678 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8679 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8680 && constructor_max_index
8681 && tree_int_cst_lt (constructor_max_index,
8682 constructor_index))
8683 process_init_element (loc,
8684 pop_init_level (loc, 1, braced_init_obstack),
8685 true, braced_init_obstack);
8686 else
8687 break;
8690 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8691 if (constructor_range_stack)
8693 /* If value is a compound literal and we'll be just using its
8694 content, don't put it into a SAVE_EXPR. */
8695 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8696 || !require_constant_value
8697 || flag_isoc99)
8699 tree semantic_type = NULL_TREE;
8700 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8702 semantic_type = TREE_TYPE (value.value);
8703 value.value = TREE_OPERAND (value.value, 0);
8705 value.value = c_save_expr (value.value);
8706 if (semantic_type)
8707 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8708 value.value);
8712 while (1)
8714 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8716 tree fieldtype;
8717 enum tree_code fieldcode;
8719 if (constructor_fields == 0)
8721 pedwarn_init (loc, 0, "excess elements in struct initializer");
8722 break;
8725 fieldtype = TREE_TYPE (constructor_fields);
8726 if (fieldtype != error_mark_node)
8727 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8728 fieldcode = TREE_CODE (fieldtype);
8730 /* Error for non-static initialization of a flexible array member. */
8731 if (fieldcode == ARRAY_TYPE
8732 && !require_constant_value
8733 && TYPE_SIZE (fieldtype) == NULL_TREE
8734 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8736 error_init (loc, "non-static initialization of a flexible "
8737 "array member");
8738 break;
8741 /* Accept a string constant to initialize a subarray. */
8742 if (value.value != 0
8743 && fieldcode == ARRAY_TYPE
8744 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8745 && string_flag)
8746 value.value = orig_value;
8747 /* Otherwise, if we have come to a subaggregate,
8748 and we don't have an element of its type, push into it. */
8749 else if (value.value != 0
8750 && value.value != error_mark_node
8751 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8752 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8753 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8755 push_init_level (loc, 1, braced_init_obstack);
8756 continue;
8759 if (value.value)
8761 push_member_name (constructor_fields);
8762 output_init_element (loc, value.value, value.original_type,
8763 strict_string, fieldtype,
8764 constructor_fields, 1, implicit,
8765 braced_init_obstack);
8766 RESTORE_SPELLING_DEPTH (constructor_depth);
8768 else
8769 /* Do the bookkeeping for an element that was
8770 directly output as a constructor. */
8772 /* For a record, keep track of end position of last field. */
8773 if (DECL_SIZE (constructor_fields))
8774 constructor_bit_index
8775 = size_binop_loc (input_location, PLUS_EXPR,
8776 bit_position (constructor_fields),
8777 DECL_SIZE (constructor_fields));
8779 /* If the current field was the first one not yet written out,
8780 it isn't now, so update. */
8781 if (constructor_unfilled_fields == constructor_fields)
8783 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8784 /* Skip any nameless bit fields. */
8785 while (constructor_unfilled_fields != 0
8786 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8787 && DECL_NAME (constructor_unfilled_fields) == 0)
8788 constructor_unfilled_fields =
8789 DECL_CHAIN (constructor_unfilled_fields);
8793 constructor_fields = DECL_CHAIN (constructor_fields);
8794 /* Skip any nameless bit fields at the beginning. */
8795 while (constructor_fields != 0
8796 && DECL_C_BIT_FIELD (constructor_fields)
8797 && DECL_NAME (constructor_fields) == 0)
8798 constructor_fields = DECL_CHAIN (constructor_fields);
8800 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8802 tree fieldtype;
8803 enum tree_code fieldcode;
8805 if (constructor_fields == 0)
8807 pedwarn_init (loc, 0,
8808 "excess elements in union initializer");
8809 break;
8812 fieldtype = TREE_TYPE (constructor_fields);
8813 if (fieldtype != error_mark_node)
8814 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8815 fieldcode = TREE_CODE (fieldtype);
8817 /* Warn that traditional C rejects initialization of unions.
8818 We skip the warning if the value is zero. This is done
8819 under the assumption that the zero initializer in user
8820 code appears conditioned on e.g. __STDC__ to avoid
8821 "missing initializer" warnings and relies on default
8822 initialization to zero in the traditional C case.
8823 We also skip the warning if the initializer is designated,
8824 again on the assumption that this must be conditional on
8825 __STDC__ anyway (and we've already complained about the
8826 member-designator already). */
8827 if (!in_system_header_at (input_location) && !constructor_designated
8828 && !(value.value && (integer_zerop (value.value)
8829 || real_zerop (value.value))))
8830 warning (OPT_Wtraditional, "traditional C rejects initialization "
8831 "of unions");
8833 /* Accept a string constant to initialize a subarray. */
8834 if (value.value != 0
8835 && fieldcode == ARRAY_TYPE
8836 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8837 && string_flag)
8838 value.value = orig_value;
8839 /* Otherwise, if we have come to a subaggregate,
8840 and we don't have an element of its type, push into it. */
8841 else if (value.value != 0
8842 && value.value != error_mark_node
8843 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8844 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8845 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8847 push_init_level (loc, 1, braced_init_obstack);
8848 continue;
8851 if (value.value)
8853 push_member_name (constructor_fields);
8854 output_init_element (loc, value.value, value.original_type,
8855 strict_string, fieldtype,
8856 constructor_fields, 1, implicit,
8857 braced_init_obstack);
8858 RESTORE_SPELLING_DEPTH (constructor_depth);
8860 else
8861 /* Do the bookkeeping for an element that was
8862 directly output as a constructor. */
8864 constructor_bit_index = DECL_SIZE (constructor_fields);
8865 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8868 constructor_fields = 0;
8870 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8872 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8873 enum tree_code eltcode = TREE_CODE (elttype);
8875 /* Accept a string constant to initialize a subarray. */
8876 if (value.value != 0
8877 && eltcode == ARRAY_TYPE
8878 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8879 && string_flag)
8880 value.value = orig_value;
8881 /* Otherwise, if we have come to a subaggregate,
8882 and we don't have an element of its type, push into it. */
8883 else if (value.value != 0
8884 && value.value != error_mark_node
8885 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8886 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8887 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8889 push_init_level (loc, 1, braced_init_obstack);
8890 continue;
8893 if (constructor_max_index != 0
8894 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8895 || integer_all_onesp (constructor_max_index)))
8897 pedwarn_init (loc, 0,
8898 "excess elements in array initializer");
8899 break;
8902 /* Now output the actual element. */
8903 if (value.value)
8905 push_array_bounds (tree_to_uhwi (constructor_index));
8906 output_init_element (loc, value.value, value.original_type,
8907 strict_string, elttype,
8908 constructor_index, 1, implicit,
8909 braced_init_obstack);
8910 RESTORE_SPELLING_DEPTH (constructor_depth);
8913 constructor_index
8914 = size_binop_loc (input_location, PLUS_EXPR,
8915 constructor_index, bitsize_one_node);
8917 if (!value.value)
8918 /* If we are doing the bookkeeping for an element that was
8919 directly output as a constructor, we must update
8920 constructor_unfilled_index. */
8921 constructor_unfilled_index = constructor_index;
8923 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8925 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8927 /* Do a basic check of initializer size. Note that vectors
8928 always have a fixed size derived from their type. */
8929 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8931 pedwarn_init (loc, 0,
8932 "excess elements in vector initializer");
8933 break;
8936 /* Now output the actual element. */
8937 if (value.value)
8939 if (TREE_CODE (value.value) == VECTOR_CST)
8940 elttype = TYPE_MAIN_VARIANT (constructor_type);
8941 output_init_element (loc, value.value, value.original_type,
8942 strict_string, elttype,
8943 constructor_index, 1, implicit,
8944 braced_init_obstack);
8947 constructor_index
8948 = size_binop_loc (input_location,
8949 PLUS_EXPR, constructor_index, bitsize_one_node);
8951 if (!value.value)
8952 /* If we are doing the bookkeeping for an element that was
8953 directly output as a constructor, we must update
8954 constructor_unfilled_index. */
8955 constructor_unfilled_index = constructor_index;
8958 /* Handle the sole element allowed in a braced initializer
8959 for a scalar variable. */
8960 else if (constructor_type != error_mark_node
8961 && constructor_fields == 0)
8963 pedwarn_init (loc, 0,
8964 "excess elements in scalar initializer");
8965 break;
8967 else
8969 if (value.value)
8970 output_init_element (loc, value.value, value.original_type,
8971 strict_string, constructor_type,
8972 NULL_TREE, 1, implicit,
8973 braced_init_obstack);
8974 constructor_fields = 0;
8977 /* Handle range initializers either at this level or anywhere higher
8978 in the designator stack. */
8979 if (constructor_range_stack)
8981 struct constructor_range_stack *p, *range_stack;
8982 int finish = 0;
8984 range_stack = constructor_range_stack;
8985 constructor_range_stack = 0;
8986 while (constructor_stack != range_stack->stack)
8988 gcc_assert (constructor_stack->implicit);
8989 process_init_element (loc,
8990 pop_init_level (loc, 1,
8991 braced_init_obstack),
8992 true, braced_init_obstack);
8994 for (p = range_stack;
8995 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8996 p = p->prev)
8998 gcc_assert (constructor_stack->implicit);
8999 process_init_element (loc,
9000 pop_init_level (loc, 1,
9001 braced_init_obstack),
9002 true, braced_init_obstack);
9005 p->index = size_binop_loc (input_location,
9006 PLUS_EXPR, p->index, bitsize_one_node);
9007 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9008 finish = 1;
9010 while (1)
9012 constructor_index = p->index;
9013 constructor_fields = p->fields;
9014 if (finish && p->range_end && p->index == p->range_start)
9016 finish = 0;
9017 p->prev = 0;
9019 p = p->next;
9020 if (!p)
9021 break;
9022 push_init_level (loc, 2, braced_init_obstack);
9023 p->stack = constructor_stack;
9024 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9025 p->index = p->range_start;
9028 if (!finish)
9029 constructor_range_stack = range_stack;
9030 continue;
9033 break;
9036 constructor_range_stack = 0;
9039 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9040 (guaranteed to be 'volatile' or null) and ARGS (represented using
9041 an ASM_EXPR node). */
9042 tree
9043 build_asm_stmt (tree cv_qualifier, tree args)
9045 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9046 ASM_VOLATILE_P (args) = 1;
9047 return add_stmt (args);
9050 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9051 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9052 SIMPLE indicates whether there was anything at all after the
9053 string in the asm expression -- asm("blah") and asm("blah" : )
9054 are subtly different. We use a ASM_EXPR node to represent this. */
9055 tree
9056 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9057 tree clobbers, tree labels, bool simple)
9059 tree tail;
9060 tree args;
9061 int i;
9062 const char *constraint;
9063 const char **oconstraints;
9064 bool allows_mem, allows_reg, is_inout;
9065 int ninputs, noutputs;
9067 ninputs = list_length (inputs);
9068 noutputs = list_length (outputs);
9069 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9071 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9073 /* Remove output conversions that change the type but not the mode. */
9074 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9076 tree output = TREE_VALUE (tail);
9078 output = c_fully_fold (output, false, NULL);
9080 /* ??? Really, this should not be here. Users should be using a
9081 proper lvalue, dammit. But there's a long history of using casts
9082 in the output operands. In cases like longlong.h, this becomes a
9083 primitive form of typechecking -- if the cast can be removed, then
9084 the output operand had a type of the proper width; otherwise we'll
9085 get an error. Gross, but ... */
9086 STRIP_NOPS (output);
9088 if (!lvalue_or_else (loc, output, lv_asm))
9089 output = error_mark_node;
9091 if (output != error_mark_node
9092 && (TREE_READONLY (output)
9093 || TYPE_READONLY (TREE_TYPE (output))
9094 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9095 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9096 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9097 readonly_error (loc, output, lv_asm);
9099 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9100 oconstraints[i] = constraint;
9102 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9103 &allows_mem, &allows_reg, &is_inout))
9105 /* If the operand is going to end up in memory,
9106 mark it addressable. */
9107 if (!allows_reg && !c_mark_addressable (output))
9108 output = error_mark_node;
9109 if (!(!allows_reg && allows_mem)
9110 && output != error_mark_node
9111 && VOID_TYPE_P (TREE_TYPE (output)))
9113 error_at (loc, "invalid use of void expression");
9114 output = error_mark_node;
9117 else
9118 output = error_mark_node;
9120 TREE_VALUE (tail) = output;
9123 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9125 tree input;
9127 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9128 input = TREE_VALUE (tail);
9130 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9131 oconstraints, &allows_mem, &allows_reg))
9133 /* If the operand is going to end up in memory,
9134 mark it addressable. */
9135 if (!allows_reg && allows_mem)
9137 input = c_fully_fold (input, false, NULL);
9139 /* Strip the nops as we allow this case. FIXME, this really
9140 should be rejected or made deprecated. */
9141 STRIP_NOPS (input);
9142 if (!c_mark_addressable (input))
9143 input = error_mark_node;
9145 else
9147 struct c_expr expr;
9148 memset (&expr, 0, sizeof (expr));
9149 expr.value = input;
9150 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9151 input = c_fully_fold (expr.value, false, NULL);
9153 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9155 error_at (loc, "invalid use of void expression");
9156 input = error_mark_node;
9160 else
9161 input = error_mark_node;
9163 TREE_VALUE (tail) = input;
9166 /* ASMs with labels cannot have outputs. This should have been
9167 enforced by the parser. */
9168 gcc_assert (outputs == NULL || labels == NULL);
9170 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9172 /* asm statements without outputs, including simple ones, are treated
9173 as volatile. */
9174 ASM_INPUT_P (args) = simple;
9175 ASM_VOLATILE_P (args) = (noutputs == 0);
9177 return args;
9180 /* Generate a goto statement to LABEL. LOC is the location of the
9181 GOTO. */
9183 tree
9184 c_finish_goto_label (location_t loc, tree label)
9186 tree decl = lookup_label_for_goto (loc, label);
9187 if (!decl)
9188 return NULL_TREE;
9189 TREE_USED (decl) = 1;
9191 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9192 SET_EXPR_LOCATION (t, loc);
9193 return add_stmt (t);
9197 /* Generate a computed goto statement to EXPR. LOC is the location of
9198 the GOTO. */
9200 tree
9201 c_finish_goto_ptr (location_t loc, tree expr)
9203 tree t;
9204 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9205 expr = c_fully_fold (expr, false, NULL);
9206 expr = convert (ptr_type_node, expr);
9207 t = build1 (GOTO_EXPR, void_type_node, expr);
9208 SET_EXPR_LOCATION (t, loc);
9209 return add_stmt (t);
9212 /* Generate a C `return' statement. RETVAL is the expression for what
9213 to return, or a null pointer for `return;' with no value. LOC is
9214 the location of the return statement, or the location of the expression,
9215 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9216 is the original type of RETVAL. */
9218 tree
9219 c_finish_return (location_t loc, tree retval, tree origtype)
9221 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9222 bool no_warning = false;
9223 bool npc = false;
9224 size_t rank = 0;
9226 if (TREE_THIS_VOLATILE (current_function_decl))
9227 warning_at (loc, 0,
9228 "function declared %<noreturn%> has a %<return%> statement");
9230 if (flag_cilkplus && contains_array_notation_expr (retval))
9232 /* Array notations are allowed in a return statement if it is inside a
9233 built-in array notation reduction function. */
9234 if (!find_rank (loc, retval, retval, false, &rank))
9235 return error_mark_node;
9236 if (rank >= 1)
9238 error_at (loc, "array notation expression cannot be used as a "
9239 "return value");
9240 return error_mark_node;
9243 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9245 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9246 "allowed");
9247 return error_mark_node;
9249 if (retval)
9251 tree semantic_type = NULL_TREE;
9252 npc = null_pointer_constant_p (retval);
9253 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9255 semantic_type = TREE_TYPE (retval);
9256 retval = TREE_OPERAND (retval, 0);
9258 retval = c_fully_fold (retval, false, NULL);
9259 if (semantic_type)
9260 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9263 if (!retval)
9265 current_function_returns_null = 1;
9266 if ((warn_return_type || flag_isoc99)
9267 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9269 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
9270 "%<return%> with no value, in "
9271 "function returning non-void");
9272 no_warning = true;
9275 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9277 current_function_returns_null = 1;
9278 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9279 pedwarn (loc, 0,
9280 "%<return%> with a value, in function returning void");
9281 else
9282 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9283 "%<return%> with expression, in function returning void");
9285 else
9287 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9288 retval, origtype, ic_return,
9289 npc, NULL_TREE, NULL_TREE, 0);
9290 tree res = DECL_RESULT (current_function_decl);
9291 tree inner;
9292 bool save;
9294 current_function_returns_value = 1;
9295 if (t == error_mark_node)
9296 return NULL_TREE;
9298 save = in_late_binary_op;
9299 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9300 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9301 in_late_binary_op = true;
9302 inner = t = convert (TREE_TYPE (res), t);
9303 in_late_binary_op = save;
9305 /* Strip any conversions, additions, and subtractions, and see if
9306 we are returning the address of a local variable. Warn if so. */
9307 while (1)
9309 switch (TREE_CODE (inner))
9311 CASE_CONVERT:
9312 case NON_LVALUE_EXPR:
9313 case PLUS_EXPR:
9314 case POINTER_PLUS_EXPR:
9315 inner = TREE_OPERAND (inner, 0);
9316 continue;
9318 case MINUS_EXPR:
9319 /* If the second operand of the MINUS_EXPR has a pointer
9320 type (or is converted from it), this may be valid, so
9321 don't give a warning. */
9323 tree op1 = TREE_OPERAND (inner, 1);
9325 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9326 && (CONVERT_EXPR_P (op1)
9327 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9328 op1 = TREE_OPERAND (op1, 0);
9330 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9331 break;
9333 inner = TREE_OPERAND (inner, 0);
9334 continue;
9337 case ADDR_EXPR:
9338 inner = TREE_OPERAND (inner, 0);
9340 while (REFERENCE_CLASS_P (inner)
9341 && TREE_CODE (inner) != INDIRECT_REF)
9342 inner = TREE_OPERAND (inner, 0);
9344 if (DECL_P (inner)
9345 && !DECL_EXTERNAL (inner)
9346 && !TREE_STATIC (inner)
9347 && DECL_CONTEXT (inner) == current_function_decl)
9349 if (TREE_CODE (inner) == LABEL_DECL)
9350 warning_at (loc, OPT_Wreturn_local_addr,
9351 "function returns address of label");
9352 else
9354 warning_at (loc, OPT_Wreturn_local_addr,
9355 "function returns address of local variable");
9356 tree zero = build_zero_cst (TREE_TYPE (res));
9357 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9360 break;
9362 default:
9363 break;
9366 break;
9369 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9370 SET_EXPR_LOCATION (retval, loc);
9372 if (warn_sequence_point)
9373 verify_sequence_points (retval);
9376 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9377 TREE_NO_WARNING (ret_stmt) |= no_warning;
9378 return add_stmt (ret_stmt);
9381 struct c_switch {
9382 /* The SWITCH_EXPR being built. */
9383 tree switch_expr;
9385 /* The original type of the testing expression, i.e. before the
9386 default conversion is applied. */
9387 tree orig_type;
9389 /* A splay-tree mapping the low element of a case range to the high
9390 element, or NULL_TREE if there is no high element. Used to
9391 determine whether or not a new case label duplicates an old case
9392 label. We need a tree, rather than simply a hash table, because
9393 of the GNU case range extension. */
9394 splay_tree cases;
9396 /* The bindings at the point of the switch. This is used for
9397 warnings crossing decls when branching to a case label. */
9398 struct c_spot_bindings *bindings;
9400 /* The next node on the stack. */
9401 struct c_switch *next;
9404 /* A stack of the currently active switch statements. The innermost
9405 switch statement is on the top of the stack. There is no need to
9406 mark the stack for garbage collection because it is only active
9407 during the processing of the body of a function, and we never
9408 collect at that point. */
9410 struct c_switch *c_switch_stack;
9412 /* Start a C switch statement, testing expression EXP. Return the new
9413 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9414 SWITCH_COND_LOC is the location of the switch's condition.
9415 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9417 tree
9418 c_start_case (location_t switch_loc,
9419 location_t switch_cond_loc,
9420 tree exp, bool explicit_cast_p)
9422 tree orig_type = error_mark_node;
9423 struct c_switch *cs;
9425 if (exp != error_mark_node)
9427 orig_type = TREE_TYPE (exp);
9429 if (!INTEGRAL_TYPE_P (orig_type))
9431 if (orig_type != error_mark_node)
9433 error_at (switch_cond_loc, "switch quantity not an integer");
9434 orig_type = error_mark_node;
9436 exp = integer_zero_node;
9438 else
9440 tree type = TYPE_MAIN_VARIANT (orig_type);
9441 tree e = exp;
9443 /* Warn if the condition has boolean value. */
9444 while (TREE_CODE (e) == COMPOUND_EXPR)
9445 e = TREE_OPERAND (e, 1);
9447 if ((TREE_CODE (type) == BOOLEAN_TYPE
9448 || truth_value_p (TREE_CODE (e)))
9449 /* Explicit cast to int suppresses this warning. */
9450 && !(TREE_CODE (type) == INTEGER_TYPE
9451 && explicit_cast_p))
9452 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9453 "switch condition has boolean value");
9455 if (!in_system_header_at (input_location)
9456 && (type == long_integer_type_node
9457 || type == long_unsigned_type_node))
9458 warning_at (switch_cond_loc,
9459 OPT_Wtraditional, "%<long%> switch expression not "
9460 "converted to %<int%> in ISO C");
9462 exp = c_fully_fold (exp, false, NULL);
9463 exp = default_conversion (exp);
9465 if (warn_sequence_point)
9466 verify_sequence_points (exp);
9470 /* Add this new SWITCH_EXPR to the stack. */
9471 cs = XNEW (struct c_switch);
9472 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9473 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9474 cs->orig_type = orig_type;
9475 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9476 cs->bindings = c_get_switch_bindings ();
9477 cs->next = c_switch_stack;
9478 c_switch_stack = cs;
9480 return add_stmt (cs->switch_expr);
9483 /* Process a case label at location LOC. */
9485 tree
9486 do_case (location_t loc, tree low_value, tree high_value)
9488 tree label = NULL_TREE;
9490 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9492 low_value = c_fully_fold (low_value, false, NULL);
9493 if (TREE_CODE (low_value) == INTEGER_CST)
9494 pedwarn (loc, OPT_Wpedantic,
9495 "case label is not an integer constant expression");
9498 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9500 high_value = c_fully_fold (high_value, false, NULL);
9501 if (TREE_CODE (high_value) == INTEGER_CST)
9502 pedwarn (input_location, OPT_Wpedantic,
9503 "case label is not an integer constant expression");
9506 if (c_switch_stack == NULL)
9508 if (low_value)
9509 error_at (loc, "case label not within a switch statement");
9510 else
9511 error_at (loc, "%<default%> label not within a switch statement");
9512 return NULL_TREE;
9515 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9516 EXPR_LOCATION (c_switch_stack->switch_expr),
9517 loc))
9518 return NULL_TREE;
9520 label = c_add_case_label (loc, c_switch_stack->cases,
9521 SWITCH_COND (c_switch_stack->switch_expr),
9522 c_switch_stack->orig_type,
9523 low_value, high_value);
9524 if (label == error_mark_node)
9525 label = NULL_TREE;
9526 return label;
9529 /* Finish the switch statement. */
9531 void
9532 c_finish_case (tree body)
9534 struct c_switch *cs = c_switch_stack;
9535 location_t switch_location;
9537 SWITCH_BODY (cs->switch_expr) = body;
9539 /* Emit warnings as needed. */
9540 switch_location = EXPR_LOCATION (cs->switch_expr);
9541 c_do_switch_warnings (cs->cases, switch_location,
9542 TREE_TYPE (cs->switch_expr),
9543 SWITCH_COND (cs->switch_expr));
9545 /* Pop the stack. */
9546 c_switch_stack = cs->next;
9547 splay_tree_delete (cs->cases);
9548 c_release_switch_bindings (cs->bindings);
9549 XDELETE (cs);
9552 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9553 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9554 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9555 statement, and was not surrounded with parenthesis. */
9557 void
9558 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9559 tree else_block, bool nested_if)
9561 tree stmt;
9563 /* If the condition has array notations, then the rank of the then_block and
9564 else_block must be either 0 or be equal to the rank of the condition. If
9565 the condition does not have array notations then break them up as it is
9566 broken up in a normal expression. */
9567 if (flag_cilkplus && contains_array_notation_expr (cond))
9569 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9570 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9571 return;
9572 if (then_block
9573 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9574 return;
9575 if (else_block
9576 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9577 return;
9578 if (cond_rank != then_rank && then_rank != 0)
9580 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9581 " and the then-block");
9582 return;
9584 else if (cond_rank != else_rank && else_rank != 0)
9586 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9587 " and the else-block");
9588 return;
9591 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9592 if (warn_parentheses && nested_if && else_block == NULL)
9594 tree inner_if = then_block;
9596 /* We know from the grammar productions that there is an IF nested
9597 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9598 it might not be exactly THEN_BLOCK, but should be the last
9599 non-container statement within. */
9600 while (1)
9601 switch (TREE_CODE (inner_if))
9603 case COND_EXPR:
9604 goto found;
9605 case BIND_EXPR:
9606 inner_if = BIND_EXPR_BODY (inner_if);
9607 break;
9608 case STATEMENT_LIST:
9609 inner_if = expr_last (then_block);
9610 break;
9611 case TRY_FINALLY_EXPR:
9612 case TRY_CATCH_EXPR:
9613 inner_if = TREE_OPERAND (inner_if, 0);
9614 break;
9615 default:
9616 gcc_unreachable ();
9618 found:
9620 if (COND_EXPR_ELSE (inner_if))
9621 warning_at (if_locus, OPT_Wparentheses,
9622 "suggest explicit braces to avoid ambiguous %<else%>");
9625 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9626 SET_EXPR_LOCATION (stmt, if_locus);
9627 add_stmt (stmt);
9630 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9631 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9632 is false for DO loops. INCR is the FOR increment expression. BODY is
9633 the statement controlled by the loop. BLAB is the break label. CLAB is
9634 the continue label. Everything is allowed to be NULL. */
9636 void
9637 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9638 tree blab, tree clab, bool cond_is_first)
9640 tree entry = NULL, exit = NULL, t;
9642 if (flag_cilkplus && contains_array_notation_expr (cond))
9644 error_at (start_locus, "array notation expression cannot be used in a "
9645 "loop%'s condition");
9646 return;
9649 /* If the condition is zero don't generate a loop construct. */
9650 if (cond && integer_zerop (cond))
9652 if (cond_is_first)
9654 t = build_and_jump (&blab);
9655 SET_EXPR_LOCATION (t, start_locus);
9656 add_stmt (t);
9659 else
9661 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9663 /* If we have an exit condition, then we build an IF with gotos either
9664 out of the loop, or to the top of it. If there's no exit condition,
9665 then we just build a jump back to the top. */
9666 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9668 if (cond && !integer_nonzerop (cond))
9670 /* Canonicalize the loop condition to the end. This means
9671 generating a branch to the loop condition. Reuse the
9672 continue label, if possible. */
9673 if (cond_is_first)
9675 if (incr || !clab)
9677 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9678 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9680 else
9681 t = build1 (GOTO_EXPR, void_type_node, clab);
9682 SET_EXPR_LOCATION (t, start_locus);
9683 add_stmt (t);
9686 t = build_and_jump (&blab);
9687 if (cond_is_first)
9688 exit = fold_build3_loc (start_locus,
9689 COND_EXPR, void_type_node, cond, exit, t);
9690 else
9691 exit = fold_build3_loc (input_location,
9692 COND_EXPR, void_type_node, cond, exit, t);
9695 add_stmt (top);
9698 if (body)
9699 add_stmt (body);
9700 if (clab)
9701 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9702 if (incr)
9703 add_stmt (incr);
9704 if (entry)
9705 add_stmt (entry);
9706 if (exit)
9707 add_stmt (exit);
9708 if (blab)
9709 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9712 tree
9713 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9715 bool skip;
9716 tree label = *label_p;
9718 /* In switch statements break is sometimes stylistically used after
9719 a return statement. This can lead to spurious warnings about
9720 control reaching the end of a non-void function when it is
9721 inlined. Note that we are calling block_may_fallthru with
9722 language specific tree nodes; this works because
9723 block_may_fallthru returns true when given something it does not
9724 understand. */
9725 skip = !block_may_fallthru (cur_stmt_list);
9727 if (!label)
9729 if (!skip)
9730 *label_p = label = create_artificial_label (loc);
9732 else if (TREE_CODE (label) == LABEL_DECL)
9734 else switch (TREE_INT_CST_LOW (label))
9736 case 0:
9737 if (is_break)
9738 error_at (loc, "break statement not within loop or switch");
9739 else
9740 error_at (loc, "continue statement not within a loop");
9741 return NULL_TREE;
9743 case 1:
9744 gcc_assert (is_break);
9745 error_at (loc, "break statement used with OpenMP for loop");
9746 return NULL_TREE;
9748 case 2:
9749 if (is_break)
9750 error ("break statement within %<#pragma simd%> loop body");
9751 else
9752 error ("continue statement within %<#pragma simd%> loop body");
9753 return NULL_TREE;
9755 default:
9756 gcc_unreachable ();
9759 if (skip)
9760 return NULL_TREE;
9762 if (!is_break)
9763 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9765 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9768 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9770 static void
9771 emit_side_effect_warnings (location_t loc, tree expr)
9773 if (expr == error_mark_node)
9775 else if (!TREE_SIDE_EFFECTS (expr))
9777 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9778 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9780 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9782 tree r = expr;
9783 location_t cloc = loc;
9784 while (TREE_CODE (r) == COMPOUND_EXPR)
9786 if (EXPR_HAS_LOCATION (r))
9787 cloc = EXPR_LOCATION (r);
9788 r = TREE_OPERAND (r, 1);
9790 if (!TREE_SIDE_EFFECTS (r)
9791 && !VOID_TYPE_P (TREE_TYPE (r))
9792 && !CONVERT_EXPR_P (r)
9793 && !TREE_NO_WARNING (r)
9794 && !TREE_NO_WARNING (expr))
9795 warning_at (cloc, OPT_Wunused_value,
9796 "right-hand operand of comma expression has no effect");
9798 else
9799 warn_if_unused_value (expr, loc);
9802 /* Process an expression as if it were a complete statement. Emit
9803 diagnostics, but do not call ADD_STMT. LOC is the location of the
9804 statement. */
9806 tree
9807 c_process_expr_stmt (location_t loc, tree expr)
9809 tree exprv;
9811 if (!expr)
9812 return NULL_TREE;
9814 expr = c_fully_fold (expr, false, NULL);
9816 if (warn_sequence_point)
9817 verify_sequence_points (expr);
9819 if (TREE_TYPE (expr) != error_mark_node
9820 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9821 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9822 error_at (loc, "expression statement has incomplete type");
9824 /* If we're not processing a statement expression, warn about unused values.
9825 Warnings for statement expressions will be emitted later, once we figure
9826 out which is the result. */
9827 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9828 && warn_unused_value)
9829 emit_side_effect_warnings (loc, expr);
9831 exprv = expr;
9832 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9833 exprv = TREE_OPERAND (exprv, 1);
9834 while (CONVERT_EXPR_P (exprv))
9835 exprv = TREE_OPERAND (exprv, 0);
9836 if (DECL_P (exprv)
9837 || handled_component_p (exprv)
9838 || TREE_CODE (exprv) == ADDR_EXPR)
9839 mark_exp_read (exprv);
9841 /* If the expression is not of a type to which we cannot assign a line
9842 number, wrap the thing in a no-op NOP_EXPR. */
9843 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9845 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9846 SET_EXPR_LOCATION (expr, loc);
9849 return expr;
9852 /* Emit an expression as a statement. LOC is the location of the
9853 expression. */
9855 tree
9856 c_finish_expr_stmt (location_t loc, tree expr)
9858 if (expr)
9859 return add_stmt (c_process_expr_stmt (loc, expr));
9860 else
9861 return NULL;
9864 /* Do the opposite and emit a statement as an expression. To begin,
9865 create a new binding level and return it. */
9867 tree
9868 c_begin_stmt_expr (void)
9870 tree ret;
9872 /* We must force a BLOCK for this level so that, if it is not expanded
9873 later, there is a way to turn off the entire subtree of blocks that
9874 are contained in it. */
9875 keep_next_level ();
9876 ret = c_begin_compound_stmt (true);
9878 c_bindings_start_stmt_expr (c_switch_stack == NULL
9879 ? NULL
9880 : c_switch_stack->bindings);
9882 /* Mark the current statement list as belonging to a statement list. */
9883 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9885 return ret;
9888 /* LOC is the location of the compound statement to which this body
9889 belongs. */
9891 tree
9892 c_finish_stmt_expr (location_t loc, tree body)
9894 tree last, type, tmp, val;
9895 tree *last_p;
9897 body = c_end_compound_stmt (loc, body, true);
9899 c_bindings_end_stmt_expr (c_switch_stack == NULL
9900 ? NULL
9901 : c_switch_stack->bindings);
9903 /* Locate the last statement in BODY. See c_end_compound_stmt
9904 about always returning a BIND_EXPR. */
9905 last_p = &BIND_EXPR_BODY (body);
9906 last = BIND_EXPR_BODY (body);
9908 continue_searching:
9909 if (TREE_CODE (last) == STATEMENT_LIST)
9911 tree_stmt_iterator i;
9913 /* This can happen with degenerate cases like ({ }). No value. */
9914 if (!TREE_SIDE_EFFECTS (last))
9915 return body;
9917 /* If we're supposed to generate side effects warnings, process
9918 all of the statements except the last. */
9919 if (warn_unused_value)
9921 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9923 location_t tloc;
9924 tree t = tsi_stmt (i);
9926 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9927 emit_side_effect_warnings (tloc, t);
9930 else
9931 i = tsi_last (last);
9932 last_p = tsi_stmt_ptr (i);
9933 last = *last_p;
9936 /* If the end of the list is exception related, then the list was split
9937 by a call to push_cleanup. Continue searching. */
9938 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9939 || TREE_CODE (last) == TRY_CATCH_EXPR)
9941 last_p = &TREE_OPERAND (last, 0);
9942 last = *last_p;
9943 goto continue_searching;
9946 if (last == error_mark_node)
9947 return last;
9949 /* In the case that the BIND_EXPR is not necessary, return the
9950 expression out from inside it. */
9951 if (last == BIND_EXPR_BODY (body)
9952 && BIND_EXPR_VARS (body) == NULL)
9954 /* Even if this looks constant, do not allow it in a constant
9955 expression. */
9956 last = c_wrap_maybe_const (last, true);
9957 /* Do not warn if the return value of a statement expression is
9958 unused. */
9959 TREE_NO_WARNING (last) = 1;
9960 return last;
9963 /* Extract the type of said expression. */
9964 type = TREE_TYPE (last);
9966 /* If we're not returning a value at all, then the BIND_EXPR that
9967 we already have is a fine expression to return. */
9968 if (!type || VOID_TYPE_P (type))
9969 return body;
9971 /* Now that we've located the expression containing the value, it seems
9972 silly to make voidify_wrapper_expr repeat the process. Create a
9973 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9974 tmp = create_tmp_var_raw (type, NULL);
9976 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9977 tree_expr_nonnegative_p giving up immediately. */
9978 val = last;
9979 if (TREE_CODE (val) == NOP_EXPR
9980 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9981 val = TREE_OPERAND (val, 0);
9983 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9984 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9987 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9988 SET_EXPR_LOCATION (t, loc);
9989 return t;
9993 /* Begin and end compound statements. This is as simple as pushing
9994 and popping new statement lists from the tree. */
9996 tree
9997 c_begin_compound_stmt (bool do_scope)
9999 tree stmt = push_stmt_list ();
10000 if (do_scope)
10001 push_scope ();
10002 return stmt;
10005 /* End a compound statement. STMT is the statement. LOC is the
10006 location of the compound statement-- this is usually the location
10007 of the opening brace. */
10009 tree
10010 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10012 tree block = NULL;
10014 if (do_scope)
10016 if (c_dialect_objc ())
10017 objc_clear_super_receiver ();
10018 block = pop_scope ();
10021 stmt = pop_stmt_list (stmt);
10022 stmt = c_build_bind_expr (loc, block, stmt);
10024 /* If this compound statement is nested immediately inside a statement
10025 expression, then force a BIND_EXPR to be created. Otherwise we'll
10026 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10027 STATEMENT_LISTs merge, and thus we can lose track of what statement
10028 was really last. */
10029 if (building_stmt_list_p ()
10030 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10031 && TREE_CODE (stmt) != BIND_EXPR)
10033 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10034 TREE_SIDE_EFFECTS (stmt) = 1;
10035 SET_EXPR_LOCATION (stmt, loc);
10038 return stmt;
10041 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10042 when the current scope is exited. EH_ONLY is true when this is not
10043 meant to apply to normal control flow transfer. */
10045 void
10046 push_cleanup (tree decl, tree cleanup, bool eh_only)
10048 enum tree_code code;
10049 tree stmt, list;
10050 bool stmt_expr;
10052 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10053 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10054 add_stmt (stmt);
10055 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10056 list = push_stmt_list ();
10057 TREE_OPERAND (stmt, 0) = list;
10058 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10061 /* Build a binary-operation expression without default conversions.
10062 CODE is the kind of expression to build.
10063 LOCATION is the operator's location.
10064 This function differs from `build' in several ways:
10065 the data type of the result is computed and recorded in it,
10066 warnings are generated if arg data types are invalid,
10067 special handling for addition and subtraction of pointers is known,
10068 and some optimization is done (operations on narrow ints
10069 are done in the narrower type when that gives the same result).
10070 Constant folding is also done before the result is returned.
10072 Note that the operands will never have enumeral types, or function
10073 or array types, because either they will have the default conversions
10074 performed or they have both just been converted to some other type in which
10075 the arithmetic is to be done. */
10077 tree
10078 build_binary_op (location_t location, enum tree_code code,
10079 tree orig_op0, tree orig_op1, int convert_p)
10081 tree type0, type1, orig_type0, orig_type1;
10082 tree eptype;
10083 enum tree_code code0, code1;
10084 tree op0, op1;
10085 tree ret = error_mark_node;
10086 const char *invalid_op_diag;
10087 bool op0_int_operands, op1_int_operands;
10088 bool int_const, int_const_or_overflow, int_operands;
10090 /* Expression code to give to the expression when it is built.
10091 Normally this is CODE, which is what the caller asked for,
10092 but in some special cases we change it. */
10093 enum tree_code resultcode = code;
10095 /* Data type in which the computation is to be performed.
10096 In the simplest cases this is the common type of the arguments. */
10097 tree result_type = NULL;
10099 /* When the computation is in excess precision, the type of the
10100 final EXCESS_PRECISION_EXPR. */
10101 tree semantic_result_type = NULL;
10103 /* Nonzero means operands have already been type-converted
10104 in whatever way is necessary.
10105 Zero means they need to be converted to RESULT_TYPE. */
10106 int converted = 0;
10108 /* Nonzero means create the expression with this type, rather than
10109 RESULT_TYPE. */
10110 tree build_type = 0;
10112 /* Nonzero means after finally constructing the expression
10113 convert it to this type. */
10114 tree final_type = 0;
10116 /* Nonzero if this is an operation like MIN or MAX which can
10117 safely be computed in short if both args are promoted shorts.
10118 Also implies COMMON.
10119 -1 indicates a bitwise operation; this makes a difference
10120 in the exact conditions for when it is safe to do the operation
10121 in a narrower mode. */
10122 int shorten = 0;
10124 /* Nonzero if this is a comparison operation;
10125 if both args are promoted shorts, compare the original shorts.
10126 Also implies COMMON. */
10127 int short_compare = 0;
10129 /* Nonzero if this is a right-shift operation, which can be computed on the
10130 original short and then promoted if the operand is a promoted short. */
10131 int short_shift = 0;
10133 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10134 int common = 0;
10136 /* True means types are compatible as far as ObjC is concerned. */
10137 bool objc_ok;
10139 /* True means this is an arithmetic operation that may need excess
10140 precision. */
10141 bool may_need_excess_precision;
10143 /* True means this is a boolean operation that converts both its
10144 operands to truth-values. */
10145 bool boolean_op = false;
10147 /* Remember whether we're doing / or %. */
10148 bool doing_div_or_mod = false;
10150 /* Remember whether we're doing << or >>. */
10151 bool doing_shift = false;
10153 /* Tree holding instrumentation expression. */
10154 tree instrument_expr = NULL;
10156 if (location == UNKNOWN_LOCATION)
10157 location = input_location;
10159 op0 = orig_op0;
10160 op1 = orig_op1;
10162 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10163 if (op0_int_operands)
10164 op0 = remove_c_maybe_const_expr (op0);
10165 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10166 if (op1_int_operands)
10167 op1 = remove_c_maybe_const_expr (op1);
10168 int_operands = (op0_int_operands && op1_int_operands);
10169 if (int_operands)
10171 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10172 && TREE_CODE (orig_op1) == INTEGER_CST);
10173 int_const = (int_const_or_overflow
10174 && !TREE_OVERFLOW (orig_op0)
10175 && !TREE_OVERFLOW (orig_op1));
10177 else
10178 int_const = int_const_or_overflow = false;
10180 /* Do not apply default conversion in mixed vector/scalar expression. */
10181 if (convert_p
10182 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10183 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10185 op0 = default_conversion (op0);
10186 op1 = default_conversion (op1);
10189 /* When Cilk Plus is enabled and there are array notations inside op0, then
10190 we check to see if there are builtin array notation functions. If
10191 so, then we take on the type of the array notation inside it. */
10192 if (flag_cilkplus && contains_array_notation_expr (op0))
10193 orig_type0 = type0 = find_correct_array_notation_type (op0);
10194 else
10195 orig_type0 = type0 = TREE_TYPE (op0);
10197 if (flag_cilkplus && contains_array_notation_expr (op1))
10198 orig_type1 = type1 = find_correct_array_notation_type (op1);
10199 else
10200 orig_type1 = type1 = TREE_TYPE (op1);
10202 /* The expression codes of the data types of the arguments tell us
10203 whether the arguments are integers, floating, pointers, etc. */
10204 code0 = TREE_CODE (type0);
10205 code1 = TREE_CODE (type1);
10207 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10208 STRIP_TYPE_NOPS (op0);
10209 STRIP_TYPE_NOPS (op1);
10211 /* If an error was already reported for one of the arguments,
10212 avoid reporting another error. */
10214 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10215 return error_mark_node;
10217 if ((invalid_op_diag
10218 = targetm.invalid_binary_op (code, type0, type1)))
10220 error_at (location, invalid_op_diag);
10221 return error_mark_node;
10224 switch (code)
10226 case PLUS_EXPR:
10227 case MINUS_EXPR:
10228 case MULT_EXPR:
10229 case TRUNC_DIV_EXPR:
10230 case CEIL_DIV_EXPR:
10231 case FLOOR_DIV_EXPR:
10232 case ROUND_DIV_EXPR:
10233 case EXACT_DIV_EXPR:
10234 may_need_excess_precision = true;
10235 break;
10236 default:
10237 may_need_excess_precision = false;
10238 break;
10240 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10242 op0 = TREE_OPERAND (op0, 0);
10243 type0 = TREE_TYPE (op0);
10245 else if (may_need_excess_precision
10246 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10248 type0 = eptype;
10249 op0 = convert (eptype, op0);
10251 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10253 op1 = TREE_OPERAND (op1, 0);
10254 type1 = TREE_TYPE (op1);
10256 else if (may_need_excess_precision
10257 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10259 type1 = eptype;
10260 op1 = convert (eptype, op1);
10263 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10265 /* In case when one of the operands of the binary operation is
10266 a vector and another is a scalar -- convert scalar to vector. */
10267 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10269 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10270 true);
10272 switch (convert_flag)
10274 case stv_error:
10275 return error_mark_node;
10276 case stv_firstarg:
10278 bool maybe_const = true;
10279 tree sc;
10280 sc = c_fully_fold (op0, false, &maybe_const);
10281 sc = save_expr (sc);
10282 sc = convert (TREE_TYPE (type1), sc);
10283 op0 = build_vector_from_val (type1, sc);
10284 if (!maybe_const)
10285 op0 = c_wrap_maybe_const (op0, true);
10286 orig_type0 = type0 = TREE_TYPE (op0);
10287 code0 = TREE_CODE (type0);
10288 converted = 1;
10289 break;
10291 case stv_secondarg:
10293 bool maybe_const = true;
10294 tree sc;
10295 sc = c_fully_fold (op1, false, &maybe_const);
10296 sc = save_expr (sc);
10297 sc = convert (TREE_TYPE (type0), sc);
10298 op1 = build_vector_from_val (type0, sc);
10299 if (!maybe_const)
10300 op1 = c_wrap_maybe_const (op1, true);
10301 orig_type1 = type1 = TREE_TYPE (op1);
10302 code1 = TREE_CODE (type1);
10303 converted = 1;
10304 break;
10306 default:
10307 break;
10311 switch (code)
10313 case PLUS_EXPR:
10314 /* Handle the pointer + int case. */
10315 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10317 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10318 goto return_build_binary_op;
10320 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10322 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10323 goto return_build_binary_op;
10325 else
10326 common = 1;
10327 break;
10329 case MINUS_EXPR:
10330 /* Subtraction of two similar pointers.
10331 We must subtract them as integers, then divide by object size. */
10332 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10333 && comp_target_types (location, type0, type1))
10335 ret = pointer_diff (location, op0, op1);
10336 goto return_build_binary_op;
10338 /* Handle pointer minus int. Just like pointer plus int. */
10339 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10341 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10342 goto return_build_binary_op;
10344 else
10345 common = 1;
10346 break;
10348 case MULT_EXPR:
10349 common = 1;
10350 break;
10352 case TRUNC_DIV_EXPR:
10353 case CEIL_DIV_EXPR:
10354 case FLOOR_DIV_EXPR:
10355 case ROUND_DIV_EXPR:
10356 case EXACT_DIV_EXPR:
10357 doing_div_or_mod = true;
10358 warn_for_div_by_zero (location, op1);
10360 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10361 || code0 == FIXED_POINT_TYPE
10362 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10363 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10364 || code1 == FIXED_POINT_TYPE
10365 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10367 enum tree_code tcode0 = code0, tcode1 = code1;
10369 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10370 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10371 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10372 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10374 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10375 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10376 resultcode = RDIV_EXPR;
10377 else
10378 /* Although it would be tempting to shorten always here, that
10379 loses on some targets, since the modulo instruction is
10380 undefined if the quotient can't be represented in the
10381 computation mode. We shorten only if unsigned or if
10382 dividing by something we know != -1. */
10383 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10384 || (TREE_CODE (op1) == INTEGER_CST
10385 && !integer_all_onesp (op1)));
10386 common = 1;
10388 break;
10390 case BIT_AND_EXPR:
10391 case BIT_IOR_EXPR:
10392 case BIT_XOR_EXPR:
10393 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10394 shorten = -1;
10395 /* Allow vector types which are not floating point types. */
10396 else if (code0 == VECTOR_TYPE
10397 && code1 == VECTOR_TYPE
10398 && !VECTOR_FLOAT_TYPE_P (type0)
10399 && !VECTOR_FLOAT_TYPE_P (type1))
10400 common = 1;
10401 break;
10403 case TRUNC_MOD_EXPR:
10404 case FLOOR_MOD_EXPR:
10405 doing_div_or_mod = true;
10406 warn_for_div_by_zero (location, op1);
10408 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10409 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10410 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10411 common = 1;
10412 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10414 /* Although it would be tempting to shorten always here, that loses
10415 on some targets, since the modulo instruction is undefined if the
10416 quotient can't be represented in the computation mode. We shorten
10417 only if unsigned or if dividing by something we know != -1. */
10418 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10419 || (TREE_CODE (op1) == INTEGER_CST
10420 && !integer_all_onesp (op1)));
10421 common = 1;
10423 break;
10425 case TRUTH_ANDIF_EXPR:
10426 case TRUTH_ORIF_EXPR:
10427 case TRUTH_AND_EXPR:
10428 case TRUTH_OR_EXPR:
10429 case TRUTH_XOR_EXPR:
10430 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10431 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10432 || code0 == FIXED_POINT_TYPE)
10433 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10434 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10435 || code1 == FIXED_POINT_TYPE))
10437 /* Result of these operations is always an int,
10438 but that does not mean the operands should be
10439 converted to ints! */
10440 result_type = integer_type_node;
10441 if (op0_int_operands)
10443 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10444 op0 = remove_c_maybe_const_expr (op0);
10446 else
10447 op0 = c_objc_common_truthvalue_conversion (location, op0);
10448 if (op1_int_operands)
10450 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10451 op1 = remove_c_maybe_const_expr (op1);
10453 else
10454 op1 = c_objc_common_truthvalue_conversion (location, op1);
10455 converted = 1;
10456 boolean_op = true;
10458 if (code == TRUTH_ANDIF_EXPR)
10460 int_const_or_overflow = (int_operands
10461 && TREE_CODE (orig_op0) == INTEGER_CST
10462 && (op0 == truthvalue_false_node
10463 || TREE_CODE (orig_op1) == INTEGER_CST));
10464 int_const = (int_const_or_overflow
10465 && !TREE_OVERFLOW (orig_op0)
10466 && (op0 == truthvalue_false_node
10467 || !TREE_OVERFLOW (orig_op1)));
10469 else if (code == TRUTH_ORIF_EXPR)
10471 int_const_or_overflow = (int_operands
10472 && TREE_CODE (orig_op0) == INTEGER_CST
10473 && (op0 == truthvalue_true_node
10474 || TREE_CODE (orig_op1) == INTEGER_CST));
10475 int_const = (int_const_or_overflow
10476 && !TREE_OVERFLOW (orig_op0)
10477 && (op0 == truthvalue_true_node
10478 || !TREE_OVERFLOW (orig_op1)));
10480 break;
10482 /* Shift operations: result has same type as first operand;
10483 always convert second operand to int.
10484 Also set SHORT_SHIFT if shifting rightward. */
10486 case RSHIFT_EXPR:
10487 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10488 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10490 result_type = type0;
10491 converted = 1;
10493 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10494 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10495 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10496 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10498 result_type = type0;
10499 converted = 1;
10501 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10502 && code1 == INTEGER_TYPE)
10504 doing_shift = true;
10505 if (TREE_CODE (op1) == INTEGER_CST)
10507 if (tree_int_cst_sgn (op1) < 0)
10509 int_const = false;
10510 if (c_inhibit_evaluation_warnings == 0)
10511 warning_at (location, 0, "right shift count is negative");
10513 else
10515 if (!integer_zerop (op1))
10516 short_shift = 1;
10518 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10520 int_const = false;
10521 if (c_inhibit_evaluation_warnings == 0)
10522 warning_at (location, 0, "right shift count >= width "
10523 "of type");
10528 /* Use the type of the value to be shifted. */
10529 result_type = type0;
10530 /* Convert the non vector shift-count to an integer, regardless
10531 of size of value being shifted. */
10532 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10533 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10534 op1 = convert (integer_type_node, op1);
10535 /* Avoid converting op1 to result_type later. */
10536 converted = 1;
10538 break;
10540 case LSHIFT_EXPR:
10541 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10542 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10544 result_type = type0;
10545 converted = 1;
10547 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10548 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10549 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10550 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10552 result_type = type0;
10553 converted = 1;
10555 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10556 && code1 == INTEGER_TYPE)
10558 doing_shift = true;
10559 if (TREE_CODE (op1) == INTEGER_CST)
10561 if (tree_int_cst_sgn (op1) < 0)
10563 int_const = false;
10564 if (c_inhibit_evaluation_warnings == 0)
10565 warning_at (location, 0, "left shift count is negative");
10568 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10570 int_const = false;
10571 if (c_inhibit_evaluation_warnings == 0)
10572 warning_at (location, 0, "left shift count >= width of "
10573 "type");
10577 /* Use the type of the value to be shifted. */
10578 result_type = type0;
10579 /* Convert the non vector shift-count to an integer, regardless
10580 of size of value being shifted. */
10581 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10582 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10583 op1 = convert (integer_type_node, op1);
10584 /* Avoid converting op1 to result_type later. */
10585 converted = 1;
10587 break;
10589 case EQ_EXPR:
10590 case NE_EXPR:
10591 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10593 tree intt;
10594 if (!vector_types_compatible_elements_p (type0, type1))
10596 error_at (location, "comparing vectors with different "
10597 "element types");
10598 return error_mark_node;
10601 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10603 error_at (location, "comparing vectors with different "
10604 "number of elements");
10605 return error_mark_node;
10608 /* Always construct signed integer vector type. */
10609 intt = c_common_type_for_size (GET_MODE_BITSIZE
10610 (TYPE_MODE (TREE_TYPE (type0))), 0);
10611 result_type = build_opaque_vector_type (intt,
10612 TYPE_VECTOR_SUBPARTS (type0));
10613 converted = 1;
10614 break;
10616 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10617 warning_at (location,
10618 OPT_Wfloat_equal,
10619 "comparing floating point with == or != is unsafe");
10620 /* Result of comparison is always int,
10621 but don't convert the args to int! */
10622 build_type = integer_type_node;
10623 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10624 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10625 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10626 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10627 short_compare = 1;
10628 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10630 if (TREE_CODE (op0) == ADDR_EXPR
10631 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10633 if (code == EQ_EXPR)
10634 warning_at (location,
10635 OPT_Waddress,
10636 "the comparison will always evaluate as %<false%> "
10637 "for the address of %qD will never be NULL",
10638 TREE_OPERAND (op0, 0));
10639 else
10640 warning_at (location,
10641 OPT_Waddress,
10642 "the comparison will always evaluate as %<true%> "
10643 "for the address of %qD will never be NULL",
10644 TREE_OPERAND (op0, 0));
10646 result_type = type0;
10648 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10650 if (TREE_CODE (op1) == ADDR_EXPR
10651 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10653 if (code == EQ_EXPR)
10654 warning_at (location,
10655 OPT_Waddress,
10656 "the comparison will always evaluate as %<false%> "
10657 "for the address of %qD will never be NULL",
10658 TREE_OPERAND (op1, 0));
10659 else
10660 warning_at (location,
10661 OPT_Waddress,
10662 "the comparison will always evaluate as %<true%> "
10663 "for the address of %qD will never be NULL",
10664 TREE_OPERAND (op1, 0));
10666 result_type = type1;
10668 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10670 tree tt0 = TREE_TYPE (type0);
10671 tree tt1 = TREE_TYPE (type1);
10672 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10673 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10674 addr_space_t as_common = ADDR_SPACE_GENERIC;
10676 /* Anything compares with void *. void * compares with anything.
10677 Otherwise, the targets must be compatible
10678 and both must be object or both incomplete. */
10679 if (comp_target_types (location, type0, type1))
10680 result_type = common_pointer_type (type0, type1);
10681 else if (!addr_space_superset (as0, as1, &as_common))
10683 error_at (location, "comparison of pointers to "
10684 "disjoint address spaces");
10685 return error_mark_node;
10687 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10689 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10690 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10691 "comparison of %<void *%> with function pointer");
10693 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10695 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10696 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10697 "comparison of %<void *%> with function pointer");
10699 else
10700 /* Avoid warning about the volatile ObjC EH puts on decls. */
10701 if (!objc_ok)
10702 pedwarn (location, 0,
10703 "comparison of distinct pointer types lacks a cast");
10705 if (result_type == NULL_TREE)
10707 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10708 result_type = build_pointer_type
10709 (build_qualified_type (void_type_node, qual));
10712 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10714 result_type = type0;
10715 pedwarn (location, 0, "comparison between pointer and integer");
10717 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10719 result_type = type1;
10720 pedwarn (location, 0, "comparison between pointer and integer");
10722 break;
10724 case LE_EXPR:
10725 case GE_EXPR:
10726 case LT_EXPR:
10727 case GT_EXPR:
10728 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10730 tree intt;
10731 if (!vector_types_compatible_elements_p (type0, type1))
10733 error_at (location, "comparing vectors with different "
10734 "element types");
10735 return error_mark_node;
10738 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10740 error_at (location, "comparing vectors with different "
10741 "number of elements");
10742 return error_mark_node;
10745 /* Always construct signed integer vector type. */
10746 intt = c_common_type_for_size (GET_MODE_BITSIZE
10747 (TYPE_MODE (TREE_TYPE (type0))), 0);
10748 result_type = build_opaque_vector_type (intt,
10749 TYPE_VECTOR_SUBPARTS (type0));
10750 converted = 1;
10751 break;
10753 build_type = integer_type_node;
10754 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10755 || code0 == FIXED_POINT_TYPE)
10756 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10757 || code1 == FIXED_POINT_TYPE))
10758 short_compare = 1;
10759 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10761 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10762 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10763 addr_space_t as_common;
10765 if (comp_target_types (location, type0, type1))
10767 result_type = common_pointer_type (type0, type1);
10768 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10769 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10770 pedwarn (location, 0,
10771 "comparison of complete and incomplete pointers");
10772 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10773 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10774 "ordered comparisons of pointers to functions");
10775 else if (null_pointer_constant_p (orig_op0)
10776 || null_pointer_constant_p (orig_op1))
10777 warning_at (location, OPT_Wextra,
10778 "ordered comparison of pointer with null pointer");
10781 else if (!addr_space_superset (as0, as1, &as_common))
10783 error_at (location, "comparison of pointers to "
10784 "disjoint address spaces");
10785 return error_mark_node;
10787 else
10789 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10790 result_type = build_pointer_type
10791 (build_qualified_type (void_type_node, qual));
10792 pedwarn (location, 0,
10793 "comparison of distinct pointer types lacks a cast");
10796 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10798 result_type = type0;
10799 if (pedantic)
10800 pedwarn (location, OPT_Wpedantic,
10801 "ordered comparison of pointer with integer zero");
10802 else if (extra_warnings)
10803 warning_at (location, OPT_Wextra,
10804 "ordered comparison of pointer with integer zero");
10806 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10808 result_type = type1;
10809 if (pedantic)
10810 pedwarn (location, OPT_Wpedantic,
10811 "ordered comparison of pointer with integer zero");
10812 else if (extra_warnings)
10813 warning_at (location, OPT_Wextra,
10814 "ordered comparison of pointer with integer zero");
10816 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10818 result_type = type0;
10819 pedwarn (location, 0, "comparison between pointer and integer");
10821 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10823 result_type = type1;
10824 pedwarn (location, 0, "comparison between pointer and integer");
10826 break;
10828 default:
10829 gcc_unreachable ();
10832 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10833 return error_mark_node;
10835 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10836 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10837 || !vector_types_compatible_elements_p (type0, type1)))
10839 binary_op_error (location, code, type0, type1);
10840 return error_mark_node;
10843 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10844 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10846 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10847 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10849 bool first_complex = (code0 == COMPLEX_TYPE);
10850 bool second_complex = (code1 == COMPLEX_TYPE);
10851 int none_complex = (!first_complex && !second_complex);
10853 if (shorten || common || short_compare)
10855 result_type = c_common_type (type0, type1);
10856 do_warn_double_promotion (result_type, type0, type1,
10857 "implicit conversion from %qT to %qT "
10858 "to match other operand of binary "
10859 "expression",
10860 location);
10861 if (result_type == error_mark_node)
10862 return error_mark_node;
10865 if (first_complex != second_complex
10866 && (code == PLUS_EXPR
10867 || code == MINUS_EXPR
10868 || code == MULT_EXPR
10869 || (code == TRUNC_DIV_EXPR && first_complex))
10870 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10871 && flag_signed_zeros)
10873 /* An operation on mixed real/complex operands must be
10874 handled specially, but the language-independent code can
10875 more easily optimize the plain complex arithmetic if
10876 -fno-signed-zeros. */
10877 tree real_type = TREE_TYPE (result_type);
10878 tree real, imag;
10879 if (type0 != orig_type0 || type1 != orig_type1)
10881 gcc_assert (may_need_excess_precision && common);
10882 semantic_result_type = c_common_type (orig_type0, orig_type1);
10884 if (first_complex)
10886 if (TREE_TYPE (op0) != result_type)
10887 op0 = convert_and_check (location, result_type, op0);
10888 if (TREE_TYPE (op1) != real_type)
10889 op1 = convert_and_check (location, real_type, op1);
10891 else
10893 if (TREE_TYPE (op0) != real_type)
10894 op0 = convert_and_check (location, real_type, op0);
10895 if (TREE_TYPE (op1) != result_type)
10896 op1 = convert_and_check (location, result_type, op1);
10898 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10899 return error_mark_node;
10900 if (first_complex)
10902 op0 = c_save_expr (op0);
10903 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10904 op0, 1);
10905 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10906 op0, 1);
10907 switch (code)
10909 case MULT_EXPR:
10910 case TRUNC_DIV_EXPR:
10911 op1 = c_save_expr (op1);
10912 imag = build2 (resultcode, real_type, imag, op1);
10913 /* Fall through. */
10914 case PLUS_EXPR:
10915 case MINUS_EXPR:
10916 real = build2 (resultcode, real_type, real, op1);
10917 break;
10918 default:
10919 gcc_unreachable();
10922 else
10924 op1 = c_save_expr (op1);
10925 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10926 op1, 1);
10927 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10928 op1, 1);
10929 switch (code)
10931 case MULT_EXPR:
10932 op0 = c_save_expr (op0);
10933 imag = build2 (resultcode, real_type, op0, imag);
10934 /* Fall through. */
10935 case PLUS_EXPR:
10936 real = build2 (resultcode, real_type, op0, real);
10937 break;
10938 case MINUS_EXPR:
10939 real = build2 (resultcode, real_type, op0, real);
10940 imag = build1 (NEGATE_EXPR, real_type, imag);
10941 break;
10942 default:
10943 gcc_unreachable();
10946 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10947 goto return_build_binary_op;
10950 /* For certain operations (which identify themselves by shorten != 0)
10951 if both args were extended from the same smaller type,
10952 do the arithmetic in that type and then extend.
10954 shorten !=0 and !=1 indicates a bitwise operation.
10955 For them, this optimization is safe only if
10956 both args are zero-extended or both are sign-extended.
10957 Otherwise, we might change the result.
10958 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10959 but calculated in (unsigned short) it would be (unsigned short)-1. */
10961 if (shorten && none_complex)
10963 final_type = result_type;
10964 result_type = shorten_binary_op (result_type, op0, op1,
10965 shorten == -1);
10968 /* Shifts can be shortened if shifting right. */
10970 if (short_shift)
10972 int unsigned_arg;
10973 tree arg0 = get_narrower (op0, &unsigned_arg);
10975 final_type = result_type;
10977 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10978 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10980 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10981 && tree_int_cst_sgn (op1) > 0
10982 /* We can shorten only if the shift count is less than the
10983 number of bits in the smaller type size. */
10984 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10985 /* We cannot drop an unsigned shift after sign-extension. */
10986 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10988 /* Do an unsigned shift if the operand was zero-extended. */
10989 result_type
10990 = c_common_signed_or_unsigned_type (unsigned_arg,
10991 TREE_TYPE (arg0));
10992 /* Convert value-to-be-shifted to that type. */
10993 if (TREE_TYPE (op0) != result_type)
10994 op0 = convert (result_type, op0);
10995 converted = 1;
10999 /* Comparison operations are shortened too but differently.
11000 They identify themselves by setting short_compare = 1. */
11002 if (short_compare)
11004 /* Don't write &op0, etc., because that would prevent op0
11005 from being kept in a register.
11006 Instead, make copies of the our local variables and
11007 pass the copies by reference, then copy them back afterward. */
11008 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11009 enum tree_code xresultcode = resultcode;
11010 tree val
11011 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11012 &xresultcode);
11014 if (val != 0)
11016 ret = val;
11017 goto return_build_binary_op;
11020 op0 = xop0, op1 = xop1;
11021 converted = 1;
11022 resultcode = xresultcode;
11024 if (c_inhibit_evaluation_warnings == 0)
11026 bool op0_maybe_const = true;
11027 bool op1_maybe_const = true;
11028 tree orig_op0_folded, orig_op1_folded;
11030 if (in_late_binary_op)
11032 orig_op0_folded = orig_op0;
11033 orig_op1_folded = orig_op1;
11035 else
11037 /* Fold for the sake of possible warnings, as in
11038 build_conditional_expr. This requires the
11039 "original" values to be folded, not just op0 and
11040 op1. */
11041 c_inhibit_evaluation_warnings++;
11042 op0 = c_fully_fold (op0, require_constant_value,
11043 &op0_maybe_const);
11044 op1 = c_fully_fold (op1, require_constant_value,
11045 &op1_maybe_const);
11046 c_inhibit_evaluation_warnings--;
11047 orig_op0_folded = c_fully_fold (orig_op0,
11048 require_constant_value,
11049 NULL);
11050 orig_op1_folded = c_fully_fold (orig_op1,
11051 require_constant_value,
11052 NULL);
11055 if (warn_sign_compare)
11056 warn_for_sign_compare (location, orig_op0_folded,
11057 orig_op1_folded, op0, op1,
11058 result_type, resultcode);
11059 if (!in_late_binary_op && !int_operands)
11061 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11062 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11063 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11064 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11070 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11071 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11072 Then the expression will be built.
11073 It will be given type FINAL_TYPE if that is nonzero;
11074 otherwise, it will be given type RESULT_TYPE. */
11076 if (!result_type)
11078 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11079 return error_mark_node;
11082 if (build_type == NULL_TREE)
11084 build_type = result_type;
11085 if ((type0 != orig_type0 || type1 != orig_type1)
11086 && !boolean_op)
11088 gcc_assert (may_need_excess_precision && common);
11089 semantic_result_type = c_common_type (orig_type0, orig_type1);
11093 if (!converted)
11095 op0 = ep_convert_and_check (location, result_type, op0,
11096 semantic_result_type);
11097 op1 = ep_convert_and_check (location, result_type, op1,
11098 semantic_result_type);
11100 /* This can happen if one operand has a vector type, and the other
11101 has a different type. */
11102 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11103 return error_mark_node;
11106 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11107 | SANITIZE_FLOAT_DIVIDE))
11108 && current_function_decl != 0
11109 && !lookup_attribute ("no_sanitize_undefined",
11110 DECL_ATTRIBUTES (current_function_decl))
11111 && (doing_div_or_mod || doing_shift))
11113 /* OP0 and/or OP1 might have side-effects. */
11114 op0 = c_save_expr (op0);
11115 op1 = c_save_expr (op1);
11116 op0 = c_fully_fold (op0, false, NULL);
11117 op1 = c_fully_fold (op1, false, NULL);
11118 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11119 | SANITIZE_FLOAT_DIVIDE)))
11120 instrument_expr = ubsan_instrument_division (location, op0, op1);
11121 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11122 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11125 /* Treat expressions in initializers specially as they can't trap. */
11126 if (int_const_or_overflow)
11127 ret = (require_constant_value
11128 ? fold_build2_initializer_loc (location, resultcode, build_type,
11129 op0, op1)
11130 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11131 else
11132 ret = build2 (resultcode, build_type, op0, op1);
11133 if (final_type != 0)
11134 ret = convert (final_type, ret);
11136 return_build_binary_op:
11137 gcc_assert (ret != error_mark_node);
11138 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11139 ret = (int_operands
11140 ? note_integer_operands (ret)
11141 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11142 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11143 && !in_late_binary_op)
11144 ret = note_integer_operands (ret);
11145 if (semantic_result_type)
11146 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11147 protected_set_expr_location (ret, location);
11149 if (instrument_expr != NULL)
11150 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11151 instrument_expr, ret);
11153 return ret;
11157 /* Convert EXPR to be a truth-value, validating its type for this
11158 purpose. LOCATION is the source location for the expression. */
11160 tree
11161 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11163 bool int_const, int_operands;
11165 switch (TREE_CODE (TREE_TYPE (expr)))
11167 case ARRAY_TYPE:
11168 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11169 return error_mark_node;
11171 case RECORD_TYPE:
11172 error_at (location, "used struct type value where scalar is required");
11173 return error_mark_node;
11175 case UNION_TYPE:
11176 error_at (location, "used union type value where scalar is required");
11177 return error_mark_node;
11179 case VOID_TYPE:
11180 error_at (location, "void value not ignored as it ought to be");
11181 return error_mark_node;
11183 case FUNCTION_TYPE:
11184 gcc_unreachable ();
11186 case VECTOR_TYPE:
11187 error_at (location, "used vector type where scalar is required");
11188 return error_mark_node;
11190 default:
11191 break;
11194 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11195 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11196 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11198 expr = remove_c_maybe_const_expr (expr);
11199 expr = build2 (NE_EXPR, integer_type_node, expr,
11200 convert (TREE_TYPE (expr), integer_zero_node));
11201 expr = note_integer_operands (expr);
11203 else
11204 /* ??? Should we also give an error for vectors rather than leaving
11205 those to give errors later? */
11206 expr = c_common_truthvalue_conversion (location, expr);
11208 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11210 if (TREE_OVERFLOW (expr))
11211 return expr;
11212 else
11213 return note_integer_operands (expr);
11215 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11216 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11217 return expr;
11221 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11222 required. */
11224 tree
11225 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11227 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11229 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11230 /* Executing a compound literal inside a function reinitializes
11231 it. */
11232 if (!TREE_STATIC (decl))
11233 *se = true;
11234 return decl;
11236 else
11237 return expr;
11240 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11242 tree
11243 c_begin_omp_parallel (void)
11245 tree block;
11247 keep_next_level ();
11248 block = c_begin_compound_stmt (true);
11250 return block;
11253 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11254 statement. LOC is the location of the OMP_PARALLEL. */
11256 tree
11257 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11259 tree stmt;
11261 block = c_end_compound_stmt (loc, block, true);
11263 stmt = make_node (OMP_PARALLEL);
11264 TREE_TYPE (stmt) = void_type_node;
11265 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11266 OMP_PARALLEL_BODY (stmt) = block;
11267 SET_EXPR_LOCATION (stmt, loc);
11269 return add_stmt (stmt);
11272 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11274 tree
11275 c_begin_omp_task (void)
11277 tree block;
11279 keep_next_level ();
11280 block = c_begin_compound_stmt (true);
11282 return block;
11285 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11286 statement. LOC is the location of the #pragma. */
11288 tree
11289 c_finish_omp_task (location_t loc, tree clauses, tree block)
11291 tree stmt;
11293 block = c_end_compound_stmt (loc, block, true);
11295 stmt = make_node (OMP_TASK);
11296 TREE_TYPE (stmt) = void_type_node;
11297 OMP_TASK_CLAUSES (stmt) = clauses;
11298 OMP_TASK_BODY (stmt) = block;
11299 SET_EXPR_LOCATION (stmt, loc);
11301 return add_stmt (stmt);
11304 /* Generate GOMP_cancel call for #pragma omp cancel. */
11306 void
11307 c_finish_omp_cancel (location_t loc, tree clauses)
11309 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11310 int mask = 0;
11311 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11312 mask = 1;
11313 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11314 mask = 2;
11315 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11316 mask = 4;
11317 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11318 mask = 8;
11319 else
11321 error_at (loc, "%<#pragma omp cancel must specify one of "
11322 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11323 "clauses");
11324 return;
11326 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11327 if (ifc != NULL_TREE)
11329 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11330 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11331 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11332 build_zero_cst (type));
11334 else
11335 ifc = boolean_true_node;
11336 tree stmt = build_call_expr_loc (loc, fn, 2,
11337 build_int_cst (integer_type_node, mask),
11338 ifc);
11339 add_stmt (stmt);
11342 /* Generate GOMP_cancellation_point call for
11343 #pragma omp cancellation point. */
11345 void
11346 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11348 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11349 int mask = 0;
11350 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11351 mask = 1;
11352 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11353 mask = 2;
11354 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11355 mask = 4;
11356 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11357 mask = 8;
11358 else
11360 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11361 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11362 "clauses");
11363 return;
11365 tree stmt = build_call_expr_loc (loc, fn, 1,
11366 build_int_cst (integer_type_node, mask));
11367 add_stmt (stmt);
11370 /* Helper function for handle_omp_array_sections. Called recursively
11371 to handle multiple array-section-subscripts. C is the clause,
11372 T current expression (initially OMP_CLAUSE_DECL), which is either
11373 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11374 expression if specified, TREE_VALUE length expression if specified,
11375 TREE_CHAIN is what it has been specified after, or some decl.
11376 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11377 set to true if any of the array-section-subscript could have length
11378 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11379 first array-section-subscript which is known not to have length
11380 of one. Given say:
11381 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11382 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11383 all are or may have length of 1, array-section-subscript [:2] is the
11384 first one knonwn not to have length 1. For array-section-subscript
11385 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11386 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11387 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11388 case though, as some lengths could be zero. */
11390 static tree
11391 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11392 bool &maybe_zero_len, unsigned int &first_non_one)
11394 tree ret, low_bound, length, type;
11395 if (TREE_CODE (t) != TREE_LIST)
11397 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11398 return error_mark_node;
11399 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11401 if (DECL_P (t))
11402 error_at (OMP_CLAUSE_LOCATION (c),
11403 "%qD is not a variable in %qs clause", t,
11404 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11405 else
11406 error_at (OMP_CLAUSE_LOCATION (c),
11407 "%qE is not a variable in %qs clause", t,
11408 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11409 return error_mark_node;
11411 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11412 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11414 error_at (OMP_CLAUSE_LOCATION (c),
11415 "%qD is threadprivate variable in %qs clause", t,
11416 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11417 return error_mark_node;
11419 return t;
11422 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11423 maybe_zero_len, first_non_one);
11424 if (ret == error_mark_node || ret == NULL_TREE)
11425 return ret;
11427 type = TREE_TYPE (ret);
11428 low_bound = TREE_PURPOSE (t);
11429 length = TREE_VALUE (t);
11431 if (low_bound == error_mark_node || length == error_mark_node)
11432 return error_mark_node;
11434 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11436 error_at (OMP_CLAUSE_LOCATION (c),
11437 "low bound %qE of array section does not have integral type",
11438 low_bound);
11439 return error_mark_node;
11441 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11443 error_at (OMP_CLAUSE_LOCATION (c),
11444 "length %qE of array section does not have integral type",
11445 length);
11446 return error_mark_node;
11448 if (low_bound
11449 && TREE_CODE (low_bound) == INTEGER_CST
11450 && TYPE_PRECISION (TREE_TYPE (low_bound))
11451 > TYPE_PRECISION (sizetype))
11452 low_bound = fold_convert (sizetype, low_bound);
11453 if (length
11454 && TREE_CODE (length) == INTEGER_CST
11455 && TYPE_PRECISION (TREE_TYPE (length))
11456 > TYPE_PRECISION (sizetype))
11457 length = fold_convert (sizetype, length);
11458 if (low_bound == NULL_TREE)
11459 low_bound = integer_zero_node;
11461 if (length != NULL_TREE)
11463 if (!integer_nonzerop (length))
11464 maybe_zero_len = true;
11465 if (first_non_one == types.length ()
11466 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11467 first_non_one++;
11469 if (TREE_CODE (type) == ARRAY_TYPE)
11471 if (length == NULL_TREE
11472 && (TYPE_DOMAIN (type) == NULL_TREE
11473 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11475 error_at (OMP_CLAUSE_LOCATION (c),
11476 "for unknown bound array type length expression must "
11477 "be specified");
11478 return error_mark_node;
11480 if (TREE_CODE (low_bound) == INTEGER_CST
11481 && tree_int_cst_sgn (low_bound) == -1)
11483 error_at (OMP_CLAUSE_LOCATION (c),
11484 "negative low bound in array section in %qs clause",
11485 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11486 return error_mark_node;
11488 if (length != NULL_TREE
11489 && TREE_CODE (length) == INTEGER_CST
11490 && tree_int_cst_sgn (length) == -1)
11492 error_at (OMP_CLAUSE_LOCATION (c),
11493 "negative length in array section in %qs clause",
11494 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11495 return error_mark_node;
11497 if (TYPE_DOMAIN (type)
11498 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11499 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11500 == INTEGER_CST)
11502 tree size = size_binop (PLUS_EXPR,
11503 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11504 size_one_node);
11505 if (TREE_CODE (low_bound) == INTEGER_CST)
11507 if (tree_int_cst_lt (size, low_bound))
11509 error_at (OMP_CLAUSE_LOCATION (c),
11510 "low bound %qE above array section size "
11511 "in %qs clause", low_bound,
11512 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11513 return error_mark_node;
11515 if (tree_int_cst_equal (size, low_bound))
11516 maybe_zero_len = true;
11517 else if (length == NULL_TREE
11518 && first_non_one == types.length ()
11519 && tree_int_cst_equal
11520 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11521 low_bound))
11522 first_non_one++;
11524 else if (length == NULL_TREE)
11526 maybe_zero_len = true;
11527 if (first_non_one == types.length ())
11528 first_non_one++;
11530 if (length && TREE_CODE (length) == INTEGER_CST)
11532 if (tree_int_cst_lt (size, length))
11534 error_at (OMP_CLAUSE_LOCATION (c),
11535 "length %qE above array section size "
11536 "in %qs clause", length,
11537 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11538 return error_mark_node;
11540 if (TREE_CODE (low_bound) == INTEGER_CST)
11542 tree lbpluslen
11543 = size_binop (PLUS_EXPR,
11544 fold_convert (sizetype, low_bound),
11545 fold_convert (sizetype, length));
11546 if (TREE_CODE (lbpluslen) == INTEGER_CST
11547 && tree_int_cst_lt (size, lbpluslen))
11549 error_at (OMP_CLAUSE_LOCATION (c),
11550 "high bound %qE above array section size "
11551 "in %qs clause", lbpluslen,
11552 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11553 return error_mark_node;
11558 else if (length == NULL_TREE)
11560 maybe_zero_len = true;
11561 if (first_non_one == types.length ())
11562 first_non_one++;
11565 /* For [lb:] we will need to evaluate lb more than once. */
11566 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11568 tree lb = c_save_expr (low_bound);
11569 if (lb != low_bound)
11571 TREE_PURPOSE (t) = lb;
11572 low_bound = lb;
11576 else if (TREE_CODE (type) == POINTER_TYPE)
11578 if (length == NULL_TREE)
11580 error_at (OMP_CLAUSE_LOCATION (c),
11581 "for pointer type length expression must be specified");
11582 return error_mark_node;
11584 /* If there is a pointer type anywhere but in the very first
11585 array-section-subscript, the array section can't be contiguous. */
11586 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11587 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11589 error_at (OMP_CLAUSE_LOCATION (c),
11590 "array section is not contiguous in %qs clause",
11591 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11592 return error_mark_node;
11595 else
11597 error_at (OMP_CLAUSE_LOCATION (c),
11598 "%qE does not have pointer or array type", ret);
11599 return error_mark_node;
11601 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11602 types.safe_push (TREE_TYPE (ret));
11603 /* We will need to evaluate lb more than once. */
11604 tree lb = c_save_expr (low_bound);
11605 if (lb != low_bound)
11607 TREE_PURPOSE (t) = lb;
11608 low_bound = lb;
11610 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11611 return ret;
11614 /* Handle array sections for clause C. */
11616 static bool
11617 handle_omp_array_sections (tree c)
11619 bool maybe_zero_len = false;
11620 unsigned int first_non_one = 0;
11621 vec<tree> types = vNULL;
11622 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11623 maybe_zero_len, first_non_one);
11624 if (first == error_mark_node)
11626 types.release ();
11627 return true;
11629 if (first == NULL_TREE)
11631 types.release ();
11632 return false;
11634 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11636 tree t = OMP_CLAUSE_DECL (c);
11637 tree tem = NULL_TREE;
11638 types.release ();
11639 /* Need to evaluate side effects in the length expressions
11640 if any. */
11641 while (TREE_CODE (t) == TREE_LIST)
11643 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11645 if (tem == NULL_TREE)
11646 tem = TREE_VALUE (t);
11647 else
11648 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11649 TREE_VALUE (t), tem);
11651 t = TREE_CHAIN (t);
11653 if (tem)
11654 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11655 first = c_fully_fold (first, false, NULL);
11656 OMP_CLAUSE_DECL (c) = first;
11658 else
11660 unsigned int num = types.length (), i;
11661 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11662 tree condition = NULL_TREE;
11664 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11665 maybe_zero_len = true;
11667 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11668 t = TREE_CHAIN (t))
11670 tree low_bound = TREE_PURPOSE (t);
11671 tree length = TREE_VALUE (t);
11673 i--;
11674 if (low_bound
11675 && TREE_CODE (low_bound) == INTEGER_CST
11676 && TYPE_PRECISION (TREE_TYPE (low_bound))
11677 > TYPE_PRECISION (sizetype))
11678 low_bound = fold_convert (sizetype, low_bound);
11679 if (length
11680 && TREE_CODE (length) == INTEGER_CST
11681 && TYPE_PRECISION (TREE_TYPE (length))
11682 > TYPE_PRECISION (sizetype))
11683 length = fold_convert (sizetype, length);
11684 if (low_bound == NULL_TREE)
11685 low_bound = integer_zero_node;
11686 if (!maybe_zero_len && i > first_non_one)
11688 if (integer_nonzerop (low_bound))
11689 goto do_warn_noncontiguous;
11690 if (length != NULL_TREE
11691 && TREE_CODE (length) == INTEGER_CST
11692 && TYPE_DOMAIN (types[i])
11693 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11694 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11695 == INTEGER_CST)
11697 tree size;
11698 size = size_binop (PLUS_EXPR,
11699 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11700 size_one_node);
11701 if (!tree_int_cst_equal (length, size))
11703 do_warn_noncontiguous:
11704 error_at (OMP_CLAUSE_LOCATION (c),
11705 "array section is not contiguous in %qs "
11706 "clause",
11707 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11708 types.release ();
11709 return true;
11712 if (length != NULL_TREE
11713 && TREE_SIDE_EFFECTS (length))
11715 if (side_effects == NULL_TREE)
11716 side_effects = length;
11717 else
11718 side_effects = build2 (COMPOUND_EXPR,
11719 TREE_TYPE (side_effects),
11720 length, side_effects);
11723 else
11725 tree l;
11727 if (i > first_non_one && length && integer_nonzerop (length))
11728 continue;
11729 if (length)
11730 l = fold_convert (sizetype, length);
11731 else
11733 l = size_binop (PLUS_EXPR,
11734 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11735 size_one_node);
11736 l = size_binop (MINUS_EXPR, l,
11737 fold_convert (sizetype, low_bound));
11739 if (i > first_non_one)
11741 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11742 size_zero_node);
11743 if (condition == NULL_TREE)
11744 condition = l;
11745 else
11746 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11747 l, condition);
11749 else if (size == NULL_TREE)
11751 size = size_in_bytes (TREE_TYPE (types[i]));
11752 size = size_binop (MULT_EXPR, size, l);
11753 if (condition)
11754 size = fold_build3 (COND_EXPR, sizetype, condition,
11755 size, size_zero_node);
11757 else
11758 size = size_binop (MULT_EXPR, size, l);
11761 types.release ();
11762 if (side_effects)
11763 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11764 first = c_fully_fold (first, false, NULL);
11765 OMP_CLAUSE_DECL (c) = first;
11766 if (size)
11767 size = c_fully_fold (size, false, NULL);
11768 OMP_CLAUSE_SIZE (c) = size;
11769 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11770 return false;
11771 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11772 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11773 if (!c_mark_addressable (t))
11774 return false;
11775 OMP_CLAUSE_DECL (c2) = t;
11776 t = build_fold_addr_expr (first);
11777 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11778 tree ptr = OMP_CLAUSE_DECL (c2);
11779 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11780 ptr = build_fold_addr_expr (ptr);
11781 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11782 ptrdiff_type_node, t,
11783 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11784 ptrdiff_type_node, ptr));
11785 t = c_fully_fold (t, false, NULL);
11786 OMP_CLAUSE_SIZE (c2) = t;
11787 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11788 OMP_CLAUSE_CHAIN (c) = c2;
11790 return false;
11793 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11794 an inline call. But, remap
11795 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11796 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11798 static tree
11799 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11800 tree decl, tree placeholder)
11802 copy_body_data id;
11803 hash_map<tree, tree> decl_map;
11805 decl_map.put (omp_decl1, placeholder);
11806 decl_map.put (omp_decl2, decl);
11807 memset (&id, 0, sizeof (id));
11808 id.src_fn = DECL_CONTEXT (omp_decl1);
11809 id.dst_fn = current_function_decl;
11810 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11811 id.decl_map = &decl_map;
11813 id.copy_decl = copy_decl_no_change;
11814 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11815 id.transform_new_cfg = true;
11816 id.transform_return_to_modify = false;
11817 id.transform_lang_insert_block = NULL;
11818 id.eh_lp_nr = 0;
11819 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11820 return stmt;
11823 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11824 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11826 static tree
11827 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11829 if (*tp == (tree) data)
11830 return *tp;
11831 return NULL_TREE;
11834 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11835 Remove any elements from the list that are invalid. */
11837 tree
11838 c_finish_omp_clauses (tree clauses)
11840 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11841 bitmap_head aligned_head;
11842 tree c, t, *pc;
11843 bool branch_seen = false;
11844 bool copyprivate_seen = false;
11845 tree *nowait_clause = NULL;
11847 bitmap_obstack_initialize (NULL);
11848 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11849 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11850 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11851 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11853 for (pc = &clauses, c = clauses; c ; c = *pc)
11855 bool remove = false;
11856 bool need_complete = false;
11857 bool need_implicitly_determined = false;
11859 switch (OMP_CLAUSE_CODE (c))
11861 case OMP_CLAUSE_SHARED:
11862 need_implicitly_determined = true;
11863 goto check_dup_generic;
11865 case OMP_CLAUSE_PRIVATE:
11866 need_complete = true;
11867 need_implicitly_determined = true;
11868 goto check_dup_generic;
11870 case OMP_CLAUSE_REDUCTION:
11871 need_implicitly_determined = true;
11872 t = OMP_CLAUSE_DECL (c);
11873 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11874 && (FLOAT_TYPE_P (TREE_TYPE (t))
11875 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11877 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11878 const char *r_name = NULL;
11880 switch (r_code)
11882 case PLUS_EXPR:
11883 case MULT_EXPR:
11884 case MINUS_EXPR:
11885 break;
11886 case MIN_EXPR:
11887 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11888 r_name = "min";
11889 break;
11890 case MAX_EXPR:
11891 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11892 r_name = "max";
11893 break;
11894 case BIT_AND_EXPR:
11895 r_name = "&";
11896 break;
11897 case BIT_XOR_EXPR:
11898 r_name = "^";
11899 break;
11900 case BIT_IOR_EXPR:
11901 r_name = "|";
11902 break;
11903 case TRUTH_ANDIF_EXPR:
11904 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11905 r_name = "&&";
11906 break;
11907 case TRUTH_ORIF_EXPR:
11908 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11909 r_name = "||";
11910 break;
11911 default:
11912 gcc_unreachable ();
11914 if (r_name)
11916 error_at (OMP_CLAUSE_LOCATION (c),
11917 "%qE has invalid type for %<reduction(%s)%>",
11918 t, r_name);
11919 remove = true;
11920 break;
11923 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11925 error_at (OMP_CLAUSE_LOCATION (c),
11926 "user defined reduction not found for %qD", t);
11927 remove = true;
11928 break;
11930 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11932 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11933 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11934 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11935 VAR_DECL, NULL_TREE, type);
11936 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11937 DECL_ARTIFICIAL (placeholder) = 1;
11938 DECL_IGNORED_P (placeholder) = 1;
11939 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11940 c_mark_addressable (placeholder);
11941 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11942 c_mark_addressable (OMP_CLAUSE_DECL (c));
11943 OMP_CLAUSE_REDUCTION_MERGE (c)
11944 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11945 TREE_VEC_ELT (list, 0),
11946 TREE_VEC_ELT (list, 1),
11947 OMP_CLAUSE_DECL (c), placeholder);
11948 OMP_CLAUSE_REDUCTION_MERGE (c)
11949 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11950 void_type_node, NULL_TREE,
11951 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11952 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11953 if (TREE_VEC_LENGTH (list) == 6)
11955 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11956 c_mark_addressable (OMP_CLAUSE_DECL (c));
11957 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11958 c_mark_addressable (placeholder);
11959 tree init = TREE_VEC_ELT (list, 5);
11960 if (init == error_mark_node)
11961 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11962 OMP_CLAUSE_REDUCTION_INIT (c)
11963 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11964 TREE_VEC_ELT (list, 3),
11965 OMP_CLAUSE_DECL (c), placeholder);
11966 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11967 OMP_CLAUSE_REDUCTION_INIT (c)
11968 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11969 OMP_CLAUSE_REDUCTION_INIT (c));
11970 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11971 c_find_omp_placeholder_r,
11972 placeholder, NULL))
11973 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11975 else
11977 tree init;
11978 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11979 init = build_constructor (TREE_TYPE (t), NULL);
11980 else
11981 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11982 OMP_CLAUSE_REDUCTION_INIT (c)
11983 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11985 OMP_CLAUSE_REDUCTION_INIT (c)
11986 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11987 void_type_node, NULL_TREE,
11988 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11989 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11991 goto check_dup_generic;
11993 case OMP_CLAUSE_COPYPRIVATE:
11994 copyprivate_seen = true;
11995 if (nowait_clause)
11997 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11998 "%<nowait%> clause must not be used together "
11999 "with %<copyprivate%>");
12000 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12001 nowait_clause = NULL;
12003 goto check_dup_generic;
12005 case OMP_CLAUSE_COPYIN:
12006 t = OMP_CLAUSE_DECL (c);
12007 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12009 error_at (OMP_CLAUSE_LOCATION (c),
12010 "%qE must be %<threadprivate%> for %<copyin%>", t);
12011 remove = true;
12012 break;
12014 goto check_dup_generic;
12016 case OMP_CLAUSE_LINEAR:
12017 t = OMP_CLAUSE_DECL (c);
12018 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12019 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12021 error_at (OMP_CLAUSE_LOCATION (c),
12022 "linear clause applied to non-integral non-pointer "
12023 "variable with type %qT", TREE_TYPE (t));
12024 remove = true;
12025 break;
12027 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12029 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12030 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12031 OMP_CLAUSE_DECL (c), s);
12032 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12033 sizetype, s, OMP_CLAUSE_DECL (c));
12034 if (s == error_mark_node)
12035 s = size_one_node;
12036 OMP_CLAUSE_LINEAR_STEP (c) = s;
12038 else
12039 OMP_CLAUSE_LINEAR_STEP (c)
12040 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12041 goto check_dup_generic;
12043 check_dup_generic:
12044 t = OMP_CLAUSE_DECL (c);
12045 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12047 error_at (OMP_CLAUSE_LOCATION (c),
12048 "%qE is not a variable in clause %qs", t,
12049 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12050 remove = true;
12052 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12053 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12054 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12056 error_at (OMP_CLAUSE_LOCATION (c),
12057 "%qE appears more than once in data clauses", t);
12058 remove = true;
12060 else
12061 bitmap_set_bit (&generic_head, DECL_UID (t));
12062 break;
12064 case OMP_CLAUSE_FIRSTPRIVATE:
12065 t = OMP_CLAUSE_DECL (c);
12066 need_complete = true;
12067 need_implicitly_determined = true;
12068 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12070 error_at (OMP_CLAUSE_LOCATION (c),
12071 "%qE is not a variable in clause %<firstprivate%>", t);
12072 remove = true;
12074 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12075 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12077 error_at (OMP_CLAUSE_LOCATION (c),
12078 "%qE appears more than once in data clauses", t);
12079 remove = true;
12081 else
12082 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12083 break;
12085 case OMP_CLAUSE_LASTPRIVATE:
12086 t = OMP_CLAUSE_DECL (c);
12087 need_complete = true;
12088 need_implicitly_determined = true;
12089 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12091 error_at (OMP_CLAUSE_LOCATION (c),
12092 "%qE is not a variable in clause %<lastprivate%>", t);
12093 remove = true;
12095 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12096 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12098 error_at (OMP_CLAUSE_LOCATION (c),
12099 "%qE appears more than once in data clauses", t);
12100 remove = true;
12102 else
12103 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12104 break;
12106 case OMP_CLAUSE_ALIGNED:
12107 t = OMP_CLAUSE_DECL (c);
12108 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12110 error_at (OMP_CLAUSE_LOCATION (c),
12111 "%qE is not a variable in %<aligned%> clause", t);
12112 remove = true;
12114 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12115 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12117 error_at (OMP_CLAUSE_LOCATION (c),
12118 "%qE in %<aligned%> clause is neither a pointer nor "
12119 "an array", t);
12120 remove = true;
12122 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12124 error_at (OMP_CLAUSE_LOCATION (c),
12125 "%qE appears more than once in %<aligned%> clauses",
12127 remove = true;
12129 else
12130 bitmap_set_bit (&aligned_head, DECL_UID (t));
12131 break;
12133 case OMP_CLAUSE_DEPEND:
12134 t = OMP_CLAUSE_DECL (c);
12135 if (TREE_CODE (t) == TREE_LIST)
12137 if (handle_omp_array_sections (c))
12138 remove = true;
12139 break;
12141 if (t == error_mark_node)
12142 remove = true;
12143 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12145 error_at (OMP_CLAUSE_LOCATION (c),
12146 "%qE is not a variable in %<depend%> clause", t);
12147 remove = true;
12149 else if (!c_mark_addressable (t))
12150 remove = true;
12151 break;
12153 case OMP_CLAUSE_MAP:
12154 case OMP_CLAUSE_TO:
12155 case OMP_CLAUSE_FROM:
12156 t = OMP_CLAUSE_DECL (c);
12157 if (TREE_CODE (t) == TREE_LIST)
12159 if (handle_omp_array_sections (c))
12160 remove = true;
12161 else
12163 t = OMP_CLAUSE_DECL (c);
12164 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12166 error_at (OMP_CLAUSE_LOCATION (c),
12167 "array section does not have mappable type "
12168 "in %qs clause",
12169 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12170 remove = true;
12173 break;
12175 if (t == error_mark_node)
12176 remove = true;
12177 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12179 error_at (OMP_CLAUSE_LOCATION (c),
12180 "%qE is not a variable in %qs clause", t,
12181 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12182 remove = true;
12184 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12186 error_at (OMP_CLAUSE_LOCATION (c),
12187 "%qD is threadprivate variable in %qs clause", t,
12188 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12189 remove = true;
12191 else if (!c_mark_addressable (t))
12192 remove = true;
12193 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12194 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12195 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12197 error_at (OMP_CLAUSE_LOCATION (c),
12198 "%qD does not have a mappable type in %qs clause", t,
12199 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12200 remove = true;
12202 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12204 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12205 error ("%qD appears more than once in motion clauses", t);
12206 else
12207 error ("%qD appears more than once in map clauses", t);
12208 remove = true;
12210 else
12211 bitmap_set_bit (&generic_head, DECL_UID (t));
12212 break;
12214 case OMP_CLAUSE_UNIFORM:
12215 t = OMP_CLAUSE_DECL (c);
12216 if (TREE_CODE (t) != PARM_DECL)
12218 if (DECL_P (t))
12219 error_at (OMP_CLAUSE_LOCATION (c),
12220 "%qD is not an argument in %<uniform%> clause", t);
12221 else
12222 error_at (OMP_CLAUSE_LOCATION (c),
12223 "%qE is not an argument in %<uniform%> clause", t);
12224 remove = true;
12225 break;
12227 goto check_dup_generic;
12229 case OMP_CLAUSE_NOWAIT:
12230 if (copyprivate_seen)
12232 error_at (OMP_CLAUSE_LOCATION (c),
12233 "%<nowait%> clause must not be used together "
12234 "with %<copyprivate%>");
12235 remove = true;
12236 break;
12238 nowait_clause = pc;
12239 pc = &OMP_CLAUSE_CHAIN (c);
12240 continue;
12242 case OMP_CLAUSE_IF:
12243 case OMP_CLAUSE_NUM_THREADS:
12244 case OMP_CLAUSE_NUM_TEAMS:
12245 case OMP_CLAUSE_THREAD_LIMIT:
12246 case OMP_CLAUSE_SCHEDULE:
12247 case OMP_CLAUSE_ORDERED:
12248 case OMP_CLAUSE_DEFAULT:
12249 case OMP_CLAUSE_UNTIED:
12250 case OMP_CLAUSE_COLLAPSE:
12251 case OMP_CLAUSE_FINAL:
12252 case OMP_CLAUSE_MERGEABLE:
12253 case OMP_CLAUSE_SAFELEN:
12254 case OMP_CLAUSE_SIMDLEN:
12255 case OMP_CLAUSE_DEVICE:
12256 case OMP_CLAUSE_DIST_SCHEDULE:
12257 case OMP_CLAUSE_PARALLEL:
12258 case OMP_CLAUSE_FOR:
12259 case OMP_CLAUSE_SECTIONS:
12260 case OMP_CLAUSE_TASKGROUP:
12261 case OMP_CLAUSE_PROC_BIND:
12262 pc = &OMP_CLAUSE_CHAIN (c);
12263 continue;
12265 case OMP_CLAUSE_INBRANCH:
12266 case OMP_CLAUSE_NOTINBRANCH:
12267 if (branch_seen)
12269 error_at (OMP_CLAUSE_LOCATION (c),
12270 "%<inbranch%> clause is incompatible with "
12271 "%<notinbranch%>");
12272 remove = true;
12273 break;
12275 branch_seen = true;
12276 pc = &OMP_CLAUSE_CHAIN (c);
12277 continue;
12279 default:
12280 gcc_unreachable ();
12283 if (!remove)
12285 t = OMP_CLAUSE_DECL (c);
12287 if (need_complete)
12289 t = require_complete_type (t);
12290 if (t == error_mark_node)
12291 remove = true;
12294 if (need_implicitly_determined)
12296 const char *share_name = NULL;
12298 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12299 share_name = "threadprivate";
12300 else switch (c_omp_predetermined_sharing (t))
12302 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12303 break;
12304 case OMP_CLAUSE_DEFAULT_SHARED:
12305 /* const vars may be specified in firstprivate clause. */
12306 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12307 && TREE_READONLY (t))
12308 break;
12309 share_name = "shared";
12310 break;
12311 case OMP_CLAUSE_DEFAULT_PRIVATE:
12312 share_name = "private";
12313 break;
12314 default:
12315 gcc_unreachable ();
12317 if (share_name)
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "%qE is predetermined %qs for %qs",
12321 t, share_name,
12322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12323 remove = true;
12328 if (remove)
12329 *pc = OMP_CLAUSE_CHAIN (c);
12330 else
12331 pc = &OMP_CLAUSE_CHAIN (c);
12334 bitmap_obstack_release (NULL);
12335 return clauses;
12338 /* Create a transaction node. */
12340 tree
12341 c_finish_transaction (location_t loc, tree block, int flags)
12343 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12344 if (flags & TM_STMT_ATTR_OUTER)
12345 TRANSACTION_EXPR_OUTER (stmt) = 1;
12346 if (flags & TM_STMT_ATTR_RELAXED)
12347 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12348 return add_stmt (stmt);
12351 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12352 down to the element type of an array. */
12354 tree
12355 c_build_qualified_type (tree type, int type_quals)
12357 if (type == error_mark_node)
12358 return type;
12360 if (TREE_CODE (type) == ARRAY_TYPE)
12362 tree t;
12363 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12364 type_quals);
12366 /* See if we already have an identically qualified type. */
12367 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12369 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12370 && TYPE_NAME (t) == TYPE_NAME (type)
12371 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12372 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12373 TYPE_ATTRIBUTES (type)))
12374 break;
12376 if (!t)
12378 tree domain = TYPE_DOMAIN (type);
12380 t = build_variant_type_copy (type);
12381 TREE_TYPE (t) = element_type;
12383 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12384 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12385 SET_TYPE_STRUCTURAL_EQUALITY (t);
12386 else if (TYPE_CANONICAL (element_type) != element_type
12387 || (domain && TYPE_CANONICAL (domain) != domain))
12389 tree unqualified_canon
12390 = build_array_type (TYPE_CANONICAL (element_type),
12391 domain? TYPE_CANONICAL (domain)
12392 : NULL_TREE);
12393 TYPE_CANONICAL (t)
12394 = c_build_qualified_type (unqualified_canon, type_quals);
12396 else
12397 TYPE_CANONICAL (t) = t;
12399 return t;
12402 /* A restrict-qualified pointer type must be a pointer to object or
12403 incomplete type. Note that the use of POINTER_TYPE_P also allows
12404 REFERENCE_TYPEs, which is appropriate for C++. */
12405 if ((type_quals & TYPE_QUAL_RESTRICT)
12406 && (!POINTER_TYPE_P (type)
12407 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12409 error ("invalid use of %<restrict%>");
12410 type_quals &= ~TYPE_QUAL_RESTRICT;
12413 return build_qualified_type (type, type_quals);
12416 /* Build a VA_ARG_EXPR for the C parser. */
12418 tree
12419 c_build_va_arg (location_t loc, tree expr, tree type)
12421 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12422 warning_at (loc, OPT_Wc___compat,
12423 "C++ requires promoted type, not enum type, in %<va_arg%>");
12424 return build_va_arg (loc, expr, type);
12427 /* Return truthvalue of whether T1 is the same tree structure as T2.
12428 Return 1 if they are the same. Return 0 if they are different. */
12430 bool
12431 c_tree_equal (tree t1, tree t2)
12433 enum tree_code code1, code2;
12435 if (t1 == t2)
12436 return true;
12437 if (!t1 || !t2)
12438 return false;
12440 for (code1 = TREE_CODE (t1);
12441 CONVERT_EXPR_CODE_P (code1)
12442 || code1 == NON_LVALUE_EXPR;
12443 code1 = TREE_CODE (t1))
12444 t1 = TREE_OPERAND (t1, 0);
12445 for (code2 = TREE_CODE (t2);
12446 CONVERT_EXPR_CODE_P (code2)
12447 || code2 == NON_LVALUE_EXPR;
12448 code2 = TREE_CODE (t2))
12449 t2 = TREE_OPERAND (t2, 0);
12451 /* They might have become equal now. */
12452 if (t1 == t2)
12453 return true;
12455 if (code1 != code2)
12456 return false;
12458 switch (code1)
12460 case INTEGER_CST:
12461 return wi::eq_p (t1, t2);
12463 case REAL_CST:
12464 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12466 case STRING_CST:
12467 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12468 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12469 TREE_STRING_LENGTH (t1));
12471 case FIXED_CST:
12472 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12473 TREE_FIXED_CST (t2));
12475 case COMPLEX_CST:
12476 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12477 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12479 case VECTOR_CST:
12480 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12482 case CONSTRUCTOR:
12483 /* We need to do this when determining whether or not two
12484 non-type pointer to member function template arguments
12485 are the same. */
12486 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12487 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12488 return false;
12490 tree field, value;
12491 unsigned int i;
12492 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12494 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12495 if (!c_tree_equal (field, elt2->index)
12496 || !c_tree_equal (value, elt2->value))
12497 return false;
12500 return true;
12502 case TREE_LIST:
12503 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12504 return false;
12505 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12506 return false;
12507 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12509 case SAVE_EXPR:
12510 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12512 case CALL_EXPR:
12514 tree arg1, arg2;
12515 call_expr_arg_iterator iter1, iter2;
12516 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12517 return false;
12518 for (arg1 = first_call_expr_arg (t1, &iter1),
12519 arg2 = first_call_expr_arg (t2, &iter2);
12520 arg1 && arg2;
12521 arg1 = next_call_expr_arg (&iter1),
12522 arg2 = next_call_expr_arg (&iter2))
12523 if (!c_tree_equal (arg1, arg2))
12524 return false;
12525 if (arg1 || arg2)
12526 return false;
12527 return true;
12530 case TARGET_EXPR:
12532 tree o1 = TREE_OPERAND (t1, 0);
12533 tree o2 = TREE_OPERAND (t2, 0);
12535 /* Special case: if either target is an unallocated VAR_DECL,
12536 it means that it's going to be unified with whatever the
12537 TARGET_EXPR is really supposed to initialize, so treat it
12538 as being equivalent to anything. */
12539 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12540 && !DECL_RTL_SET_P (o1))
12541 /*Nop*/;
12542 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12543 && !DECL_RTL_SET_P (o2))
12544 /*Nop*/;
12545 else if (!c_tree_equal (o1, o2))
12546 return false;
12548 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12551 case COMPONENT_REF:
12552 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12553 return false;
12554 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12556 case PARM_DECL:
12557 case VAR_DECL:
12558 case CONST_DECL:
12559 case FIELD_DECL:
12560 case FUNCTION_DECL:
12561 case IDENTIFIER_NODE:
12562 case SSA_NAME:
12563 return false;
12565 case TREE_VEC:
12567 unsigned ix;
12568 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12569 return false;
12570 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12571 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12572 TREE_VEC_ELT (t2, ix)))
12573 return false;
12574 return true;
12577 default:
12578 break;
12581 switch (TREE_CODE_CLASS (code1))
12583 case tcc_unary:
12584 case tcc_binary:
12585 case tcc_comparison:
12586 case tcc_expression:
12587 case tcc_vl_exp:
12588 case tcc_reference:
12589 case tcc_statement:
12591 int i, n = TREE_OPERAND_LENGTH (t1);
12593 switch (code1)
12595 case PREINCREMENT_EXPR:
12596 case PREDECREMENT_EXPR:
12597 case POSTINCREMENT_EXPR:
12598 case POSTDECREMENT_EXPR:
12599 n = 1;
12600 break;
12601 case ARRAY_REF:
12602 n = 2;
12603 break;
12604 default:
12605 break;
12608 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12609 && n != TREE_OPERAND_LENGTH (t2))
12610 return false;
12612 for (i = 0; i < n; ++i)
12613 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12614 return false;
12616 return true;
12619 case tcc_type:
12620 return comptypes (t1, t2);
12621 default:
12622 gcc_unreachable ();
12624 /* We can get here with --disable-checking. */
12625 return false;
12628 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12629 spawn-helper and BODY is the newly created body for FNDECL. */
12631 void
12632 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12634 tree list = alloc_stmt_list ();
12635 tree frame = make_cilk_frame (fndecl);
12636 tree dtor = create_cilk_function_exit (frame, false, true);
12637 add_local_decl (cfun, frame);
12639 DECL_SAVED_TREE (fndecl) = list;
12640 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12641 frame);
12642 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12643 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12645 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12646 append_to_statement_list (detach_expr, &body_list);
12648 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12649 body = fold_build_cleanup_point_expr (void_type_node, body);
12651 append_to_statement_list (body, &body_list);
12652 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12653 body_list, dtor), &list);