2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / c / c-typeck.c
blob65aad45651c9b29ecac7b217244b3f123c6d4558
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"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56 enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
63 /* The level of nesting inside "__alignof__". */
64 int in_alignof;
66 /* The level of nesting inside "sizeof". */
67 int in_sizeof;
69 /* The level of nesting inside "typeof". */
70 int in_typeof;
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg;
76 /* Nonzero if we've already printed a "missing braces around initializer"
77 message within this initializer. */
78 static int missing_braces_mentioned;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
87 static int comp_target_types (location_t, tree, tree);
88 static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
91 static tree lookup_field (tree, tree);
92 static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
95 static tree pointer_diff (location_t, tree, tree);
96 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
97 enum impl_conv, bool, tree, tree, int);
98 static tree valid_compound_expr_initializer (tree, tree);
99 static void push_string (const char *);
100 static void push_member_name (tree);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (int, const char *);
104 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
105 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
106 struct obstack *);
107 static void output_pending_init_elements (int, struct obstack *);
108 static int set_designator (int, struct obstack *);
109 static void push_range_stack (tree, struct obstack *);
110 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
111 static void set_nonincremental_init (struct obstack *);
112 static void set_nonincremental_init_from_string (tree, struct obstack *);
113 static tree find_init_member (tree, struct obstack *);
114 static void readonly_warning (tree, enum lvalue_use);
115 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
116 static void record_maybe_used_decl (tree);
117 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
119 /* Return true if EXP is a null pointer constant, false otherwise. */
121 static bool
122 null_pointer_constant_p (const_tree expr)
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type = TREE_TYPE (expr);
127 return (TREE_CODE (expr) == INTEGER_CST
128 && !TREE_OVERFLOW (expr)
129 && integer_zerop (expr)
130 && (INTEGRAL_TYPE_P (type)
131 || (TREE_CODE (type) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type))
133 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
136 /* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141 static tree
142 note_integer_operands (tree expr)
144 tree ret;
145 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 ret = copy_node (expr);
148 TREE_OVERFLOW (ret) = 1;
150 else
152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 return ret;
158 /* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 expression. */
163 static inline tree
164 remove_c_maybe_const_expr (tree expr)
166 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
167 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 else
169 return expr;
172 \f/* This is a cache to hold if two types are compatible or not. */
174 struct tagged_tu_seen_cache {
175 const struct tagged_tu_seen_cache * next;
176 const_tree t1;
177 const_tree t2;
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
180 int val;
183 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
184 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186 /* Do `exp = require_complete_type (exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.) */
189 tree
190 require_complete_type (tree value)
192 tree type = TREE_TYPE (value);
194 if (value == error_mark_node || type == error_mark_node)
195 return error_mark_node;
197 /* First, detect a valid value with a complete type. */
198 if (COMPLETE_TYPE_P (type))
199 return value;
201 c_incomplete_type_error (value, type);
202 return error_mark_node;
205 /* Print an error message for invalid use of an incomplete type.
206 VALUE is the expression that was used (or 0 if that isn't known)
207 and TYPE is the type that was invalid. */
209 void
210 c_incomplete_type_error (const_tree value, const_tree type)
212 const char *type_code_string;
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type) == ERROR_MARK)
216 return;
218 if (value != 0 && (TREE_CODE (value) == VAR_DECL
219 || TREE_CODE (value) == PARM_DECL))
220 error ("%qD has an incomplete type", value);
221 else
223 retry:
224 /* We must print an error message. Be clever about what it says. */
226 switch (TREE_CODE (type))
228 case RECORD_TYPE:
229 type_code_string = "struct";
230 break;
232 case UNION_TYPE:
233 type_code_string = "union";
234 break;
236 case ENUMERAL_TYPE:
237 type_code_string = "enum";
238 break;
240 case VOID_TYPE:
241 error ("invalid use of void expression");
242 return;
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 error ("invalid use of flexible array member");
250 return;
252 type = TREE_TYPE (type);
253 goto retry;
255 error ("invalid use of array with unspecified bounds");
256 return;
258 default:
259 gcc_unreachable ();
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
263 error ("invalid use of undefined type %<%s %E%>",
264 type_code_string, TYPE_NAME (type));
265 else
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
267 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
271 /* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
274 tree
275 c_type_promotes_to (tree type)
277 tree ret = NULL_TREE;
279 if (TYPE_MAIN_VARIANT (type) == float_type_node)
280 ret = double_type_node;
281 else if (c_promoting_integer_type_p (type))
283 /* Preserve unsignedness if not really getting any wider. */
284 if (TYPE_UNSIGNED (type)
285 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
286 ret = unsigned_type_node;
287 else
288 ret = integer_type_node;
291 if (ret != NULL_TREE)
292 return (TYPE_ATOMIC (type)
293 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
294 : ret);
296 return type;
299 /* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
303 static bool
304 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
306 if (as1 == as2)
308 *common = as1;
309 return true;
311 else if (targetm.addr_space.subset_p (as1, as2))
313 *common = as2;
314 return true;
316 else if (targetm.addr_space.subset_p (as2, as1))
318 *common = as1;
319 return true;
321 else
322 return false;
325 /* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
328 static tree
329 qualify_type (tree type, tree like)
331 addr_space_t as_type = TYPE_ADDR_SPACE (type);
332 addr_space_t as_like = TYPE_ADDR_SPACE (like);
333 addr_space_t as_common;
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type, as_like, &as_common))
339 as_common = as_type;
340 error ("%qT and %qT are in disjoint named address spaces",
341 type, like);
344 return c_build_qualified_type (type,
345 TYPE_QUALS_NO_ADDR_SPACE (type)
346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
347 | ENCODE_QUAL_ADDR_SPACE (as_common));
350 /* Return true iff the given tree T is a variable length array. */
352 bool
353 c_vla_type_p (const_tree t)
355 if (TREE_CODE (t) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t))
357 return true;
358 return false;
361 /* Return the composite type of two compatible types.
363 We assume that comptypes has already been done and returned
364 nonzero; if that isn't so, this may crash. In particular, we
365 assume that qualifiers match. */
367 tree
368 composite_type (tree t1, tree t2)
370 enum tree_code code1;
371 enum tree_code code2;
372 tree attributes;
374 /* Save time if the two types are the same. */
376 if (t1 == t2) return t1;
378 /* If one type is nonsense, use the other. */
379 if (t1 == error_mark_node)
380 return t2;
381 if (t2 == error_mark_node)
382 return t1;
384 code1 = TREE_CODE (t1);
385 code2 = TREE_CODE (t2);
387 /* Merge the attributes. */
388 attributes = targetm.merge_type_attributes (t1, t2);
390 /* If one is an enumerated type and the other is the compatible
391 integer type, the composite type might be either of the two
392 (DR#013 question 3). For consistency, use the enumerated type as
393 the composite type. */
395 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
396 return t1;
397 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
398 return t2;
400 gcc_assert (code1 == code2);
402 switch (code1)
404 case POINTER_TYPE:
405 /* For two pointers, do this recursively on the target type. */
407 tree pointed_to_1 = TREE_TYPE (t1);
408 tree pointed_to_2 = TREE_TYPE (t2);
409 tree target = composite_type (pointed_to_1, pointed_to_2);
410 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
415 case ARRAY_TYPE:
417 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
418 int quals;
419 tree unqual_elt;
420 tree d1 = TYPE_DOMAIN (t1);
421 tree d2 = TYPE_DOMAIN (t2);
422 bool d1_variable, d2_variable;
423 bool d1_zero, d2_zero;
424 bool t1_complete, t2_complete;
426 /* We should not have any type quals on arrays at all. */
427 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
428 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
430 t1_complete = COMPLETE_TYPE_P (t1);
431 t2_complete = COMPLETE_TYPE_P (t2);
433 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
434 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
436 d1_variable = (!d1_zero
437 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
438 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
439 d2_variable = (!d2_zero
440 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
441 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
442 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
443 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
445 /* Save space: see if the result is identical to one of the args. */
446 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
447 && (d2_variable || d2_zero || !d1_variable))
448 return build_type_attribute_variant (t1, attributes);
449 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
450 && (d1_variable || d1_zero || !d2_variable))
451 return build_type_attribute_variant (t2, attributes);
453 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
454 return build_type_attribute_variant (t1, attributes);
455 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
456 return build_type_attribute_variant (t2, attributes);
458 /* Merge the element types, and have a size if either arg has
459 one. We may have qualifiers on the element types. To set
460 up TYPE_MAIN_VARIANT correctly, we need to form the
461 composite of the unqualified types and add the qualifiers
462 back at the end. */
463 quals = TYPE_QUALS (strip_array_types (elt));
464 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
465 t1 = build_array_type (unqual_elt,
466 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
467 && (d2_variable
468 || d2_zero
469 || !d1_variable))
470 ? t1
471 : t2));
472 /* Ensure a composite type involving a zero-length array type
473 is a zero-length type not an incomplete type. */
474 if (d1_zero && d2_zero
475 && (t1_complete || t2_complete)
476 && !COMPLETE_TYPE_P (t1))
478 TYPE_SIZE (t1) = bitsize_zero_node;
479 TYPE_SIZE_UNIT (t1) = size_zero_node;
481 t1 = c_build_qualified_type (t1, quals);
482 return build_type_attribute_variant (t1, attributes);
485 case ENUMERAL_TYPE:
486 case RECORD_TYPE:
487 case UNION_TYPE:
488 if (attributes != NULL)
490 /* Try harder not to create a new aggregate type. */
491 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
492 return t1;
493 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
494 return t2;
496 return build_type_attribute_variant (t1, attributes);
498 case FUNCTION_TYPE:
499 /* Function types: prefer the one that specified arg types.
500 If both do, merge the arg types. Also merge the return types. */
502 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
503 tree p1 = TYPE_ARG_TYPES (t1);
504 tree p2 = TYPE_ARG_TYPES (t2);
505 int len;
506 tree newargs, n;
507 int i;
509 /* Save space: see if the result is identical to one of the args. */
510 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
511 return build_type_attribute_variant (t1, attributes);
512 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
513 return build_type_attribute_variant (t2, attributes);
515 /* Simple way if one arg fails to specify argument types. */
516 if (TYPE_ARG_TYPES (t1) == 0)
518 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
519 t1 = build_type_attribute_variant (t1, attributes);
520 return qualify_type (t1, t2);
522 if (TYPE_ARG_TYPES (t2) == 0)
524 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
525 t1 = build_type_attribute_variant (t1, attributes);
526 return qualify_type (t1, t2);
529 /* If both args specify argument types, we must merge the two
530 lists, argument by argument. */
532 len = list_length (p1);
533 newargs = 0;
535 for (i = 0; i < len; i++)
536 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
538 n = newargs;
540 for (; p1;
541 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
543 /* A null type means arg type is not specified.
544 Take whatever the other function type has. */
545 if (TREE_VALUE (p1) == 0)
547 TREE_VALUE (n) = TREE_VALUE (p2);
548 goto parm_done;
550 if (TREE_VALUE (p2) == 0)
552 TREE_VALUE (n) = TREE_VALUE (p1);
553 goto parm_done;
556 /* Given wait (union {union wait *u; int *i} *)
557 and wait (union wait *),
558 prefer union wait * as type of parm. */
559 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
560 && TREE_VALUE (p1) != TREE_VALUE (p2))
562 tree memb;
563 tree mv2 = TREE_VALUE (p2);
564 if (mv2 && mv2 != error_mark_node
565 && TREE_CODE (mv2) != ARRAY_TYPE)
566 mv2 = TYPE_MAIN_VARIANT (mv2);
567 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
568 memb; memb = DECL_CHAIN (memb))
570 tree mv3 = TREE_TYPE (memb);
571 if (mv3 && mv3 != error_mark_node
572 && TREE_CODE (mv3) != ARRAY_TYPE)
573 mv3 = TYPE_MAIN_VARIANT (mv3);
574 if (comptypes (mv3, mv2))
576 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
577 TREE_VALUE (p2));
578 pedwarn (input_location, OPT_Wpedantic,
579 "function types not truly compatible in ISO C");
580 goto parm_done;
584 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
585 && TREE_VALUE (p2) != TREE_VALUE (p1))
587 tree memb;
588 tree mv1 = TREE_VALUE (p1);
589 if (mv1 && mv1 != error_mark_node
590 && TREE_CODE (mv1) != ARRAY_TYPE)
591 mv1 = TYPE_MAIN_VARIANT (mv1);
592 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
593 memb; memb = DECL_CHAIN (memb))
595 tree mv3 = TREE_TYPE (memb);
596 if (mv3 && mv3 != error_mark_node
597 && TREE_CODE (mv3) != ARRAY_TYPE)
598 mv3 = TYPE_MAIN_VARIANT (mv3);
599 if (comptypes (mv3, mv1))
601 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
602 TREE_VALUE (p1));
603 pedwarn (input_location, OPT_Wpedantic,
604 "function types not truly compatible in ISO C");
605 goto parm_done;
609 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
610 parm_done: ;
613 t1 = build_function_type (valtype, newargs);
614 t1 = qualify_type (t1, t2);
615 /* ... falls through ... */
618 default:
619 return build_type_attribute_variant (t1, attributes);
624 /* Return the type of a conditional expression between pointers to
625 possibly differently qualified versions of compatible types.
627 We assume that comp_target_types has already been done and returned
628 nonzero; if that isn't so, this may crash. */
630 static tree
631 common_pointer_type (tree t1, tree t2)
633 tree attributes;
634 tree pointed_to_1, mv1;
635 tree pointed_to_2, mv2;
636 tree target;
637 unsigned target_quals;
638 addr_space_t as1, as2, as_common;
639 int quals1, quals2;
641 /* Save time if the two types are the same. */
643 if (t1 == t2) return t1;
645 /* If one type is nonsense, use the other. */
646 if (t1 == error_mark_node)
647 return t2;
648 if (t2 == error_mark_node)
649 return t1;
651 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
652 && TREE_CODE (t2) == POINTER_TYPE);
654 /* Merge the attributes. */
655 attributes = targetm.merge_type_attributes (t1, t2);
657 /* Find the composite type of the target types, and combine the
658 qualifiers of the two types' targets. Do not lose qualifiers on
659 array element types by taking the TYPE_MAIN_VARIANT. */
660 mv1 = pointed_to_1 = TREE_TYPE (t1);
661 mv2 = pointed_to_2 = TREE_TYPE (t2);
662 if (TREE_CODE (mv1) != ARRAY_TYPE)
663 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
664 if (TREE_CODE (mv2) != ARRAY_TYPE)
665 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
666 target = composite_type (mv1, mv2);
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
671 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
672 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
674 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
675 target_quals = (quals1 & quals2);
676 else
677 target_quals = (quals1 | quals2);
679 /* If the two named address spaces are different, determine the common
680 superset address space. This is guaranteed to exist due to the
681 assumption that comp_target_type returned non-zero. */
682 as1 = TYPE_ADDR_SPACE (pointed_to_1);
683 as2 = TYPE_ADDR_SPACE (pointed_to_2);
684 if (!addr_space_superset (as1, as2, &as_common))
685 gcc_unreachable ();
687 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
689 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
690 return build_type_attribute_variant (t1, attributes);
693 /* Return the common type for two arithmetic types under the usual
694 arithmetic conversions. The default conversions have already been
695 applied, and enumerated types converted to their compatible integer
696 types. The resulting type is unqualified and has no attributes.
698 This is the type for the result of most arithmetic operations
699 if the operands have the given two types. */
701 static tree
702 c_common_type (tree t1, tree t2)
704 enum tree_code code1;
705 enum tree_code code2;
707 /* If one type is nonsense, use the other. */
708 if (t1 == error_mark_node)
709 return t2;
710 if (t2 == error_mark_node)
711 return t1;
713 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
714 t1 = TYPE_MAIN_VARIANT (t1);
716 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
717 t2 = TYPE_MAIN_VARIANT (t2);
719 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
720 t1 = build_type_attribute_variant (t1, NULL_TREE);
722 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
723 t2 = build_type_attribute_variant (t2, NULL_TREE);
725 /* Save time if the two types are the same. */
727 if (t1 == t2) return t1;
729 code1 = TREE_CODE (t1);
730 code2 = TREE_CODE (t2);
732 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
733 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
734 || code1 == INTEGER_TYPE);
735 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
736 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
737 || code2 == INTEGER_TYPE);
739 /* When one operand is a decimal float type, the other operand cannot be
740 a generic float type or a complex type. We also disallow vector types
741 here. */
742 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
743 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
745 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
747 error ("can%'t mix operands of decimal float and vector types");
748 return error_mark_node;
750 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
752 error ("can%'t mix operands of decimal float and complex types");
753 return error_mark_node;
755 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
757 error ("can%'t mix operands of decimal float and other float types");
758 return error_mark_node;
762 /* If one type is a vector type, return that type. (How the usual
763 arithmetic conversions apply to the vector types extension is not
764 precisely specified.) */
765 if (code1 == VECTOR_TYPE)
766 return t1;
768 if (code2 == VECTOR_TYPE)
769 return t2;
771 /* If one type is complex, form the common type of the non-complex
772 components, then make that complex. Use T1 or T2 if it is the
773 required type. */
774 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
776 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
777 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
778 tree subtype = c_common_type (subtype1, subtype2);
780 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
781 return t1;
782 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
783 return t2;
784 else
785 return build_complex_type (subtype);
788 /* If only one is real, use it as the result. */
790 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
791 return t1;
793 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
794 return t2;
796 /* If both are real and either are decimal floating point types, use
797 the decimal floating point type with the greater precision. */
799 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
801 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
803 return dfloat128_type_node;
804 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
805 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
806 return dfloat64_type_node;
807 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
808 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
809 return dfloat32_type_node;
812 /* Deal with fixed-point types. */
813 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
815 unsigned int unsignedp = 0, satp = 0;
816 enum machine_mode m1, m2;
817 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
819 m1 = TYPE_MODE (t1);
820 m2 = TYPE_MODE (t2);
822 /* If one input type is saturating, the result type is saturating. */
823 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
824 satp = 1;
826 /* If both fixed-point types are unsigned, the result type is unsigned.
827 When mixing fixed-point and integer types, follow the sign of the
828 fixed-point type.
829 Otherwise, the result type is signed. */
830 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
831 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
832 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t1))
834 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t2)))
836 unsignedp = 1;
838 /* The result type is signed. */
839 if (unsignedp == 0)
841 /* If the input type is unsigned, we need to convert to the
842 signed type. */
843 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
845 enum mode_class mclass = (enum mode_class) 0;
846 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
867 if (code1 == FIXED_POINT_TYPE)
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
872 else
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
879 if (code2 == FIXED_POINT_TYPE)
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
884 else
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
897 /* Both real or both integers; use the one with greater precision. */
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
918 return long_long_integer_type_node;
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
936 /* Likewise, prefer long double to double even if same size. */
937 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
938 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
939 return long_double_type_node;
941 /* Likewise, prefer double to float even if same size.
942 We got a couple of embedded targets with 32 bit doubles, and the
943 pdp11 might have 64 bit floats. */
944 if (TYPE_MAIN_VARIANT (t1) == double_type_node
945 || TYPE_MAIN_VARIANT (t2) == double_type_node)
946 return double_type_node;
948 /* Otherwise prefer the unsigned one. */
950 if (TYPE_UNSIGNED (t1))
951 return t1;
952 else
953 return t2;
956 /* Wrapper around c_common_type that is used by c-common.c and other
957 front end optimizations that remove promotions. ENUMERAL_TYPEs
958 are allowed here and are converted to their compatible integer types.
959 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
960 preferably a non-Boolean type as the common type. */
961 tree
962 common_type (tree t1, tree t2)
964 if (TREE_CODE (t1) == ENUMERAL_TYPE)
965 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
966 if (TREE_CODE (t2) == ENUMERAL_TYPE)
967 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
969 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
970 if (TREE_CODE (t1) == BOOLEAN_TYPE
971 && TREE_CODE (t2) == BOOLEAN_TYPE)
972 return boolean_type_node;
974 /* If either type is BOOLEAN_TYPE, then return the other. */
975 if (TREE_CODE (t1) == BOOLEAN_TYPE)
976 return t2;
977 if (TREE_CODE (t2) == BOOLEAN_TYPE)
978 return t1;
980 return c_common_type (t1, t2);
983 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
984 or various other operations. Return 2 if they are compatible
985 but a warning may be needed if you use them together. */
988 comptypes (tree type1, tree type2)
990 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
991 int val;
993 val = comptypes_internal (type1, type2, NULL, NULL);
994 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
996 return val;
999 /* Like comptypes, but if it returns non-zero because enum and int are
1000 compatible, it sets *ENUM_AND_INT_P to true. */
1002 static int
1003 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1008 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1011 return val;
1014 /* Like comptypes, but if it returns nonzero for different types, it
1015 sets *DIFFERENT_TYPES_P to true. */
1018 comptypes_check_different_types (tree type1, tree type2,
1019 bool *different_types_p)
1021 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1022 int val;
1024 val = comptypes_internal (type1, type2, NULL, different_types_p);
1025 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1027 return val;
1030 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1031 or various other operations. Return 2 if they are compatible
1032 but a warning may be needed if you use them together. If
1033 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1034 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1035 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1036 NULL, and the types are compatible but different enough not to be
1037 permitted in C11 typedef redeclarations, then this sets
1038 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1039 false, but may or may not be set if the types are incompatible.
1040 This differs from comptypes, in that we don't free the seen
1041 types. */
1043 static int
1044 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1045 bool *different_types_p)
1047 const_tree t1 = type1;
1048 const_tree t2 = type2;
1049 int attrval, val;
1051 /* Suppress errors caused by previously reported errors. */
1053 if (t1 == t2 || !t1 || !t2
1054 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1055 return 1;
1057 /* Enumerated types are compatible with integer types, but this is
1058 not transitive: two enumerated types in the same translation unit
1059 are compatible with each other only if they are the same type. */
1061 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1063 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1064 if (TREE_CODE (t2) != VOID_TYPE)
1066 if (enum_and_int_p != NULL)
1067 *enum_and_int_p = true;
1068 if (different_types_p != NULL)
1069 *different_types_p = true;
1072 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1074 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1075 if (TREE_CODE (t1) != VOID_TYPE)
1077 if (enum_and_int_p != NULL)
1078 *enum_and_int_p = true;
1079 if (different_types_p != NULL)
1080 *different_types_p = true;
1084 if (t1 == t2)
1085 return 1;
1087 /* Different classes of types can't be compatible. */
1089 if (TREE_CODE (t1) != TREE_CODE (t2))
1090 return 0;
1092 /* Qualifiers must match. C99 6.7.3p9 */
1094 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1095 return 0;
1097 /* Allow for two different type nodes which have essentially the same
1098 definition. Note that we already checked for equality of the type
1099 qualifiers (just above). */
1101 if (TREE_CODE (t1) != ARRAY_TYPE
1102 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1103 return 1;
1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1106 if (!(attrval = comp_type_attributes (t1, t2)))
1107 return 0;
1109 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1110 val = 0;
1112 switch (TREE_CODE (t1))
1114 case POINTER_TYPE:
1115 /* Do not remove mode or aliasing information. */
1116 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1117 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1118 break;
1119 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1120 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1121 enum_and_int_p, different_types_p));
1122 break;
1124 case FUNCTION_TYPE:
1125 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1126 different_types_p);
1127 break;
1129 case ARRAY_TYPE:
1131 tree d1 = TYPE_DOMAIN (t1);
1132 tree d2 = TYPE_DOMAIN (t2);
1133 bool d1_variable, d2_variable;
1134 bool d1_zero, d2_zero;
1135 val = 1;
1137 /* Target types must match incl. qualifiers. */
1138 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1139 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1140 enum_and_int_p,
1141 different_types_p)))
1142 return 0;
1144 if (different_types_p != NULL
1145 && (d1 == 0) != (d2 == 0))
1146 *different_types_p = true;
1147 /* Sizes must match unless one is missing or variable. */
1148 if (d1 == 0 || d2 == 0 || d1 == d2)
1149 break;
1151 d1_zero = !TYPE_MAX_VALUE (d1);
1152 d2_zero = !TYPE_MAX_VALUE (d2);
1154 d1_variable = (!d1_zero
1155 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1156 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1157 d2_variable = (!d2_zero
1158 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1159 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1160 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1161 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1163 if (different_types_p != NULL
1164 && d1_variable != d2_variable)
1165 *different_types_p = true;
1166 if (d1_variable || d2_variable)
1167 break;
1168 if (d1_zero && d2_zero)
1169 break;
1170 if (d1_zero || d2_zero
1171 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1172 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1173 val = 0;
1175 break;
1178 case ENUMERAL_TYPE:
1179 case RECORD_TYPE:
1180 case UNION_TYPE:
1181 if (val != 1 && !same_translation_unit_p (t1, t2))
1183 tree a1 = TYPE_ATTRIBUTES (t1);
1184 tree a2 = TYPE_ATTRIBUTES (t2);
1186 if (! attribute_list_contained (a1, a2)
1187 && ! attribute_list_contained (a2, a1))
1188 break;
1190 if (attrval != 2)
1191 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1192 different_types_p);
1193 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 different_types_p);
1196 break;
1198 case VECTOR_TYPE:
1199 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1200 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1201 enum_and_int_p, different_types_p));
1202 break;
1204 default:
1205 break;
1207 return attrval == 2 && val == 1 ? 2 : val;
1210 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1211 their qualifiers, except for named address spaces. If the pointers point to
1212 different named addresses, then we must determine if one address space is a
1213 subset of the other. */
1215 static int
1216 comp_target_types (location_t location, tree ttl, tree ttr)
1218 int val;
1219 tree mvl = TREE_TYPE (ttl);
1220 tree mvr = TREE_TYPE (ttr);
1221 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1222 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1223 addr_space_t as_common;
1224 bool enum_and_int_p;
1226 /* Fail if pointers point to incompatible address spaces. */
1227 if (!addr_space_superset (asl, asr, &as_common))
1228 return 0;
1230 /* Do not lose qualifiers on element types of array types that are
1231 pointer targets by taking their TYPE_MAIN_VARIANT. */
1232 if (TREE_CODE (mvl) != ARRAY_TYPE)
1233 mvl = (TYPE_ATOMIC (mvl)
1234 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1235 : TYPE_MAIN_VARIANT (mvl));
1236 if (TREE_CODE (mvr) != ARRAY_TYPE)
1237 mvr = (TYPE_ATOMIC (mvr)
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1239 : TYPE_MAIN_VARIANT (mvr));
1240 enum_and_int_p = false;
1241 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1243 if (val == 2)
1244 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1246 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1247 warning_at (location, OPT_Wc___compat,
1248 "pointer target types incompatible in C++");
1250 return val;
1253 /* Subroutines of `comptypes'. */
1255 /* Determine whether two trees derive from the same translation unit.
1256 If the CONTEXT chain ends in a null, that tree's context is still
1257 being parsed, so if two trees have context chains ending in null,
1258 they're in the same translation unit. */
1260 same_translation_unit_p (const_tree t1, const_tree t2)
1262 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1263 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1265 case tcc_declaration:
1266 t1 = DECL_CONTEXT (t1); break;
1267 case tcc_type:
1268 t1 = TYPE_CONTEXT (t1); break;
1269 case tcc_exceptional:
1270 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1271 default: gcc_unreachable ();
1274 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1275 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1277 case tcc_declaration:
1278 t2 = DECL_CONTEXT (t2); break;
1279 case tcc_type:
1280 t2 = TYPE_CONTEXT (t2); break;
1281 case tcc_exceptional:
1282 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1283 default: gcc_unreachable ();
1286 return t1 == t2;
1289 /* Allocate the seen two types, assuming that they are compatible. */
1291 static struct tagged_tu_seen_cache *
1292 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1294 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1295 tu->next = tagged_tu_seen_base;
1296 tu->t1 = t1;
1297 tu->t2 = t2;
1299 tagged_tu_seen_base = tu;
1301 /* The C standard says that two structures in different translation
1302 units are compatible with each other only if the types of their
1303 fields are compatible (among other things). We assume that they
1304 are compatible until proven otherwise when building the cache.
1305 An example where this can occur is:
1306 struct a
1308 struct a *next;
1310 If we are comparing this against a similar struct in another TU,
1311 and did not assume they were compatible, we end up with an infinite
1312 loop. */
1313 tu->val = 1;
1314 return tu;
1317 /* Free the seen types until we get to TU_TIL. */
1319 static void
1320 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1322 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1323 while (tu != tu_til)
1325 const struct tagged_tu_seen_cache *const tu1
1326 = (const struct tagged_tu_seen_cache *) tu;
1327 tu = tu1->next;
1328 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1330 tagged_tu_seen_base = tu_til;
1333 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1334 compatible. If the two types are not the same (which has been
1335 checked earlier), this can only happen when multiple translation
1336 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1337 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1338 comptypes_internal. */
1340 static int
1341 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1342 bool *enum_and_int_p, bool *different_types_p)
1344 tree s1, s2;
1345 bool needs_warning = false;
1347 /* We have to verify that the tags of the types are the same. This
1348 is harder than it looks because this may be a typedef, so we have
1349 to go look at the original type. It may even be a typedef of a
1350 typedef...
1351 In the case of compiler-created builtin structs the TYPE_DECL
1352 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1353 while (TYPE_NAME (t1)
1354 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1355 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1356 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1358 while (TYPE_NAME (t2)
1359 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1360 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1361 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1363 /* C90 didn't have the requirement that the two tags be the same. */
1364 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1365 return 0;
1367 /* C90 didn't say what happened if one or both of the types were
1368 incomplete; we choose to follow C99 rules here, which is that they
1369 are compatible. */
1370 if (TYPE_SIZE (t1) == NULL
1371 || TYPE_SIZE (t2) == NULL)
1372 return 1;
1375 const struct tagged_tu_seen_cache * tts_i;
1376 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1377 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1378 return tts_i->val;
1381 switch (TREE_CODE (t1))
1383 case ENUMERAL_TYPE:
1385 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1386 /* Speed up the case where the type values are in the same order. */
1387 tree tv1 = TYPE_VALUES (t1);
1388 tree tv2 = TYPE_VALUES (t2);
1390 if (tv1 == tv2)
1392 return 1;
1395 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1397 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1398 break;
1399 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1401 tu->val = 0;
1402 return 0;
1406 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1408 return 1;
1410 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1412 tu->val = 0;
1413 return 0;
1416 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1418 tu->val = 0;
1419 return 0;
1422 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1424 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1425 if (s2 == NULL
1426 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1428 tu->val = 0;
1429 return 0;
1432 return 1;
1435 case UNION_TYPE:
1437 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1438 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1440 tu->val = 0;
1441 return 0;
1444 /* Speed up the common case where the fields are in the same order. */
1445 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1446 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1448 int result;
1450 if (DECL_NAME (s1) != DECL_NAME (s2))
1451 break;
1452 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1453 enum_and_int_p, different_types_p);
1455 if (result != 1 && !DECL_NAME (s1))
1456 break;
1457 if (result == 0)
1459 tu->val = 0;
1460 return 0;
1462 if (result == 2)
1463 needs_warning = true;
1465 if (TREE_CODE (s1) == FIELD_DECL
1466 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1467 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1469 tu->val = 0;
1470 return 0;
1473 if (!s1 && !s2)
1475 tu->val = needs_warning ? 2 : 1;
1476 return tu->val;
1479 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1481 bool ok = false;
1483 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1484 if (DECL_NAME (s1) == DECL_NAME (s2))
1486 int result;
1488 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1489 enum_and_int_p,
1490 different_types_p);
1492 if (result != 1 && !DECL_NAME (s1))
1493 continue;
1494 if (result == 0)
1496 tu->val = 0;
1497 return 0;
1499 if (result == 2)
1500 needs_warning = true;
1502 if (TREE_CODE (s1) == FIELD_DECL
1503 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1504 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1505 break;
1507 ok = true;
1508 break;
1510 if (!ok)
1512 tu->val = 0;
1513 return 0;
1516 tu->val = needs_warning ? 2 : 10;
1517 return tu->val;
1520 case RECORD_TYPE:
1522 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1524 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1525 s1 && s2;
1526 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1528 int result;
1529 if (TREE_CODE (s1) != TREE_CODE (s2)
1530 || DECL_NAME (s1) != DECL_NAME (s2))
1531 break;
1532 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1533 enum_and_int_p, different_types_p);
1534 if (result == 0)
1535 break;
1536 if (result == 2)
1537 needs_warning = true;
1539 if (TREE_CODE (s1) == FIELD_DECL
1540 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1541 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1542 break;
1544 if (s1 && s2)
1545 tu->val = 0;
1546 else
1547 tu->val = needs_warning ? 2 : 1;
1548 return tu->val;
1551 default:
1552 gcc_unreachable ();
1556 /* Return 1 if two function types F1 and F2 are compatible.
1557 If either type specifies no argument types,
1558 the other must specify a fixed number of self-promoting arg types.
1559 Otherwise, if one type specifies only the number of arguments,
1560 the other must specify that number of self-promoting arg types.
1561 Otherwise, the argument types must match.
1562 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1564 static int
1565 function_types_compatible_p (const_tree f1, const_tree f2,
1566 bool *enum_and_int_p, bool *different_types_p)
1568 tree args1, args2;
1569 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1570 int val = 1;
1571 int val1;
1572 tree ret1, ret2;
1574 ret1 = TREE_TYPE (f1);
1575 ret2 = TREE_TYPE (f2);
1577 /* 'volatile' qualifiers on a function's return type used to mean
1578 the function is noreturn. */
1579 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1580 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1581 if (TYPE_VOLATILE (ret1))
1582 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1583 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1584 if (TYPE_VOLATILE (ret2))
1585 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1586 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1587 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1588 if (val == 0)
1589 return 0;
1591 args1 = TYPE_ARG_TYPES (f1);
1592 args2 = TYPE_ARG_TYPES (f2);
1594 if (different_types_p != NULL
1595 && (args1 == 0) != (args2 == 0))
1596 *different_types_p = true;
1598 /* An unspecified parmlist matches any specified parmlist
1599 whose argument types don't need default promotions. */
1601 if (args1 == 0)
1603 if (!self_promoting_args_p (args2))
1604 return 0;
1605 /* If one of these types comes from a non-prototype fn definition,
1606 compare that with the other type's arglist.
1607 If they don't match, ask for a warning (but no error). */
1608 if (TYPE_ACTUAL_ARG_TYPES (f1)
1609 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1610 enum_and_int_p, different_types_p))
1611 val = 2;
1612 return val;
1614 if (args2 == 0)
1616 if (!self_promoting_args_p (args1))
1617 return 0;
1618 if (TYPE_ACTUAL_ARG_TYPES (f2)
1619 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1620 enum_and_int_p, different_types_p))
1621 val = 2;
1622 return val;
1625 /* Both types have argument lists: compare them and propagate results. */
1626 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1627 different_types_p);
1628 return val1 != 1 ? val1 : val;
1631 /* Check two lists of types for compatibility, returning 0 for
1632 incompatible, 1 for compatible, or 2 for compatible with
1633 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1634 comptypes_internal. */
1636 static int
1637 type_lists_compatible_p (const_tree args1, const_tree args2,
1638 bool *enum_and_int_p, bool *different_types_p)
1640 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1641 int val = 1;
1642 int newval = 0;
1644 while (1)
1646 tree a1, mv1, a2, mv2;
1647 if (args1 == 0 && args2 == 0)
1648 return val;
1649 /* If one list is shorter than the other,
1650 they fail to match. */
1651 if (args1 == 0 || args2 == 0)
1652 return 0;
1653 mv1 = a1 = TREE_VALUE (args1);
1654 mv2 = a2 = TREE_VALUE (args2);
1655 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1656 mv1 = (TYPE_ATOMIC (mv1)
1657 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1658 TYPE_QUAL_ATOMIC)
1659 : TYPE_MAIN_VARIANT (mv1));
1660 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1661 mv2 = (TYPE_ATOMIC (mv2)
1662 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1663 TYPE_QUAL_ATOMIC)
1664 : TYPE_MAIN_VARIANT (mv2));
1665 /* A null pointer instead of a type
1666 means there is supposed to be an argument
1667 but nothing is specified about what type it has.
1668 So match anything that self-promotes. */
1669 if (different_types_p != NULL
1670 && (a1 == 0) != (a2 == 0))
1671 *different_types_p = true;
1672 if (a1 == 0)
1674 if (c_type_promotes_to (a2) != a2)
1675 return 0;
1677 else if (a2 == 0)
1679 if (c_type_promotes_to (a1) != a1)
1680 return 0;
1682 /* If one of the lists has an error marker, ignore this arg. */
1683 else if (TREE_CODE (a1) == ERROR_MARK
1684 || TREE_CODE (a2) == ERROR_MARK)
1686 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1687 different_types_p)))
1689 if (different_types_p != NULL)
1690 *different_types_p = true;
1691 /* Allow wait (union {union wait *u; int *i} *)
1692 and wait (union wait *) to be compatible. */
1693 if (TREE_CODE (a1) == UNION_TYPE
1694 && (TYPE_NAME (a1) == 0
1695 || TYPE_TRANSPARENT_AGGR (a1))
1696 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1697 && tree_int_cst_equal (TYPE_SIZE (a1),
1698 TYPE_SIZE (a2)))
1700 tree memb;
1701 for (memb = TYPE_FIELDS (a1);
1702 memb; memb = DECL_CHAIN (memb))
1704 tree mv3 = TREE_TYPE (memb);
1705 if (mv3 && mv3 != error_mark_node
1706 && TREE_CODE (mv3) != ARRAY_TYPE)
1707 mv3 = (TYPE_ATOMIC (mv3)
1708 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1709 TYPE_QUAL_ATOMIC)
1710 : TYPE_MAIN_VARIANT (mv3));
1711 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1712 different_types_p))
1713 break;
1715 if (memb == 0)
1716 return 0;
1718 else if (TREE_CODE (a2) == UNION_TYPE
1719 && (TYPE_NAME (a2) == 0
1720 || TYPE_TRANSPARENT_AGGR (a2))
1721 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a2),
1723 TYPE_SIZE (a1)))
1725 tree memb;
1726 for (memb = TYPE_FIELDS (a2);
1727 memb; memb = DECL_CHAIN (memb))
1729 tree mv3 = TREE_TYPE (memb);
1730 if (mv3 && mv3 != error_mark_node
1731 && TREE_CODE (mv3) != ARRAY_TYPE)
1732 mv3 = (TYPE_ATOMIC (mv3)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1734 TYPE_QUAL_ATOMIC)
1735 : TYPE_MAIN_VARIANT (mv3));
1736 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1737 different_types_p))
1738 break;
1740 if (memb == 0)
1741 return 0;
1743 else
1744 return 0;
1747 /* comptypes said ok, but record if it said to warn. */
1748 if (newval > val)
1749 val = newval;
1751 args1 = TREE_CHAIN (args1);
1752 args2 = TREE_CHAIN (args2);
1756 /* Compute the size to increment a pointer by. */
1758 static tree
1759 c_size_in_bytes (const_tree type)
1761 enum tree_code code = TREE_CODE (type);
1763 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1764 return size_one_node;
1766 if (!COMPLETE_OR_VOID_TYPE_P (type))
1768 error ("arithmetic on pointer to an incomplete type");
1769 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 ("void value not ignored as it ought to be");
2111 return error_mark_node;
2114 exp = require_complete_type (exp);
2115 if (exp == error_mark_node)
2116 return error_mark_node;
2118 promoted_type = targetm.promoted_type (type);
2119 if (promoted_type)
2120 return convert (promoted_type, exp);
2122 if (INTEGRAL_TYPE_P (type))
2123 return perform_integral_promotions (exp);
2125 return exp;
2128 /* Look up COMPONENT in a structure or union TYPE.
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2137 static tree
2138 lookup_field (tree type, tree component)
2140 tree field;
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2147 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2149 int bot, top, half;
2150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2152 field = TYPE_FIELDS (type);
2153 bot = 0;
2154 top = TYPE_LANG_SPECIFIC (type)->s->len;
2155 while (top - bot > 1)
2157 half = (top - bot + 1) >> 1;
2158 field = field_array[bot+half];
2160 if (DECL_NAME (field) == NULL_TREE)
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2165 field = field_array[bot++];
2166 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2169 tree anon = lookup_field (TREE_TYPE (field), component);
2171 if (anon)
2172 return tree_cons (NULL_TREE, field, anon);
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2180 == TYPE_DECL)
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2182 == component))
2183 break;
2187 /* Entire record is only anon unions. */
2188 if (bot > top)
2189 return NULL_TREE;
2191 /* Restart the binary search, with new lower bound. */
2192 continue;
2195 if (DECL_NAME (field) == component)
2196 break;
2197 if (DECL_NAME (field) < component)
2198 bot += half;
2199 else
2200 top = bot + half;
2203 if (DECL_NAME (field_array[bot]) == component)
2204 field = field_array[bot];
2205 else if (DECL_NAME (field) != component)
2206 return NULL_TREE;
2208 else
2210 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2212 if (DECL_NAME (field) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2216 tree anon = lookup_field (TREE_TYPE (field), component);
2218 if (anon)
2219 return tree_cons (NULL_TREE, field, anon);
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2223 name. */
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2228 == component))
2229 break;
2232 if (DECL_NAME (field) == component)
2233 break;
2236 if (field == NULL_TREE)
2237 return NULL_TREE;
2240 return tree_cons (NULL_TREE, field, NULL_TREE);
2243 /* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
2247 tree
2248 build_component_ref (location_t loc, tree datum, tree component)
2250 tree type = TREE_TYPE (datum);
2251 enum tree_code code = TREE_CODE (type);
2252 tree field = NULL;
2253 tree ref;
2254 bool datum_lvalue = lvalue_p (datum);
2256 if (!objc_is_public (datum, component))
2257 return error_mark_node;
2259 /* Detect Objective-C property syntax object.property. */
2260 if (c_dialect_objc ()
2261 && (ref = objc_maybe_build_component_ref (datum, component)))
2262 return ref;
2264 /* See if there is a field or component with name COMPONENT. */
2266 if (code == RECORD_TYPE || code == UNION_TYPE)
2268 if (!COMPLETE_TYPE_P (type))
2270 c_incomplete_type_error (NULL_TREE, type);
2271 return error_mark_node;
2274 field = lookup_field (type, component);
2276 if (!field)
2278 error_at (loc, "%qT has no member named %qE", type, component);
2279 return error_mark_node;
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
2289 tree subdatum = TREE_VALUE (field);
2290 int quals;
2291 tree subtype;
2292 bool use_datum_quals;
2294 if (TREE_TYPE (subdatum) == error_mark_node)
2295 return error_mark_node;
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals = (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2304 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2305 if (use_datum_quals)
2306 quals |= TYPE_QUALS (TREE_TYPE (datum));
2307 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2309 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2310 NULL_TREE);
2311 SET_EXPR_LOCATION (ref, loc);
2312 if (TREE_READONLY (subdatum)
2313 || (use_datum_quals && TREE_READONLY (datum)))
2314 TREE_READONLY (ref) = 1;
2315 if (TREE_THIS_VOLATILE (subdatum)
2316 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2317 TREE_THIS_VOLATILE (ref) = 1;
2319 if (TREE_DEPRECATED (subdatum))
2320 warn_deprecated_use (subdatum, NULL_TREE);
2322 datum = ref;
2324 field = TREE_CHAIN (field);
2326 while (field);
2328 return ref;
2330 else if (code != ERROR_MARK)
2331 error_at (loc,
2332 "request for member %qE in something not a structure or union",
2333 component);
2335 return error_mark_node;
2338 /* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
2340 ERRORSTRING is the name of the operator to appear in error messages.
2342 LOC is the location to use for the generated tree. */
2344 tree
2345 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2347 tree pointer = default_conversion (ptr);
2348 tree type = TREE_TYPE (pointer);
2349 tree ref;
2351 if (TREE_CODE (type) == POINTER_TYPE)
2353 if (CONVERT_EXPR_P (pointer)
2354 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing > 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2361 type, TREE_OPERAND (pointer, 0)))
2362 TREE_NO_WARNING (pointer) = 1;
2365 if (TREE_CODE (pointer) == ADDR_EXPR
2366 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2367 == TREE_TYPE (type)))
2369 ref = TREE_OPERAND (pointer, 0);
2370 protected_set_expr_location (ref, loc);
2371 return ref;
2373 else
2375 tree t = TREE_TYPE (type);
2377 ref = build1 (INDIRECT_REF, t, pointer);
2379 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2381 error_at (loc, "dereferencing pointer to incomplete type");
2382 return error_mark_node;
2384 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2385 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref) = TYPE_READONLY (t);
2395 TREE_SIDE_EFFECTS (ref)
2396 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2397 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2398 protected_set_expr_location (ref, loc);
2399 return ref;
2402 else if (TREE_CODE (pointer) != ERROR_MARK)
2403 invalid_indirection_error (loc, type, errstring);
2405 return error_mark_node;
2408 /* This handles expressions of the form "a[i]", which denotes
2409 an array reference.
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
2415 by functions).
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2420 LOC is the location to use for the returned expression. */
2422 tree
2423 build_array_ref (location_t loc, tree array, tree index)
2425 tree ret;
2426 bool swapped = false;
2427 if (TREE_TYPE (array) == error_mark_node
2428 || TREE_TYPE (index) == error_mark_node)
2429 return error_mark_node;
2431 if (flag_cilkplus && contains_array_notation_expr (index))
2433 size_t rank = 0;
2434 if (!find_rank (loc, index, index, true, &rank))
2435 return error_mark_node;
2436 if (rank > 1)
2438 error_at (loc, "rank of the array's index is greater than 1");
2439 return error_mark_node;
2442 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2443 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2447 tree temp;
2448 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2451 error_at (loc,
2452 "subscripted value is neither array nor pointer nor vector");
2454 return error_mark_node;
2456 temp = array;
2457 array = index;
2458 index = temp;
2459 swapped = true;
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2464 error_at (loc, "array subscript is not an integer");
2465 return error_mark_node;
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2470 error_at (loc, "subscripted value is pointer to function");
2471 return error_mark_node;
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2476 if (!swapped)
2477 warn_array_subscript_with_type_char (index);
2479 /* Apply default promotions *after* noticing character types. */
2480 index = default_conversion (index);
2482 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2484 convert_vector_to_pointer_for_subscript (loc, &array, index);
2486 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2488 tree rval, type;
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index) != INTEGER_CST
2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2498 if (!c_mark_addressable (array))
2499 return error_mark_node;
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index) == INTEGER_CST
2506 && TYPE_DOMAIN (TREE_TYPE (array))
2507 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2509 if (!c_mark_addressable (array))
2510 return error_mark_node;
2513 if (pedantic)
2515 tree foo = array;
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
2518 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2519 pedwarn (loc, OPT_Wpedantic,
2520 "ISO C forbids subscripting %<register%> array");
2521 else if (!flag_isoc99 && !lvalue_p (foo))
2522 pedwarn (loc, OPT_Wpedantic,
2523 "ISO C90 forbids subscripting non-lvalue array");
2526 type = TREE_TYPE (TREE_TYPE (array));
2527 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2528 /* Array ref is const/volatile if the array elements are
2529 or if the array is. */
2530 TREE_READONLY (rval)
2531 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2532 | TREE_READONLY (array));
2533 TREE_SIDE_EFFECTS (rval)
2534 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2535 | TREE_SIDE_EFFECTS (array));
2536 TREE_THIS_VOLATILE (rval)
2537 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2538 /* This was added by rms on 16 Nov 91.
2539 It fixes vol struct foo *a; a->elts[1]
2540 in an inline function.
2541 Hope it doesn't break something else. */
2542 | TREE_THIS_VOLATILE (array));
2543 ret = require_complete_type (rval);
2544 protected_set_expr_location (ret, loc);
2545 return ret;
2547 else
2549 tree ar = default_conversion (array);
2551 if (ar == error_mark_node)
2552 return ar;
2554 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2555 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2557 return build_indirect_ref
2558 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2559 RO_ARRAY_INDEXING);
2563 /* Build an external reference to identifier ID. FUN indicates
2564 whether this will be used for a function call. LOC is the source
2565 location of the identifier. This sets *TYPE to the type of the
2566 identifier, which is not the same as the type of the returned value
2567 for CONST_DECLs defined as enum constants. If the type of the
2568 identifier is not available, *TYPE is set to NULL. */
2569 tree
2570 build_external_ref (location_t loc, tree id, int fun, tree *type)
2572 tree ref;
2573 tree decl = lookup_name (id);
2575 /* In Objective-C, an instance variable (ivar) may be preferred to
2576 whatever lookup_name() found. */
2577 decl = objc_lookup_ivar (decl, id);
2579 *type = NULL;
2580 if (decl && decl != error_mark_node)
2582 ref = decl;
2583 *type = TREE_TYPE (ref);
2585 else if (fun)
2586 /* Implicit function declaration. */
2587 ref = implicitly_declare (loc, id);
2588 else if (decl == error_mark_node)
2589 /* Don't complain about something that's already been
2590 complained about. */
2591 return error_mark_node;
2592 else
2594 undeclared_variable (loc, id);
2595 return error_mark_node;
2598 if (TREE_TYPE (ref) == error_mark_node)
2599 return error_mark_node;
2601 if (TREE_DEPRECATED (ref))
2602 warn_deprecated_use (ref, NULL_TREE);
2604 /* Recursive call does not count as usage. */
2605 if (ref != current_function_decl)
2607 TREE_USED (ref) = 1;
2610 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2612 if (!in_sizeof && !in_typeof)
2613 C_DECL_USED (ref) = 1;
2614 else if (DECL_INITIAL (ref) == 0
2615 && DECL_EXTERNAL (ref)
2616 && !TREE_PUBLIC (ref))
2617 record_maybe_used_decl (ref);
2620 if (TREE_CODE (ref) == CONST_DECL)
2622 used_types_insert (TREE_TYPE (ref));
2624 if (warn_cxx_compat
2625 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2626 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2628 warning_at (loc, OPT_Wc___compat,
2629 ("enum constant defined in struct or union "
2630 "is not visible in C++"));
2631 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2634 ref = DECL_INITIAL (ref);
2635 TREE_CONSTANT (ref) = 1;
2637 else if (current_function_decl != 0
2638 && !DECL_FILE_SCOPE_P (current_function_decl)
2639 && (TREE_CODE (ref) == VAR_DECL
2640 || TREE_CODE (ref) == PARM_DECL
2641 || TREE_CODE (ref) == FUNCTION_DECL))
2643 tree context = decl_function_context (ref);
2645 if (context != 0 && context != current_function_decl)
2646 DECL_NONLOCAL (ref) = 1;
2648 /* C99 6.7.4p3: An inline definition of a function with external
2649 linkage ... shall not contain a reference to an identifier with
2650 internal linkage. */
2651 else if (current_function_decl != 0
2652 && DECL_DECLARED_INLINE_P (current_function_decl)
2653 && DECL_EXTERNAL (current_function_decl)
2654 && VAR_OR_FUNCTION_DECL_P (ref)
2655 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2656 && ! TREE_PUBLIC (ref)
2657 && DECL_CONTEXT (ref) != current_function_decl)
2658 record_inline_static (loc, current_function_decl, ref,
2659 csi_internal);
2661 return ref;
2664 /* Record details of decls possibly used inside sizeof or typeof. */
2665 struct maybe_used_decl
2667 /* The decl. */
2668 tree decl;
2669 /* The level seen at (in_sizeof + in_typeof). */
2670 int level;
2671 /* The next one at this level or above, or NULL. */
2672 struct maybe_used_decl *next;
2675 static struct maybe_used_decl *maybe_used_decls;
2677 /* Record that DECL, an undefined static function reference seen
2678 inside sizeof or typeof, might be used if the operand of sizeof is
2679 a VLA type or the operand of typeof is a variably modified
2680 type. */
2682 static void
2683 record_maybe_used_decl (tree decl)
2685 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2686 t->decl = decl;
2687 t->level = in_sizeof + in_typeof;
2688 t->next = maybe_used_decls;
2689 maybe_used_decls = t;
2692 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2693 USED is false, just discard them. If it is true, mark them used
2694 (if no longer inside sizeof or typeof) or move them to the next
2695 level up (if still inside sizeof or typeof). */
2697 void
2698 pop_maybe_used (bool used)
2700 struct maybe_used_decl *p = maybe_used_decls;
2701 int cur_level = in_sizeof + in_typeof;
2702 while (p && p->level > cur_level)
2704 if (used)
2706 if (cur_level == 0)
2707 C_DECL_USED (p->decl) = 1;
2708 else
2709 p->level = cur_level;
2711 p = p->next;
2713 if (!used || cur_level == 0)
2714 maybe_used_decls = p;
2717 /* Return the result of sizeof applied to EXPR. */
2719 struct c_expr
2720 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2722 struct c_expr ret;
2723 if (expr.value == error_mark_node)
2725 ret.value = error_mark_node;
2726 ret.original_code = ERROR_MARK;
2727 ret.original_type = NULL;
2728 pop_maybe_used (false);
2730 else
2732 bool expr_const_operands = true;
2733 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2734 &expr_const_operands);
2735 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2736 c_last_sizeof_arg = expr.value;
2737 ret.original_code = SIZEOF_EXPR;
2738 ret.original_type = NULL;
2739 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2741 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2742 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2743 folded_expr, ret.value);
2744 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2745 SET_EXPR_LOCATION (ret.value, loc);
2747 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2749 return ret;
2752 /* Return the result of sizeof applied to T, a structure for the type
2753 name passed to sizeof (rather than the type itself). LOC is the
2754 location of the original expression. */
2756 struct c_expr
2757 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2759 tree type;
2760 struct c_expr ret;
2761 tree type_expr = NULL_TREE;
2762 bool type_expr_const = true;
2763 type = groktypename (t, &type_expr, &type_expr_const);
2764 ret.value = c_sizeof (loc, type);
2765 c_last_sizeof_arg = type;
2766 ret.original_code = SIZEOF_EXPR;
2767 ret.original_type = NULL;
2768 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2769 && c_vla_type_p (type))
2771 /* If the type is a [*] array, it is a VLA but is represented as
2772 having a size of zero. In such a case we must ensure that
2773 the result of sizeof does not get folded to a constant by
2774 c_fully_fold, because if the size is evaluated the result is
2775 not constant and so constraints on zero or negative size
2776 arrays must not be applied when this sizeof call is inside
2777 another array declarator. */
2778 if (!type_expr)
2779 type_expr = integer_zero_node;
2780 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2781 type_expr, ret.value);
2782 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2784 pop_maybe_used (type != error_mark_node
2785 ? C_TYPE_VARIABLE_SIZE (type) : false);
2786 return ret;
2789 /* Build a function call to function FUNCTION with parameters PARAMS.
2790 The function call is at LOC.
2791 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2792 TREE_VALUE of each node is a parameter-expression.
2793 FUNCTION's data type may be a function type or a pointer-to-function. */
2795 tree
2796 build_function_call (location_t loc, tree function, tree params)
2798 vec<tree, va_gc> *v;
2799 tree ret;
2801 vec_alloc (v, list_length (params));
2802 for (; params; params = TREE_CHAIN (params))
2803 v->quick_push (TREE_VALUE (params));
2804 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2805 vec_free (v);
2806 return ret;
2809 /* Give a note about the location of the declaration of DECL. */
2811 static void inform_declaration (tree decl)
2813 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2814 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2817 /* Build a function call to function FUNCTION with parameters PARAMS.
2818 ORIGTYPES, if not NULL, is a vector of types; each element is
2819 either NULL or the original type of the corresponding element in
2820 PARAMS. The original type may differ from TREE_TYPE of the
2821 parameter for enums. FUNCTION's data type may be a function type
2822 or pointer-to-function. This function changes the elements of
2823 PARAMS. */
2825 tree
2826 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2827 tree function, vec<tree, va_gc> *params,
2828 vec<tree, va_gc> *origtypes)
2830 tree fntype, fundecl = 0;
2831 tree name = NULL_TREE, result;
2832 tree tem;
2833 int nargs;
2834 tree *argarray;
2837 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2838 STRIP_TYPE_NOPS (function);
2840 /* Convert anything with function type to a pointer-to-function. */
2841 if (TREE_CODE (function) == FUNCTION_DECL)
2843 name = DECL_NAME (function);
2845 if (flag_tm)
2846 tm_malloc_replacement (function);
2847 fundecl = function;
2848 /* Atomic functions have type checking/casting already done. They are
2849 often rewritten and don't match the original parameter list. */
2850 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2851 origtypes = NULL;
2853 if (flag_cilkplus
2854 && is_cilkplus_reduce_builtin (function))
2855 origtypes = NULL;
2857 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2858 function = function_to_pointer_conversion (loc, function);
2860 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2861 expressions, like those used for ObjC messenger dispatches. */
2862 if (params && !params->is_empty ())
2863 function = objc_rewrite_function_call (function, (*params)[0]);
2865 function = c_fully_fold (function, false, NULL);
2867 fntype = TREE_TYPE (function);
2869 if (TREE_CODE (fntype) == ERROR_MARK)
2870 return error_mark_node;
2872 if (!(TREE_CODE (fntype) == POINTER_TYPE
2873 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2875 if (!flag_diagnostics_show_caret)
2876 error_at (loc,
2877 "called object %qE is not a function or function pointer",
2878 function);
2879 else if (DECL_P (function))
2881 error_at (loc,
2882 "called object %qD is not a function or function pointer",
2883 function);
2884 inform_declaration (function);
2886 else
2887 error_at (loc,
2888 "called object is not a function or function pointer");
2889 return error_mark_node;
2892 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2893 current_function_returns_abnormally = 1;
2895 /* fntype now gets the type of function pointed to. */
2896 fntype = TREE_TYPE (fntype);
2898 /* Convert the parameters to the types declared in the
2899 function prototype, or apply default promotions. */
2901 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2902 origtypes, function, fundecl);
2903 if (nargs < 0)
2904 return error_mark_node;
2906 /* Check that the function is called through a compatible prototype.
2907 If it is not, warn. */
2908 if (CONVERT_EXPR_P (function)
2909 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2910 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2911 && !comptypes (fntype, TREE_TYPE (tem)))
2913 tree return_type = TREE_TYPE (fntype);
2915 /* This situation leads to run-time undefined behavior. We can't,
2916 therefore, simply error unless we can prove that all possible
2917 executions of the program must execute the code. */
2918 warning_at (loc, 0, "function called through a non-compatible type");
2920 if (VOID_TYPE_P (return_type)
2921 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2922 pedwarn (loc, 0,
2923 "function with qualified void return type called");
2926 argarray = vec_safe_address (params);
2928 /* Check that arguments to builtin functions match the expectations. */
2929 if (fundecl
2930 && DECL_BUILT_IN (fundecl)
2931 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2932 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2933 return error_mark_node;
2935 /* Check that the arguments to the function are valid. */
2936 check_function_arguments (fntype, nargs, argarray);
2938 if (name != NULL_TREE
2939 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2941 if (require_constant_value)
2942 result =
2943 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2944 function, nargs, argarray);
2945 else
2946 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2947 function, nargs, argarray);
2948 if (TREE_CODE (result) == NOP_EXPR
2949 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2950 STRIP_TYPE_NOPS (result);
2952 else
2953 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2954 function, nargs, argarray);
2956 if (VOID_TYPE_P (TREE_TYPE (result)))
2958 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2959 pedwarn (loc, 0,
2960 "function with qualified void return type called");
2961 return result;
2963 return require_complete_type (result);
2966 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2968 tree
2969 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2970 tree function, vec<tree, va_gc> *params,
2971 vec<tree, va_gc> *origtypes)
2973 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2974 STRIP_TYPE_NOPS (function);
2976 /* Convert anything with function type to a pointer-to-function. */
2977 if (TREE_CODE (function) == FUNCTION_DECL)
2979 /* Implement type-directed function overloading for builtins.
2980 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2981 handle all the type checking. The result is a complete expression
2982 that implements this function call. */
2983 tree tem = resolve_overloaded_builtin (loc, function, params);
2984 if (tem)
2985 return tem;
2987 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
2990 /* Convert the argument expressions in the vector VALUES
2991 to the types in the list TYPELIST.
2993 If TYPELIST is exhausted, or when an element has NULL as its type,
2994 perform the default conversions.
2996 ORIGTYPES is the original types of the expressions in VALUES. This
2997 holds the type of enum values which have been converted to integral
2998 types. It may be NULL.
3000 FUNCTION is a tree for the called function. It is used only for
3001 error messages, where it is formatted with %qE.
3003 This is also where warnings about wrong number of args are generated.
3005 ARG_LOC are locations of function arguments (if any).
3007 Returns the actual number of arguments processed (which may be less
3008 than the length of VALUES in some error situations), or -1 on
3009 failure. */
3011 static int
3012 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3013 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3014 tree function, tree fundecl)
3016 tree typetail, val;
3017 unsigned int parmnum;
3018 bool error_args = false;
3019 const bool type_generic = fundecl
3020 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3021 bool type_generic_remove_excess_precision = false;
3022 tree selector;
3024 /* Change pointer to function to the function itself for
3025 diagnostics. */
3026 if (TREE_CODE (function) == ADDR_EXPR
3027 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3028 function = TREE_OPERAND (function, 0);
3030 /* Handle an ObjC selector specially for diagnostics. */
3031 selector = objc_message_selector ();
3033 /* For type-generic built-in functions, determine whether excess
3034 precision should be removed (classification) or not
3035 (comparison). */
3036 if (type_generic
3037 && DECL_BUILT_IN (fundecl)
3038 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3040 switch (DECL_FUNCTION_CODE (fundecl))
3042 case BUILT_IN_ISFINITE:
3043 case BUILT_IN_ISINF:
3044 case BUILT_IN_ISINF_SIGN:
3045 case BUILT_IN_ISNAN:
3046 case BUILT_IN_ISNORMAL:
3047 case BUILT_IN_FPCLASSIFY:
3048 type_generic_remove_excess_precision = true;
3049 break;
3051 default:
3052 type_generic_remove_excess_precision = false;
3053 break;
3056 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3057 return vec_safe_length (values);
3059 /* Scan the given expressions and types, producing individual
3060 converted arguments. */
3062 for (typetail = typelist, parmnum = 0;
3063 values && values->iterate (parmnum, &val);
3064 ++parmnum)
3066 tree type = typetail ? TREE_VALUE (typetail) : 0;
3067 tree valtype = TREE_TYPE (val);
3068 tree rname = function;
3069 int argnum = parmnum + 1;
3070 const char *invalid_func_diag;
3071 bool excess_precision = false;
3072 bool npc;
3073 tree parmval;
3075 if (type == void_type_node)
3077 if (selector)
3078 error_at (loc, "too many arguments to method %qE", selector);
3079 else
3080 error_at (loc, "too many arguments to function %qE", function);
3081 inform_declaration (fundecl);
3082 return parmnum;
3085 if (selector && argnum > 2)
3087 rname = selector;
3088 argnum -= 2;
3091 npc = null_pointer_constant_p (val);
3093 /* If there is excess precision and a prototype, convert once to
3094 the required type rather than converting via the semantic
3095 type. Likewise without a prototype a float value represented
3096 as long double should be converted once to double. But for
3097 type-generic classification functions excess precision must
3098 be removed here. */
3099 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3100 && (type || !type_generic || !type_generic_remove_excess_precision))
3102 val = TREE_OPERAND (val, 0);
3103 excess_precision = true;
3105 val = c_fully_fold (val, false, NULL);
3106 STRIP_TYPE_NOPS (val);
3108 val = require_complete_type (val);
3110 if (type != 0)
3112 /* Formal parm type is specified by a function prototype. */
3114 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3116 error ("type of formal parameter %d is incomplete", parmnum + 1);
3117 parmval = val;
3119 else
3121 tree origtype;
3123 /* Optionally warn about conversions that
3124 differ from the default conversions. */
3125 if (warn_traditional_conversion || warn_traditional)
3127 unsigned int formal_prec = TYPE_PRECISION (type);
3129 if (INTEGRAL_TYPE_P (type)
3130 && TREE_CODE (valtype) == REAL_TYPE)
3131 warning (0, "passing argument %d of %qE as integer "
3132 "rather than floating due to prototype",
3133 argnum, rname);
3134 if (INTEGRAL_TYPE_P (type)
3135 && TREE_CODE (valtype) == COMPLEX_TYPE)
3136 warning (0, "passing argument %d of %qE as integer "
3137 "rather than complex due to prototype",
3138 argnum, rname);
3139 else if (TREE_CODE (type) == COMPLEX_TYPE
3140 && TREE_CODE (valtype) == REAL_TYPE)
3141 warning (0, "passing argument %d of %qE as complex "
3142 "rather than floating due to prototype",
3143 argnum, rname);
3144 else if (TREE_CODE (type) == REAL_TYPE
3145 && INTEGRAL_TYPE_P (valtype))
3146 warning (0, "passing argument %d of %qE as floating "
3147 "rather than integer due to prototype",
3148 argnum, rname);
3149 else if (TREE_CODE (type) == COMPLEX_TYPE
3150 && INTEGRAL_TYPE_P (valtype))
3151 warning (0, "passing argument %d of %qE as complex "
3152 "rather than integer due to prototype",
3153 argnum, rname);
3154 else if (TREE_CODE (type) == REAL_TYPE
3155 && TREE_CODE (valtype) == COMPLEX_TYPE)
3156 warning (0, "passing argument %d of %qE as floating "
3157 "rather than complex due to prototype",
3158 argnum, rname);
3159 /* ??? At some point, messages should be written about
3160 conversions between complex types, but that's too messy
3161 to do now. */
3162 else if (TREE_CODE (type) == REAL_TYPE
3163 && TREE_CODE (valtype) == REAL_TYPE)
3165 /* Warn if any argument is passed as `float',
3166 since without a prototype it would be `double'. */
3167 if (formal_prec == TYPE_PRECISION (float_type_node)
3168 && type != dfloat32_type_node)
3169 warning (0, "passing argument %d of %qE as %<float%> "
3170 "rather than %<double%> due to prototype",
3171 argnum, rname);
3173 /* Warn if mismatch between argument and prototype
3174 for decimal float types. Warn of conversions with
3175 binary float types and of precision narrowing due to
3176 prototype. */
3177 else if (type != valtype
3178 && (type == dfloat32_type_node
3179 || type == dfloat64_type_node
3180 || type == dfloat128_type_node
3181 || valtype == dfloat32_type_node
3182 || valtype == dfloat64_type_node
3183 || valtype == dfloat128_type_node)
3184 && (formal_prec
3185 <= TYPE_PRECISION (valtype)
3186 || (type == dfloat128_type_node
3187 && (valtype
3188 != dfloat64_type_node
3189 && (valtype
3190 != dfloat32_type_node)))
3191 || (type == dfloat64_type_node
3192 && (valtype
3193 != dfloat32_type_node))))
3194 warning (0, "passing argument %d of %qE as %qT "
3195 "rather than %qT due to prototype",
3196 argnum, rname, type, valtype);
3199 /* Detect integer changing in width or signedness.
3200 These warnings are only activated with
3201 -Wtraditional-conversion, not with -Wtraditional. */
3202 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3203 && INTEGRAL_TYPE_P (valtype))
3205 tree would_have_been = default_conversion (val);
3206 tree type1 = TREE_TYPE (would_have_been);
3208 if (TREE_CODE (type) == ENUMERAL_TYPE
3209 && (TYPE_MAIN_VARIANT (type)
3210 == TYPE_MAIN_VARIANT (valtype)))
3211 /* No warning if function asks for enum
3212 and the actual arg is that enum type. */
3214 else if (formal_prec != TYPE_PRECISION (type1))
3215 warning (OPT_Wtraditional_conversion,
3216 "passing argument %d of %qE "
3217 "with different width due to prototype",
3218 argnum, rname);
3219 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3221 /* Don't complain if the formal parameter type
3222 is an enum, because we can't tell now whether
3223 the value was an enum--even the same enum. */
3224 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3226 else if (TREE_CODE (val) == INTEGER_CST
3227 && int_fits_type_p (val, type))
3228 /* Change in signedness doesn't matter
3229 if a constant value is unaffected. */
3231 /* If the value is extended from a narrower
3232 unsigned type, it doesn't matter whether we
3233 pass it as signed or unsigned; the value
3234 certainly is the same either way. */
3235 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3236 && TYPE_UNSIGNED (valtype))
3238 else if (TYPE_UNSIGNED (type))
3239 warning (OPT_Wtraditional_conversion,
3240 "passing argument %d of %qE "
3241 "as unsigned due to prototype",
3242 argnum, rname);
3243 else
3244 warning (OPT_Wtraditional_conversion,
3245 "passing argument %d of %qE "
3246 "as signed due to prototype", argnum, rname);
3250 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3251 sake of better warnings from convert_and_check. */
3252 if (excess_precision)
3253 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3254 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3255 bool arg_loc_ok = !arg_loc.is_empty ()
3256 /* Some __atomic_* builtins have additional
3257 hidden argument at position 0. */
3258 && values->length () == arg_loc.length ();
3259 parmval = convert_for_assignment (loc,
3260 arg_loc_ok ? arg_loc[parmnum]
3261 : UNKNOWN_LOCATION, type,
3262 val, origtype, ic_argpass,
3263 npc, fundecl, function,
3264 parmnum + 1);
3266 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3267 && INTEGRAL_TYPE_P (type)
3268 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3269 parmval = default_conversion (parmval);
3272 else if (TREE_CODE (valtype) == REAL_TYPE
3273 && (TYPE_PRECISION (valtype)
3274 <= TYPE_PRECISION (double_type_node))
3275 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3276 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3277 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3279 if (type_generic)
3280 parmval = val;
3281 else
3283 /* Convert `float' to `double'. */
3284 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3285 warning_at (arg_loc[parmnum], OPT_Wdouble_promotion,
3286 "implicit conversion from %qT to %qT when passing "
3287 "argument to function",
3288 valtype, double_type_node);
3289 parmval = convert (double_type_node, val);
3292 else if (excess_precision && !type_generic)
3293 /* A "double" argument with excess precision being passed
3294 without a prototype or in variable arguments. */
3295 parmval = convert (valtype, val);
3296 else if ((invalid_func_diag =
3297 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3299 error (invalid_func_diag);
3300 return -1;
3302 else
3303 /* Convert `short' and `char' to full-size `int'. */
3304 parmval = default_conversion (val);
3306 (*values)[parmnum] = parmval;
3307 if (parmval == error_mark_node)
3308 error_args = true;
3310 if (typetail)
3311 typetail = TREE_CHAIN (typetail);
3314 gcc_assert (parmnum == vec_safe_length (values));
3316 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3318 error_at (loc, "too few arguments to function %qE", function);
3319 inform_declaration (fundecl);
3320 return -1;
3323 return error_args ? -1 : (int) parmnum;
3326 /* This is the entry point used by the parser to build unary operators
3327 in the input. CODE, a tree_code, specifies the unary operator, and
3328 ARG is the operand. For unary plus, the C parser currently uses
3329 CONVERT_EXPR for code.
3331 LOC is the location to use for the tree generated.
3334 struct c_expr
3335 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3337 struct c_expr result;
3339 result.value = build_unary_op (loc, code, arg.value, 0);
3340 result.original_code = code;
3341 result.original_type = NULL;
3343 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3344 overflow_warning (loc, result.value);
3346 return result;
3349 /* This is the entry point used by the parser to build binary operators
3350 in the input. CODE, a tree_code, specifies the binary operator, and
3351 ARG1 and ARG2 are the operands. In addition to constructing the
3352 expression, we check for operands that were written with other binary
3353 operators in a way that is likely to confuse the user.
3355 LOCATION is the location of the binary operator. */
3357 struct c_expr
3358 parser_build_binary_op (location_t location, enum tree_code code,
3359 struct c_expr arg1, struct c_expr arg2)
3361 struct c_expr result;
3363 enum tree_code code1 = arg1.original_code;
3364 enum tree_code code2 = arg2.original_code;
3365 tree type1 = (arg1.original_type
3366 ? arg1.original_type
3367 : TREE_TYPE (arg1.value));
3368 tree type2 = (arg2.original_type
3369 ? arg2.original_type
3370 : TREE_TYPE (arg2.value));
3372 result.value = build_binary_op (location, code,
3373 arg1.value, arg2.value, 1);
3374 result.original_code = code;
3375 result.original_type = NULL;
3377 if (TREE_CODE (result.value) == ERROR_MARK)
3378 return result;
3380 if (location != UNKNOWN_LOCATION)
3381 protected_set_expr_location (result.value, location);
3383 /* Check for cases such as x+y<<z which users are likely
3384 to misinterpret. */
3385 if (warn_parentheses)
3386 warn_about_parentheses (location, code, code1, arg1.value, code2,
3387 arg2.value);
3389 if (warn_logical_op)
3390 warn_logical_operator (location, code, TREE_TYPE (result.value),
3391 code1, arg1.value, code2, arg2.value);
3393 /* Warn about comparisons against string literals, with the exception
3394 of testing for equality or inequality of a string literal with NULL. */
3395 if (code == EQ_EXPR || code == NE_EXPR)
3397 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3398 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3399 warning_at (location, OPT_Waddress,
3400 "comparison with string literal results in unspecified behavior");
3402 else if (TREE_CODE_CLASS (code) == tcc_comparison
3403 && (code1 == STRING_CST || code2 == STRING_CST))
3404 warning_at (location, OPT_Waddress,
3405 "comparison with string literal results in unspecified behavior");
3407 if (TREE_OVERFLOW_P (result.value)
3408 && !TREE_OVERFLOW_P (arg1.value)
3409 && !TREE_OVERFLOW_P (arg2.value))
3410 overflow_warning (location, result.value);
3412 /* Warn about comparisons of different enum types. */
3413 if (warn_enum_compare
3414 && TREE_CODE_CLASS (code) == tcc_comparison
3415 && TREE_CODE (type1) == ENUMERAL_TYPE
3416 && TREE_CODE (type2) == ENUMERAL_TYPE
3417 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3418 warning_at (location, OPT_Wenum_compare,
3419 "comparison between %qT and %qT",
3420 type1, type2);
3422 return result;
3425 /* Return a tree for the difference of pointers OP0 and OP1.
3426 The resulting tree has type int. */
3428 static tree
3429 pointer_diff (location_t loc, tree op0, tree op1)
3431 tree restype = ptrdiff_type_node;
3432 tree result, inttype;
3434 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3435 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3436 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3437 tree con0, con1, lit0, lit1;
3438 tree orig_op1 = op1;
3440 /* If the operands point into different address spaces, we need to
3441 explicitly convert them to pointers into the common address space
3442 before we can subtract the numerical address values. */
3443 if (as0 != as1)
3445 addr_space_t as_common;
3446 tree common_type;
3448 /* Determine the common superset address space. This is guaranteed
3449 to exist because the caller verified that comp_target_types
3450 returned non-zero. */
3451 if (!addr_space_superset (as0, as1, &as_common))
3452 gcc_unreachable ();
3454 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3455 op0 = convert (common_type, op0);
3456 op1 = convert (common_type, op1);
3459 /* Determine integer type to perform computations in. This will usually
3460 be the same as the result type (ptrdiff_t), but may need to be a wider
3461 type if pointers for the address space are wider than ptrdiff_t. */
3462 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3463 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3464 else
3465 inttype = restype;
3468 if (TREE_CODE (target_type) == VOID_TYPE)
3469 pedwarn (loc, OPT_Wpointer_arith,
3470 "pointer of type %<void *%> used in subtraction");
3471 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3472 pedwarn (loc, OPT_Wpointer_arith,
3473 "pointer to a function used in subtraction");
3475 /* If the conversion to ptrdiff_type does anything like widening or
3476 converting a partial to an integral mode, we get a convert_expression
3477 that is in the way to do any simplifications.
3478 (fold-const.c doesn't know that the extra bits won't be needed.
3479 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3480 different mode in place.)
3481 So first try to find a common term here 'by hand'; we want to cover
3482 at least the cases that occur in legal static initializers. */
3483 if (CONVERT_EXPR_P (op0)
3484 && (TYPE_PRECISION (TREE_TYPE (op0))
3485 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3486 con0 = TREE_OPERAND (op0, 0);
3487 else
3488 con0 = op0;
3489 if (CONVERT_EXPR_P (op1)
3490 && (TYPE_PRECISION (TREE_TYPE (op1))
3491 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3492 con1 = TREE_OPERAND (op1, 0);
3493 else
3494 con1 = op1;
3496 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3498 lit0 = TREE_OPERAND (con0, 1);
3499 con0 = TREE_OPERAND (con0, 0);
3501 else
3502 lit0 = integer_zero_node;
3504 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3506 lit1 = TREE_OPERAND (con1, 1);
3507 con1 = TREE_OPERAND (con1, 0);
3509 else
3510 lit1 = integer_zero_node;
3512 if (operand_equal_p (con0, con1, 0))
3514 op0 = lit0;
3515 op1 = lit1;
3519 /* First do the subtraction as integers;
3520 then drop through to build the divide operator.
3521 Do not do default conversions on the minus operator
3522 in case restype is a short type. */
3524 op0 = build_binary_op (loc,
3525 MINUS_EXPR, convert (inttype, op0),
3526 convert (inttype, op1), 0);
3527 /* This generates an error if op1 is pointer to incomplete type. */
3528 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3529 error_at (loc, "arithmetic on pointer to an incomplete type");
3531 /* This generates an error if op0 is pointer to incomplete type. */
3532 op1 = c_size_in_bytes (target_type);
3534 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3535 error_at (loc, "arithmetic on pointer to an empty aggregate");
3537 /* Divide by the size, in easiest possible way. */
3538 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3539 op0, convert (inttype, op1));
3541 /* Convert to final result type if necessary. */
3542 return convert (restype, result);
3545 /* Expand atomic compound assignments into an approriate sequence as
3546 specified by the C11 standard section 6.5.16.2.
3547 given
3548 _Atomic T1 E1
3549 T2 E2
3550 E1 op= E2
3552 This sequence is used for all types for which these operations are
3553 supported.
3555 In addition, built-in versions of the 'fe' prefixed routines may
3556 need to be invoked for floating point (real, complex or vector) when
3557 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3559 T1 newval;
3560 T1 old;
3561 T1 *addr
3562 T2 val
3563 fenv_t fenv
3565 addr = &E1;
3566 val = (E2);
3567 __atomic_load (addr, &old, SEQ_CST);
3568 feholdexcept (&fenv);
3569 loop:
3570 newval = old op val;
3571 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3572 SEQ_CST))
3573 goto done;
3574 feclearexcept (FE_ALL_EXCEPT);
3575 goto loop:
3576 done:
3577 feupdateenv (&fenv);
3579 Also note that the compiler is simply issuing the generic form of
3580 the atomic operations. This requires temp(s) and has their address
3581 taken. The atomic processing is smart enough to figure out when the
3582 size of an object can utilize a lock-free version, and convert the
3583 built-in call to the appropriate lock-free routine. The optimizers
3584 will then dispose of any temps that are no longer required, and
3585 lock-free implementations are utilized as long as there is target
3586 support for the required size.
3588 If the operator is NOP_EXPR, then this is a simple assignment, and
3589 an __atomic_store is issued to perform the assignment rather than
3590 the above loop.
3594 /* Build an atomic assignment at LOC, expanding into the proper
3595 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3596 the result of the operation, unless RETURN_OLD_P in which case
3597 return the old value of LHS (this is only for postincrement and
3598 postdecrement). */
3599 static tree
3600 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3601 tree rhs, bool return_old_p)
3603 tree fndecl, func_call;
3604 vec<tree, va_gc> *params;
3605 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3606 tree old, old_addr;
3607 tree compound_stmt;
3608 tree stmt, goto_stmt;
3609 tree loop_label, loop_decl, done_label, done_decl;
3611 tree lhs_type = TREE_TYPE (lhs);
3612 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3613 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3614 tree rhs_type = TREE_TYPE (rhs);
3616 gcc_assert (TYPE_ATOMIC (lhs_type));
3618 if (return_old_p)
3619 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3621 /* Allocate enough vector items for a compare_exchange. */
3622 vec_alloc (params, 6);
3624 /* Create a compound statement to hold the sequence of statements
3625 with a loop. */
3626 compound_stmt = c_begin_compound_stmt (false);
3628 /* Fold the RHS if it hasn't already been folded. */
3629 if (modifycode != NOP_EXPR)
3630 rhs = c_fully_fold (rhs, false, NULL);
3632 /* Remove the qualifiers for the rest of the expressions and create
3633 the VAL temp variable to hold the RHS. */
3634 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3635 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3636 val = create_tmp_var (nonatomic_rhs_type, NULL);
3637 TREE_ADDRESSABLE (val) = 1;
3638 TREE_NO_WARNING (val) = 1;
3639 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3640 SET_EXPR_LOCATION (rhs, loc);
3641 add_stmt (rhs);
3643 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3644 an atomic_store. */
3645 if (modifycode == NOP_EXPR)
3647 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3648 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3649 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3650 params->quick_push (lhs_addr);
3651 params->quick_push (rhs);
3652 params->quick_push (seq_cst);
3653 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3654 add_stmt (func_call);
3656 /* Finish the compound statement. */
3657 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3659 /* VAL is the value which was stored, return a COMPOUND_STMT of
3660 the statement and that value. */
3661 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3664 /* Create the variables and labels required for the op= form. */
3665 old = create_tmp_var (nonatomic_lhs_type, NULL);
3666 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3667 TREE_ADDRESSABLE (old) = 1;
3668 TREE_NO_WARNING (old) = 1;
3670 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3671 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3672 TREE_ADDRESSABLE (newval) = 1;
3674 loop_decl = create_artificial_label (loc);
3675 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3677 done_decl = create_artificial_label (loc);
3678 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3680 /* __atomic_load (addr, &old, SEQ_CST). */
3681 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3682 params->quick_push (lhs_addr);
3683 params->quick_push (old_addr);
3684 params->quick_push (seq_cst);
3685 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3686 add_stmt (func_call);
3687 params->truncate (0);
3689 /* Create the expressions for floating-point environment
3690 manipulation, if required. */
3691 bool need_fenv = (flag_trapping_math
3692 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3693 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3694 if (need_fenv)
3695 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3697 if (hold_call)
3698 add_stmt (hold_call);
3700 /* loop: */
3701 add_stmt (loop_label);
3703 /* newval = old + val; */
3704 rhs = build_binary_op (loc, modifycode, old, val, 1);
3705 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3706 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3707 NULL_TREE, 0);
3708 if (rhs != error_mark_node)
3710 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3711 SET_EXPR_LOCATION (rhs, loc);
3712 add_stmt (rhs);
3715 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3716 goto done; */
3717 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3718 params->quick_push (lhs_addr);
3719 params->quick_push (old_addr);
3720 params->quick_push (newval_addr);
3721 params->quick_push (integer_zero_node);
3722 params->quick_push (seq_cst);
3723 params->quick_push (seq_cst);
3724 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3726 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3727 SET_EXPR_LOCATION (goto_stmt, loc);
3729 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3730 SET_EXPR_LOCATION (stmt, loc);
3731 add_stmt (stmt);
3733 if (clear_call)
3734 add_stmt (clear_call);
3736 /* goto loop; */
3737 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3738 SET_EXPR_LOCATION (goto_stmt, loc);
3739 add_stmt (goto_stmt);
3741 /* done: */
3742 add_stmt (done_label);
3744 if (update_call)
3745 add_stmt (update_call);
3747 /* Finish the compound statement. */
3748 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3750 /* NEWVAL is the value that was successfully stored, return a
3751 COMPOUND_EXPR of the statement and the appropriate value. */
3752 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3753 return_old_p ? old : newval);
3756 /* Construct and perhaps optimize a tree representation
3757 for a unary operation. CODE, a tree_code, specifies the operation
3758 and XARG is the operand.
3759 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3760 the default promotions (such as from short to int).
3761 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3762 allows non-lvalues; this is only used to handle conversion of non-lvalue
3763 arrays to pointers in C99.
3765 LOCATION is the location of the operator. */
3767 tree
3768 build_unary_op (location_t location,
3769 enum tree_code code, tree xarg, int flag)
3771 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3772 tree arg = xarg;
3773 tree argtype = 0;
3774 enum tree_code typecode;
3775 tree val;
3776 tree ret = error_mark_node;
3777 tree eptype = NULL_TREE;
3778 int noconvert = flag;
3779 const char *invalid_op_diag;
3780 bool int_operands;
3782 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3783 if (int_operands)
3784 arg = remove_c_maybe_const_expr (arg);
3786 if (code != ADDR_EXPR)
3787 arg = require_complete_type (arg);
3789 typecode = TREE_CODE (TREE_TYPE (arg));
3790 if (typecode == ERROR_MARK)
3791 return error_mark_node;
3792 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3793 typecode = INTEGER_TYPE;
3795 if ((invalid_op_diag
3796 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3798 error_at (location, invalid_op_diag);
3799 return error_mark_node;
3802 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3804 eptype = TREE_TYPE (arg);
3805 arg = TREE_OPERAND (arg, 0);
3808 switch (code)
3810 case CONVERT_EXPR:
3811 /* This is used for unary plus, because a CONVERT_EXPR
3812 is enough to prevent anybody from looking inside for
3813 associativity, but won't generate any code. */
3814 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3815 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3816 || typecode == VECTOR_TYPE))
3818 error_at (location, "wrong type argument to unary plus");
3819 return error_mark_node;
3821 else if (!noconvert)
3822 arg = default_conversion (arg);
3823 arg = non_lvalue_loc (location, arg);
3824 break;
3826 case NEGATE_EXPR:
3827 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3828 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3829 || typecode == VECTOR_TYPE))
3831 error_at (location, "wrong type argument to unary minus");
3832 return error_mark_node;
3834 else if (!noconvert)
3835 arg = default_conversion (arg);
3836 break;
3838 case BIT_NOT_EXPR:
3839 /* ~ works on integer types and non float vectors. */
3840 if (typecode == INTEGER_TYPE
3841 || (typecode == VECTOR_TYPE
3842 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3844 if (!noconvert)
3845 arg = default_conversion (arg);
3847 else if (typecode == COMPLEX_TYPE)
3849 code = CONJ_EXPR;
3850 pedwarn (location, OPT_Wpedantic,
3851 "ISO C does not support %<~%> for complex conjugation");
3852 if (!noconvert)
3853 arg = default_conversion (arg);
3855 else
3857 error_at (location, "wrong type argument to bit-complement");
3858 return error_mark_node;
3860 break;
3862 case ABS_EXPR:
3863 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3865 error_at (location, "wrong type argument to abs");
3866 return error_mark_node;
3868 else if (!noconvert)
3869 arg = default_conversion (arg);
3870 break;
3872 case CONJ_EXPR:
3873 /* Conjugating a real value is a no-op, but allow it anyway. */
3874 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3875 || typecode == COMPLEX_TYPE))
3877 error_at (location, "wrong type argument to conjugation");
3878 return error_mark_node;
3880 else if (!noconvert)
3881 arg = default_conversion (arg);
3882 break;
3884 case TRUTH_NOT_EXPR:
3885 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3886 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3887 && typecode != COMPLEX_TYPE)
3889 error_at (location,
3890 "wrong type argument to unary exclamation mark");
3891 return error_mark_node;
3893 if (int_operands)
3895 arg = c_objc_common_truthvalue_conversion (location, xarg);
3896 arg = remove_c_maybe_const_expr (arg);
3898 else
3899 arg = c_objc_common_truthvalue_conversion (location, arg);
3900 ret = invert_truthvalue_loc (location, arg);
3901 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3902 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3903 location = EXPR_LOCATION (ret);
3904 goto return_build_unary_op;
3906 case REALPART_EXPR:
3907 case IMAGPART_EXPR:
3908 ret = build_real_imag_expr (location, code, arg);
3909 if (ret == error_mark_node)
3910 return error_mark_node;
3911 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3912 eptype = TREE_TYPE (eptype);
3913 goto return_build_unary_op;
3915 case PREINCREMENT_EXPR:
3916 case POSTINCREMENT_EXPR:
3917 case PREDECREMENT_EXPR:
3918 case POSTDECREMENT_EXPR:
3920 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3922 tree inner = build_unary_op (location, code,
3923 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3924 if (inner == error_mark_node)
3925 return error_mark_node;
3926 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3927 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3928 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3929 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3930 goto return_build_unary_op;
3933 /* Complain about anything that is not a true lvalue. In
3934 Objective-C, skip this check for property_refs. */
3935 if (!objc_is_property_ref (arg)
3936 && !lvalue_or_else (location,
3937 arg, ((code == PREINCREMENT_EXPR
3938 || code == POSTINCREMENT_EXPR)
3939 ? lv_increment
3940 : lv_decrement)))
3941 return error_mark_node;
3943 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3945 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3946 warning_at (location, OPT_Wc___compat,
3947 "increment of enumeration value is invalid in C++");
3948 else
3949 warning_at (location, OPT_Wc___compat,
3950 "decrement of enumeration value is invalid in C++");
3953 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3954 arg = c_fully_fold (arg, false, NULL);
3956 bool atomic_op;
3957 atomic_op = really_atomic_lvalue (arg);
3959 /* Increment or decrement the real part of the value,
3960 and don't change the imaginary part. */
3961 if (typecode == COMPLEX_TYPE)
3963 tree real, imag;
3965 pedwarn (location, OPT_Wpedantic,
3966 "ISO C does not support %<++%> and %<--%> on complex types");
3968 if (!atomic_op)
3970 arg = stabilize_reference (arg);
3971 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3972 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3973 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3974 if (real == error_mark_node || imag == error_mark_node)
3975 return error_mark_node;
3976 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3977 real, imag);
3978 goto return_build_unary_op;
3982 /* Report invalid types. */
3984 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3985 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3986 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
3988 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3989 error_at (location, "wrong type argument to increment");
3990 else
3991 error_at (location, "wrong type argument to decrement");
3993 return error_mark_node;
3997 tree inc;
3999 argtype = TREE_TYPE (arg);
4001 /* Compute the increment. */
4003 if (typecode == POINTER_TYPE)
4005 /* If pointer target is an undefined struct,
4006 we just cannot know how to do the arithmetic. */
4007 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4009 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4010 error_at (location,
4011 "increment of pointer to unknown structure");
4012 else
4013 error_at (location,
4014 "decrement of pointer to unknown structure");
4016 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4017 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4019 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4020 pedwarn (location, OPT_Wpointer_arith,
4021 "wrong type argument to increment");
4022 else
4023 pedwarn (location, OPT_Wpointer_arith,
4024 "wrong type argument to decrement");
4027 inc = c_size_in_bytes (TREE_TYPE (argtype));
4028 inc = convert_to_ptrofftype_loc (location, inc);
4030 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4032 /* For signed fract types, we invert ++ to -- or
4033 -- to ++, and change inc from 1 to -1, because
4034 it is not possible to represent 1 in signed fract constants.
4035 For unsigned fract types, the result always overflows and
4036 we get an undefined (original) or the maximum value. */
4037 if (code == PREINCREMENT_EXPR)
4038 code = PREDECREMENT_EXPR;
4039 else if (code == PREDECREMENT_EXPR)
4040 code = PREINCREMENT_EXPR;
4041 else if (code == POSTINCREMENT_EXPR)
4042 code = POSTDECREMENT_EXPR;
4043 else /* code == POSTDECREMENT_EXPR */
4044 code = POSTINCREMENT_EXPR;
4046 inc = integer_minus_one_node;
4047 inc = convert (argtype, inc);
4049 else
4051 inc = VECTOR_TYPE_P (argtype)
4052 ? build_one_cst (argtype)
4053 : integer_one_node;
4054 inc = convert (argtype, inc);
4057 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4058 need to ask Objective-C to build the increment or decrement
4059 expression for it. */
4060 if (objc_is_property_ref (arg))
4061 return objc_build_incr_expr_for_property_ref (location, code,
4062 arg, inc);
4064 /* Report a read-only lvalue. */
4065 if (TYPE_READONLY (argtype))
4067 readonly_error (location, arg,
4068 ((code == PREINCREMENT_EXPR
4069 || code == POSTINCREMENT_EXPR)
4070 ? lv_increment : lv_decrement));
4071 return error_mark_node;
4073 else if (TREE_READONLY (arg))
4074 readonly_warning (arg,
4075 ((code == PREINCREMENT_EXPR
4076 || code == POSTINCREMENT_EXPR)
4077 ? lv_increment : lv_decrement));
4079 /* If the argument is atomic, use the special code sequences for
4080 atomic compound assignment. */
4081 if (atomic_op)
4083 arg = stabilize_reference (arg);
4084 ret = build_atomic_assign (location, arg,
4085 ((code == PREINCREMENT_EXPR
4086 || code == POSTINCREMENT_EXPR)
4087 ? PLUS_EXPR
4088 : MINUS_EXPR),
4089 (FRACT_MODE_P (TYPE_MODE (argtype))
4090 ? inc
4091 : integer_one_node),
4092 (code == POSTINCREMENT_EXPR
4093 || code == POSTDECREMENT_EXPR));
4094 goto return_build_unary_op;
4097 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4098 val = boolean_increment (code, arg);
4099 else
4100 val = build2 (code, TREE_TYPE (arg), arg, inc);
4101 TREE_SIDE_EFFECTS (val) = 1;
4102 if (TREE_CODE (val) != code)
4103 TREE_NO_WARNING (val) = 1;
4104 ret = val;
4105 goto return_build_unary_op;
4108 case ADDR_EXPR:
4109 /* Note that this operation never does default_conversion. */
4111 /* The operand of unary '&' must be an lvalue (which excludes
4112 expressions of type void), or, in C99, the result of a [] or
4113 unary '*' operator. */
4114 if (VOID_TYPE_P (TREE_TYPE (arg))
4115 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4116 && (TREE_CODE (arg) != INDIRECT_REF
4117 || !flag_isoc99))
4118 pedwarn (location, 0, "taking address of expression of type %<void%>");
4120 /* Let &* cancel out to simplify resulting code. */
4121 if (TREE_CODE (arg) == INDIRECT_REF)
4123 /* Don't let this be an lvalue. */
4124 if (lvalue_p (TREE_OPERAND (arg, 0)))
4125 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4126 ret = TREE_OPERAND (arg, 0);
4127 goto return_build_unary_op;
4130 /* For &x[y], return x+y */
4131 if (TREE_CODE (arg) == ARRAY_REF)
4133 tree op0 = TREE_OPERAND (arg, 0);
4134 if (!c_mark_addressable (op0))
4135 return error_mark_node;
4138 /* Anything not already handled and not a true memory reference
4139 or a non-lvalue array is an error. */
4140 else if (typecode != FUNCTION_TYPE && !flag
4141 && !lvalue_or_else (location, arg, lv_addressof))
4142 return error_mark_node;
4144 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4145 folding later. */
4146 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4148 tree inner = build_unary_op (location, code,
4149 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4150 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4151 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4152 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4153 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4154 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4155 goto return_build_unary_op;
4158 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4159 argtype = TREE_TYPE (arg);
4161 /* If the lvalue is const or volatile, merge that into the type
4162 to which the address will point. This is only needed
4163 for function types. */
4164 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4165 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4166 && TREE_CODE (argtype) == FUNCTION_TYPE)
4168 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4169 int quals = orig_quals;
4171 if (TREE_READONLY (arg))
4172 quals |= TYPE_QUAL_CONST;
4173 if (TREE_THIS_VOLATILE (arg))
4174 quals |= TYPE_QUAL_VOLATILE;
4176 argtype = c_build_qualified_type (argtype, quals);
4179 if (!c_mark_addressable (arg))
4180 return error_mark_node;
4182 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4183 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4185 argtype = build_pointer_type (argtype);
4187 /* ??? Cope with user tricks that amount to offsetof. Delete this
4188 when we have proper support for integer constant expressions. */
4189 val = get_base_address (arg);
4190 if (val && TREE_CODE (val) == INDIRECT_REF
4191 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4193 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4194 goto return_build_unary_op;
4197 val = build1 (ADDR_EXPR, argtype, arg);
4199 ret = val;
4200 goto return_build_unary_op;
4202 default:
4203 gcc_unreachable ();
4206 if (argtype == 0)
4207 argtype = TREE_TYPE (arg);
4208 if (TREE_CODE (arg) == INTEGER_CST)
4209 ret = (require_constant_value
4210 ? fold_build1_initializer_loc (location, code, argtype, arg)
4211 : fold_build1_loc (location, code, argtype, arg));
4212 else
4213 ret = build1 (code, argtype, arg);
4214 return_build_unary_op:
4215 gcc_assert (ret != error_mark_node);
4216 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4217 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4218 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4219 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4220 ret = note_integer_operands (ret);
4221 if (eptype)
4222 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4223 protected_set_expr_location (ret, location);
4224 return ret;
4227 /* Return nonzero if REF is an lvalue valid for this language.
4228 Lvalues can be assigned, unless their type has TYPE_READONLY.
4229 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4231 bool
4232 lvalue_p (const_tree ref)
4234 const enum tree_code code = TREE_CODE (ref);
4236 switch (code)
4238 case REALPART_EXPR:
4239 case IMAGPART_EXPR:
4240 case COMPONENT_REF:
4241 return lvalue_p (TREE_OPERAND (ref, 0));
4243 case C_MAYBE_CONST_EXPR:
4244 return lvalue_p (TREE_OPERAND (ref, 1));
4246 case COMPOUND_LITERAL_EXPR:
4247 case STRING_CST:
4248 return 1;
4250 case INDIRECT_REF:
4251 case ARRAY_REF:
4252 case ARRAY_NOTATION_REF:
4253 case VAR_DECL:
4254 case PARM_DECL:
4255 case RESULT_DECL:
4256 case ERROR_MARK:
4257 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4258 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4260 case BIND_EXPR:
4261 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4263 default:
4264 return 0;
4268 /* Give a warning for storing in something that is read-only in GCC
4269 terms but not const in ISO C terms. */
4271 static void
4272 readonly_warning (tree arg, enum lvalue_use use)
4274 switch (use)
4276 case lv_assign:
4277 warning (0, "assignment of read-only location %qE", arg);
4278 break;
4279 case lv_increment:
4280 warning (0, "increment of read-only location %qE", arg);
4281 break;
4282 case lv_decrement:
4283 warning (0, "decrement of read-only location %qE", arg);
4284 break;
4285 default:
4286 gcc_unreachable ();
4288 return;
4292 /* Return nonzero if REF is an lvalue valid for this language;
4293 otherwise, print an error message and return zero. USE says
4294 how the lvalue is being used and so selects the error message.
4295 LOCATION is the location at which any error should be reported. */
4297 static int
4298 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4300 int win = lvalue_p (ref);
4302 if (!win)
4303 lvalue_error (loc, use);
4305 return win;
4308 /* Mark EXP saying that we need to be able to take the
4309 address of it; it should not be allocated in a register.
4310 Returns true if successful. */
4312 bool
4313 c_mark_addressable (tree exp)
4315 tree x = exp;
4317 while (1)
4318 switch (TREE_CODE (x))
4320 case COMPONENT_REF:
4321 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4323 error
4324 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4325 return false;
4328 /* ... fall through ... */
4330 case ADDR_EXPR:
4331 case ARRAY_REF:
4332 case REALPART_EXPR:
4333 case IMAGPART_EXPR:
4334 x = TREE_OPERAND (x, 0);
4335 break;
4337 case COMPOUND_LITERAL_EXPR:
4338 case CONSTRUCTOR:
4339 TREE_ADDRESSABLE (x) = 1;
4340 return true;
4342 case VAR_DECL:
4343 case CONST_DECL:
4344 case PARM_DECL:
4345 case RESULT_DECL:
4346 if (C_DECL_REGISTER (x)
4347 && DECL_NONLOCAL (x))
4349 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4351 error
4352 ("global register variable %qD used in nested function", x);
4353 return false;
4355 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4357 else if (C_DECL_REGISTER (x))
4359 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4360 error ("address of global register variable %qD requested", x);
4361 else
4362 error ("address of register variable %qD requested", x);
4363 return false;
4366 /* drops in */
4367 case FUNCTION_DECL:
4368 TREE_ADDRESSABLE (x) = 1;
4369 /* drops out */
4370 default:
4371 return true;
4375 /* Convert EXPR to TYPE, warning about conversion problems with
4376 constants. SEMANTIC_TYPE is the type this conversion would use
4377 without excess precision. If SEMANTIC_TYPE is NULL, this function
4378 is equivalent to convert_and_check. This function is a wrapper that
4379 handles conversions that may be different than
4380 the usual ones because of excess precision. */
4382 static tree
4383 ep_convert_and_check (location_t loc, tree type, tree expr,
4384 tree semantic_type)
4386 if (TREE_TYPE (expr) == type)
4387 return expr;
4389 if (!semantic_type)
4390 return convert_and_check (loc, type, expr);
4392 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4393 && TREE_TYPE (expr) != semantic_type)
4395 /* For integers, we need to check the real conversion, not
4396 the conversion to the excess precision type. */
4397 expr = convert_and_check (loc, semantic_type, expr);
4399 /* Result type is the excess precision type, which should be
4400 large enough, so do not check. */
4401 return convert (type, expr);
4404 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4405 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4406 if folded to an integer constant then the unselected half may
4407 contain arbitrary operations not normally permitted in constant
4408 expressions. Set the location of the expression to LOC. */
4410 tree
4411 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4412 tree op1, tree op1_original_type, tree op2,
4413 tree op2_original_type)
4415 tree type1;
4416 tree type2;
4417 enum tree_code code1;
4418 enum tree_code code2;
4419 tree result_type = NULL;
4420 tree semantic_result_type = NULL;
4421 tree orig_op1 = op1, orig_op2 = op2;
4422 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4423 bool ifexp_int_operands;
4424 tree ret;
4426 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4427 if (op1_int_operands)
4428 op1 = remove_c_maybe_const_expr (op1);
4429 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4430 if (op2_int_operands)
4431 op2 = remove_c_maybe_const_expr (op2);
4432 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4433 if (ifexp_int_operands)
4434 ifexp = remove_c_maybe_const_expr (ifexp);
4436 /* Promote both alternatives. */
4438 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4439 op1 = default_conversion (op1);
4440 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4441 op2 = default_conversion (op2);
4443 if (TREE_CODE (ifexp) == ERROR_MARK
4444 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4445 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4446 return error_mark_node;
4448 type1 = TREE_TYPE (op1);
4449 code1 = TREE_CODE (type1);
4450 type2 = TREE_TYPE (op2);
4451 code2 = TREE_CODE (type2);
4453 /* C90 does not permit non-lvalue arrays in conditional expressions.
4454 In C99 they will be pointers by now. */
4455 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4457 error_at (colon_loc, "non-lvalue array in conditional expression");
4458 return error_mark_node;
4461 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4462 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4463 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4464 || code1 == COMPLEX_TYPE)
4465 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4466 || code2 == COMPLEX_TYPE))
4468 semantic_result_type = c_common_type (type1, type2);
4469 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4471 op1 = TREE_OPERAND (op1, 0);
4472 type1 = TREE_TYPE (op1);
4473 gcc_assert (TREE_CODE (type1) == code1);
4475 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4477 op2 = TREE_OPERAND (op2, 0);
4478 type2 = TREE_TYPE (op2);
4479 gcc_assert (TREE_CODE (type2) == code2);
4483 if (warn_cxx_compat)
4485 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4486 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4488 if (TREE_CODE (t1) == ENUMERAL_TYPE
4489 && TREE_CODE (t2) == ENUMERAL_TYPE
4490 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4491 warning_at (colon_loc, OPT_Wc___compat,
4492 ("different enum types in conditional is "
4493 "invalid in C++: %qT vs %qT"),
4494 t1, t2);
4497 /* Quickly detect the usual case where op1 and op2 have the same type
4498 after promotion. */
4499 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4501 if (type1 == type2)
4502 result_type = type1;
4503 else
4504 result_type = TYPE_MAIN_VARIANT (type1);
4506 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4507 || code1 == COMPLEX_TYPE)
4508 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4509 || code2 == COMPLEX_TYPE))
4511 result_type = c_common_type (type1, type2);
4512 do_warn_double_promotion (result_type, type1, type2,
4513 "implicit conversion from %qT to %qT to "
4514 "match other result of conditional",
4515 colon_loc);
4517 /* If -Wsign-compare, warn here if type1 and type2 have
4518 different signedness. We'll promote the signed to unsigned
4519 and later code won't know it used to be different.
4520 Do this check on the original types, so that explicit casts
4521 will be considered, but default promotions won't. */
4522 if (c_inhibit_evaluation_warnings == 0)
4524 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4525 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4527 if (unsigned_op1 ^ unsigned_op2)
4529 bool ovf;
4531 /* Do not warn if the result type is signed, since the
4532 signed type will only be chosen if it can represent
4533 all the values of the unsigned type. */
4534 if (!TYPE_UNSIGNED (result_type))
4535 /* OK */;
4536 else
4538 bool op1_maybe_const = true;
4539 bool op2_maybe_const = true;
4541 /* Do not warn if the signed quantity is an
4542 unsuffixed integer literal (or some static
4543 constant expression involving such literals) and
4544 it is non-negative. This warning requires the
4545 operands to be folded for best results, so do
4546 that folding in this case even without
4547 warn_sign_compare to avoid warning options
4548 possibly affecting code generation. */
4549 c_inhibit_evaluation_warnings
4550 += (ifexp == truthvalue_false_node);
4551 op1 = c_fully_fold (op1, require_constant_value,
4552 &op1_maybe_const);
4553 c_inhibit_evaluation_warnings
4554 -= (ifexp == truthvalue_false_node);
4556 c_inhibit_evaluation_warnings
4557 += (ifexp == truthvalue_true_node);
4558 op2 = c_fully_fold (op2, require_constant_value,
4559 &op2_maybe_const);
4560 c_inhibit_evaluation_warnings
4561 -= (ifexp == truthvalue_true_node);
4563 if (warn_sign_compare)
4565 if ((unsigned_op2
4566 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4567 || (unsigned_op1
4568 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4569 /* OK */;
4570 else
4571 warning_at (colon_loc, OPT_Wsign_compare,
4572 ("signed and unsigned type in "
4573 "conditional expression"));
4575 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4576 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4577 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4578 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4583 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4585 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4586 pedwarn (colon_loc, OPT_Wpedantic,
4587 "ISO C forbids conditional expr with only one void side");
4588 result_type = void_type_node;
4590 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4592 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4593 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4594 addr_space_t as_common;
4596 if (comp_target_types (colon_loc, type1, type2))
4597 result_type = common_pointer_type (type1, type2);
4598 else if (null_pointer_constant_p (orig_op1))
4599 result_type = type2;
4600 else if (null_pointer_constant_p (orig_op2))
4601 result_type = type1;
4602 else if (!addr_space_superset (as1, as2, &as_common))
4604 error_at (colon_loc, "pointers to disjoint address spaces "
4605 "used in conditional expression");
4606 return error_mark_node;
4608 else if (VOID_TYPE_P (TREE_TYPE (type1))
4609 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4611 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4612 pedwarn (colon_loc, OPT_Wpedantic,
4613 "ISO C forbids conditional expr between "
4614 "%<void *%> and function pointer");
4615 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4616 TREE_TYPE (type2)));
4618 else if (VOID_TYPE_P (TREE_TYPE (type2))
4619 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4621 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4622 pedwarn (colon_loc, OPT_Wpedantic,
4623 "ISO C forbids conditional expr between "
4624 "%<void *%> and function pointer");
4625 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4626 TREE_TYPE (type1)));
4628 /* Objective-C pointer comparisons are a bit more lenient. */
4629 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4630 result_type = objc_common_type (type1, type2);
4631 else
4633 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4635 pedwarn (colon_loc, 0,
4636 "pointer type mismatch in conditional expression");
4637 result_type = build_pointer_type
4638 (build_qualified_type (void_type_node, qual));
4641 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4643 if (!null_pointer_constant_p (orig_op2))
4644 pedwarn (colon_loc, 0,
4645 "pointer/integer type mismatch in conditional expression");
4646 else
4648 op2 = null_pointer_node;
4650 result_type = type1;
4652 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4654 if (!null_pointer_constant_p (orig_op1))
4655 pedwarn (colon_loc, 0,
4656 "pointer/integer type mismatch in conditional expression");
4657 else
4659 op1 = null_pointer_node;
4661 result_type = type2;
4664 if (!result_type)
4666 if (flag_cond_mismatch)
4667 result_type = void_type_node;
4668 else
4670 error_at (colon_loc, "type mismatch in conditional expression");
4671 return error_mark_node;
4675 /* Merge const and volatile flags of the incoming types. */
4676 result_type
4677 = build_type_variant (result_type,
4678 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4679 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4681 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4682 semantic_result_type);
4683 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4684 semantic_result_type);
4686 if (ifexp_bcp && ifexp == truthvalue_true_node)
4688 op2_int_operands = true;
4689 op1 = c_fully_fold (op1, require_constant_value, NULL);
4691 if (ifexp_bcp && ifexp == truthvalue_false_node)
4693 op1_int_operands = true;
4694 op2 = c_fully_fold (op2, require_constant_value, NULL);
4696 int_const = int_operands = (ifexp_int_operands
4697 && op1_int_operands
4698 && op2_int_operands);
4699 if (int_operands)
4701 int_const = ((ifexp == truthvalue_true_node
4702 && TREE_CODE (orig_op1) == INTEGER_CST
4703 && !TREE_OVERFLOW (orig_op1))
4704 || (ifexp == truthvalue_false_node
4705 && TREE_CODE (orig_op2) == INTEGER_CST
4706 && !TREE_OVERFLOW (orig_op2)));
4708 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4709 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4710 else
4712 if (int_operands)
4714 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4715 nested inside of the expression. */
4716 op1 = c_fully_fold (op1, false, NULL);
4717 op2 = c_fully_fold (op2, false, NULL);
4719 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4720 if (int_operands)
4721 ret = note_integer_operands (ret);
4723 if (semantic_result_type)
4724 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4726 protected_set_expr_location (ret, colon_loc);
4727 return ret;
4730 /* Return a compound expression that performs two expressions and
4731 returns the value of the second of them.
4733 LOC is the location of the COMPOUND_EXPR. */
4735 tree
4736 build_compound_expr (location_t loc, tree expr1, tree expr2)
4738 bool expr1_int_operands, expr2_int_operands;
4739 tree eptype = NULL_TREE;
4740 tree ret;
4742 if (flag_cilkplus
4743 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4744 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4746 error_at (loc,
4747 "spawned function call cannot be part of a comma expression");
4748 return error_mark_node;
4750 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4751 if (expr1_int_operands)
4752 expr1 = remove_c_maybe_const_expr (expr1);
4753 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4754 if (expr2_int_operands)
4755 expr2 = remove_c_maybe_const_expr (expr2);
4757 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4758 expr1 = TREE_OPERAND (expr1, 0);
4759 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4761 eptype = TREE_TYPE (expr2);
4762 expr2 = TREE_OPERAND (expr2, 0);
4765 if (!TREE_SIDE_EFFECTS (expr1))
4767 /* The left-hand operand of a comma expression is like an expression
4768 statement: with -Wunused, we should warn if it doesn't have
4769 any side-effects, unless it was explicitly cast to (void). */
4770 if (warn_unused_value)
4772 if (VOID_TYPE_P (TREE_TYPE (expr1))
4773 && CONVERT_EXPR_P (expr1))
4774 ; /* (void) a, b */
4775 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4776 && TREE_CODE (expr1) == COMPOUND_EXPR
4777 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4778 ; /* (void) a, (void) b, c */
4779 else
4780 warning_at (loc, OPT_Wunused_value,
4781 "left-hand operand of comma expression has no effect");
4784 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4785 && warn_unused_value)
4787 tree r = expr1;
4788 location_t cloc = loc;
4789 while (TREE_CODE (r) == COMPOUND_EXPR)
4791 if (EXPR_HAS_LOCATION (r))
4792 cloc = EXPR_LOCATION (r);
4793 r = TREE_OPERAND (r, 1);
4795 if (!TREE_SIDE_EFFECTS (r)
4796 && !VOID_TYPE_P (TREE_TYPE (r))
4797 && !CONVERT_EXPR_P (r))
4798 warning_at (cloc, OPT_Wunused_value,
4799 "right-hand operand of comma expression has no effect");
4802 /* With -Wunused, we should also warn if the left-hand operand does have
4803 side-effects, but computes a value which is not used. For example, in
4804 `foo() + bar(), baz()' the result of the `+' operator is not used,
4805 so we should issue a warning. */
4806 else if (warn_unused_value)
4807 warn_if_unused_value (expr1, loc);
4809 if (expr2 == error_mark_node)
4810 return error_mark_node;
4812 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4814 if (flag_isoc99
4815 && expr1_int_operands
4816 && expr2_int_operands)
4817 ret = note_integer_operands (ret);
4819 if (eptype)
4820 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4822 protected_set_expr_location (ret, loc);
4823 return ret;
4826 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4827 which we are casting. OTYPE is the type of the expression being
4828 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4829 of the cast. -Wcast-qual appeared on the command line. Named
4830 address space qualifiers are not handled here, because they result
4831 in different warnings. */
4833 static void
4834 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4836 tree in_type = type;
4837 tree in_otype = otype;
4838 int added = 0;
4839 int discarded = 0;
4840 bool is_const;
4842 /* Check that the qualifiers on IN_TYPE are a superset of the
4843 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4844 nodes is uninteresting and we stop as soon as we hit a
4845 non-POINTER_TYPE node on either type. */
4848 in_otype = TREE_TYPE (in_otype);
4849 in_type = TREE_TYPE (in_type);
4851 /* GNU C allows cv-qualified function types. 'const' means the
4852 function is very pure, 'volatile' means it can't return. We
4853 need to warn when such qualifiers are added, not when they're
4854 taken away. */
4855 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4856 && TREE_CODE (in_type) == FUNCTION_TYPE)
4857 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4858 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4859 else
4860 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4861 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4863 while (TREE_CODE (in_type) == POINTER_TYPE
4864 && TREE_CODE (in_otype) == POINTER_TYPE);
4866 if (added)
4867 warning_at (loc, OPT_Wcast_qual,
4868 "cast adds %q#v qualifier to function type", added);
4870 if (discarded)
4871 /* There are qualifiers present in IN_OTYPE that are not present
4872 in IN_TYPE. */
4873 warning_at (loc, OPT_Wcast_qual,
4874 "cast discards %qv qualifier from pointer target type",
4875 discarded);
4877 if (added || discarded)
4878 return;
4880 /* A cast from **T to const **T is unsafe, because it can cause a
4881 const value to be changed with no additional warning. We only
4882 issue this warning if T is the same on both sides, and we only
4883 issue the warning if there are the same number of pointers on
4884 both sides, as otherwise the cast is clearly unsafe anyhow. A
4885 cast is unsafe when a qualifier is added at one level and const
4886 is not present at all outer levels.
4888 To issue this warning, we check at each level whether the cast
4889 adds new qualifiers not already seen. We don't need to special
4890 case function types, as they won't have the same
4891 TYPE_MAIN_VARIANT. */
4893 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4894 return;
4895 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4896 return;
4898 in_type = type;
4899 in_otype = otype;
4900 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4903 in_type = TREE_TYPE (in_type);
4904 in_otype = TREE_TYPE (in_otype);
4905 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4906 && !is_const)
4908 warning_at (loc, OPT_Wcast_qual,
4909 "to be safe all intermediate pointers in cast from "
4910 "%qT to %qT must be %<const%> qualified",
4911 otype, type);
4912 break;
4914 if (is_const)
4915 is_const = TYPE_READONLY (in_type);
4917 while (TREE_CODE (in_type) == POINTER_TYPE);
4920 /* Build an expression representing a cast to type TYPE of expression EXPR.
4921 LOC is the location of the cast-- typically the open paren of the cast. */
4923 tree
4924 build_c_cast (location_t loc, tree type, tree expr)
4926 tree value;
4928 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4929 expr = TREE_OPERAND (expr, 0);
4931 value = expr;
4933 if (type == error_mark_node || expr == error_mark_node)
4934 return error_mark_node;
4936 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4937 only in <protocol> qualifications. But when constructing cast expressions,
4938 the protocols do matter and must be kept around. */
4939 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4940 return build1 (NOP_EXPR, type, expr);
4942 type = TYPE_MAIN_VARIANT (type);
4944 if (TREE_CODE (type) == ARRAY_TYPE)
4946 error_at (loc, "cast specifies array type");
4947 return error_mark_node;
4950 if (TREE_CODE (type) == FUNCTION_TYPE)
4952 error_at (loc, "cast specifies function type");
4953 return error_mark_node;
4956 if (!VOID_TYPE_P (type))
4958 value = require_complete_type (value);
4959 if (value == error_mark_node)
4960 return error_mark_node;
4963 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4965 if (TREE_CODE (type) == RECORD_TYPE
4966 || TREE_CODE (type) == UNION_TYPE)
4967 pedwarn (loc, OPT_Wpedantic,
4968 "ISO C forbids casting nonscalar to the same type");
4970 else if (TREE_CODE (type) == UNION_TYPE)
4972 tree field;
4974 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4975 if (TREE_TYPE (field) != error_mark_node
4976 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4977 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4978 break;
4980 if (field)
4982 tree t;
4983 bool maybe_const = true;
4985 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4986 t = c_fully_fold (value, false, &maybe_const);
4987 t = build_constructor_single (type, field, t);
4988 if (!maybe_const)
4989 t = c_wrap_maybe_const (t, true);
4990 t = digest_init (loc, type, t,
4991 NULL_TREE, false, true, 0);
4992 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4993 return t;
4995 error_at (loc, "cast to union type from type not present in union");
4996 return error_mark_node;
4998 else
5000 tree otype, ovalue;
5002 if (type == void_type_node)
5004 tree t = build1 (CONVERT_EXPR, type, value);
5005 SET_EXPR_LOCATION (t, loc);
5006 return t;
5009 otype = TREE_TYPE (value);
5011 /* Optionally warn about potentially worrisome casts. */
5012 if (warn_cast_qual
5013 && TREE_CODE (type) == POINTER_TYPE
5014 && TREE_CODE (otype) == POINTER_TYPE)
5015 handle_warn_cast_qual (loc, type, otype);
5017 /* Warn about conversions between pointers to disjoint
5018 address spaces. */
5019 if (TREE_CODE (type) == POINTER_TYPE
5020 && TREE_CODE (otype) == POINTER_TYPE
5021 && !null_pointer_constant_p (value))
5023 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5024 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5025 addr_space_t as_common;
5027 if (!addr_space_superset (as_to, as_from, &as_common))
5029 if (ADDR_SPACE_GENERIC_P (as_from))
5030 warning_at (loc, 0, "cast to %s address space pointer "
5031 "from disjoint generic address space pointer",
5032 c_addr_space_name (as_to));
5034 else if (ADDR_SPACE_GENERIC_P (as_to))
5035 warning_at (loc, 0, "cast to generic address space pointer "
5036 "from disjoint %s address space pointer",
5037 c_addr_space_name (as_from));
5039 else
5040 warning_at (loc, 0, "cast to %s address space pointer "
5041 "from disjoint %s address space pointer",
5042 c_addr_space_name (as_to),
5043 c_addr_space_name (as_from));
5047 /* Warn about possible alignment problems. */
5048 if (STRICT_ALIGNMENT
5049 && TREE_CODE (type) == POINTER_TYPE
5050 && TREE_CODE (otype) == POINTER_TYPE
5051 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5052 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5053 /* Don't warn about opaque types, where the actual alignment
5054 restriction is unknown. */
5055 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5056 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5057 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5058 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5059 warning_at (loc, OPT_Wcast_align,
5060 "cast increases required alignment of target type");
5062 if (TREE_CODE (type) == INTEGER_TYPE
5063 && TREE_CODE (otype) == POINTER_TYPE
5064 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5065 /* Unlike conversion of integers to pointers, where the
5066 warning is disabled for converting constants because
5067 of cases such as SIG_*, warn about converting constant
5068 pointers to integers. In some cases it may cause unwanted
5069 sign extension, and a warning is appropriate. */
5070 warning_at (loc, OPT_Wpointer_to_int_cast,
5071 "cast from pointer to integer of different size");
5073 if (TREE_CODE (value) == CALL_EXPR
5074 && TREE_CODE (type) != TREE_CODE (otype))
5075 warning_at (loc, OPT_Wbad_function_cast,
5076 "cast from function call of type %qT "
5077 "to non-matching type %qT", otype, type);
5079 if (TREE_CODE (type) == POINTER_TYPE
5080 && TREE_CODE (otype) == INTEGER_TYPE
5081 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5082 /* Don't warn about converting any constant. */
5083 && !TREE_CONSTANT (value))
5084 warning_at (loc,
5085 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5086 "of different size");
5088 if (warn_strict_aliasing <= 2)
5089 strict_aliasing_warning (otype, type, expr);
5091 /* If pedantic, warn for conversions between function and object
5092 pointer types, except for converting a null pointer constant
5093 to function pointer type. */
5094 if (pedantic
5095 && TREE_CODE (type) == POINTER_TYPE
5096 && TREE_CODE (otype) == POINTER_TYPE
5097 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5098 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5099 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5100 "conversion of function pointer to object pointer type");
5102 if (pedantic
5103 && TREE_CODE (type) == POINTER_TYPE
5104 && TREE_CODE (otype) == POINTER_TYPE
5105 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5106 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5107 && !null_pointer_constant_p (value))
5108 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5109 "conversion of object pointer to function pointer type");
5111 ovalue = value;
5112 value = convert (type, value);
5114 /* Ignore any integer overflow caused by the cast. */
5115 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5117 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5119 if (!TREE_OVERFLOW (value))
5121 /* Avoid clobbering a shared constant. */
5122 value = copy_node (value);
5123 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5126 else if (TREE_OVERFLOW (value))
5127 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5128 value = build_int_cst_wide (TREE_TYPE (value),
5129 TREE_INT_CST_LOW (value),
5130 TREE_INT_CST_HIGH (value));
5134 /* Don't let a cast be an lvalue. */
5135 if (value == expr)
5136 value = non_lvalue_loc (loc, value);
5138 /* Don't allow the results of casting to floating-point or complex
5139 types be confused with actual constants, or casts involving
5140 integer and pointer types other than direct integer-to-integer
5141 and integer-to-pointer be confused with integer constant
5142 expressions and null pointer constants. */
5143 if (TREE_CODE (value) == REAL_CST
5144 || TREE_CODE (value) == COMPLEX_CST
5145 || (TREE_CODE (value) == INTEGER_CST
5146 && !((TREE_CODE (expr) == INTEGER_CST
5147 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5148 || TREE_CODE (expr) == REAL_CST
5149 || TREE_CODE (expr) == COMPLEX_CST)))
5150 value = build1 (NOP_EXPR, type, value);
5152 if (CAN_HAVE_LOCATION_P (value))
5153 SET_EXPR_LOCATION (value, loc);
5154 return value;
5157 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5158 location of the open paren of the cast, or the position of the cast
5159 expr. */
5160 tree
5161 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5163 tree type;
5164 tree type_expr = NULL_TREE;
5165 bool type_expr_const = true;
5166 tree ret;
5167 int saved_wsp = warn_strict_prototypes;
5169 /* This avoids warnings about unprototyped casts on
5170 integers. E.g. "#define SIG_DFL (void(*)())0". */
5171 if (TREE_CODE (expr) == INTEGER_CST)
5172 warn_strict_prototypes = 0;
5173 type = groktypename (type_name, &type_expr, &type_expr_const);
5174 warn_strict_prototypes = saved_wsp;
5176 ret = build_c_cast (loc, type, expr);
5177 if (type_expr)
5179 bool inner_expr_const = true;
5180 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5181 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5182 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5183 && inner_expr_const);
5184 SET_EXPR_LOCATION (ret, loc);
5187 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5188 SET_EXPR_LOCATION (ret, loc);
5190 /* C++ does not permits types to be defined in a cast, but it
5191 allows references to incomplete types. */
5192 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5193 warning_at (loc, OPT_Wc___compat,
5194 "defining a type in a cast is invalid in C++");
5196 return ret;
5199 /* Build an assignment expression of lvalue LHS from value RHS.
5200 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5201 may differ from TREE_TYPE (LHS) for an enum bitfield.
5202 MODIFYCODE is the code for a binary operator that we use
5203 to combine the old value of LHS with RHS to get the new value.
5204 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5205 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5206 which may differ from TREE_TYPE (RHS) for an enum value.
5208 LOCATION is the location of the MODIFYCODE operator.
5209 RHS_LOC is the location of the RHS. */
5211 tree
5212 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5213 enum tree_code modifycode,
5214 location_t rhs_loc, tree rhs, tree rhs_origtype)
5216 tree result;
5217 tree newrhs;
5218 tree rhseval = NULL_TREE;
5219 tree rhs_semantic_type = NULL_TREE;
5220 tree lhstype = TREE_TYPE (lhs);
5221 tree olhstype = lhstype;
5222 bool npc;
5223 bool is_atomic_op;
5225 /* Types that aren't fully specified cannot be used in assignments. */
5226 lhs = require_complete_type (lhs);
5228 /* Avoid duplicate error messages from operands that had errors. */
5229 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5230 return error_mark_node;
5232 /* Ensure an error for assigning a non-lvalue array to an array in
5233 C90. */
5234 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5236 error_at (location, "assignment to expression with array type");
5237 return error_mark_node;
5240 /* For ObjC properties, defer this check. */
5241 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5242 return error_mark_node;
5244 is_atomic_op = really_atomic_lvalue (lhs);
5246 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5248 rhs_semantic_type = TREE_TYPE (rhs);
5249 rhs = TREE_OPERAND (rhs, 0);
5252 newrhs = rhs;
5254 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5256 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5257 lhs_origtype, modifycode, rhs_loc, rhs,
5258 rhs_origtype);
5259 if (inner == error_mark_node)
5260 return error_mark_node;
5261 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5262 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5263 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5264 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5265 protected_set_expr_location (result, location);
5266 return result;
5269 /* If a binary op has been requested, combine the old LHS value with the RHS
5270 producing the value we should actually store into the LHS. */
5272 if (modifycode != NOP_EXPR)
5274 lhs = c_fully_fold (lhs, false, NULL);
5275 lhs = stabilize_reference (lhs);
5277 /* Construct the RHS for any non-atomic compound assignemnt. */
5278 if (!is_atomic_op)
5280 /* If in LHS op= RHS the RHS has side-effects, ensure they
5281 are preevaluated before the rest of the assignment expression's
5282 side-effects, because RHS could contain e.g. function calls
5283 that modify LHS. */
5284 if (TREE_SIDE_EFFECTS (rhs))
5286 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5287 rhseval = newrhs;
5289 newrhs = build_binary_op (location,
5290 modifycode, lhs, newrhs, 1);
5292 /* The original type of the right hand side is no longer
5293 meaningful. */
5294 rhs_origtype = NULL_TREE;
5298 if (c_dialect_objc ())
5300 /* Check if we are modifying an Objective-C property reference;
5301 if so, we need to generate setter calls. */
5302 result = objc_maybe_build_modify_expr (lhs, newrhs);
5303 if (result)
5304 goto return_result;
5306 /* Else, do the check that we postponed for Objective-C. */
5307 if (!lvalue_or_else (location, lhs, lv_assign))
5308 return error_mark_node;
5311 /* Give an error for storing in something that is 'const'. */
5313 if (TYPE_READONLY (lhstype)
5314 || ((TREE_CODE (lhstype) == RECORD_TYPE
5315 || TREE_CODE (lhstype) == UNION_TYPE)
5316 && C_TYPE_FIELDS_READONLY (lhstype)))
5318 readonly_error (location, lhs, lv_assign);
5319 return error_mark_node;
5321 else if (TREE_READONLY (lhs))
5322 readonly_warning (lhs, lv_assign);
5324 /* If storing into a structure or union member,
5325 it has probably been given type `int'.
5326 Compute the type that would go with
5327 the actual amount of storage the member occupies. */
5329 if (TREE_CODE (lhs) == COMPONENT_REF
5330 && (TREE_CODE (lhstype) == INTEGER_TYPE
5331 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5332 || TREE_CODE (lhstype) == REAL_TYPE
5333 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5334 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5336 /* If storing in a field that is in actuality a short or narrower than one,
5337 we must store in the field in its actual type. */
5339 if (lhstype != TREE_TYPE (lhs))
5341 lhs = copy_node (lhs);
5342 TREE_TYPE (lhs) = lhstype;
5345 /* Issue -Wc++-compat warnings about an assignment to an enum type
5346 when LHS does not have its original type. This happens for,
5347 e.g., an enum bitfield in a struct. */
5348 if (warn_cxx_compat
5349 && lhs_origtype != NULL_TREE
5350 && lhs_origtype != lhstype
5351 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5353 tree checktype = (rhs_origtype != NULL_TREE
5354 ? rhs_origtype
5355 : TREE_TYPE (rhs));
5356 if (checktype != error_mark_node
5357 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5358 || (is_atomic_op && modifycode != NOP_EXPR)))
5359 warning_at (location, OPT_Wc___compat,
5360 "enum conversion in assignment is invalid in C++");
5363 /* If the lhs is atomic, remove that qualifier. */
5364 if (is_atomic_op)
5366 lhstype = build_qualified_type (lhstype,
5367 (TYPE_QUALS (lhstype)
5368 & ~TYPE_QUAL_ATOMIC));
5369 olhstype = build_qualified_type (olhstype,
5370 (TYPE_QUALS (lhstype)
5371 & ~TYPE_QUAL_ATOMIC));
5374 /* Convert new value to destination type. Fold it first, then
5375 restore any excess precision information, for the sake of
5376 conversion warnings. */
5378 if (!(is_atomic_op && modifycode != NOP_EXPR))
5380 npc = null_pointer_constant_p (newrhs);
5381 newrhs = c_fully_fold (newrhs, false, NULL);
5382 if (rhs_semantic_type)
5383 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5384 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5385 rhs_origtype, ic_assign, npc,
5386 NULL_TREE, NULL_TREE, 0);
5387 if (TREE_CODE (newrhs) == ERROR_MARK)
5388 return error_mark_node;
5391 /* Emit ObjC write barrier, if necessary. */
5392 if (c_dialect_objc () && flag_objc_gc)
5394 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5395 if (result)
5397 protected_set_expr_location (result, location);
5398 goto return_result;
5402 /* Scan operands. */
5404 if (is_atomic_op)
5405 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5406 else
5408 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5409 TREE_SIDE_EFFECTS (result) = 1;
5410 protected_set_expr_location (result, location);
5413 /* If we got the LHS in a different type for storing in,
5414 convert the result back to the nominal type of LHS
5415 so that the value we return always has the same type
5416 as the LHS argument. */
5418 if (olhstype == TREE_TYPE (result))
5419 goto return_result;
5421 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5422 rhs_origtype, ic_assign, false, NULL_TREE,
5423 NULL_TREE, 0);
5424 protected_set_expr_location (result, location);
5426 return_result:
5427 if (rhseval)
5428 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5429 return result;
5432 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5433 This is used to implement -fplan9-extensions. */
5435 static bool
5436 find_anonymous_field_with_type (tree struct_type, tree type)
5438 tree field;
5439 bool found;
5441 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5442 || TREE_CODE (struct_type) == UNION_TYPE);
5443 found = false;
5444 for (field = TYPE_FIELDS (struct_type);
5445 field != NULL_TREE;
5446 field = TREE_CHAIN (field))
5448 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5449 ? c_build_qualified_type (TREE_TYPE (field),
5450 TYPE_QUAL_ATOMIC)
5451 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5452 if (DECL_NAME (field) == NULL
5453 && comptypes (type, fieldtype))
5455 if (found)
5456 return false;
5457 found = true;
5459 else if (DECL_NAME (field) == NULL
5460 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5461 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5462 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5464 if (found)
5465 return false;
5466 found = true;
5469 return found;
5472 /* RHS is an expression whose type is pointer to struct. If there is
5473 an anonymous field in RHS with type TYPE, then return a pointer to
5474 that field in RHS. This is used with -fplan9-extensions. This
5475 returns NULL if no conversion could be found. */
5477 static tree
5478 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5480 tree rhs_struct_type, lhs_main_type;
5481 tree field, found_field;
5482 bool found_sub_field;
5483 tree ret;
5485 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5486 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5487 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5488 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5490 gcc_assert (POINTER_TYPE_P (type));
5491 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5492 ? c_build_qualified_type (TREE_TYPE (type),
5493 TYPE_QUAL_ATOMIC)
5494 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5496 found_field = NULL_TREE;
5497 found_sub_field = false;
5498 for (field = TYPE_FIELDS (rhs_struct_type);
5499 field != NULL_TREE;
5500 field = TREE_CHAIN (field))
5502 if (DECL_NAME (field) != NULL_TREE
5503 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5504 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5505 continue;
5506 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5507 ? c_build_qualified_type (TREE_TYPE (field),
5508 TYPE_QUAL_ATOMIC)
5509 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5510 if (comptypes (lhs_main_type, fieldtype))
5512 if (found_field != NULL_TREE)
5513 return NULL_TREE;
5514 found_field = field;
5516 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5517 lhs_main_type))
5519 if (found_field != NULL_TREE)
5520 return NULL_TREE;
5521 found_field = field;
5522 found_sub_field = true;
5526 if (found_field == NULL_TREE)
5527 return NULL_TREE;
5529 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5530 build_fold_indirect_ref (rhs), found_field,
5531 NULL_TREE);
5532 ret = build_fold_addr_expr_loc (location, ret);
5534 if (found_sub_field)
5536 ret = convert_to_anonymous_field (location, type, ret);
5537 gcc_assert (ret != NULL_TREE);
5540 return ret;
5543 /* Convert value RHS to type TYPE as preparation for an assignment to
5544 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5545 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5546 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5547 constant before any folding.
5548 The real work of conversion is done by `convert'.
5549 The purpose of this function is to generate error messages
5550 for assignments that are not allowed in C.
5551 ERRTYPE says whether it is argument passing, assignment,
5552 initialization or return.
5554 LOCATION is the location of the assignment, EXPR_LOC is the location of
5555 the RHS or, for a function, location of an argument.
5556 FUNCTION is a tree for the function being called.
5557 PARMNUM is the number of the argument, for printing in error messages. */
5559 static tree
5560 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5561 tree rhs, tree origtype, enum impl_conv errtype,
5562 bool null_pointer_constant, tree fundecl,
5563 tree function, int parmnum)
5565 enum tree_code codel = TREE_CODE (type);
5566 tree orig_rhs = rhs;
5567 tree rhstype;
5568 enum tree_code coder;
5569 tree rname = NULL_TREE;
5570 bool objc_ok = false;
5572 if (errtype == ic_argpass)
5574 tree selector;
5575 /* Change pointer to function to the function itself for
5576 diagnostics. */
5577 if (TREE_CODE (function) == ADDR_EXPR
5578 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5579 function = TREE_OPERAND (function, 0);
5581 /* Handle an ObjC selector specially for diagnostics. */
5582 selector = objc_message_selector ();
5583 rname = function;
5584 if (selector && parmnum > 2)
5586 rname = selector;
5587 parmnum -= 2;
5591 /* This macro is used to emit diagnostics to ensure that all format
5592 strings are complete sentences, visible to gettext and checked at
5593 compile time. */
5594 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5595 do { \
5596 switch (errtype) \
5598 case ic_argpass: \
5599 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5600 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5601 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5602 "expected %qT but argument is of type %qT", \
5603 type, rhstype); \
5604 break; \
5605 case ic_assign: \
5606 pedwarn (LOCATION, OPT, AS); \
5607 break; \
5608 case ic_init: \
5609 pedwarn_init (LOCATION, OPT, IN); \
5610 break; \
5611 case ic_return: \
5612 pedwarn (LOCATION, OPT, RE); \
5613 break; \
5614 default: \
5615 gcc_unreachable (); \
5617 } while (0)
5619 /* This macro is used to emit diagnostics to ensure that all format
5620 strings are complete sentences, visible to gettext and checked at
5621 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5622 extra parameter to enumerate qualifiers. */
5624 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5625 do { \
5626 switch (errtype) \
5628 case ic_argpass: \
5629 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5630 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5631 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5632 "expected %qT but argument is of type %qT", \
5633 type, rhstype); \
5634 break; \
5635 case ic_assign: \
5636 pedwarn (LOCATION, OPT, AS, QUALS); \
5637 break; \
5638 case ic_init: \
5639 pedwarn (LOCATION, OPT, IN, QUALS); \
5640 break; \
5641 case ic_return: \
5642 pedwarn (LOCATION, OPT, RE, QUALS); \
5643 break; \
5644 default: \
5645 gcc_unreachable (); \
5647 } while (0)
5649 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5650 rhs = TREE_OPERAND (rhs, 0);
5652 rhstype = TREE_TYPE (rhs);
5653 coder = TREE_CODE (rhstype);
5655 if (coder == ERROR_MARK)
5656 return error_mark_node;
5658 if (c_dialect_objc ())
5660 int parmno;
5662 switch (errtype)
5664 case ic_return:
5665 parmno = 0;
5666 break;
5668 case ic_assign:
5669 parmno = -1;
5670 break;
5672 case ic_init:
5673 parmno = -2;
5674 break;
5676 default:
5677 parmno = parmnum;
5678 break;
5681 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5684 if (warn_cxx_compat)
5686 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5687 if (checktype != error_mark_node
5688 && TREE_CODE (type) == ENUMERAL_TYPE
5689 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5691 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5692 G_("enum conversion when passing argument "
5693 "%d of %qE is invalid in C++"),
5694 G_("enum conversion in assignment is "
5695 "invalid in C++"),
5696 G_("enum conversion in initialization is "
5697 "invalid in C++"),
5698 G_("enum conversion in return is "
5699 "invalid in C++"));
5703 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5704 return rhs;
5706 if (coder == VOID_TYPE)
5708 /* Except for passing an argument to an unprototyped function,
5709 this is a constraint violation. When passing an argument to
5710 an unprototyped function, it is compile-time undefined;
5711 making it a constraint in that case was rejected in
5712 DR#252. */
5713 error_at (location, "void value not ignored as it ought to be");
5714 return error_mark_node;
5716 rhs = require_complete_type (rhs);
5717 if (rhs == error_mark_node)
5718 return error_mark_node;
5719 /* A non-reference type can convert to a reference. This handles
5720 va_start, va_copy and possibly port built-ins. */
5721 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5723 if (!lvalue_p (rhs))
5725 error_at (location, "cannot pass rvalue to reference parameter");
5726 return error_mark_node;
5728 if (!c_mark_addressable (rhs))
5729 return error_mark_node;
5730 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5731 SET_EXPR_LOCATION (rhs, location);
5733 rhs = convert_for_assignment (location, expr_loc,
5734 build_pointer_type (TREE_TYPE (type)),
5735 rhs, origtype, errtype,
5736 null_pointer_constant, fundecl, function,
5737 parmnum);
5738 if (rhs == error_mark_node)
5739 return error_mark_node;
5741 rhs = build1 (NOP_EXPR, type, rhs);
5742 SET_EXPR_LOCATION (rhs, location);
5743 return rhs;
5745 /* Some types can interconvert without explicit casts. */
5746 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5747 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5748 return convert (type, rhs);
5749 /* Arithmetic types all interconvert, and enum is treated like int. */
5750 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5751 || codel == FIXED_POINT_TYPE
5752 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5753 || codel == BOOLEAN_TYPE)
5754 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5755 || coder == FIXED_POINT_TYPE
5756 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5757 || coder == BOOLEAN_TYPE))
5759 tree ret;
5760 bool save = in_late_binary_op;
5761 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5762 in_late_binary_op = true;
5763 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5764 ? expr_loc : location, type, orig_rhs);
5765 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5766 in_late_binary_op = save;
5767 return ret;
5770 /* Aggregates in different TUs might need conversion. */
5771 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5772 && codel == coder
5773 && comptypes (type, rhstype))
5774 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5775 ? expr_loc : location, type, rhs);
5777 /* Conversion to a transparent union or record from its member types.
5778 This applies only to function arguments. */
5779 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5780 && TYPE_TRANSPARENT_AGGR (type))
5781 && errtype == ic_argpass)
5783 tree memb, marginal_memb = NULL_TREE;
5785 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5787 tree memb_type = TREE_TYPE (memb);
5789 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5790 TYPE_MAIN_VARIANT (rhstype)))
5791 break;
5793 if (TREE_CODE (memb_type) != POINTER_TYPE)
5794 continue;
5796 if (coder == POINTER_TYPE)
5798 tree ttl = TREE_TYPE (memb_type);
5799 tree ttr = TREE_TYPE (rhstype);
5801 /* Any non-function converts to a [const][volatile] void *
5802 and vice versa; otherwise, targets must be the same.
5803 Meanwhile, the lhs target must have all the qualifiers of
5804 the rhs. */
5805 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5806 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5807 || comp_target_types (location, memb_type, rhstype))
5809 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5810 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5811 /* If this type won't generate any warnings, use it. */
5812 if (lquals == rquals
5813 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5814 && TREE_CODE (ttl) == FUNCTION_TYPE)
5815 ? ((lquals | rquals) == rquals)
5816 : ((lquals | rquals) == lquals)))
5817 break;
5819 /* Keep looking for a better type, but remember this one. */
5820 if (!marginal_memb)
5821 marginal_memb = memb;
5825 /* Can convert integer zero to any pointer type. */
5826 if (null_pointer_constant)
5828 rhs = null_pointer_node;
5829 break;
5833 if (memb || marginal_memb)
5835 if (!memb)
5837 /* We have only a marginally acceptable member type;
5838 it needs a warning. */
5839 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5840 tree ttr = TREE_TYPE (rhstype);
5842 /* Const and volatile mean something different for function
5843 types, so the usual warnings are not appropriate. */
5844 if (TREE_CODE (ttr) == FUNCTION_TYPE
5845 && TREE_CODE (ttl) == FUNCTION_TYPE)
5847 /* Because const and volatile on functions are
5848 restrictions that say the function will not do
5849 certain things, it is okay to use a const or volatile
5850 function where an ordinary one is wanted, but not
5851 vice-versa. */
5852 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5853 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5854 WARN_FOR_QUALIFIERS (location, 0,
5855 G_("passing argument %d of %qE "
5856 "makes %q#v qualified function "
5857 "pointer from unqualified"),
5858 G_("assignment makes %q#v qualified "
5859 "function pointer from "
5860 "unqualified"),
5861 G_("initialization makes %q#v qualified "
5862 "function pointer from "
5863 "unqualified"),
5864 G_("return makes %q#v qualified function "
5865 "pointer from unqualified"),
5866 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5868 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5869 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5870 WARN_FOR_QUALIFIERS (location, 0,
5871 G_("passing argument %d of %qE discards "
5872 "%qv qualifier from pointer target type"),
5873 G_("assignment discards %qv qualifier "
5874 "from pointer target type"),
5875 G_("initialization discards %qv qualifier "
5876 "from pointer target type"),
5877 G_("return discards %qv qualifier from "
5878 "pointer target type"),
5879 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5881 memb = marginal_memb;
5884 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5885 pedwarn (location, OPT_Wpedantic,
5886 "ISO C prohibits argument conversion to union type");
5888 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5889 return build_constructor_single (type, memb, rhs);
5893 /* Conversions among pointers */
5894 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5895 && (coder == codel))
5897 tree ttl = TREE_TYPE (type);
5898 tree ttr = TREE_TYPE (rhstype);
5899 tree mvl = ttl;
5900 tree mvr = ttr;
5901 bool is_opaque_pointer;
5902 int target_cmp = 0; /* Cache comp_target_types () result. */
5903 addr_space_t asl;
5904 addr_space_t asr;
5906 if (TREE_CODE (mvl) != ARRAY_TYPE)
5907 mvl = (TYPE_ATOMIC (mvl)
5908 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5909 TYPE_QUAL_ATOMIC)
5910 : TYPE_MAIN_VARIANT (mvl));
5911 if (TREE_CODE (mvr) != ARRAY_TYPE)
5912 mvr = (TYPE_ATOMIC (mvr)
5913 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5914 TYPE_QUAL_ATOMIC)
5915 : TYPE_MAIN_VARIANT (mvr));
5916 /* Opaque pointers are treated like void pointers. */
5917 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5919 /* The Plan 9 compiler permits a pointer to a struct to be
5920 automatically converted into a pointer to an anonymous field
5921 within the struct. */
5922 if (flag_plan9_extensions
5923 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5924 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5925 && mvl != mvr)
5927 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5928 if (new_rhs != NULL_TREE)
5930 rhs = new_rhs;
5931 rhstype = TREE_TYPE (rhs);
5932 coder = TREE_CODE (rhstype);
5933 ttr = TREE_TYPE (rhstype);
5934 mvr = TYPE_MAIN_VARIANT (ttr);
5938 /* C++ does not allow the implicit conversion void* -> T*. However,
5939 for the purpose of reducing the number of false positives, we
5940 tolerate the special case of
5942 int *p = NULL;
5944 where NULL is typically defined in C to be '(void *) 0'. */
5945 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5946 warning_at (location, OPT_Wc___compat,
5947 "request for implicit conversion "
5948 "from %qT to %qT not permitted in C++", rhstype, type);
5950 /* See if the pointers point to incompatible address spaces. */
5951 asl = TYPE_ADDR_SPACE (ttl);
5952 asr = TYPE_ADDR_SPACE (ttr);
5953 if (!null_pointer_constant_p (rhs)
5954 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5956 switch (errtype)
5958 case ic_argpass:
5959 error_at (location, "passing argument %d of %qE from pointer to "
5960 "non-enclosed address space", parmnum, rname);
5961 break;
5962 case ic_assign:
5963 error_at (location, "assignment from pointer to "
5964 "non-enclosed address space");
5965 break;
5966 case ic_init:
5967 error_at (location, "initialization from pointer to "
5968 "non-enclosed address space");
5969 break;
5970 case ic_return:
5971 error_at (location, "return from pointer to "
5972 "non-enclosed address space");
5973 break;
5974 default:
5975 gcc_unreachable ();
5977 return error_mark_node;
5980 /* Check if the right-hand side has a format attribute but the
5981 left-hand side doesn't. */
5982 if (warn_suggest_attribute_format
5983 && check_missing_format_attribute (type, rhstype))
5985 switch (errtype)
5987 case ic_argpass:
5988 warning_at (location, OPT_Wsuggest_attribute_format,
5989 "argument %d of %qE might be "
5990 "a candidate for a format attribute",
5991 parmnum, rname);
5992 break;
5993 case ic_assign:
5994 warning_at (location, OPT_Wsuggest_attribute_format,
5995 "assignment left-hand side might be "
5996 "a candidate for a format attribute");
5997 break;
5998 case ic_init:
5999 warning_at (location, OPT_Wsuggest_attribute_format,
6000 "initialization left-hand side might be "
6001 "a candidate for a format attribute");
6002 break;
6003 case ic_return:
6004 warning_at (location, OPT_Wsuggest_attribute_format,
6005 "return type might be "
6006 "a candidate for a format attribute");
6007 break;
6008 default:
6009 gcc_unreachable ();
6013 /* Any non-function converts to a [const][volatile] void *
6014 and vice versa; otherwise, targets must be the same.
6015 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6016 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6017 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6018 || (target_cmp = comp_target_types (location, type, rhstype))
6019 || is_opaque_pointer
6020 || ((c_common_unsigned_type (mvl)
6021 == c_common_unsigned_type (mvr))
6022 && (c_common_signed_type (mvl)
6023 == c_common_signed_type (mvr))
6024 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6026 if (pedantic
6027 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6029 (VOID_TYPE_P (ttr)
6030 && !null_pointer_constant
6031 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6032 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
6033 G_("ISO C forbids passing argument %d of "
6034 "%qE between function pointer "
6035 "and %<void *%>"),
6036 G_("ISO C forbids assignment between "
6037 "function pointer and %<void *%>"),
6038 G_("ISO C forbids initialization between "
6039 "function pointer and %<void *%>"),
6040 G_("ISO C forbids return between function "
6041 "pointer and %<void *%>"));
6042 /* Const and volatile mean something different for function types,
6043 so the usual warnings are not appropriate. */
6044 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6045 && TREE_CODE (ttl) != FUNCTION_TYPE)
6047 /* Assignments between atomic and non-atomic objects are OK. */
6048 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6049 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6051 WARN_FOR_QUALIFIERS (location, 0,
6052 G_("passing argument %d of %qE discards "
6053 "%qv qualifier from pointer target type"),
6054 G_("assignment discards %qv qualifier "
6055 "from pointer target type"),
6056 G_("initialization discards %qv qualifier "
6057 "from pointer target type"),
6058 G_("return discards %qv qualifier from "
6059 "pointer target type"),
6060 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6062 /* If this is not a case of ignoring a mismatch in signedness,
6063 no warning. */
6064 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6065 || target_cmp)
6067 /* If there is a mismatch, do warn. */
6068 else if (warn_pointer_sign)
6069 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
6070 G_("pointer targets in passing argument "
6071 "%d of %qE differ in signedness"),
6072 G_("pointer targets in assignment "
6073 "differ in signedness"),
6074 G_("pointer targets in initialization "
6075 "differ in signedness"),
6076 G_("pointer targets in return differ "
6077 "in signedness"));
6079 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6080 && TREE_CODE (ttr) == FUNCTION_TYPE)
6082 /* Because const and volatile on functions are restrictions
6083 that say the function will not do certain things,
6084 it is okay to use a const or volatile function
6085 where an ordinary one is wanted, but not vice-versa. */
6086 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6087 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6088 WARN_FOR_QUALIFIERS (location, 0,
6089 G_("passing argument %d of %qE makes "
6090 "%q#v qualified function pointer "
6091 "from unqualified"),
6092 G_("assignment makes %q#v qualified function "
6093 "pointer from unqualified"),
6094 G_("initialization makes %q#v qualified "
6095 "function pointer from unqualified"),
6096 G_("return makes %q#v qualified function "
6097 "pointer from unqualified"),
6098 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6101 else
6102 /* Avoid warning about the volatile ObjC EH puts on decls. */
6103 if (!objc_ok)
6104 WARN_FOR_ASSIGNMENT (location, 0,
6105 G_("passing argument %d of %qE from "
6106 "incompatible pointer type"),
6107 G_("assignment from incompatible pointer type"),
6108 G_("initialization from incompatible "
6109 "pointer type"),
6110 G_("return from incompatible pointer type"));
6112 return convert (type, rhs);
6114 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6116 /* ??? This should not be an error when inlining calls to
6117 unprototyped functions. */
6118 error_at (location, "invalid use of non-lvalue array");
6119 return error_mark_node;
6121 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6123 /* An explicit constant 0 can convert to a pointer,
6124 or one that results from arithmetic, even including
6125 a cast to integer type. */
6126 if (!null_pointer_constant)
6127 WARN_FOR_ASSIGNMENT (location, 0,
6128 G_("passing argument %d of %qE makes "
6129 "pointer from integer without a cast"),
6130 G_("assignment makes pointer from integer "
6131 "without a cast"),
6132 G_("initialization makes pointer from "
6133 "integer without a cast"),
6134 G_("return makes pointer from integer "
6135 "without a cast"));
6137 return convert (type, rhs);
6139 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6141 WARN_FOR_ASSIGNMENT (location, 0,
6142 G_("passing argument %d of %qE makes integer "
6143 "from pointer without a cast"),
6144 G_("assignment makes integer from pointer "
6145 "without a cast"),
6146 G_("initialization makes integer from pointer "
6147 "without a cast"),
6148 G_("return makes integer from pointer "
6149 "without a cast"));
6150 return convert (type, rhs);
6152 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6154 tree ret;
6155 bool save = in_late_binary_op;
6156 in_late_binary_op = true;
6157 ret = convert (type, rhs);
6158 in_late_binary_op = save;
6159 return ret;
6162 switch (errtype)
6164 case ic_argpass:
6165 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
6166 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6167 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6168 "expected %qT but argument is of type %qT", type, rhstype);
6169 break;
6170 case ic_assign:
6171 error_at (location, "incompatible types when assigning to type %qT from "
6172 "type %qT", type, rhstype);
6173 break;
6174 case ic_init:
6175 error_at (location,
6176 "incompatible types when initializing type %qT using type %qT",
6177 type, rhstype);
6178 break;
6179 case ic_return:
6180 error_at (location,
6181 "incompatible types when returning type %qT but %qT was "
6182 "expected", rhstype, type);
6183 break;
6184 default:
6185 gcc_unreachable ();
6188 return error_mark_node;
6191 /* If VALUE is a compound expr all of whose expressions are constant, then
6192 return its value. Otherwise, return error_mark_node.
6194 This is for handling COMPOUND_EXPRs as initializer elements
6195 which is allowed with a warning when -pedantic is specified. */
6197 static tree
6198 valid_compound_expr_initializer (tree value, tree endtype)
6200 if (TREE_CODE (value) == COMPOUND_EXPR)
6202 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6203 == error_mark_node)
6204 return error_mark_node;
6205 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6206 endtype);
6208 else if (!initializer_constant_valid_p (value, endtype))
6209 return error_mark_node;
6210 else
6211 return value;
6214 /* Perform appropriate conversions on the initial value of a variable,
6215 store it in the declaration DECL,
6216 and print any error messages that are appropriate.
6217 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6218 If the init is invalid, store an ERROR_MARK.
6220 INIT_LOC is the location of the initial value. */
6222 void
6223 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6225 tree value, type;
6226 bool npc = false;
6228 /* If variable's type was invalidly declared, just ignore it. */
6230 type = TREE_TYPE (decl);
6231 if (TREE_CODE (type) == ERROR_MARK)
6232 return;
6234 /* Digest the specified initializer into an expression. */
6236 if (init)
6237 npc = null_pointer_constant_p (init);
6238 value = digest_init (init_loc, type, init, origtype, npc,
6239 true, TREE_STATIC (decl));
6241 /* Store the expression if valid; else report error. */
6243 if (!in_system_header_at (input_location)
6244 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6245 warning (OPT_Wtraditional, "traditional C rejects automatic "
6246 "aggregate initialization");
6248 DECL_INITIAL (decl) = value;
6250 /* ANSI wants warnings about out-of-range constant initializers. */
6251 STRIP_TYPE_NOPS (value);
6252 if (TREE_STATIC (decl))
6253 constant_expression_warning (value);
6255 /* Check if we need to set array size from compound literal size. */
6256 if (TREE_CODE (type) == ARRAY_TYPE
6257 && TYPE_DOMAIN (type) == 0
6258 && value != error_mark_node)
6260 tree inside_init = init;
6262 STRIP_TYPE_NOPS (inside_init);
6263 inside_init = fold (inside_init);
6265 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6267 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6269 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6271 /* For int foo[] = (int [3]){1}; we need to set array size
6272 now since later on array initializer will be just the
6273 brace enclosed list of the compound literal. */
6274 tree etype = strip_array_types (TREE_TYPE (decl));
6275 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6276 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6277 layout_type (type);
6278 layout_decl (cldecl, 0);
6279 TREE_TYPE (decl)
6280 = c_build_qualified_type (type, TYPE_QUALS (etype));
6286 /* Methods for storing and printing names for error messages. */
6288 /* Implement a spelling stack that allows components of a name to be pushed
6289 and popped. Each element on the stack is this structure. */
6291 struct spelling
6293 int kind;
6294 union
6296 unsigned HOST_WIDE_INT i;
6297 const char *s;
6298 } u;
6301 #define SPELLING_STRING 1
6302 #define SPELLING_MEMBER 2
6303 #define SPELLING_BOUNDS 3
6305 static struct spelling *spelling; /* Next stack element (unused). */
6306 static struct spelling *spelling_base; /* Spelling stack base. */
6307 static int spelling_size; /* Size of the spelling stack. */
6309 /* Macros to save and restore the spelling stack around push_... functions.
6310 Alternative to SAVE_SPELLING_STACK. */
6312 #define SPELLING_DEPTH() (spelling - spelling_base)
6313 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6315 /* Push an element on the spelling stack with type KIND and assign VALUE
6316 to MEMBER. */
6318 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6320 int depth = SPELLING_DEPTH (); \
6322 if (depth >= spelling_size) \
6324 spelling_size += 10; \
6325 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6326 spelling_size); \
6327 RESTORE_SPELLING_DEPTH (depth); \
6330 spelling->kind = (KIND); \
6331 spelling->MEMBER = (VALUE); \
6332 spelling++; \
6335 /* Push STRING on the stack. Printed literally. */
6337 static void
6338 push_string (const char *string)
6340 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6343 /* Push a member name on the stack. Printed as '.' STRING. */
6345 static void
6346 push_member_name (tree decl)
6348 const char *const string
6349 = (DECL_NAME (decl)
6350 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6351 : _("<anonymous>"));
6352 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6355 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6357 static void
6358 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6360 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6363 /* Compute the maximum size in bytes of the printed spelling. */
6365 static int
6366 spelling_length (void)
6368 int size = 0;
6369 struct spelling *p;
6371 for (p = spelling_base; p < spelling; p++)
6373 if (p->kind == SPELLING_BOUNDS)
6374 size += 25;
6375 else
6376 size += strlen (p->u.s) + 1;
6379 return size;
6382 /* Print the spelling to BUFFER and return it. */
6384 static char *
6385 print_spelling (char *buffer)
6387 char *d = buffer;
6388 struct spelling *p;
6390 for (p = spelling_base; p < spelling; p++)
6391 if (p->kind == SPELLING_BOUNDS)
6393 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6394 d += strlen (d);
6396 else
6398 const char *s;
6399 if (p->kind == SPELLING_MEMBER)
6400 *d++ = '.';
6401 for (s = p->u.s; (*d = *s++); d++)
6404 *d++ = '\0';
6405 return buffer;
6408 /* Issue an error message for a bad initializer component.
6409 GMSGID identifies the message.
6410 The component name is taken from the spelling stack. */
6412 void
6413 error_init (const char *gmsgid)
6415 char *ofwhat;
6417 /* The gmsgid may be a format string with %< and %>. */
6418 error (gmsgid);
6419 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6420 if (*ofwhat)
6421 error ("(near initialization for %qs)", ofwhat);
6424 /* Issue a pedantic warning for a bad initializer component. OPT is
6425 the option OPT_* (from options.h) controlling this warning or 0 if
6426 it is unconditionally given. GMSGID identifies the message. The
6427 component name is taken from the spelling stack. */
6429 void
6430 pedwarn_init (location_t location, int opt, const char *gmsgid)
6432 char *ofwhat;
6434 /* The gmsgid may be a format string with %< and %>. */
6435 pedwarn (location, opt, gmsgid);
6436 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6437 if (*ofwhat)
6438 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6441 /* Issue a warning for a bad initializer component.
6443 OPT is the OPT_W* value corresponding to the warning option that
6444 controls this warning. GMSGID identifies the message. The
6445 component name is taken from the spelling stack. */
6447 static void
6448 warning_init (int opt, const char *gmsgid)
6450 char *ofwhat;
6452 /* The gmsgid may be a format string with %< and %>. */
6453 warning (opt, gmsgid);
6454 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6455 if (*ofwhat)
6456 warning (opt, "(near initialization for %qs)", ofwhat);
6459 /* If TYPE is an array type and EXPR is a parenthesized string
6460 constant, warn if pedantic that EXPR is being used to initialize an
6461 object of type TYPE. */
6463 void
6464 maybe_warn_string_init (tree type, struct c_expr expr)
6466 if (pedantic
6467 && TREE_CODE (type) == ARRAY_TYPE
6468 && TREE_CODE (expr.value) == STRING_CST
6469 && expr.original_code != STRING_CST)
6470 pedwarn_init (input_location, OPT_Wpedantic,
6471 "array initialized from parenthesized string constant");
6474 /* Digest the parser output INIT as an initializer for type TYPE.
6475 Return a C expression of type TYPE to represent the initial value.
6477 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6479 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6481 If INIT is a string constant, STRICT_STRING is true if it is
6482 unparenthesized or we should not warn here for it being parenthesized.
6483 For other types of INIT, STRICT_STRING is not used.
6485 INIT_LOC is the location of the INIT.
6487 REQUIRE_CONSTANT requests an error if non-constant initializers or
6488 elements are seen. */
6490 static tree
6491 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6492 bool null_pointer_constant, bool strict_string,
6493 int require_constant)
6495 enum tree_code code = TREE_CODE (type);
6496 tree inside_init = init;
6497 tree semantic_type = NULL_TREE;
6498 bool maybe_const = true;
6500 if (type == error_mark_node
6501 || !init
6502 || init == error_mark_node
6503 || TREE_TYPE (init) == error_mark_node)
6504 return error_mark_node;
6506 STRIP_TYPE_NOPS (inside_init);
6508 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6510 semantic_type = TREE_TYPE (inside_init);
6511 inside_init = TREE_OPERAND (inside_init, 0);
6513 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6514 inside_init = decl_constant_value_for_optimization (inside_init);
6516 /* Initialization of an array of chars from a string constant
6517 optionally enclosed in braces. */
6519 if (code == ARRAY_TYPE && inside_init
6520 && TREE_CODE (inside_init) == STRING_CST)
6522 tree typ1
6523 = (TYPE_ATOMIC (TREE_TYPE (type))
6524 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6525 TYPE_QUAL_ATOMIC)
6526 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6527 /* Note that an array could be both an array of character type
6528 and an array of wchar_t if wchar_t is signed char or unsigned
6529 char. */
6530 bool char_array = (typ1 == char_type_node
6531 || typ1 == signed_char_type_node
6532 || typ1 == unsigned_char_type_node);
6533 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6534 bool char16_array = !!comptypes (typ1, char16_type_node);
6535 bool char32_array = !!comptypes (typ1, char32_type_node);
6537 if (char_array || wchar_array || char16_array || char32_array)
6539 struct c_expr expr;
6540 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6541 expr.value = inside_init;
6542 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6543 expr.original_type = NULL;
6544 maybe_warn_string_init (type, expr);
6546 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6547 pedwarn_init (init_loc, OPT_Wpedantic,
6548 "initialization of a flexible array member");
6550 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6551 TYPE_MAIN_VARIANT (type)))
6552 return inside_init;
6554 if (char_array)
6556 if (typ2 != char_type_node)
6558 error_init ("char-array initialized from wide string");
6559 return error_mark_node;
6562 else
6564 if (typ2 == char_type_node)
6566 error_init ("wide character array initialized from non-wide "
6567 "string");
6568 return error_mark_node;
6570 else if (!comptypes(typ1, typ2))
6572 error_init ("wide character array initialized from "
6573 "incompatible wide string");
6574 return error_mark_node;
6578 TREE_TYPE (inside_init) = type;
6579 if (TYPE_DOMAIN (type) != 0
6580 && TYPE_SIZE (type) != 0
6581 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6583 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6585 /* Subtract the size of a single (possibly wide) character
6586 because it's ok to ignore the terminating null char
6587 that is counted in the length of the constant. */
6588 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6589 (len
6590 - (TYPE_PRECISION (typ1)
6591 / BITS_PER_UNIT))))
6592 pedwarn_init (init_loc, 0,
6593 ("initializer-string for array of chars "
6594 "is too long"));
6595 else if (warn_cxx_compat
6596 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6597 warning_at (init_loc, OPT_Wc___compat,
6598 ("initializer-string for array chars "
6599 "is too long for C++"));
6602 return inside_init;
6604 else if (INTEGRAL_TYPE_P (typ1))
6606 error_init ("array of inappropriate type initialized "
6607 "from string constant");
6608 return error_mark_node;
6612 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6613 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6614 below and handle as a constructor. */
6615 if (code == VECTOR_TYPE
6616 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6617 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6618 && TREE_CONSTANT (inside_init))
6620 if (TREE_CODE (inside_init) == VECTOR_CST
6621 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6622 TYPE_MAIN_VARIANT (type)))
6623 return inside_init;
6625 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6627 unsigned HOST_WIDE_INT ix;
6628 tree value;
6629 bool constant_p = true;
6631 /* Iterate through elements and check if all constructor
6632 elements are *_CSTs. */
6633 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6634 if (!CONSTANT_CLASS_P (value))
6636 constant_p = false;
6637 break;
6640 if (constant_p)
6641 return build_vector_from_ctor (type,
6642 CONSTRUCTOR_ELTS (inside_init));
6646 if (warn_sequence_point)
6647 verify_sequence_points (inside_init);
6649 /* Any type can be initialized
6650 from an expression of the same type, optionally with braces. */
6652 if (inside_init && TREE_TYPE (inside_init) != 0
6653 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6654 TYPE_MAIN_VARIANT (type))
6655 || (code == ARRAY_TYPE
6656 && comptypes (TREE_TYPE (inside_init), type))
6657 || (code == VECTOR_TYPE
6658 && comptypes (TREE_TYPE (inside_init), type))
6659 || (code == POINTER_TYPE
6660 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6661 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6662 TREE_TYPE (type)))))
6664 if (code == POINTER_TYPE)
6666 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6668 if (TREE_CODE (inside_init) == STRING_CST
6669 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6670 inside_init = array_to_pointer_conversion
6671 (init_loc, inside_init);
6672 else
6674 error_init ("invalid use of non-lvalue array");
6675 return error_mark_node;
6680 if (code == VECTOR_TYPE)
6681 /* Although the types are compatible, we may require a
6682 conversion. */
6683 inside_init = convert (type, inside_init);
6685 if (require_constant
6686 && (code == VECTOR_TYPE || !flag_isoc99)
6687 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6689 /* As an extension, allow initializing objects with static storage
6690 duration with compound literals (which are then treated just as
6691 the brace enclosed list they contain). Also allow this for
6692 vectors, as we can only assign them with compound literals. */
6693 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6694 inside_init = DECL_INITIAL (decl);
6697 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6698 && TREE_CODE (inside_init) != CONSTRUCTOR)
6700 error_init ("array initialized from non-constant array expression");
6701 return error_mark_node;
6704 /* Compound expressions can only occur here if -Wpedantic or
6705 -pedantic-errors is specified. In the later case, we always want
6706 an error. In the former case, we simply want a warning. */
6707 if (require_constant && pedantic
6708 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6710 inside_init
6711 = valid_compound_expr_initializer (inside_init,
6712 TREE_TYPE (inside_init));
6713 if (inside_init == error_mark_node)
6714 error_init ("initializer element is not constant");
6715 else
6716 pedwarn_init (init_loc, OPT_Wpedantic,
6717 "initializer element is not constant");
6718 if (flag_pedantic_errors)
6719 inside_init = error_mark_node;
6721 else if (require_constant
6722 && !initializer_constant_valid_p (inside_init,
6723 TREE_TYPE (inside_init)))
6725 error_init ("initializer element is not constant");
6726 inside_init = error_mark_node;
6728 else if (require_constant && !maybe_const)
6729 pedwarn_init (init_loc, 0,
6730 "initializer element is not a constant expression");
6732 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6733 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6734 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6735 type, inside_init, origtype,
6736 ic_init, null_pointer_constant,
6737 NULL_TREE, NULL_TREE, 0);
6738 return inside_init;
6741 /* Handle scalar types, including conversions. */
6743 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6744 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6745 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6747 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6748 && (TREE_CODE (init) == STRING_CST
6749 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6750 inside_init = init = array_to_pointer_conversion (init_loc, init);
6751 if (semantic_type)
6752 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6753 inside_init);
6754 inside_init
6755 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6756 inside_init, origtype, ic_init,
6757 null_pointer_constant, NULL_TREE, NULL_TREE,
6760 /* Check to see if we have already given an error message. */
6761 if (inside_init == error_mark_node)
6763 else if (require_constant && !TREE_CONSTANT (inside_init))
6765 error_init ("initializer element is not constant");
6766 inside_init = error_mark_node;
6768 else if (require_constant
6769 && !initializer_constant_valid_p (inside_init,
6770 TREE_TYPE (inside_init)))
6772 error_init ("initializer element is not computable at load time");
6773 inside_init = error_mark_node;
6775 else if (require_constant && !maybe_const)
6776 pedwarn_init (init_loc, 0,
6777 "initializer element is not a constant expression");
6779 return inside_init;
6782 /* Come here only for records and arrays. */
6784 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6786 error_init ("variable-sized object may not be initialized");
6787 return error_mark_node;
6790 error_init ("invalid initializer");
6791 return error_mark_node;
6794 /* Handle initializers that use braces. */
6796 /* Type of object we are accumulating a constructor for.
6797 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6798 static tree constructor_type;
6800 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6801 left to fill. */
6802 static tree constructor_fields;
6804 /* For an ARRAY_TYPE, this is the specified index
6805 at which to store the next element we get. */
6806 static tree constructor_index;
6808 /* For an ARRAY_TYPE, this is the maximum index. */
6809 static tree constructor_max_index;
6811 /* For a RECORD_TYPE, this is the first field not yet written out. */
6812 static tree constructor_unfilled_fields;
6814 /* For an ARRAY_TYPE, this is the index of the first element
6815 not yet written out. */
6816 static tree constructor_unfilled_index;
6818 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6819 This is so we can generate gaps between fields, when appropriate. */
6820 static tree constructor_bit_index;
6822 /* If we are saving up the elements rather than allocating them,
6823 this is the list of elements so far (in reverse order,
6824 most recent first). */
6825 static vec<constructor_elt, va_gc> *constructor_elements;
6827 /* 1 if constructor should be incrementally stored into a constructor chain,
6828 0 if all the elements should be kept in AVL tree. */
6829 static int constructor_incremental;
6831 /* 1 if so far this constructor's elements are all compile-time constants. */
6832 static int constructor_constant;
6834 /* 1 if so far this constructor's elements are all valid address constants. */
6835 static int constructor_simple;
6837 /* 1 if this constructor has an element that cannot be part of a
6838 constant expression. */
6839 static int constructor_nonconst;
6841 /* 1 if this constructor is erroneous so far. */
6842 static int constructor_erroneous;
6844 /* Structure for managing pending initializer elements, organized as an
6845 AVL tree. */
6847 struct init_node
6849 struct init_node *left, *right;
6850 struct init_node *parent;
6851 int balance;
6852 tree purpose;
6853 tree value;
6854 tree origtype;
6857 /* Tree of pending elements at this constructor level.
6858 These are elements encountered out of order
6859 which belong at places we haven't reached yet in actually
6860 writing the output.
6861 Will never hold tree nodes across GC runs. */
6862 static struct init_node *constructor_pending_elts;
6864 /* The SPELLING_DEPTH of this constructor. */
6865 static int constructor_depth;
6867 /* DECL node for which an initializer is being read.
6868 0 means we are reading a constructor expression
6869 such as (struct foo) {...}. */
6870 static tree constructor_decl;
6872 /* Nonzero if this is an initializer for a top-level decl. */
6873 static int constructor_top_level;
6875 /* Nonzero if there were any member designators in this initializer. */
6876 static int constructor_designated;
6878 /* Nesting depth of designator list. */
6879 static int designator_depth;
6881 /* Nonzero if there were diagnosed errors in this designator list. */
6882 static int designator_erroneous;
6885 /* This stack has a level for each implicit or explicit level of
6886 structuring in the initializer, including the outermost one. It
6887 saves the values of most of the variables above. */
6889 struct constructor_range_stack;
6891 struct constructor_stack
6893 struct constructor_stack *next;
6894 tree type;
6895 tree fields;
6896 tree index;
6897 tree max_index;
6898 tree unfilled_index;
6899 tree unfilled_fields;
6900 tree bit_index;
6901 vec<constructor_elt, va_gc> *elements;
6902 struct init_node *pending_elts;
6903 int offset;
6904 int depth;
6905 /* If value nonzero, this value should replace the entire
6906 constructor at this level. */
6907 struct c_expr replacement_value;
6908 struct constructor_range_stack *range_stack;
6909 char constant;
6910 char simple;
6911 char nonconst;
6912 char implicit;
6913 char erroneous;
6914 char outer;
6915 char incremental;
6916 char designated;
6919 static struct constructor_stack *constructor_stack;
6921 /* This stack represents designators from some range designator up to
6922 the last designator in the list. */
6924 struct constructor_range_stack
6926 struct constructor_range_stack *next, *prev;
6927 struct constructor_stack *stack;
6928 tree range_start;
6929 tree index;
6930 tree range_end;
6931 tree fields;
6934 static struct constructor_range_stack *constructor_range_stack;
6936 /* This stack records separate initializers that are nested.
6937 Nested initializers can't happen in ANSI C, but GNU C allows them
6938 in cases like { ... (struct foo) { ... } ... }. */
6940 struct initializer_stack
6942 struct initializer_stack *next;
6943 tree decl;
6944 struct constructor_stack *constructor_stack;
6945 struct constructor_range_stack *constructor_range_stack;
6946 vec<constructor_elt, va_gc> *elements;
6947 struct spelling *spelling;
6948 struct spelling *spelling_base;
6949 int spelling_size;
6950 char top_level;
6951 char require_constant_value;
6952 char require_constant_elements;
6955 static struct initializer_stack *initializer_stack;
6957 /* Prepare to parse and output the initializer for variable DECL. */
6959 void
6960 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6962 const char *locus;
6963 struct initializer_stack *p = XNEW (struct initializer_stack);
6965 p->decl = constructor_decl;
6966 p->require_constant_value = require_constant_value;
6967 p->require_constant_elements = require_constant_elements;
6968 p->constructor_stack = constructor_stack;
6969 p->constructor_range_stack = constructor_range_stack;
6970 p->elements = constructor_elements;
6971 p->spelling = spelling;
6972 p->spelling_base = spelling_base;
6973 p->spelling_size = spelling_size;
6974 p->top_level = constructor_top_level;
6975 p->next = initializer_stack;
6976 initializer_stack = p;
6978 constructor_decl = decl;
6979 constructor_designated = 0;
6980 constructor_top_level = top_level;
6982 if (decl != 0 && decl != error_mark_node)
6984 require_constant_value = TREE_STATIC (decl);
6985 require_constant_elements
6986 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6987 /* For a scalar, you can always use any value to initialize,
6988 even within braces. */
6989 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6990 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6991 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6992 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6993 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6995 else
6997 require_constant_value = 0;
6998 require_constant_elements = 0;
6999 locus = _("(anonymous)");
7002 constructor_stack = 0;
7003 constructor_range_stack = 0;
7005 missing_braces_mentioned = 0;
7007 spelling_base = 0;
7008 spelling_size = 0;
7009 RESTORE_SPELLING_DEPTH (0);
7011 if (locus)
7012 push_string (locus);
7015 void
7016 finish_init (void)
7018 struct initializer_stack *p = initializer_stack;
7020 /* Free the whole constructor stack of this initializer. */
7021 while (constructor_stack)
7023 struct constructor_stack *q = constructor_stack;
7024 constructor_stack = q->next;
7025 free (q);
7028 gcc_assert (!constructor_range_stack);
7030 /* Pop back to the data of the outer initializer (if any). */
7031 free (spelling_base);
7033 constructor_decl = p->decl;
7034 require_constant_value = p->require_constant_value;
7035 require_constant_elements = p->require_constant_elements;
7036 constructor_stack = p->constructor_stack;
7037 constructor_range_stack = p->constructor_range_stack;
7038 constructor_elements = p->elements;
7039 spelling = p->spelling;
7040 spelling_base = p->spelling_base;
7041 spelling_size = p->spelling_size;
7042 constructor_top_level = p->top_level;
7043 initializer_stack = p->next;
7044 free (p);
7047 /* Call here when we see the initializer is surrounded by braces.
7048 This is instead of a call to push_init_level;
7049 it is matched by a call to pop_init_level.
7051 TYPE is the type to initialize, for a constructor expression.
7052 For an initializer for a decl, TYPE is zero. */
7054 void
7055 really_start_incremental_init (tree type)
7057 struct constructor_stack *p = XNEW (struct constructor_stack);
7059 if (type == 0)
7060 type = TREE_TYPE (constructor_decl);
7062 if (TREE_CODE (type) == VECTOR_TYPE
7063 && TYPE_VECTOR_OPAQUE (type))
7064 error ("opaque vector types cannot be initialized");
7066 p->type = constructor_type;
7067 p->fields = constructor_fields;
7068 p->index = constructor_index;
7069 p->max_index = constructor_max_index;
7070 p->unfilled_index = constructor_unfilled_index;
7071 p->unfilled_fields = constructor_unfilled_fields;
7072 p->bit_index = constructor_bit_index;
7073 p->elements = constructor_elements;
7074 p->constant = constructor_constant;
7075 p->simple = constructor_simple;
7076 p->nonconst = constructor_nonconst;
7077 p->erroneous = constructor_erroneous;
7078 p->pending_elts = constructor_pending_elts;
7079 p->depth = constructor_depth;
7080 p->replacement_value.value = 0;
7081 p->replacement_value.original_code = ERROR_MARK;
7082 p->replacement_value.original_type = NULL;
7083 p->implicit = 0;
7084 p->range_stack = 0;
7085 p->outer = 0;
7086 p->incremental = constructor_incremental;
7087 p->designated = constructor_designated;
7088 p->next = 0;
7089 constructor_stack = p;
7091 constructor_constant = 1;
7092 constructor_simple = 1;
7093 constructor_nonconst = 0;
7094 constructor_depth = SPELLING_DEPTH ();
7095 constructor_elements = NULL;
7096 constructor_pending_elts = 0;
7097 constructor_type = type;
7098 constructor_incremental = 1;
7099 constructor_designated = 0;
7100 designator_depth = 0;
7101 designator_erroneous = 0;
7103 if (TREE_CODE (constructor_type) == RECORD_TYPE
7104 || TREE_CODE (constructor_type) == UNION_TYPE)
7106 constructor_fields = TYPE_FIELDS (constructor_type);
7107 /* Skip any nameless bit fields at the beginning. */
7108 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7109 && DECL_NAME (constructor_fields) == 0)
7110 constructor_fields = DECL_CHAIN (constructor_fields);
7112 constructor_unfilled_fields = constructor_fields;
7113 constructor_bit_index = bitsize_zero_node;
7115 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7117 if (TYPE_DOMAIN (constructor_type))
7119 constructor_max_index
7120 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7122 /* Detect non-empty initializations of zero-length arrays. */
7123 if (constructor_max_index == NULL_TREE
7124 && TYPE_SIZE (constructor_type))
7125 constructor_max_index = integer_minus_one_node;
7127 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7128 to initialize VLAs will cause a proper error; avoid tree
7129 checking errors as well by setting a safe value. */
7130 if (constructor_max_index
7131 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7132 constructor_max_index = integer_minus_one_node;
7134 constructor_index
7135 = convert (bitsizetype,
7136 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7138 else
7140 constructor_index = bitsize_zero_node;
7141 constructor_max_index = NULL_TREE;
7144 constructor_unfilled_index = constructor_index;
7146 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7148 /* Vectors are like simple fixed-size arrays. */
7149 constructor_max_index =
7150 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7151 constructor_index = bitsize_zero_node;
7152 constructor_unfilled_index = constructor_index;
7154 else
7156 /* Handle the case of int x = {5}; */
7157 constructor_fields = constructor_type;
7158 constructor_unfilled_fields = constructor_type;
7162 /* Push down into a subobject, for initialization.
7163 If this is for an explicit set of braces, IMPLICIT is 0.
7164 If it is because the next element belongs at a lower level,
7165 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7167 void
7168 push_init_level (int implicit, struct obstack * braced_init_obstack)
7170 struct constructor_stack *p;
7171 tree value = NULL_TREE;
7173 /* If we've exhausted any levels that didn't have braces,
7174 pop them now. If implicit == 1, this will have been done in
7175 process_init_element; do not repeat it here because in the case
7176 of excess initializers for an empty aggregate this leads to an
7177 infinite cycle of popping a level and immediately recreating
7178 it. */
7179 if (implicit != 1)
7181 while (constructor_stack->implicit)
7183 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7184 || TREE_CODE (constructor_type) == UNION_TYPE)
7185 && constructor_fields == 0)
7186 process_init_element (pop_init_level (1, braced_init_obstack),
7187 true, braced_init_obstack);
7188 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7189 && constructor_max_index
7190 && tree_int_cst_lt (constructor_max_index,
7191 constructor_index))
7192 process_init_element (pop_init_level (1, braced_init_obstack),
7193 true, braced_init_obstack);
7194 else
7195 break;
7199 /* Unless this is an explicit brace, we need to preserve previous
7200 content if any. */
7201 if (implicit)
7203 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7204 || TREE_CODE (constructor_type) == UNION_TYPE)
7205 && constructor_fields)
7206 value = find_init_member (constructor_fields, braced_init_obstack);
7207 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7208 value = find_init_member (constructor_index, braced_init_obstack);
7211 p = XNEW (struct constructor_stack);
7212 p->type = constructor_type;
7213 p->fields = constructor_fields;
7214 p->index = constructor_index;
7215 p->max_index = constructor_max_index;
7216 p->unfilled_index = constructor_unfilled_index;
7217 p->unfilled_fields = constructor_unfilled_fields;
7218 p->bit_index = constructor_bit_index;
7219 p->elements = constructor_elements;
7220 p->constant = constructor_constant;
7221 p->simple = constructor_simple;
7222 p->nonconst = constructor_nonconst;
7223 p->erroneous = constructor_erroneous;
7224 p->pending_elts = constructor_pending_elts;
7225 p->depth = constructor_depth;
7226 p->replacement_value.value = 0;
7227 p->replacement_value.original_code = ERROR_MARK;
7228 p->replacement_value.original_type = NULL;
7229 p->implicit = implicit;
7230 p->outer = 0;
7231 p->incremental = constructor_incremental;
7232 p->designated = constructor_designated;
7233 p->next = constructor_stack;
7234 p->range_stack = 0;
7235 constructor_stack = p;
7237 constructor_constant = 1;
7238 constructor_simple = 1;
7239 constructor_nonconst = 0;
7240 constructor_depth = SPELLING_DEPTH ();
7241 constructor_elements = NULL;
7242 constructor_incremental = 1;
7243 constructor_designated = 0;
7244 constructor_pending_elts = 0;
7245 if (!implicit)
7247 p->range_stack = constructor_range_stack;
7248 constructor_range_stack = 0;
7249 designator_depth = 0;
7250 designator_erroneous = 0;
7253 /* Don't die if an entire brace-pair level is superfluous
7254 in the containing level. */
7255 if (constructor_type == 0)
7257 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7258 || TREE_CODE (constructor_type) == UNION_TYPE)
7260 /* Don't die if there are extra init elts at the end. */
7261 if (constructor_fields == 0)
7262 constructor_type = 0;
7263 else
7265 constructor_type = TREE_TYPE (constructor_fields);
7266 push_member_name (constructor_fields);
7267 constructor_depth++;
7270 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7272 constructor_type = TREE_TYPE (constructor_type);
7273 push_array_bounds (tree_to_uhwi (constructor_index));
7274 constructor_depth++;
7277 if (constructor_type == 0)
7279 error_init ("extra brace group at end of initializer");
7280 constructor_fields = 0;
7281 constructor_unfilled_fields = 0;
7282 return;
7285 if (value && TREE_CODE (value) == CONSTRUCTOR)
7287 constructor_constant = TREE_CONSTANT (value);
7288 constructor_simple = TREE_STATIC (value);
7289 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7290 constructor_elements = CONSTRUCTOR_ELTS (value);
7291 if (!vec_safe_is_empty (constructor_elements)
7292 && (TREE_CODE (constructor_type) == RECORD_TYPE
7293 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7294 set_nonincremental_init (braced_init_obstack);
7297 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7299 missing_braces_mentioned = 1;
7300 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
7303 if (TREE_CODE (constructor_type) == RECORD_TYPE
7304 || TREE_CODE (constructor_type) == UNION_TYPE)
7306 constructor_fields = TYPE_FIELDS (constructor_type);
7307 /* Skip any nameless bit fields at the beginning. */
7308 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7309 && DECL_NAME (constructor_fields) == 0)
7310 constructor_fields = DECL_CHAIN (constructor_fields);
7312 constructor_unfilled_fields = constructor_fields;
7313 constructor_bit_index = bitsize_zero_node;
7315 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7317 /* Vectors are like simple fixed-size arrays. */
7318 constructor_max_index =
7319 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7320 constructor_index = bitsize_int (0);
7321 constructor_unfilled_index = constructor_index;
7323 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7325 if (TYPE_DOMAIN (constructor_type))
7327 constructor_max_index
7328 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7330 /* Detect non-empty initializations of zero-length arrays. */
7331 if (constructor_max_index == NULL_TREE
7332 && TYPE_SIZE (constructor_type))
7333 constructor_max_index = integer_minus_one_node;
7335 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7336 to initialize VLAs will cause a proper error; avoid tree
7337 checking errors as well by setting a safe value. */
7338 if (constructor_max_index
7339 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7340 constructor_max_index = integer_minus_one_node;
7342 constructor_index
7343 = convert (bitsizetype,
7344 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7346 else
7347 constructor_index = bitsize_zero_node;
7349 constructor_unfilled_index = constructor_index;
7350 if (value && TREE_CODE (value) == STRING_CST)
7352 /* We need to split the char/wchar array into individual
7353 characters, so that we don't have to special case it
7354 everywhere. */
7355 set_nonincremental_init_from_string (value, braced_init_obstack);
7358 else
7360 if (constructor_type != error_mark_node)
7361 warning_init (0, "braces around scalar initializer");
7362 constructor_fields = constructor_type;
7363 constructor_unfilled_fields = constructor_type;
7367 /* At the end of an implicit or explicit brace level,
7368 finish up that level of constructor. If a single expression
7369 with redundant braces initialized that level, return the
7370 c_expr structure for that expression. Otherwise, the original_code
7371 element is set to ERROR_MARK.
7372 If we were outputting the elements as they are read, return 0 as the value
7373 from inner levels (process_init_element ignores that),
7374 but return error_mark_node as the value from the outermost level
7375 (that's what we want to put in DECL_INITIAL).
7376 Otherwise, return a CONSTRUCTOR expression as the value. */
7378 struct c_expr
7379 pop_init_level (int implicit, struct obstack * braced_init_obstack)
7381 struct constructor_stack *p;
7382 struct c_expr ret;
7383 ret.value = 0;
7384 ret.original_code = ERROR_MARK;
7385 ret.original_type = NULL;
7387 if (implicit == 0)
7389 /* When we come to an explicit close brace,
7390 pop any inner levels that didn't have explicit braces. */
7391 while (constructor_stack->implicit)
7393 process_init_element (pop_init_level (1, braced_init_obstack),
7394 true, braced_init_obstack);
7396 gcc_assert (!constructor_range_stack);
7399 /* Now output all pending elements. */
7400 constructor_incremental = 1;
7401 output_pending_init_elements (1, braced_init_obstack);
7403 p = constructor_stack;
7405 /* Error for initializing a flexible array member, or a zero-length
7406 array member in an inappropriate context. */
7407 if (constructor_type && constructor_fields
7408 && TREE_CODE (constructor_type) == ARRAY_TYPE
7409 && TYPE_DOMAIN (constructor_type)
7410 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7412 /* Silently discard empty initializations. The parser will
7413 already have pedwarned for empty brackets. */
7414 if (integer_zerop (constructor_unfilled_index))
7415 constructor_type = NULL_TREE;
7416 else
7418 gcc_assert (!TYPE_SIZE (constructor_type));
7420 if (constructor_depth > 2)
7421 error_init ("initialization of flexible array member in a nested context");
7422 else
7423 pedwarn_init (input_location, OPT_Wpedantic,
7424 "initialization of a flexible array member");
7426 /* We have already issued an error message for the existence
7427 of a flexible array member not at the end of the structure.
7428 Discard the initializer so that we do not die later. */
7429 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7430 constructor_type = NULL_TREE;
7434 /* Warn when some struct elements are implicitly initialized to zero. */
7435 if (warn_missing_field_initializers
7436 && constructor_type
7437 && TREE_CODE (constructor_type) == RECORD_TYPE
7438 && constructor_unfilled_fields)
7440 bool constructor_zeroinit =
7441 (vec_safe_length (constructor_elements) == 1
7442 && integer_zerop ((*constructor_elements)[0].value));
7444 /* Do not warn for flexible array members or zero-length arrays. */
7445 while (constructor_unfilled_fields
7446 && (!DECL_SIZE (constructor_unfilled_fields)
7447 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7448 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7450 if (constructor_unfilled_fields
7451 /* Do not warn if this level of the initializer uses member
7452 designators; it is likely to be deliberate. */
7453 && !constructor_designated
7454 /* Do not warn about initializing with ` = {0}'. */
7455 && !constructor_zeroinit)
7457 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7458 "missing initializer for field %qD of %qT",
7459 constructor_unfilled_fields,
7460 constructor_type))
7461 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7462 "%qD declared here", constructor_unfilled_fields);
7466 /* Pad out the end of the structure. */
7467 if (p->replacement_value.value)
7468 /* If this closes a superfluous brace pair,
7469 just pass out the element between them. */
7470 ret = p->replacement_value;
7471 else if (constructor_type == 0)
7473 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7474 && TREE_CODE (constructor_type) != UNION_TYPE
7475 && TREE_CODE (constructor_type) != ARRAY_TYPE
7476 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7478 /* A nonincremental scalar initializer--just return
7479 the element, after verifying there is just one. */
7480 if (vec_safe_is_empty (constructor_elements))
7482 if (!constructor_erroneous)
7483 error_init ("empty scalar initializer");
7484 ret.value = error_mark_node;
7486 else if (vec_safe_length (constructor_elements) != 1)
7488 error_init ("extra elements in scalar initializer");
7489 ret.value = (*constructor_elements)[0].value;
7491 else
7492 ret.value = (*constructor_elements)[0].value;
7494 else
7496 if (constructor_erroneous)
7497 ret.value = error_mark_node;
7498 else
7500 ret.value = build_constructor (constructor_type,
7501 constructor_elements);
7502 if (constructor_constant)
7503 TREE_CONSTANT (ret.value) = 1;
7504 if (constructor_constant && constructor_simple)
7505 TREE_STATIC (ret.value) = 1;
7506 if (constructor_nonconst)
7507 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7511 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7513 if (constructor_nonconst)
7514 ret.original_code = C_MAYBE_CONST_EXPR;
7515 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7516 ret.original_code = ERROR_MARK;
7519 constructor_type = p->type;
7520 constructor_fields = p->fields;
7521 constructor_index = p->index;
7522 constructor_max_index = p->max_index;
7523 constructor_unfilled_index = p->unfilled_index;
7524 constructor_unfilled_fields = p->unfilled_fields;
7525 constructor_bit_index = p->bit_index;
7526 constructor_elements = p->elements;
7527 constructor_constant = p->constant;
7528 constructor_simple = p->simple;
7529 constructor_nonconst = p->nonconst;
7530 constructor_erroneous = p->erroneous;
7531 constructor_incremental = p->incremental;
7532 constructor_designated = p->designated;
7533 constructor_pending_elts = p->pending_elts;
7534 constructor_depth = p->depth;
7535 if (!p->implicit)
7536 constructor_range_stack = p->range_stack;
7537 RESTORE_SPELLING_DEPTH (constructor_depth);
7539 constructor_stack = p->next;
7540 free (p);
7542 if (ret.value == 0 && constructor_stack == 0)
7543 ret.value = error_mark_node;
7544 return ret;
7547 /* Common handling for both array range and field name designators.
7548 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7550 static int
7551 set_designator (int array, struct obstack * braced_init_obstack)
7553 tree subtype;
7554 enum tree_code subcode;
7556 /* Don't die if an entire brace-pair level is superfluous
7557 in the containing level. */
7558 if (constructor_type == 0)
7559 return 1;
7561 /* If there were errors in this designator list already, bail out
7562 silently. */
7563 if (designator_erroneous)
7564 return 1;
7566 if (!designator_depth)
7568 gcc_assert (!constructor_range_stack);
7570 /* Designator list starts at the level of closest explicit
7571 braces. */
7572 while (constructor_stack->implicit)
7574 process_init_element (pop_init_level (1, braced_init_obstack),
7575 true, braced_init_obstack);
7577 constructor_designated = 1;
7578 return 0;
7581 switch (TREE_CODE (constructor_type))
7583 case RECORD_TYPE:
7584 case UNION_TYPE:
7585 subtype = TREE_TYPE (constructor_fields);
7586 if (subtype != error_mark_node)
7587 subtype = TYPE_MAIN_VARIANT (subtype);
7588 break;
7589 case ARRAY_TYPE:
7590 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7591 break;
7592 default:
7593 gcc_unreachable ();
7596 subcode = TREE_CODE (subtype);
7597 if (array && subcode != ARRAY_TYPE)
7599 error_init ("array index in non-array initializer");
7600 return 1;
7602 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7604 error_init ("field name not in record or union initializer");
7605 return 1;
7608 constructor_designated = 1;
7609 push_init_level (2, braced_init_obstack);
7610 return 0;
7613 /* If there are range designators in designator list, push a new designator
7614 to constructor_range_stack. RANGE_END is end of such stack range or
7615 NULL_TREE if there is no range designator at this level. */
7617 static void
7618 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7620 struct constructor_range_stack *p;
7622 p = (struct constructor_range_stack *)
7623 obstack_alloc (braced_init_obstack,
7624 sizeof (struct constructor_range_stack));
7625 p->prev = constructor_range_stack;
7626 p->next = 0;
7627 p->fields = constructor_fields;
7628 p->range_start = constructor_index;
7629 p->index = constructor_index;
7630 p->stack = constructor_stack;
7631 p->range_end = range_end;
7632 if (constructor_range_stack)
7633 constructor_range_stack->next = p;
7634 constructor_range_stack = p;
7637 /* Within an array initializer, specify the next index to be initialized.
7638 FIRST is that index. If LAST is nonzero, then initialize a range
7639 of indices, running from FIRST through LAST. */
7641 void
7642 set_init_index (tree first, tree last,
7643 struct obstack * braced_init_obstack)
7645 if (set_designator (1, braced_init_obstack))
7646 return;
7648 designator_erroneous = 1;
7650 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7651 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7653 error_init ("array index in initializer not of integer type");
7654 return;
7657 if (TREE_CODE (first) != INTEGER_CST)
7659 first = c_fully_fold (first, false, NULL);
7660 if (TREE_CODE (first) == INTEGER_CST)
7661 pedwarn_init (input_location, OPT_Wpedantic,
7662 "array index in initializer is not "
7663 "an integer constant expression");
7666 if (last && TREE_CODE (last) != INTEGER_CST)
7668 last = c_fully_fold (last, false, NULL);
7669 if (TREE_CODE (last) == INTEGER_CST)
7670 pedwarn_init (input_location, OPT_Wpedantic,
7671 "array index in initializer is not "
7672 "an integer constant expression");
7675 if (TREE_CODE (first) != INTEGER_CST)
7676 error_init ("nonconstant array index in initializer");
7677 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7678 error_init ("nonconstant array index in initializer");
7679 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7680 error_init ("array index in non-array initializer");
7681 else if (tree_int_cst_sgn (first) == -1)
7682 error_init ("array index in initializer exceeds array bounds");
7683 else if (constructor_max_index
7684 && tree_int_cst_lt (constructor_max_index, first))
7685 error_init ("array index in initializer exceeds array bounds");
7686 else
7688 constant_expression_warning (first);
7689 if (last)
7690 constant_expression_warning (last);
7691 constructor_index = convert (bitsizetype, first);
7692 if (tree_int_cst_lt (constructor_index, first))
7694 constructor_index = copy_node (constructor_index);
7695 TREE_OVERFLOW (constructor_index) = 1;
7698 if (last)
7700 if (tree_int_cst_equal (first, last))
7701 last = 0;
7702 else if (tree_int_cst_lt (last, first))
7704 error_init ("empty index range in initializer");
7705 last = 0;
7707 else
7709 last = convert (bitsizetype, last);
7710 if (constructor_max_index != 0
7711 && tree_int_cst_lt (constructor_max_index, last))
7713 error_init ("array index range in initializer exceeds array bounds");
7714 last = 0;
7719 designator_depth++;
7720 designator_erroneous = 0;
7721 if (constructor_range_stack || last)
7722 push_range_stack (last, braced_init_obstack);
7726 /* Within a struct initializer, specify the next field to be initialized. */
7728 void
7729 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7731 tree field;
7733 if (set_designator (0, braced_init_obstack))
7734 return;
7736 designator_erroneous = 1;
7738 if (TREE_CODE (constructor_type) != RECORD_TYPE
7739 && TREE_CODE (constructor_type) != UNION_TYPE)
7741 error_init ("field name not in record or union initializer");
7742 return;
7745 field = lookup_field (constructor_type, fieldname);
7747 if (field == 0)
7748 error ("unknown field %qE specified in initializer", fieldname);
7749 else
7752 constructor_fields = TREE_VALUE (field);
7753 designator_depth++;
7754 designator_erroneous = 0;
7755 if (constructor_range_stack)
7756 push_range_stack (NULL_TREE, braced_init_obstack);
7757 field = TREE_CHAIN (field);
7758 if (field)
7760 if (set_designator (0, braced_init_obstack))
7761 return;
7764 while (field != NULL_TREE);
7767 /* Add a new initializer to the tree of pending initializers. PURPOSE
7768 identifies the initializer, either array index or field in a structure.
7769 VALUE is the value of that index or field. If ORIGTYPE is not
7770 NULL_TREE, it is the original type of VALUE.
7772 IMPLICIT is true if value comes from pop_init_level (1),
7773 the new initializer has been merged with the existing one
7774 and thus no warnings should be emitted about overriding an
7775 existing initializer. */
7777 static void
7778 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7779 struct obstack * braced_init_obstack)
7781 struct init_node *p, **q, *r;
7783 q = &constructor_pending_elts;
7784 p = 0;
7786 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7788 while (*q != 0)
7790 p = *q;
7791 if (tree_int_cst_lt (purpose, p->purpose))
7792 q = &p->left;
7793 else if (tree_int_cst_lt (p->purpose, purpose))
7794 q = &p->right;
7795 else
7797 if (!implicit)
7799 if (TREE_SIDE_EFFECTS (p->value))
7800 warning_init (0, "initialized field with side-effects overwritten");
7801 else if (warn_override_init)
7802 warning_init (OPT_Woverride_init, "initialized field overwritten");
7804 p->value = value;
7805 p->origtype = origtype;
7806 return;
7810 else
7812 tree bitpos;
7814 bitpos = bit_position (purpose);
7815 while (*q != NULL)
7817 p = *q;
7818 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7819 q = &p->left;
7820 else if (p->purpose != purpose)
7821 q = &p->right;
7822 else
7824 if (!implicit)
7826 if (TREE_SIDE_EFFECTS (p->value))
7827 warning_init (0, "initialized field with side-effects overwritten");
7828 else if (warn_override_init)
7829 warning_init (OPT_Woverride_init, "initialized field overwritten");
7831 p->value = value;
7832 p->origtype = origtype;
7833 return;
7838 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7839 sizeof (struct init_node));
7840 r->purpose = purpose;
7841 r->value = value;
7842 r->origtype = origtype;
7844 *q = r;
7845 r->parent = p;
7846 r->left = 0;
7847 r->right = 0;
7848 r->balance = 0;
7850 while (p)
7852 struct init_node *s;
7854 if (r == p->left)
7856 if (p->balance == 0)
7857 p->balance = -1;
7858 else if (p->balance < 0)
7860 if (r->balance < 0)
7862 /* L rotation. */
7863 p->left = r->right;
7864 if (p->left)
7865 p->left->parent = p;
7866 r->right = p;
7868 p->balance = 0;
7869 r->balance = 0;
7871 s = p->parent;
7872 p->parent = r;
7873 r->parent = s;
7874 if (s)
7876 if (s->left == p)
7877 s->left = r;
7878 else
7879 s->right = r;
7881 else
7882 constructor_pending_elts = r;
7884 else
7886 /* LR rotation. */
7887 struct init_node *t = r->right;
7889 r->right = t->left;
7890 if (r->right)
7891 r->right->parent = r;
7892 t->left = r;
7894 p->left = t->right;
7895 if (p->left)
7896 p->left->parent = p;
7897 t->right = p;
7899 p->balance = t->balance < 0;
7900 r->balance = -(t->balance > 0);
7901 t->balance = 0;
7903 s = p->parent;
7904 p->parent = t;
7905 r->parent = t;
7906 t->parent = s;
7907 if (s)
7909 if (s->left == p)
7910 s->left = t;
7911 else
7912 s->right = t;
7914 else
7915 constructor_pending_elts = t;
7917 break;
7919 else
7921 /* p->balance == +1; growth of left side balances the node. */
7922 p->balance = 0;
7923 break;
7926 else /* r == p->right */
7928 if (p->balance == 0)
7929 /* Growth propagation from right side. */
7930 p->balance++;
7931 else if (p->balance > 0)
7933 if (r->balance > 0)
7935 /* R rotation. */
7936 p->right = r->left;
7937 if (p->right)
7938 p->right->parent = p;
7939 r->left = p;
7941 p->balance = 0;
7942 r->balance = 0;
7944 s = p->parent;
7945 p->parent = r;
7946 r->parent = s;
7947 if (s)
7949 if (s->left == p)
7950 s->left = r;
7951 else
7952 s->right = r;
7954 else
7955 constructor_pending_elts = r;
7957 else /* r->balance == -1 */
7959 /* RL rotation */
7960 struct init_node *t = r->left;
7962 r->left = t->right;
7963 if (r->left)
7964 r->left->parent = r;
7965 t->right = r;
7967 p->right = t->left;
7968 if (p->right)
7969 p->right->parent = p;
7970 t->left = p;
7972 r->balance = (t->balance < 0);
7973 p->balance = -(t->balance > 0);
7974 t->balance = 0;
7976 s = p->parent;
7977 p->parent = t;
7978 r->parent = t;
7979 t->parent = s;
7980 if (s)
7982 if (s->left == p)
7983 s->left = t;
7984 else
7985 s->right = t;
7987 else
7988 constructor_pending_elts = t;
7990 break;
7992 else
7994 /* p->balance == -1; growth of right side balances the node. */
7995 p->balance = 0;
7996 break;
8000 r = p;
8001 p = p->parent;
8005 /* Build AVL tree from a sorted chain. */
8007 static void
8008 set_nonincremental_init (struct obstack * braced_init_obstack)
8010 unsigned HOST_WIDE_INT ix;
8011 tree index, value;
8013 if (TREE_CODE (constructor_type) != RECORD_TYPE
8014 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8015 return;
8017 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8019 add_pending_init (index, value, NULL_TREE, true,
8020 braced_init_obstack);
8022 constructor_elements = NULL;
8023 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8025 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8026 /* Skip any nameless bit fields at the beginning. */
8027 while (constructor_unfilled_fields != 0
8028 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8029 && DECL_NAME (constructor_unfilled_fields) == 0)
8030 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8033 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8035 if (TYPE_DOMAIN (constructor_type))
8036 constructor_unfilled_index
8037 = convert (bitsizetype,
8038 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8039 else
8040 constructor_unfilled_index = bitsize_zero_node;
8042 constructor_incremental = 0;
8045 /* Build AVL tree from a string constant. */
8047 static void
8048 set_nonincremental_init_from_string (tree str,
8049 struct obstack * braced_init_obstack)
8051 tree value, purpose, type;
8052 HOST_WIDE_INT val[2];
8053 const char *p, *end;
8054 int byte, wchar_bytes, charwidth, bitpos;
8056 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8058 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8059 charwidth = TYPE_PRECISION (char_type_node);
8060 type = TREE_TYPE (constructor_type);
8061 p = TREE_STRING_POINTER (str);
8062 end = p + TREE_STRING_LENGTH (str);
8064 for (purpose = bitsize_zero_node;
8065 p < end
8066 && !(constructor_max_index
8067 && tree_int_cst_lt (constructor_max_index, purpose));
8068 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8070 if (wchar_bytes == 1)
8072 val[1] = (unsigned char) *p++;
8073 val[0] = 0;
8075 else
8077 val[0] = 0;
8078 val[1] = 0;
8079 for (byte = 0; byte < wchar_bytes; byte++)
8081 if (BYTES_BIG_ENDIAN)
8082 bitpos = (wchar_bytes - byte - 1) * charwidth;
8083 else
8084 bitpos = byte * charwidth;
8085 val[bitpos < HOST_BITS_PER_WIDE_INT]
8086 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8087 << (bitpos % HOST_BITS_PER_WIDE_INT);
8091 if (!TYPE_UNSIGNED (type))
8093 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8094 if (bitpos < HOST_BITS_PER_WIDE_INT)
8096 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8098 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8099 val[0] = -1;
8102 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8104 if (val[1] < 0)
8105 val[0] = -1;
8107 else if (val[0] & (((HOST_WIDE_INT) 1)
8108 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8109 val[0] |= ((HOST_WIDE_INT) -1)
8110 << (bitpos - HOST_BITS_PER_WIDE_INT);
8113 value = build_int_cst_wide (type, val[1], val[0]);
8114 add_pending_init (purpose, value, NULL_TREE, true,
8115 braced_init_obstack);
8118 constructor_incremental = 0;
8121 /* Return value of FIELD in pending initializer or zero if the field was
8122 not initialized yet. */
8124 static tree
8125 find_init_member (tree field, struct obstack * braced_init_obstack)
8127 struct init_node *p;
8129 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8131 if (constructor_incremental
8132 && tree_int_cst_lt (field, constructor_unfilled_index))
8133 set_nonincremental_init (braced_init_obstack);
8135 p = constructor_pending_elts;
8136 while (p)
8138 if (tree_int_cst_lt (field, p->purpose))
8139 p = p->left;
8140 else if (tree_int_cst_lt (p->purpose, field))
8141 p = p->right;
8142 else
8143 return p->value;
8146 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8148 tree bitpos = bit_position (field);
8150 if (constructor_incremental
8151 && (!constructor_unfilled_fields
8152 || tree_int_cst_lt (bitpos,
8153 bit_position (constructor_unfilled_fields))))
8154 set_nonincremental_init (braced_init_obstack);
8156 p = constructor_pending_elts;
8157 while (p)
8159 if (field == p->purpose)
8160 return p->value;
8161 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8162 p = p->left;
8163 else
8164 p = p->right;
8167 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8169 if (!vec_safe_is_empty (constructor_elements)
8170 && (constructor_elements->last ().index == field))
8171 return constructor_elements->last ().value;
8173 return 0;
8176 /* "Output" the next constructor element.
8177 At top level, really output it to assembler code now.
8178 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8179 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8180 TYPE is the data type that the containing data type wants here.
8181 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8182 If VALUE is a string constant, STRICT_STRING is true if it is
8183 unparenthesized or we should not warn here for it being parenthesized.
8184 For other types of VALUE, STRICT_STRING is not used.
8186 PENDING if non-nil means output pending elements that belong
8187 right after this element. (PENDING is normally 1;
8188 it is 0 while outputting pending elements, to avoid recursion.)
8190 IMPLICIT is true if value comes from pop_init_level (1),
8191 the new initializer has been merged with the existing one
8192 and thus no warnings should be emitted about overriding an
8193 existing initializer. */
8195 static void
8196 output_init_element (tree value, tree origtype, bool strict_string, tree type,
8197 tree field, int pending, bool implicit,
8198 struct obstack * braced_init_obstack)
8200 tree semantic_type = NULL_TREE;
8201 bool maybe_const = true;
8202 bool npc;
8204 if (type == error_mark_node || value == error_mark_node)
8206 constructor_erroneous = 1;
8207 return;
8209 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8210 && (TREE_CODE (value) == STRING_CST
8211 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8212 && !(TREE_CODE (value) == STRING_CST
8213 && TREE_CODE (type) == ARRAY_TYPE
8214 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8215 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8216 TYPE_MAIN_VARIANT (type)))
8217 value = array_to_pointer_conversion (input_location, value);
8219 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8220 && require_constant_value && !flag_isoc99 && pending)
8222 /* As an extension, allow initializing objects with static storage
8223 duration with compound literals (which are then treated just as
8224 the brace enclosed list they contain). */
8225 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8226 value = DECL_INITIAL (decl);
8229 npc = null_pointer_constant_p (value);
8230 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8232 semantic_type = TREE_TYPE (value);
8233 value = TREE_OPERAND (value, 0);
8235 value = c_fully_fold (value, require_constant_value, &maybe_const);
8237 if (value == error_mark_node)
8238 constructor_erroneous = 1;
8239 else if (!TREE_CONSTANT (value))
8240 constructor_constant = 0;
8241 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8242 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8243 || TREE_CODE (constructor_type) == UNION_TYPE)
8244 && DECL_C_BIT_FIELD (field)
8245 && TREE_CODE (value) != INTEGER_CST))
8246 constructor_simple = 0;
8247 if (!maybe_const)
8248 constructor_nonconst = 1;
8250 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8252 if (require_constant_value)
8254 error_init ("initializer element is not constant");
8255 value = error_mark_node;
8257 else if (require_constant_elements)
8258 pedwarn (input_location, 0,
8259 "initializer element is not computable at load time");
8261 else if (!maybe_const
8262 && (require_constant_value || require_constant_elements))
8263 pedwarn_init (input_location, 0,
8264 "initializer element is not a constant expression");
8266 /* Issue -Wc++-compat warnings about initializing a bitfield with
8267 enum type. */
8268 if (warn_cxx_compat
8269 && field != NULL_TREE
8270 && TREE_CODE (field) == FIELD_DECL
8271 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8272 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8273 != TYPE_MAIN_VARIANT (type))
8274 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8276 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8277 if (checktype != error_mark_node
8278 && (TYPE_MAIN_VARIANT (checktype)
8279 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8280 warning_init (OPT_Wc___compat,
8281 "enum conversion in initialization is invalid in C++");
8284 /* If this field is empty (and not at the end of structure),
8285 don't do anything other than checking the initializer. */
8286 if (field
8287 && (TREE_TYPE (field) == error_mark_node
8288 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8289 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8290 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8291 || DECL_CHAIN (field)))))
8292 return;
8294 if (semantic_type)
8295 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8296 value = digest_init (input_location, type, value, origtype, npc,
8297 strict_string, require_constant_value);
8298 if (value == error_mark_node)
8300 constructor_erroneous = 1;
8301 return;
8303 if (require_constant_value || require_constant_elements)
8304 constant_expression_warning (value);
8306 /* If this element doesn't come next in sequence,
8307 put it on constructor_pending_elts. */
8308 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8309 && (!constructor_incremental
8310 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8312 if (constructor_incremental
8313 && tree_int_cst_lt (field, constructor_unfilled_index))
8314 set_nonincremental_init (braced_init_obstack);
8316 add_pending_init (field, value, origtype, implicit,
8317 braced_init_obstack);
8318 return;
8320 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8321 && (!constructor_incremental
8322 || field != constructor_unfilled_fields))
8324 /* We do this for records but not for unions. In a union,
8325 no matter which field is specified, it can be initialized
8326 right away since it starts at the beginning of the union. */
8327 if (constructor_incremental)
8329 if (!constructor_unfilled_fields)
8330 set_nonincremental_init (braced_init_obstack);
8331 else
8333 tree bitpos, unfillpos;
8335 bitpos = bit_position (field);
8336 unfillpos = bit_position (constructor_unfilled_fields);
8338 if (tree_int_cst_lt (bitpos, unfillpos))
8339 set_nonincremental_init (braced_init_obstack);
8343 add_pending_init (field, value, origtype, implicit,
8344 braced_init_obstack);
8345 return;
8347 else if (TREE_CODE (constructor_type) == UNION_TYPE
8348 && !vec_safe_is_empty (constructor_elements))
8350 if (!implicit)
8352 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8353 warning_init (0,
8354 "initialized field with side-effects overwritten");
8355 else if (warn_override_init)
8356 warning_init (OPT_Woverride_init, "initialized field overwritten");
8359 /* We can have just one union field set. */
8360 constructor_elements = NULL;
8363 /* Otherwise, output this element either to
8364 constructor_elements or to the assembler file. */
8366 constructor_elt celt = {field, value};
8367 vec_safe_push (constructor_elements, celt);
8369 /* Advance the variable that indicates sequential elements output. */
8370 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8371 constructor_unfilled_index
8372 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8373 bitsize_one_node);
8374 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8376 constructor_unfilled_fields
8377 = DECL_CHAIN (constructor_unfilled_fields);
8379 /* Skip any nameless bit fields. */
8380 while (constructor_unfilled_fields != 0
8381 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8382 && DECL_NAME (constructor_unfilled_fields) == 0)
8383 constructor_unfilled_fields =
8384 DECL_CHAIN (constructor_unfilled_fields);
8386 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8387 constructor_unfilled_fields = 0;
8389 /* Now output any pending elements which have become next. */
8390 if (pending)
8391 output_pending_init_elements (0, braced_init_obstack);
8394 /* Output any pending elements which have become next.
8395 As we output elements, constructor_unfilled_{fields,index}
8396 advances, which may cause other elements to become next;
8397 if so, they too are output.
8399 If ALL is 0, we return when there are
8400 no more pending elements to output now.
8402 If ALL is 1, we output space as necessary so that
8403 we can output all the pending elements. */
8404 static void
8405 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8407 struct init_node *elt = constructor_pending_elts;
8408 tree next;
8410 retry:
8412 /* Look through the whole pending tree.
8413 If we find an element that should be output now,
8414 output it. Otherwise, set NEXT to the element
8415 that comes first among those still pending. */
8417 next = 0;
8418 while (elt)
8420 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8422 if (tree_int_cst_equal (elt->purpose,
8423 constructor_unfilled_index))
8424 output_init_element (elt->value, elt->origtype, true,
8425 TREE_TYPE (constructor_type),
8426 constructor_unfilled_index, 0, false,
8427 braced_init_obstack);
8428 else if (tree_int_cst_lt (constructor_unfilled_index,
8429 elt->purpose))
8431 /* Advance to the next smaller node. */
8432 if (elt->left)
8433 elt = elt->left;
8434 else
8436 /* We have reached the smallest node bigger than the
8437 current unfilled index. Fill the space first. */
8438 next = elt->purpose;
8439 break;
8442 else
8444 /* Advance to the next bigger node. */
8445 if (elt->right)
8446 elt = elt->right;
8447 else
8449 /* We have reached the biggest node in a subtree. Find
8450 the parent of it, which is the next bigger node. */
8451 while (elt->parent && elt->parent->right == elt)
8452 elt = elt->parent;
8453 elt = elt->parent;
8454 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8455 elt->purpose))
8457 next = elt->purpose;
8458 break;
8463 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8464 || TREE_CODE (constructor_type) == UNION_TYPE)
8466 tree ctor_unfilled_bitpos, elt_bitpos;
8468 /* If the current record is complete we are done. */
8469 if (constructor_unfilled_fields == 0)
8470 break;
8472 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8473 elt_bitpos = bit_position (elt->purpose);
8474 /* We can't compare fields here because there might be empty
8475 fields in between. */
8476 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8478 constructor_unfilled_fields = elt->purpose;
8479 output_init_element (elt->value, elt->origtype, true,
8480 TREE_TYPE (elt->purpose),
8481 elt->purpose, 0, false,
8482 braced_init_obstack);
8484 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8486 /* Advance to the next smaller node. */
8487 if (elt->left)
8488 elt = elt->left;
8489 else
8491 /* We have reached the smallest node bigger than the
8492 current unfilled field. Fill the space first. */
8493 next = elt->purpose;
8494 break;
8497 else
8499 /* Advance to the next bigger node. */
8500 if (elt->right)
8501 elt = elt->right;
8502 else
8504 /* We have reached the biggest node in a subtree. Find
8505 the parent of it, which is the next bigger node. */
8506 while (elt->parent && elt->parent->right == elt)
8507 elt = elt->parent;
8508 elt = elt->parent;
8509 if (elt
8510 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8511 bit_position (elt->purpose))))
8513 next = elt->purpose;
8514 break;
8521 /* Ordinarily return, but not if we want to output all
8522 and there are elements left. */
8523 if (!(all && next != 0))
8524 return;
8526 /* If it's not incremental, just skip over the gap, so that after
8527 jumping to retry we will output the next successive element. */
8528 if (TREE_CODE (constructor_type) == RECORD_TYPE
8529 || TREE_CODE (constructor_type) == UNION_TYPE)
8530 constructor_unfilled_fields = next;
8531 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8532 constructor_unfilled_index = next;
8534 /* ELT now points to the node in the pending tree with the next
8535 initializer to output. */
8536 goto retry;
8539 /* Add one non-braced element to the current constructor level.
8540 This adjusts the current position within the constructor's type.
8541 This may also start or terminate implicit levels
8542 to handle a partly-braced initializer.
8544 Once this has found the correct level for the new element,
8545 it calls output_init_element.
8547 IMPLICIT is true if value comes from pop_init_level (1),
8548 the new initializer has been merged with the existing one
8549 and thus no warnings should be emitted about overriding an
8550 existing initializer. */
8552 void
8553 process_init_element (struct c_expr value, bool implicit,
8554 struct obstack * braced_init_obstack)
8556 tree orig_value = value.value;
8557 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8558 bool strict_string = value.original_code == STRING_CST;
8559 bool was_designated = designator_depth != 0;
8561 designator_depth = 0;
8562 designator_erroneous = 0;
8564 /* Handle superfluous braces around string cst as in
8565 char x[] = {"foo"}; */
8566 if (string_flag
8567 && constructor_type
8568 && !was_designated
8569 && TREE_CODE (constructor_type) == ARRAY_TYPE
8570 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8571 && integer_zerop (constructor_unfilled_index))
8573 if (constructor_stack->replacement_value.value)
8574 error_init ("excess elements in char array initializer");
8575 constructor_stack->replacement_value = value;
8576 return;
8579 if (constructor_stack->replacement_value.value != 0)
8581 error_init ("excess elements in struct initializer");
8582 return;
8585 /* Ignore elements of a brace group if it is entirely superfluous
8586 and has already been diagnosed. */
8587 if (constructor_type == 0)
8588 return;
8590 /* If we've exhausted any levels that didn't have braces,
8591 pop them now. */
8592 while (constructor_stack->implicit)
8594 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8595 || TREE_CODE (constructor_type) == UNION_TYPE)
8596 && constructor_fields == 0)
8597 process_init_element (pop_init_level (1, braced_init_obstack),
8598 true, braced_init_obstack);
8599 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8600 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8601 && constructor_max_index
8602 && tree_int_cst_lt (constructor_max_index,
8603 constructor_index))
8604 process_init_element (pop_init_level (1, braced_init_obstack),
8605 true, braced_init_obstack);
8606 else
8607 break;
8610 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8611 if (constructor_range_stack)
8613 /* If value is a compound literal and we'll be just using its
8614 content, don't put it into a SAVE_EXPR. */
8615 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8616 || !require_constant_value
8617 || flag_isoc99)
8619 tree semantic_type = NULL_TREE;
8620 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8622 semantic_type = TREE_TYPE (value.value);
8623 value.value = TREE_OPERAND (value.value, 0);
8625 value.value = c_save_expr (value.value);
8626 if (semantic_type)
8627 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8628 value.value);
8632 while (1)
8634 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8636 tree fieldtype;
8637 enum tree_code fieldcode;
8639 if (constructor_fields == 0)
8641 pedwarn_init (input_location, 0,
8642 "excess elements in struct initializer");
8643 break;
8646 fieldtype = TREE_TYPE (constructor_fields);
8647 if (fieldtype != error_mark_node)
8648 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8649 fieldcode = TREE_CODE (fieldtype);
8651 /* Error for non-static initialization of a flexible array member. */
8652 if (fieldcode == ARRAY_TYPE
8653 && !require_constant_value
8654 && TYPE_SIZE (fieldtype) == NULL_TREE
8655 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8657 error_init ("non-static initialization of a flexible array member");
8658 break;
8661 /* Accept a string constant to initialize a subarray. */
8662 if (value.value != 0
8663 && fieldcode == ARRAY_TYPE
8664 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8665 && string_flag)
8666 value.value = orig_value;
8667 /* Otherwise, if we have come to a subaggregate,
8668 and we don't have an element of its type, push into it. */
8669 else if (value.value != 0
8670 && value.value != error_mark_node
8671 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8672 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8673 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8675 push_init_level (1, braced_init_obstack);
8676 continue;
8679 if (value.value)
8681 push_member_name (constructor_fields);
8682 output_init_element (value.value, value.original_type,
8683 strict_string, fieldtype,
8684 constructor_fields, 1, implicit,
8685 braced_init_obstack);
8686 RESTORE_SPELLING_DEPTH (constructor_depth);
8688 else
8689 /* Do the bookkeeping for an element that was
8690 directly output as a constructor. */
8692 /* For a record, keep track of end position of last field. */
8693 if (DECL_SIZE (constructor_fields))
8694 constructor_bit_index
8695 = size_binop_loc (input_location, PLUS_EXPR,
8696 bit_position (constructor_fields),
8697 DECL_SIZE (constructor_fields));
8699 /* If the current field was the first one not yet written out,
8700 it isn't now, so update. */
8701 if (constructor_unfilled_fields == constructor_fields)
8703 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8704 /* Skip any nameless bit fields. */
8705 while (constructor_unfilled_fields != 0
8706 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8707 && DECL_NAME (constructor_unfilled_fields) == 0)
8708 constructor_unfilled_fields =
8709 DECL_CHAIN (constructor_unfilled_fields);
8713 constructor_fields = DECL_CHAIN (constructor_fields);
8714 /* Skip any nameless bit fields at the beginning. */
8715 while (constructor_fields != 0
8716 && DECL_C_BIT_FIELD (constructor_fields)
8717 && DECL_NAME (constructor_fields) == 0)
8718 constructor_fields = DECL_CHAIN (constructor_fields);
8720 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8722 tree fieldtype;
8723 enum tree_code fieldcode;
8725 if (constructor_fields == 0)
8727 pedwarn_init (input_location, 0,
8728 "excess elements in union initializer");
8729 break;
8732 fieldtype = TREE_TYPE (constructor_fields);
8733 if (fieldtype != error_mark_node)
8734 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8735 fieldcode = TREE_CODE (fieldtype);
8737 /* Warn that traditional C rejects initialization of unions.
8738 We skip the warning if the value is zero. This is done
8739 under the assumption that the zero initializer in user
8740 code appears conditioned on e.g. __STDC__ to avoid
8741 "missing initializer" warnings and relies on default
8742 initialization to zero in the traditional C case.
8743 We also skip the warning if the initializer is designated,
8744 again on the assumption that this must be conditional on
8745 __STDC__ anyway (and we've already complained about the
8746 member-designator already). */
8747 if (!in_system_header_at (input_location) && !constructor_designated
8748 && !(value.value && (integer_zerop (value.value)
8749 || real_zerop (value.value))))
8750 warning (OPT_Wtraditional, "traditional C rejects initialization "
8751 "of unions");
8753 /* Accept a string constant to initialize a subarray. */
8754 if (value.value != 0
8755 && fieldcode == ARRAY_TYPE
8756 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8757 && string_flag)
8758 value.value = orig_value;
8759 /* Otherwise, if we have come to a subaggregate,
8760 and we don't have an element of its type, push into it. */
8761 else if (value.value != 0
8762 && value.value != error_mark_node
8763 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8764 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8765 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8767 push_init_level (1, braced_init_obstack);
8768 continue;
8771 if (value.value)
8773 push_member_name (constructor_fields);
8774 output_init_element (value.value, value.original_type,
8775 strict_string, fieldtype,
8776 constructor_fields, 1, implicit,
8777 braced_init_obstack);
8778 RESTORE_SPELLING_DEPTH (constructor_depth);
8780 else
8781 /* Do the bookkeeping for an element that was
8782 directly output as a constructor. */
8784 constructor_bit_index = DECL_SIZE (constructor_fields);
8785 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8788 constructor_fields = 0;
8790 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8792 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8793 enum tree_code eltcode = TREE_CODE (elttype);
8795 /* Accept a string constant to initialize a subarray. */
8796 if (value.value != 0
8797 && eltcode == ARRAY_TYPE
8798 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8799 && string_flag)
8800 value.value = orig_value;
8801 /* Otherwise, if we have come to a subaggregate,
8802 and we don't have an element of its type, push into it. */
8803 else if (value.value != 0
8804 && value.value != error_mark_node
8805 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8806 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8807 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8809 push_init_level (1, braced_init_obstack);
8810 continue;
8813 if (constructor_max_index != 0
8814 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8815 || integer_all_onesp (constructor_max_index)))
8817 pedwarn_init (input_location, 0,
8818 "excess elements in array initializer");
8819 break;
8822 /* Now output the actual element. */
8823 if (value.value)
8825 push_array_bounds (tree_to_uhwi (constructor_index));
8826 output_init_element (value.value, value.original_type,
8827 strict_string, elttype,
8828 constructor_index, 1, implicit,
8829 braced_init_obstack);
8830 RESTORE_SPELLING_DEPTH (constructor_depth);
8833 constructor_index
8834 = size_binop_loc (input_location, PLUS_EXPR,
8835 constructor_index, bitsize_one_node);
8837 if (!value.value)
8838 /* If we are doing the bookkeeping for an element that was
8839 directly output as a constructor, we must update
8840 constructor_unfilled_index. */
8841 constructor_unfilled_index = constructor_index;
8843 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8845 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8847 /* Do a basic check of initializer size. Note that vectors
8848 always have a fixed size derived from their type. */
8849 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8851 pedwarn_init (input_location, 0,
8852 "excess elements in vector initializer");
8853 break;
8856 /* Now output the actual element. */
8857 if (value.value)
8859 if (TREE_CODE (value.value) == VECTOR_CST)
8860 elttype = TYPE_MAIN_VARIANT (constructor_type);
8861 output_init_element (value.value, value.original_type,
8862 strict_string, elttype,
8863 constructor_index, 1, implicit,
8864 braced_init_obstack);
8867 constructor_index
8868 = size_binop_loc (input_location,
8869 PLUS_EXPR, constructor_index, bitsize_one_node);
8871 if (!value.value)
8872 /* If we are doing the bookkeeping for an element that was
8873 directly output as a constructor, we must update
8874 constructor_unfilled_index. */
8875 constructor_unfilled_index = constructor_index;
8878 /* Handle the sole element allowed in a braced initializer
8879 for a scalar variable. */
8880 else if (constructor_type != error_mark_node
8881 && constructor_fields == 0)
8883 pedwarn_init (input_location, 0,
8884 "excess elements in scalar initializer");
8885 break;
8887 else
8889 if (value.value)
8890 output_init_element (value.value, value.original_type,
8891 strict_string, constructor_type,
8892 NULL_TREE, 1, implicit,
8893 braced_init_obstack);
8894 constructor_fields = 0;
8897 /* Handle range initializers either at this level or anywhere higher
8898 in the designator stack. */
8899 if (constructor_range_stack)
8901 struct constructor_range_stack *p, *range_stack;
8902 int finish = 0;
8904 range_stack = constructor_range_stack;
8905 constructor_range_stack = 0;
8906 while (constructor_stack != range_stack->stack)
8908 gcc_assert (constructor_stack->implicit);
8909 process_init_element (pop_init_level (1,
8910 braced_init_obstack),
8911 true, braced_init_obstack);
8913 for (p = range_stack;
8914 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8915 p = p->prev)
8917 gcc_assert (constructor_stack->implicit);
8918 process_init_element (pop_init_level (1, braced_init_obstack),
8919 true, braced_init_obstack);
8922 p->index = size_binop_loc (input_location,
8923 PLUS_EXPR, p->index, bitsize_one_node);
8924 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8925 finish = 1;
8927 while (1)
8929 constructor_index = p->index;
8930 constructor_fields = p->fields;
8931 if (finish && p->range_end && p->index == p->range_start)
8933 finish = 0;
8934 p->prev = 0;
8936 p = p->next;
8937 if (!p)
8938 break;
8939 push_init_level (2, braced_init_obstack);
8940 p->stack = constructor_stack;
8941 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8942 p->index = p->range_start;
8945 if (!finish)
8946 constructor_range_stack = range_stack;
8947 continue;
8950 break;
8953 constructor_range_stack = 0;
8956 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8957 (guaranteed to be 'volatile' or null) and ARGS (represented using
8958 an ASM_EXPR node). */
8959 tree
8960 build_asm_stmt (tree cv_qualifier, tree args)
8962 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8963 ASM_VOLATILE_P (args) = 1;
8964 return add_stmt (args);
8967 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8968 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8969 SIMPLE indicates whether there was anything at all after the
8970 string in the asm expression -- asm("blah") and asm("blah" : )
8971 are subtly different. We use a ASM_EXPR node to represent this. */
8972 tree
8973 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8974 tree clobbers, tree labels, bool simple)
8976 tree tail;
8977 tree args;
8978 int i;
8979 const char *constraint;
8980 const char **oconstraints;
8981 bool allows_mem, allows_reg, is_inout;
8982 int ninputs, noutputs;
8984 ninputs = list_length (inputs);
8985 noutputs = list_length (outputs);
8986 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8988 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8990 /* Remove output conversions that change the type but not the mode. */
8991 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8993 tree output = TREE_VALUE (tail);
8995 output = c_fully_fold (output, false, NULL);
8997 /* ??? Really, this should not be here. Users should be using a
8998 proper lvalue, dammit. But there's a long history of using casts
8999 in the output operands. In cases like longlong.h, this becomes a
9000 primitive form of typechecking -- if the cast can be removed, then
9001 the output operand had a type of the proper width; otherwise we'll
9002 get an error. Gross, but ... */
9003 STRIP_NOPS (output);
9005 if (!lvalue_or_else (loc, output, lv_asm))
9006 output = error_mark_node;
9008 if (output != error_mark_node
9009 && (TREE_READONLY (output)
9010 || TYPE_READONLY (TREE_TYPE (output))
9011 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9012 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9013 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9014 readonly_error (loc, output, lv_asm);
9016 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9017 oconstraints[i] = constraint;
9019 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9020 &allows_mem, &allows_reg, &is_inout))
9022 /* If the operand is going to end up in memory,
9023 mark it addressable. */
9024 if (!allows_reg && !c_mark_addressable (output))
9025 output = error_mark_node;
9026 if (!(!allows_reg && allows_mem)
9027 && output != error_mark_node
9028 && VOID_TYPE_P (TREE_TYPE (output)))
9030 error_at (loc, "invalid use of void expression");
9031 output = error_mark_node;
9034 else
9035 output = error_mark_node;
9037 TREE_VALUE (tail) = output;
9040 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9042 tree input;
9044 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9045 input = TREE_VALUE (tail);
9047 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9048 oconstraints, &allows_mem, &allows_reg))
9050 /* If the operand is going to end up in memory,
9051 mark it addressable. */
9052 if (!allows_reg && allows_mem)
9054 input = c_fully_fold (input, false, NULL);
9056 /* Strip the nops as we allow this case. FIXME, this really
9057 should be rejected or made deprecated. */
9058 STRIP_NOPS (input);
9059 if (!c_mark_addressable (input))
9060 input = error_mark_node;
9062 else
9064 struct c_expr expr;
9065 memset (&expr, 0, sizeof (expr));
9066 expr.value = input;
9067 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9068 input = c_fully_fold (expr.value, false, NULL);
9070 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9072 error_at (loc, "invalid use of void expression");
9073 input = error_mark_node;
9077 else
9078 input = error_mark_node;
9080 TREE_VALUE (tail) = input;
9083 /* ASMs with labels cannot have outputs. This should have been
9084 enforced by the parser. */
9085 gcc_assert (outputs == NULL || labels == NULL);
9087 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9089 /* asm statements without outputs, including simple ones, are treated
9090 as volatile. */
9091 ASM_INPUT_P (args) = simple;
9092 ASM_VOLATILE_P (args) = (noutputs == 0);
9094 return args;
9097 /* Generate a goto statement to LABEL. LOC is the location of the
9098 GOTO. */
9100 tree
9101 c_finish_goto_label (location_t loc, tree label)
9103 tree decl = lookup_label_for_goto (loc, label);
9104 if (!decl)
9105 return NULL_TREE;
9106 TREE_USED (decl) = 1;
9108 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9109 SET_EXPR_LOCATION (t, loc);
9110 return add_stmt (t);
9114 /* Generate a computed goto statement to EXPR. LOC is the location of
9115 the GOTO. */
9117 tree
9118 c_finish_goto_ptr (location_t loc, tree expr)
9120 tree t;
9121 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9122 expr = c_fully_fold (expr, false, NULL);
9123 expr = convert (ptr_type_node, expr);
9124 t = build1 (GOTO_EXPR, void_type_node, expr);
9125 SET_EXPR_LOCATION (t, loc);
9126 return add_stmt (t);
9129 /* Generate a C `return' statement. RETVAL is the expression for what
9130 to return, or a null pointer for `return;' with no value. LOC is
9131 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9132 is the original type of RETVAL. */
9134 tree
9135 c_finish_return (location_t loc, tree retval, tree origtype)
9137 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9138 bool no_warning = false;
9139 bool npc = false;
9140 size_t rank = 0;
9142 if (TREE_THIS_VOLATILE (current_function_decl))
9143 warning_at (loc, 0,
9144 "function declared %<noreturn%> has a %<return%> statement");
9146 if (flag_cilkplus && contains_array_notation_expr (retval))
9148 /* Array notations are allowed in a return statement if it is inside a
9149 built-in array notation reduction function. */
9150 if (!find_rank (loc, retval, retval, false, &rank))
9151 return error_mark_node;
9152 if (rank >= 1)
9154 error_at (loc, "array notation expression cannot be used as a "
9155 "return value");
9156 return error_mark_node;
9159 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9161 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9162 "allowed");
9163 return error_mark_node;
9165 if (retval)
9167 tree semantic_type = NULL_TREE;
9168 npc = null_pointer_constant_p (retval);
9169 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9171 semantic_type = TREE_TYPE (retval);
9172 retval = TREE_OPERAND (retval, 0);
9174 retval = c_fully_fold (retval, false, NULL);
9175 if (semantic_type)
9176 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9179 if (!retval)
9181 current_function_returns_null = 1;
9182 if ((warn_return_type || flag_isoc99)
9183 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9185 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
9186 "%<return%> with no value, in "
9187 "function returning non-void");
9188 no_warning = true;
9191 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9193 current_function_returns_null = 1;
9194 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9195 pedwarn (loc, 0,
9196 "%<return%> with a value, in function returning void");
9197 else
9198 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9199 "%<return%> with expression, in function returning void");
9201 else
9203 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9204 retval, origtype, ic_return,
9205 npc, NULL_TREE, NULL_TREE, 0);
9206 tree res = DECL_RESULT (current_function_decl);
9207 tree inner;
9208 bool save;
9210 current_function_returns_value = 1;
9211 if (t == error_mark_node)
9212 return NULL_TREE;
9214 save = in_late_binary_op;
9215 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9216 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9217 in_late_binary_op = true;
9218 inner = t = convert (TREE_TYPE (res), t);
9219 in_late_binary_op = save;
9221 /* Strip any conversions, additions, and subtractions, and see if
9222 we are returning the address of a local variable. Warn if so. */
9223 while (1)
9225 switch (TREE_CODE (inner))
9227 CASE_CONVERT:
9228 case NON_LVALUE_EXPR:
9229 case PLUS_EXPR:
9230 case POINTER_PLUS_EXPR:
9231 inner = TREE_OPERAND (inner, 0);
9232 continue;
9234 case MINUS_EXPR:
9235 /* If the second operand of the MINUS_EXPR has a pointer
9236 type (or is converted from it), this may be valid, so
9237 don't give a warning. */
9239 tree op1 = TREE_OPERAND (inner, 1);
9241 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9242 && (CONVERT_EXPR_P (op1)
9243 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9244 op1 = TREE_OPERAND (op1, 0);
9246 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9247 break;
9249 inner = TREE_OPERAND (inner, 0);
9250 continue;
9253 case ADDR_EXPR:
9254 inner = TREE_OPERAND (inner, 0);
9256 while (REFERENCE_CLASS_P (inner)
9257 && TREE_CODE (inner) != INDIRECT_REF)
9258 inner = TREE_OPERAND (inner, 0);
9260 if (DECL_P (inner)
9261 && !DECL_EXTERNAL (inner)
9262 && !TREE_STATIC (inner)
9263 && DECL_CONTEXT (inner) == current_function_decl)
9264 warning_at (loc,
9265 OPT_Wreturn_local_addr, "function returns address "
9266 "of local variable");
9267 break;
9269 default:
9270 break;
9273 break;
9276 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9277 SET_EXPR_LOCATION (retval, loc);
9279 if (warn_sequence_point)
9280 verify_sequence_points (retval);
9283 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9284 TREE_NO_WARNING (ret_stmt) |= no_warning;
9285 return add_stmt (ret_stmt);
9288 struct c_switch {
9289 /* The SWITCH_EXPR being built. */
9290 tree switch_expr;
9292 /* The original type of the testing expression, i.e. before the
9293 default conversion is applied. */
9294 tree orig_type;
9296 /* A splay-tree mapping the low element of a case range to the high
9297 element, or NULL_TREE if there is no high element. Used to
9298 determine whether or not a new case label duplicates an old case
9299 label. We need a tree, rather than simply a hash table, because
9300 of the GNU case range extension. */
9301 splay_tree cases;
9303 /* The bindings at the point of the switch. This is used for
9304 warnings crossing decls when branching to a case label. */
9305 struct c_spot_bindings *bindings;
9307 /* The next node on the stack. */
9308 struct c_switch *next;
9311 /* A stack of the currently active switch statements. The innermost
9312 switch statement is on the top of the stack. There is no need to
9313 mark the stack for garbage collection because it is only active
9314 during the processing of the body of a function, and we never
9315 collect at that point. */
9317 struct c_switch *c_switch_stack;
9319 /* Start a C switch statement, testing expression EXP. Return the new
9320 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9321 SWITCH_COND_LOC is the location of the switch's condition. */
9323 tree
9324 c_start_case (location_t switch_loc,
9325 location_t switch_cond_loc,
9326 tree exp)
9328 tree orig_type = error_mark_node;
9329 struct c_switch *cs;
9331 if (exp != error_mark_node)
9333 orig_type = TREE_TYPE (exp);
9335 if (!INTEGRAL_TYPE_P (orig_type))
9337 if (orig_type != error_mark_node)
9339 error_at (switch_cond_loc, "switch quantity not an integer");
9340 orig_type = error_mark_node;
9342 exp = integer_zero_node;
9344 else
9346 tree type = TYPE_MAIN_VARIANT (orig_type);
9348 if (!in_system_header_at (input_location)
9349 && (type == long_integer_type_node
9350 || type == long_unsigned_type_node))
9351 warning_at (switch_cond_loc,
9352 OPT_Wtraditional, "%<long%> switch expression not "
9353 "converted to %<int%> in ISO C");
9355 exp = c_fully_fold (exp, false, NULL);
9356 exp = default_conversion (exp);
9358 if (warn_sequence_point)
9359 verify_sequence_points (exp);
9363 /* Add this new SWITCH_EXPR to the stack. */
9364 cs = XNEW (struct c_switch);
9365 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9366 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9367 cs->orig_type = orig_type;
9368 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9369 cs->bindings = c_get_switch_bindings ();
9370 cs->next = c_switch_stack;
9371 c_switch_stack = cs;
9373 return add_stmt (cs->switch_expr);
9376 /* Process a case label at location LOC. */
9378 tree
9379 do_case (location_t loc, tree low_value, tree high_value)
9381 tree label = NULL_TREE;
9383 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9385 low_value = c_fully_fold (low_value, false, NULL);
9386 if (TREE_CODE (low_value) == INTEGER_CST)
9387 pedwarn (input_location, OPT_Wpedantic,
9388 "case label is not an integer constant expression");
9391 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9393 high_value = c_fully_fold (high_value, false, NULL);
9394 if (TREE_CODE (high_value) == INTEGER_CST)
9395 pedwarn (input_location, OPT_Wpedantic,
9396 "case label is not an integer constant expression");
9399 if (c_switch_stack == NULL)
9401 if (low_value)
9402 error_at (loc, "case label not within a switch statement");
9403 else
9404 error_at (loc, "%<default%> label not within a switch statement");
9405 return NULL_TREE;
9408 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9409 EXPR_LOCATION (c_switch_stack->switch_expr),
9410 loc))
9411 return NULL_TREE;
9413 label = c_add_case_label (loc, c_switch_stack->cases,
9414 SWITCH_COND (c_switch_stack->switch_expr),
9415 c_switch_stack->orig_type,
9416 low_value, high_value);
9417 if (label == error_mark_node)
9418 label = NULL_TREE;
9419 return label;
9422 /* Finish the switch statement. */
9424 void
9425 c_finish_case (tree body)
9427 struct c_switch *cs = c_switch_stack;
9428 location_t switch_location;
9430 SWITCH_BODY (cs->switch_expr) = body;
9432 /* Emit warnings as needed. */
9433 switch_location = EXPR_LOCATION (cs->switch_expr);
9434 c_do_switch_warnings (cs->cases, switch_location,
9435 TREE_TYPE (cs->switch_expr),
9436 SWITCH_COND (cs->switch_expr));
9438 /* Pop the stack. */
9439 c_switch_stack = cs->next;
9440 splay_tree_delete (cs->cases);
9441 c_release_switch_bindings (cs->bindings);
9442 XDELETE (cs);
9445 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9446 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9447 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9448 statement, and was not surrounded with parenthesis. */
9450 void
9451 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9452 tree else_block, bool nested_if)
9454 tree stmt;
9456 /* If the condition has array notations, then the rank of the then_block and
9457 else_block must be either 0 or be equal to the rank of the condition. If
9458 the condition does not have array notations then break them up as it is
9459 broken up in a normal expression. */
9460 if (flag_cilkplus && contains_array_notation_expr (cond))
9462 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9463 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9464 return;
9465 if (then_block
9466 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9467 return;
9468 if (else_block
9469 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9470 return;
9471 if (cond_rank != then_rank && then_rank != 0)
9473 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9474 " and the then-block");
9475 return;
9477 else if (cond_rank != else_rank && else_rank != 0)
9479 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9480 " and the else-block");
9481 return;
9484 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9485 if (warn_parentheses && nested_if && else_block == NULL)
9487 tree inner_if = then_block;
9489 /* We know from the grammar productions that there is an IF nested
9490 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9491 it might not be exactly THEN_BLOCK, but should be the last
9492 non-container statement within. */
9493 while (1)
9494 switch (TREE_CODE (inner_if))
9496 case COND_EXPR:
9497 goto found;
9498 case BIND_EXPR:
9499 inner_if = BIND_EXPR_BODY (inner_if);
9500 break;
9501 case STATEMENT_LIST:
9502 inner_if = expr_last (then_block);
9503 break;
9504 case TRY_FINALLY_EXPR:
9505 case TRY_CATCH_EXPR:
9506 inner_if = TREE_OPERAND (inner_if, 0);
9507 break;
9508 default:
9509 gcc_unreachable ();
9511 found:
9513 if (COND_EXPR_ELSE (inner_if))
9514 warning_at (if_locus, OPT_Wparentheses,
9515 "suggest explicit braces to avoid ambiguous %<else%>");
9518 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9519 SET_EXPR_LOCATION (stmt, if_locus);
9520 add_stmt (stmt);
9523 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9524 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9525 is false for DO loops. INCR is the FOR increment expression. BODY is
9526 the statement controlled by the loop. BLAB is the break label. CLAB is
9527 the continue label. Everything is allowed to be NULL. */
9529 void
9530 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9531 tree blab, tree clab, bool cond_is_first)
9533 tree entry = NULL, exit = NULL, t;
9535 if (flag_cilkplus && contains_array_notation_expr (cond))
9537 error_at (start_locus, "array notation expression cannot be used in a "
9538 "loop%'s condition");
9539 return;
9542 /* If the condition is zero don't generate a loop construct. */
9543 if (cond && integer_zerop (cond))
9545 if (cond_is_first)
9547 t = build_and_jump (&blab);
9548 SET_EXPR_LOCATION (t, start_locus);
9549 add_stmt (t);
9552 else
9554 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9556 /* If we have an exit condition, then we build an IF with gotos either
9557 out of the loop, or to the top of it. If there's no exit condition,
9558 then we just build a jump back to the top. */
9559 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9561 if (cond && !integer_nonzerop (cond))
9563 /* Canonicalize the loop condition to the end. This means
9564 generating a branch to the loop condition. Reuse the
9565 continue label, if possible. */
9566 if (cond_is_first)
9568 if (incr || !clab)
9570 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9571 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9573 else
9574 t = build1 (GOTO_EXPR, void_type_node, clab);
9575 SET_EXPR_LOCATION (t, start_locus);
9576 add_stmt (t);
9579 t = build_and_jump (&blab);
9580 if (cond_is_first)
9581 exit = fold_build3_loc (start_locus,
9582 COND_EXPR, void_type_node, cond, exit, t);
9583 else
9584 exit = fold_build3_loc (input_location,
9585 COND_EXPR, void_type_node, cond, exit, t);
9588 add_stmt (top);
9591 if (body)
9592 add_stmt (body);
9593 if (clab)
9594 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9595 if (incr)
9596 add_stmt (incr);
9597 if (entry)
9598 add_stmt (entry);
9599 if (exit)
9600 add_stmt (exit);
9601 if (blab)
9602 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9605 tree
9606 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9608 bool skip;
9609 tree label = *label_p;
9611 /* In switch statements break is sometimes stylistically used after
9612 a return statement. This can lead to spurious warnings about
9613 control reaching the end of a non-void function when it is
9614 inlined. Note that we are calling block_may_fallthru with
9615 language specific tree nodes; this works because
9616 block_may_fallthru returns true when given something it does not
9617 understand. */
9618 skip = !block_may_fallthru (cur_stmt_list);
9620 if (!label)
9622 if (!skip)
9623 *label_p = label = create_artificial_label (loc);
9625 else if (TREE_CODE (label) == LABEL_DECL)
9627 else switch (TREE_INT_CST_LOW (label))
9629 case 0:
9630 if (is_break)
9631 error_at (loc, "break statement not within loop or switch");
9632 else
9633 error_at (loc, "continue statement not within a loop");
9634 return NULL_TREE;
9636 case 1:
9637 gcc_assert (is_break);
9638 error_at (loc, "break statement used with OpenMP for loop");
9639 return NULL_TREE;
9641 case 2:
9642 if (is_break)
9643 error ("break statement within %<#pragma simd%> loop body");
9644 else
9645 error ("continue statement within %<#pragma simd%> loop body");
9646 return NULL_TREE;
9648 default:
9649 gcc_unreachable ();
9652 if (skip)
9653 return NULL_TREE;
9655 if (!is_break)
9656 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9658 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9661 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9663 static void
9664 emit_side_effect_warnings (location_t loc, tree expr)
9666 if (expr == error_mark_node)
9668 else if (!TREE_SIDE_EFFECTS (expr))
9670 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9671 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9673 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9675 tree r = expr;
9676 location_t cloc = loc;
9677 while (TREE_CODE (r) == COMPOUND_EXPR)
9679 if (EXPR_HAS_LOCATION (r))
9680 cloc = EXPR_LOCATION (r);
9681 r = TREE_OPERAND (r, 1);
9683 if (!TREE_SIDE_EFFECTS (r)
9684 && !VOID_TYPE_P (TREE_TYPE (r))
9685 && !CONVERT_EXPR_P (r)
9686 && !TREE_NO_WARNING (r)
9687 && !TREE_NO_WARNING (expr))
9688 warning_at (cloc, OPT_Wunused_value,
9689 "right-hand operand of comma expression has no effect");
9691 else
9692 warn_if_unused_value (expr, loc);
9695 /* Process an expression as if it were a complete statement. Emit
9696 diagnostics, but do not call ADD_STMT. LOC is the location of the
9697 statement. */
9699 tree
9700 c_process_expr_stmt (location_t loc, tree expr)
9702 tree exprv;
9704 if (!expr)
9705 return NULL_TREE;
9707 expr = c_fully_fold (expr, false, NULL);
9709 if (warn_sequence_point)
9710 verify_sequence_points (expr);
9712 if (TREE_TYPE (expr) != error_mark_node
9713 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9714 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9715 error_at (loc, "expression statement has incomplete type");
9717 /* If we're not processing a statement expression, warn about unused values.
9718 Warnings for statement expressions will be emitted later, once we figure
9719 out which is the result. */
9720 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9721 && warn_unused_value)
9722 emit_side_effect_warnings (loc, expr);
9724 exprv = expr;
9725 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9726 exprv = TREE_OPERAND (exprv, 1);
9727 while (CONVERT_EXPR_P (exprv))
9728 exprv = TREE_OPERAND (exprv, 0);
9729 if (DECL_P (exprv)
9730 || handled_component_p (exprv)
9731 || TREE_CODE (exprv) == ADDR_EXPR)
9732 mark_exp_read (exprv);
9734 /* If the expression is not of a type to which we cannot assign a line
9735 number, wrap the thing in a no-op NOP_EXPR. */
9736 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9738 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9739 SET_EXPR_LOCATION (expr, loc);
9742 return expr;
9745 /* Emit an expression as a statement. LOC is the location of the
9746 expression. */
9748 tree
9749 c_finish_expr_stmt (location_t loc, tree expr)
9751 if (expr)
9752 return add_stmt (c_process_expr_stmt (loc, expr));
9753 else
9754 return NULL;
9757 /* Do the opposite and emit a statement as an expression. To begin,
9758 create a new binding level and return it. */
9760 tree
9761 c_begin_stmt_expr (void)
9763 tree ret;
9765 /* We must force a BLOCK for this level so that, if it is not expanded
9766 later, there is a way to turn off the entire subtree of blocks that
9767 are contained in it. */
9768 keep_next_level ();
9769 ret = c_begin_compound_stmt (true);
9771 c_bindings_start_stmt_expr (c_switch_stack == NULL
9772 ? NULL
9773 : c_switch_stack->bindings);
9775 /* Mark the current statement list as belonging to a statement list. */
9776 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9778 return ret;
9781 /* LOC is the location of the compound statement to which this body
9782 belongs. */
9784 tree
9785 c_finish_stmt_expr (location_t loc, tree body)
9787 tree last, type, tmp, val;
9788 tree *last_p;
9790 body = c_end_compound_stmt (loc, body, true);
9792 c_bindings_end_stmt_expr (c_switch_stack == NULL
9793 ? NULL
9794 : c_switch_stack->bindings);
9796 /* Locate the last statement in BODY. See c_end_compound_stmt
9797 about always returning a BIND_EXPR. */
9798 last_p = &BIND_EXPR_BODY (body);
9799 last = BIND_EXPR_BODY (body);
9801 continue_searching:
9802 if (TREE_CODE (last) == STATEMENT_LIST)
9804 tree_stmt_iterator i;
9806 /* This can happen with degenerate cases like ({ }). No value. */
9807 if (!TREE_SIDE_EFFECTS (last))
9808 return body;
9810 /* If we're supposed to generate side effects warnings, process
9811 all of the statements except the last. */
9812 if (warn_unused_value)
9814 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9816 location_t tloc;
9817 tree t = tsi_stmt (i);
9819 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9820 emit_side_effect_warnings (tloc, t);
9823 else
9824 i = tsi_last (last);
9825 last_p = tsi_stmt_ptr (i);
9826 last = *last_p;
9829 /* If the end of the list is exception related, then the list was split
9830 by a call to push_cleanup. Continue searching. */
9831 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9832 || TREE_CODE (last) == TRY_CATCH_EXPR)
9834 last_p = &TREE_OPERAND (last, 0);
9835 last = *last_p;
9836 goto continue_searching;
9839 if (last == error_mark_node)
9840 return last;
9842 /* In the case that the BIND_EXPR is not necessary, return the
9843 expression out from inside it. */
9844 if (last == BIND_EXPR_BODY (body)
9845 && BIND_EXPR_VARS (body) == NULL)
9847 /* Even if this looks constant, do not allow it in a constant
9848 expression. */
9849 last = c_wrap_maybe_const (last, true);
9850 /* Do not warn if the return value of a statement expression is
9851 unused. */
9852 TREE_NO_WARNING (last) = 1;
9853 return last;
9856 /* Extract the type of said expression. */
9857 type = TREE_TYPE (last);
9859 /* If we're not returning a value at all, then the BIND_EXPR that
9860 we already have is a fine expression to return. */
9861 if (!type || VOID_TYPE_P (type))
9862 return body;
9864 /* Now that we've located the expression containing the value, it seems
9865 silly to make voidify_wrapper_expr repeat the process. Create a
9866 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9867 tmp = create_tmp_var_raw (type, NULL);
9869 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9870 tree_expr_nonnegative_p giving up immediately. */
9871 val = last;
9872 if (TREE_CODE (val) == NOP_EXPR
9873 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9874 val = TREE_OPERAND (val, 0);
9876 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9877 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9880 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9881 SET_EXPR_LOCATION (t, loc);
9882 return t;
9886 /* Begin and end compound statements. This is as simple as pushing
9887 and popping new statement lists from the tree. */
9889 tree
9890 c_begin_compound_stmt (bool do_scope)
9892 tree stmt = push_stmt_list ();
9893 if (do_scope)
9894 push_scope ();
9895 return stmt;
9898 /* End a compound statement. STMT is the statement. LOC is the
9899 location of the compound statement-- this is usually the location
9900 of the opening brace. */
9902 tree
9903 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9905 tree block = NULL;
9907 if (do_scope)
9909 if (c_dialect_objc ())
9910 objc_clear_super_receiver ();
9911 block = pop_scope ();
9914 stmt = pop_stmt_list (stmt);
9915 stmt = c_build_bind_expr (loc, block, stmt);
9917 /* If this compound statement is nested immediately inside a statement
9918 expression, then force a BIND_EXPR to be created. Otherwise we'll
9919 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9920 STATEMENT_LISTs merge, and thus we can lose track of what statement
9921 was really last. */
9922 if (building_stmt_list_p ()
9923 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9924 && TREE_CODE (stmt) != BIND_EXPR)
9926 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9927 TREE_SIDE_EFFECTS (stmt) = 1;
9928 SET_EXPR_LOCATION (stmt, loc);
9931 return stmt;
9934 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9935 when the current scope is exited. EH_ONLY is true when this is not
9936 meant to apply to normal control flow transfer. */
9938 void
9939 push_cleanup (tree decl, tree cleanup, bool eh_only)
9941 enum tree_code code;
9942 tree stmt, list;
9943 bool stmt_expr;
9945 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9946 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9947 add_stmt (stmt);
9948 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9949 list = push_stmt_list ();
9950 TREE_OPERAND (stmt, 0) = list;
9951 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9954 /* Build a binary-operation expression without default conversions.
9955 CODE is the kind of expression to build.
9956 LOCATION is the operator's location.
9957 This function differs from `build' in several ways:
9958 the data type of the result is computed and recorded in it,
9959 warnings are generated if arg data types are invalid,
9960 special handling for addition and subtraction of pointers is known,
9961 and some optimization is done (operations on narrow ints
9962 are done in the narrower type when that gives the same result).
9963 Constant folding is also done before the result is returned.
9965 Note that the operands will never have enumeral types, or function
9966 or array types, because either they will have the default conversions
9967 performed or they have both just been converted to some other type in which
9968 the arithmetic is to be done. */
9970 tree
9971 build_binary_op (location_t location, enum tree_code code,
9972 tree orig_op0, tree orig_op1, int convert_p)
9974 tree type0, type1, orig_type0, orig_type1;
9975 tree eptype;
9976 enum tree_code code0, code1;
9977 tree op0, op1;
9978 tree ret = error_mark_node;
9979 const char *invalid_op_diag;
9980 bool op0_int_operands, op1_int_operands;
9981 bool int_const, int_const_or_overflow, int_operands;
9983 /* Expression code to give to the expression when it is built.
9984 Normally this is CODE, which is what the caller asked for,
9985 but in some special cases we change it. */
9986 enum tree_code resultcode = code;
9988 /* Data type in which the computation is to be performed.
9989 In the simplest cases this is the common type of the arguments. */
9990 tree result_type = NULL;
9992 /* When the computation is in excess precision, the type of the
9993 final EXCESS_PRECISION_EXPR. */
9994 tree semantic_result_type = NULL;
9996 /* Nonzero means operands have already been type-converted
9997 in whatever way is necessary.
9998 Zero means they need to be converted to RESULT_TYPE. */
9999 int converted = 0;
10001 /* Nonzero means create the expression with this type, rather than
10002 RESULT_TYPE. */
10003 tree build_type = 0;
10005 /* Nonzero means after finally constructing the expression
10006 convert it to this type. */
10007 tree final_type = 0;
10009 /* Nonzero if this is an operation like MIN or MAX which can
10010 safely be computed in short if both args are promoted shorts.
10011 Also implies COMMON.
10012 -1 indicates a bitwise operation; this makes a difference
10013 in the exact conditions for when it is safe to do the operation
10014 in a narrower mode. */
10015 int shorten = 0;
10017 /* Nonzero if this is a comparison operation;
10018 if both args are promoted shorts, compare the original shorts.
10019 Also implies COMMON. */
10020 int short_compare = 0;
10022 /* Nonzero if this is a right-shift operation, which can be computed on the
10023 original short and then promoted if the operand is a promoted short. */
10024 int short_shift = 0;
10026 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10027 int common = 0;
10029 /* True means types are compatible as far as ObjC is concerned. */
10030 bool objc_ok;
10032 /* True means this is an arithmetic operation that may need excess
10033 precision. */
10034 bool may_need_excess_precision;
10036 /* True means this is a boolean operation that converts both its
10037 operands to truth-values. */
10038 bool boolean_op = false;
10040 /* Remember whether we're doing / or %. */
10041 bool doing_div_or_mod = false;
10043 /* Remember whether we're doing << or >>. */
10044 bool doing_shift = false;
10046 /* Tree holding instrumentation expression. */
10047 tree instrument_expr = NULL;
10049 if (location == UNKNOWN_LOCATION)
10050 location = input_location;
10052 op0 = orig_op0;
10053 op1 = orig_op1;
10055 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10056 if (op0_int_operands)
10057 op0 = remove_c_maybe_const_expr (op0);
10058 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10059 if (op1_int_operands)
10060 op1 = remove_c_maybe_const_expr (op1);
10061 int_operands = (op0_int_operands && op1_int_operands);
10062 if (int_operands)
10064 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10065 && TREE_CODE (orig_op1) == INTEGER_CST);
10066 int_const = (int_const_or_overflow
10067 && !TREE_OVERFLOW (orig_op0)
10068 && !TREE_OVERFLOW (orig_op1));
10070 else
10071 int_const = int_const_or_overflow = false;
10073 /* Do not apply default conversion in mixed vector/scalar expression. */
10074 if (convert_p
10075 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10076 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10078 op0 = default_conversion (op0);
10079 op1 = default_conversion (op1);
10082 /* When Cilk Plus is enabled and there are array notations inside op0, then
10083 we check to see if there are builtin array notation functions. If
10084 so, then we take on the type of the array notation inside it. */
10085 if (flag_cilkplus && contains_array_notation_expr (op0))
10086 orig_type0 = type0 = find_correct_array_notation_type (op0);
10087 else
10088 orig_type0 = type0 = TREE_TYPE (op0);
10090 if (flag_cilkplus && contains_array_notation_expr (op1))
10091 orig_type1 = type1 = find_correct_array_notation_type (op1);
10092 else
10093 orig_type1 = type1 = TREE_TYPE (op1);
10095 /* The expression codes of the data types of the arguments tell us
10096 whether the arguments are integers, floating, pointers, etc. */
10097 code0 = TREE_CODE (type0);
10098 code1 = TREE_CODE (type1);
10100 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10101 STRIP_TYPE_NOPS (op0);
10102 STRIP_TYPE_NOPS (op1);
10104 /* If an error was already reported for one of the arguments,
10105 avoid reporting another error. */
10107 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10108 return error_mark_node;
10110 if ((invalid_op_diag
10111 = targetm.invalid_binary_op (code, type0, type1)))
10113 error_at (location, invalid_op_diag);
10114 return error_mark_node;
10117 switch (code)
10119 case PLUS_EXPR:
10120 case MINUS_EXPR:
10121 case MULT_EXPR:
10122 case TRUNC_DIV_EXPR:
10123 case CEIL_DIV_EXPR:
10124 case FLOOR_DIV_EXPR:
10125 case ROUND_DIV_EXPR:
10126 case EXACT_DIV_EXPR:
10127 may_need_excess_precision = true;
10128 break;
10129 default:
10130 may_need_excess_precision = false;
10131 break;
10133 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10135 op0 = TREE_OPERAND (op0, 0);
10136 type0 = TREE_TYPE (op0);
10138 else if (may_need_excess_precision
10139 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10141 type0 = eptype;
10142 op0 = convert (eptype, op0);
10144 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10146 op1 = TREE_OPERAND (op1, 0);
10147 type1 = TREE_TYPE (op1);
10149 else if (may_need_excess_precision
10150 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10152 type1 = eptype;
10153 op1 = convert (eptype, op1);
10156 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10158 /* In case when one of the operands of the binary operation is
10159 a vector and another is a scalar -- convert scalar to vector. */
10160 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10162 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10163 true);
10165 switch (convert_flag)
10167 case stv_error:
10168 return error_mark_node;
10169 case stv_firstarg:
10171 bool maybe_const = true;
10172 tree sc;
10173 sc = c_fully_fold (op0, false, &maybe_const);
10174 sc = save_expr (sc);
10175 sc = convert (TREE_TYPE (type1), sc);
10176 op0 = build_vector_from_val (type1, sc);
10177 if (!maybe_const)
10178 op0 = c_wrap_maybe_const (op0, true);
10179 orig_type0 = type0 = TREE_TYPE (op0);
10180 code0 = TREE_CODE (type0);
10181 converted = 1;
10182 break;
10184 case stv_secondarg:
10186 bool maybe_const = true;
10187 tree sc;
10188 sc = c_fully_fold (op1, false, &maybe_const);
10189 sc = save_expr (sc);
10190 sc = convert (TREE_TYPE (type0), sc);
10191 op1 = build_vector_from_val (type0, sc);
10192 if (!maybe_const)
10193 op1 = c_wrap_maybe_const (op1, true);
10194 orig_type1 = type1 = TREE_TYPE (op1);
10195 code1 = TREE_CODE (type1);
10196 converted = 1;
10197 break;
10199 default:
10200 break;
10204 switch (code)
10206 case PLUS_EXPR:
10207 /* Handle the pointer + int case. */
10208 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10210 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10211 goto return_build_binary_op;
10213 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10215 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10216 goto return_build_binary_op;
10218 else
10219 common = 1;
10220 break;
10222 case MINUS_EXPR:
10223 /* Subtraction of two similar pointers.
10224 We must subtract them as integers, then divide by object size. */
10225 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10226 && comp_target_types (location, type0, type1))
10228 ret = pointer_diff (location, op0, op1);
10229 goto return_build_binary_op;
10231 /* Handle pointer minus int. Just like pointer plus int. */
10232 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10234 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10235 goto return_build_binary_op;
10237 else
10238 common = 1;
10239 break;
10241 case MULT_EXPR:
10242 common = 1;
10243 break;
10245 case TRUNC_DIV_EXPR:
10246 case CEIL_DIV_EXPR:
10247 case FLOOR_DIV_EXPR:
10248 case ROUND_DIV_EXPR:
10249 case EXACT_DIV_EXPR:
10250 doing_div_or_mod = true;
10251 warn_for_div_by_zero (location, op1);
10253 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10254 || code0 == FIXED_POINT_TYPE
10255 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10256 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10257 || code1 == FIXED_POINT_TYPE
10258 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10260 enum tree_code tcode0 = code0, tcode1 = code1;
10262 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10263 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10264 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10265 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10267 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10268 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10269 resultcode = RDIV_EXPR;
10270 else
10271 /* Although it would be tempting to shorten always here, that
10272 loses on some targets, since the modulo instruction is
10273 undefined if the quotient can't be represented in the
10274 computation mode. We shorten only if unsigned or if
10275 dividing by something we know != -1. */
10276 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10277 || (TREE_CODE (op1) == INTEGER_CST
10278 && !integer_all_onesp (op1)));
10279 common = 1;
10281 break;
10283 case BIT_AND_EXPR:
10284 case BIT_IOR_EXPR:
10285 case BIT_XOR_EXPR:
10286 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10287 shorten = -1;
10288 /* Allow vector types which are not floating point types. */
10289 else if (code0 == VECTOR_TYPE
10290 && code1 == VECTOR_TYPE
10291 && !VECTOR_FLOAT_TYPE_P (type0)
10292 && !VECTOR_FLOAT_TYPE_P (type1))
10293 common = 1;
10294 break;
10296 case TRUNC_MOD_EXPR:
10297 case FLOOR_MOD_EXPR:
10298 doing_div_or_mod = true;
10299 warn_for_div_by_zero (location, op1);
10301 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10302 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10303 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10304 common = 1;
10305 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10307 /* Although it would be tempting to shorten always here, that loses
10308 on some targets, since the modulo instruction is undefined if the
10309 quotient can't be represented in the computation mode. We shorten
10310 only if unsigned or if dividing by something we know != -1. */
10311 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10312 || (TREE_CODE (op1) == INTEGER_CST
10313 && !integer_all_onesp (op1)));
10314 common = 1;
10316 break;
10318 case TRUTH_ANDIF_EXPR:
10319 case TRUTH_ORIF_EXPR:
10320 case TRUTH_AND_EXPR:
10321 case TRUTH_OR_EXPR:
10322 case TRUTH_XOR_EXPR:
10323 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10324 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10325 || code0 == FIXED_POINT_TYPE)
10326 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10327 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10328 || code1 == FIXED_POINT_TYPE))
10330 /* Result of these operations is always an int,
10331 but that does not mean the operands should be
10332 converted to ints! */
10333 result_type = integer_type_node;
10334 if (op0_int_operands)
10336 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10337 op0 = remove_c_maybe_const_expr (op0);
10339 else
10340 op0 = c_objc_common_truthvalue_conversion (location, op0);
10341 if (op1_int_operands)
10343 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10344 op1 = remove_c_maybe_const_expr (op1);
10346 else
10347 op1 = c_objc_common_truthvalue_conversion (location, op1);
10348 converted = 1;
10349 boolean_op = true;
10351 if (code == TRUTH_ANDIF_EXPR)
10353 int_const_or_overflow = (int_operands
10354 && TREE_CODE (orig_op0) == INTEGER_CST
10355 && (op0 == truthvalue_false_node
10356 || TREE_CODE (orig_op1) == INTEGER_CST));
10357 int_const = (int_const_or_overflow
10358 && !TREE_OVERFLOW (orig_op0)
10359 && (op0 == truthvalue_false_node
10360 || !TREE_OVERFLOW (orig_op1)));
10362 else if (code == TRUTH_ORIF_EXPR)
10364 int_const_or_overflow = (int_operands
10365 && TREE_CODE (orig_op0) == INTEGER_CST
10366 && (op0 == truthvalue_true_node
10367 || TREE_CODE (orig_op1) == INTEGER_CST));
10368 int_const = (int_const_or_overflow
10369 && !TREE_OVERFLOW (orig_op0)
10370 && (op0 == truthvalue_true_node
10371 || !TREE_OVERFLOW (orig_op1)));
10373 break;
10375 /* Shift operations: result has same type as first operand;
10376 always convert second operand to int.
10377 Also set SHORT_SHIFT if shifting rightward. */
10379 case RSHIFT_EXPR:
10380 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10381 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10383 result_type = type0;
10384 converted = 1;
10386 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10387 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10388 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10389 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10391 result_type = type0;
10392 converted = 1;
10394 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10395 && code1 == INTEGER_TYPE)
10397 doing_shift = true;
10398 if (TREE_CODE (op1) == INTEGER_CST)
10400 if (tree_int_cst_sgn (op1) < 0)
10402 int_const = false;
10403 if (c_inhibit_evaluation_warnings == 0)
10404 warning (0, "right shift count is negative");
10406 else
10408 if (!integer_zerop (op1))
10409 short_shift = 1;
10411 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10413 int_const = false;
10414 if (c_inhibit_evaluation_warnings == 0)
10415 warning (0, "right shift count >= width of type");
10420 /* Use the type of the value to be shifted. */
10421 result_type = type0;
10422 /* Convert the non vector shift-count to an integer, regardless
10423 of size of value being shifted. */
10424 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10425 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10426 op1 = convert (integer_type_node, op1);
10427 /* Avoid converting op1 to result_type later. */
10428 converted = 1;
10430 break;
10432 case LSHIFT_EXPR:
10433 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10434 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10436 result_type = type0;
10437 converted = 1;
10439 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10440 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10441 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10442 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10444 result_type = type0;
10445 converted = 1;
10447 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10448 && code1 == INTEGER_TYPE)
10450 doing_shift = true;
10451 if (TREE_CODE (op1) == INTEGER_CST)
10453 if (tree_int_cst_sgn (op1) < 0)
10455 int_const = false;
10456 if (c_inhibit_evaluation_warnings == 0)
10457 warning (0, "left shift count is negative");
10460 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10462 int_const = false;
10463 if (c_inhibit_evaluation_warnings == 0)
10464 warning (0, "left shift count >= width of type");
10468 /* Use the type of the value to be shifted. */
10469 result_type = type0;
10470 /* Convert the non vector shift-count to an integer, regardless
10471 of size of value being shifted. */
10472 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10473 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10474 op1 = convert (integer_type_node, op1);
10475 /* Avoid converting op1 to result_type later. */
10476 converted = 1;
10478 break;
10480 case EQ_EXPR:
10481 case NE_EXPR:
10482 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10484 tree intt;
10485 if (!vector_types_compatible_elements_p (type0, type1))
10487 error_at (location, "comparing vectors with different "
10488 "element types");
10489 return error_mark_node;
10492 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10494 error_at (location, "comparing vectors with different "
10495 "number of elements");
10496 return error_mark_node;
10499 /* Always construct signed integer vector type. */
10500 intt = c_common_type_for_size (GET_MODE_BITSIZE
10501 (TYPE_MODE (TREE_TYPE (type0))), 0);
10502 result_type = build_opaque_vector_type (intt,
10503 TYPE_VECTOR_SUBPARTS (type0));
10504 converted = 1;
10505 break;
10507 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10508 warning_at (location,
10509 OPT_Wfloat_equal,
10510 "comparing floating point with == or != is unsafe");
10511 /* Result of comparison is always int,
10512 but don't convert the args to int! */
10513 build_type = integer_type_node;
10514 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10515 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10516 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10517 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10518 short_compare = 1;
10519 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10521 if (TREE_CODE (op0) == ADDR_EXPR
10522 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10524 if (code == EQ_EXPR)
10525 warning_at (location,
10526 OPT_Waddress,
10527 "the comparison will always evaluate as %<false%> "
10528 "for the address of %qD will never be NULL",
10529 TREE_OPERAND (op0, 0));
10530 else
10531 warning_at (location,
10532 OPT_Waddress,
10533 "the comparison will always evaluate as %<true%> "
10534 "for the address of %qD will never be NULL",
10535 TREE_OPERAND (op0, 0));
10537 result_type = type0;
10539 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10541 if (TREE_CODE (op1) == ADDR_EXPR
10542 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10544 if (code == EQ_EXPR)
10545 warning_at (location,
10546 OPT_Waddress,
10547 "the comparison will always evaluate as %<false%> "
10548 "for the address of %qD will never be NULL",
10549 TREE_OPERAND (op1, 0));
10550 else
10551 warning_at (location,
10552 OPT_Waddress,
10553 "the comparison will always evaluate as %<true%> "
10554 "for the address of %qD will never be NULL",
10555 TREE_OPERAND (op1, 0));
10557 result_type = type1;
10559 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10561 tree tt0 = TREE_TYPE (type0);
10562 tree tt1 = TREE_TYPE (type1);
10563 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10564 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10565 addr_space_t as_common = ADDR_SPACE_GENERIC;
10567 /* Anything compares with void *. void * compares with anything.
10568 Otherwise, the targets must be compatible
10569 and both must be object or both incomplete. */
10570 if (comp_target_types (location, type0, type1))
10571 result_type = common_pointer_type (type0, type1);
10572 else if (!addr_space_superset (as0, as1, &as_common))
10574 error_at (location, "comparison of pointers to "
10575 "disjoint address spaces");
10576 return error_mark_node;
10578 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10580 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10581 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10582 "comparison of %<void *%> with function pointer");
10584 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10586 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10587 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10588 "comparison of %<void *%> with function pointer");
10590 else
10591 /* Avoid warning about the volatile ObjC EH puts on decls. */
10592 if (!objc_ok)
10593 pedwarn (location, 0,
10594 "comparison of distinct pointer types lacks a cast");
10596 if (result_type == NULL_TREE)
10598 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10599 result_type = build_pointer_type
10600 (build_qualified_type (void_type_node, qual));
10603 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10605 result_type = type0;
10606 pedwarn (location, 0, "comparison between pointer and integer");
10608 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10610 result_type = type1;
10611 pedwarn (location, 0, "comparison between pointer and integer");
10613 break;
10615 case LE_EXPR:
10616 case GE_EXPR:
10617 case LT_EXPR:
10618 case GT_EXPR:
10619 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10621 tree intt;
10622 if (!vector_types_compatible_elements_p (type0, type1))
10624 error_at (location, "comparing vectors with different "
10625 "element types");
10626 return error_mark_node;
10629 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10631 error_at (location, "comparing vectors with different "
10632 "number of elements");
10633 return error_mark_node;
10636 /* Always construct signed integer vector type. */
10637 intt = c_common_type_for_size (GET_MODE_BITSIZE
10638 (TYPE_MODE (TREE_TYPE (type0))), 0);
10639 result_type = build_opaque_vector_type (intt,
10640 TYPE_VECTOR_SUBPARTS (type0));
10641 converted = 1;
10642 break;
10644 build_type = integer_type_node;
10645 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10646 || code0 == FIXED_POINT_TYPE)
10647 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10648 || code1 == FIXED_POINT_TYPE))
10649 short_compare = 1;
10650 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10652 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10653 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10654 addr_space_t as_common;
10656 if (comp_target_types (location, type0, type1))
10658 result_type = common_pointer_type (type0, type1);
10659 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10660 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10661 pedwarn (location, 0,
10662 "comparison of complete and incomplete pointers");
10663 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10664 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10665 "ordered comparisons of pointers to functions");
10666 else if (null_pointer_constant_p (orig_op0)
10667 || null_pointer_constant_p (orig_op1))
10668 warning_at (location, OPT_Wextra,
10669 "ordered comparison of pointer with null pointer");
10672 else if (!addr_space_superset (as0, as1, &as_common))
10674 error_at (location, "comparison of pointers to "
10675 "disjoint address spaces");
10676 return error_mark_node;
10678 else
10680 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10681 result_type = build_pointer_type
10682 (build_qualified_type (void_type_node, qual));
10683 pedwarn (location, 0,
10684 "comparison of distinct pointer types lacks a cast");
10687 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10689 result_type = type0;
10690 if (pedantic)
10691 pedwarn (location, OPT_Wpedantic,
10692 "ordered comparison of pointer with integer zero");
10693 else if (extra_warnings)
10694 warning_at (location, OPT_Wextra,
10695 "ordered comparison of pointer with integer zero");
10697 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10699 result_type = type1;
10700 if (pedantic)
10701 pedwarn (location, OPT_Wpedantic,
10702 "ordered comparison of pointer with integer zero");
10703 else if (extra_warnings)
10704 warning_at (location, OPT_Wextra,
10705 "ordered comparison of pointer with integer zero");
10707 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10709 result_type = type0;
10710 pedwarn (location, 0, "comparison between pointer and integer");
10712 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10714 result_type = type1;
10715 pedwarn (location, 0, "comparison between pointer and integer");
10717 break;
10719 default:
10720 gcc_unreachable ();
10723 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10724 return error_mark_node;
10726 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10727 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10728 || !vector_types_compatible_elements_p (type0, type1)))
10730 binary_op_error (location, code, type0, type1);
10731 return error_mark_node;
10734 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10735 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10737 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10738 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10740 bool first_complex = (code0 == COMPLEX_TYPE);
10741 bool second_complex = (code1 == COMPLEX_TYPE);
10742 int none_complex = (!first_complex && !second_complex);
10744 if (shorten || common || short_compare)
10746 result_type = c_common_type (type0, type1);
10747 do_warn_double_promotion (result_type, type0, type1,
10748 "implicit conversion from %qT to %qT "
10749 "to match other operand of binary "
10750 "expression",
10751 location);
10752 if (result_type == error_mark_node)
10753 return error_mark_node;
10756 if (first_complex != second_complex
10757 && (code == PLUS_EXPR
10758 || code == MINUS_EXPR
10759 || code == MULT_EXPR
10760 || (code == TRUNC_DIV_EXPR && first_complex))
10761 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10762 && flag_signed_zeros)
10764 /* An operation on mixed real/complex operands must be
10765 handled specially, but the language-independent code can
10766 more easily optimize the plain complex arithmetic if
10767 -fno-signed-zeros. */
10768 tree real_type = TREE_TYPE (result_type);
10769 tree real, imag;
10770 if (type0 != orig_type0 || type1 != orig_type1)
10772 gcc_assert (may_need_excess_precision && common);
10773 semantic_result_type = c_common_type (orig_type0, orig_type1);
10775 if (first_complex)
10777 if (TREE_TYPE (op0) != result_type)
10778 op0 = convert_and_check (location, result_type, op0);
10779 if (TREE_TYPE (op1) != real_type)
10780 op1 = convert_and_check (location, real_type, op1);
10782 else
10784 if (TREE_TYPE (op0) != real_type)
10785 op0 = convert_and_check (location, real_type, op0);
10786 if (TREE_TYPE (op1) != result_type)
10787 op1 = convert_and_check (location, result_type, op1);
10789 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10790 return error_mark_node;
10791 if (first_complex)
10793 op0 = c_save_expr (op0);
10794 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10795 op0, 1);
10796 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10797 op0, 1);
10798 switch (code)
10800 case MULT_EXPR:
10801 case TRUNC_DIV_EXPR:
10802 op1 = c_save_expr (op1);
10803 imag = build2 (resultcode, real_type, imag, op1);
10804 /* Fall through. */
10805 case PLUS_EXPR:
10806 case MINUS_EXPR:
10807 real = build2 (resultcode, real_type, real, op1);
10808 break;
10809 default:
10810 gcc_unreachable();
10813 else
10815 op1 = c_save_expr (op1);
10816 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10817 op1, 1);
10818 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10819 op1, 1);
10820 switch (code)
10822 case MULT_EXPR:
10823 op0 = c_save_expr (op0);
10824 imag = build2 (resultcode, real_type, op0, imag);
10825 /* Fall through. */
10826 case PLUS_EXPR:
10827 real = build2 (resultcode, real_type, op0, real);
10828 break;
10829 case MINUS_EXPR:
10830 real = build2 (resultcode, real_type, op0, real);
10831 imag = build1 (NEGATE_EXPR, real_type, imag);
10832 break;
10833 default:
10834 gcc_unreachable();
10837 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10838 goto return_build_binary_op;
10841 /* For certain operations (which identify themselves by shorten != 0)
10842 if both args were extended from the same smaller type,
10843 do the arithmetic in that type and then extend.
10845 shorten !=0 and !=1 indicates a bitwise operation.
10846 For them, this optimization is safe only if
10847 both args are zero-extended or both are sign-extended.
10848 Otherwise, we might change the result.
10849 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10850 but calculated in (unsigned short) it would be (unsigned short)-1. */
10852 if (shorten && none_complex)
10854 final_type = result_type;
10855 result_type = shorten_binary_op (result_type, op0, op1,
10856 shorten == -1);
10859 /* Shifts can be shortened if shifting right. */
10861 if (short_shift)
10863 int unsigned_arg;
10864 tree arg0 = get_narrower (op0, &unsigned_arg);
10866 final_type = result_type;
10868 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10869 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10871 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10872 && tree_int_cst_sgn (op1) > 0
10873 /* We can shorten only if the shift count is less than the
10874 number of bits in the smaller type size. */
10875 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10876 /* We cannot drop an unsigned shift after sign-extension. */
10877 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10879 /* Do an unsigned shift if the operand was zero-extended. */
10880 result_type
10881 = c_common_signed_or_unsigned_type (unsigned_arg,
10882 TREE_TYPE (arg0));
10883 /* Convert value-to-be-shifted to that type. */
10884 if (TREE_TYPE (op0) != result_type)
10885 op0 = convert (result_type, op0);
10886 converted = 1;
10890 /* Comparison operations are shortened too but differently.
10891 They identify themselves by setting short_compare = 1. */
10893 if (short_compare)
10895 /* Don't write &op0, etc., because that would prevent op0
10896 from being kept in a register.
10897 Instead, make copies of the our local variables and
10898 pass the copies by reference, then copy them back afterward. */
10899 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10900 enum tree_code xresultcode = resultcode;
10901 tree val
10902 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10903 &xresultcode);
10905 if (val != 0)
10907 ret = val;
10908 goto return_build_binary_op;
10911 op0 = xop0, op1 = xop1;
10912 converted = 1;
10913 resultcode = xresultcode;
10915 if (c_inhibit_evaluation_warnings == 0)
10917 bool op0_maybe_const = true;
10918 bool op1_maybe_const = true;
10919 tree orig_op0_folded, orig_op1_folded;
10921 if (in_late_binary_op)
10923 orig_op0_folded = orig_op0;
10924 orig_op1_folded = orig_op1;
10926 else
10928 /* Fold for the sake of possible warnings, as in
10929 build_conditional_expr. This requires the
10930 "original" values to be folded, not just op0 and
10931 op1. */
10932 c_inhibit_evaluation_warnings++;
10933 op0 = c_fully_fold (op0, require_constant_value,
10934 &op0_maybe_const);
10935 op1 = c_fully_fold (op1, require_constant_value,
10936 &op1_maybe_const);
10937 c_inhibit_evaluation_warnings--;
10938 orig_op0_folded = c_fully_fold (orig_op0,
10939 require_constant_value,
10940 NULL);
10941 orig_op1_folded = c_fully_fold (orig_op1,
10942 require_constant_value,
10943 NULL);
10946 if (warn_sign_compare)
10947 warn_for_sign_compare (location, orig_op0_folded,
10948 orig_op1_folded, op0, op1,
10949 result_type, resultcode);
10950 if (!in_late_binary_op && !int_operands)
10952 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10953 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10954 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10955 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10961 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10962 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10963 Then the expression will be built.
10964 It will be given type FINAL_TYPE if that is nonzero;
10965 otherwise, it will be given type RESULT_TYPE. */
10967 if (!result_type)
10969 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10970 return error_mark_node;
10973 if (build_type == NULL_TREE)
10975 build_type = result_type;
10976 if ((type0 != orig_type0 || type1 != orig_type1)
10977 && !boolean_op)
10979 gcc_assert (may_need_excess_precision && common);
10980 semantic_result_type = c_common_type (orig_type0, orig_type1);
10984 if (!converted)
10986 op0 = ep_convert_and_check (location, result_type, op0,
10987 semantic_result_type);
10988 op1 = ep_convert_and_check (location, result_type, op1,
10989 semantic_result_type);
10991 /* This can happen if one operand has a vector type, and the other
10992 has a different type. */
10993 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10994 return error_mark_node;
10997 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
10998 && current_function_decl != 0
10999 && !lookup_attribute ("no_sanitize_undefined",
11000 DECL_ATTRIBUTES (current_function_decl))
11001 && (doing_div_or_mod || doing_shift))
11003 /* OP0 and/or OP1 might have side-effects. */
11004 op0 = c_save_expr (op0);
11005 op1 = c_save_expr (op1);
11006 op0 = c_fully_fold (op0, false, NULL);
11007 op1 = c_fully_fold (op1, false, NULL);
11008 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
11009 instrument_expr = ubsan_instrument_division (location, op0, op1);
11010 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11011 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11014 /* Treat expressions in initializers specially as they can't trap. */
11015 if (int_const_or_overflow)
11016 ret = (require_constant_value
11017 ? fold_build2_initializer_loc (location, resultcode, build_type,
11018 op0, op1)
11019 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11020 else
11021 ret = build2 (resultcode, build_type, op0, op1);
11022 if (final_type != 0)
11023 ret = convert (final_type, ret);
11025 return_build_binary_op:
11026 gcc_assert (ret != error_mark_node);
11027 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11028 ret = (int_operands
11029 ? note_integer_operands (ret)
11030 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11031 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11032 && !in_late_binary_op)
11033 ret = note_integer_operands (ret);
11034 if (semantic_result_type)
11035 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11036 protected_set_expr_location (ret, location);
11038 if (instrument_expr != NULL)
11039 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11040 instrument_expr, ret);
11042 return ret;
11046 /* Convert EXPR to be a truth-value, validating its type for this
11047 purpose. LOCATION is the source location for the expression. */
11049 tree
11050 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11052 bool int_const, int_operands;
11054 switch (TREE_CODE (TREE_TYPE (expr)))
11056 case ARRAY_TYPE:
11057 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11058 return error_mark_node;
11060 case RECORD_TYPE:
11061 error_at (location, "used struct type value where scalar is required");
11062 return error_mark_node;
11064 case UNION_TYPE:
11065 error_at (location, "used union type value where scalar is required");
11066 return error_mark_node;
11068 case VOID_TYPE:
11069 error_at (location, "void value not ignored as it ought to be");
11070 return error_mark_node;
11072 case FUNCTION_TYPE:
11073 gcc_unreachable ();
11075 case VECTOR_TYPE:
11076 error_at (location, "used vector type where scalar is required");
11077 return error_mark_node;
11079 default:
11080 break;
11083 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11084 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11085 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11087 expr = remove_c_maybe_const_expr (expr);
11088 expr = build2 (NE_EXPR, integer_type_node, expr,
11089 convert (TREE_TYPE (expr), integer_zero_node));
11090 expr = note_integer_operands (expr);
11092 else
11093 /* ??? Should we also give an error for vectors rather than leaving
11094 those to give errors later? */
11095 expr = c_common_truthvalue_conversion (location, expr);
11097 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11099 if (TREE_OVERFLOW (expr))
11100 return expr;
11101 else
11102 return note_integer_operands (expr);
11104 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11105 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11106 return expr;
11110 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11111 required. */
11113 tree
11114 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11116 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11118 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11119 /* Executing a compound literal inside a function reinitializes
11120 it. */
11121 if (!TREE_STATIC (decl))
11122 *se = true;
11123 return decl;
11125 else
11126 return expr;
11129 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11131 tree
11132 c_begin_omp_parallel (void)
11134 tree block;
11136 keep_next_level ();
11137 block = c_begin_compound_stmt (true);
11139 return block;
11142 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11143 statement. LOC is the location of the OMP_PARALLEL. */
11145 tree
11146 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11148 tree stmt;
11150 block = c_end_compound_stmt (loc, block, true);
11152 stmt = make_node (OMP_PARALLEL);
11153 TREE_TYPE (stmt) = void_type_node;
11154 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11155 OMP_PARALLEL_BODY (stmt) = block;
11156 SET_EXPR_LOCATION (stmt, loc);
11158 return add_stmt (stmt);
11161 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11163 tree
11164 c_begin_omp_task (void)
11166 tree block;
11168 keep_next_level ();
11169 block = c_begin_compound_stmt (true);
11171 return block;
11174 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11175 statement. LOC is the location of the #pragma. */
11177 tree
11178 c_finish_omp_task (location_t loc, tree clauses, tree block)
11180 tree stmt;
11182 block = c_end_compound_stmt (loc, block, true);
11184 stmt = make_node (OMP_TASK);
11185 TREE_TYPE (stmt) = void_type_node;
11186 OMP_TASK_CLAUSES (stmt) = clauses;
11187 OMP_TASK_BODY (stmt) = block;
11188 SET_EXPR_LOCATION (stmt, loc);
11190 return add_stmt (stmt);
11193 /* Generate GOMP_cancel call for #pragma omp cancel. */
11195 void
11196 c_finish_omp_cancel (location_t loc, tree clauses)
11198 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11199 int mask = 0;
11200 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11201 mask = 1;
11202 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11203 mask = 2;
11204 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11205 mask = 4;
11206 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11207 mask = 8;
11208 else
11210 error_at (loc, "%<#pragma omp cancel must specify one of "
11211 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11212 "clauses");
11213 return;
11215 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11216 if (ifc != NULL_TREE)
11218 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11219 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11220 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11221 build_zero_cst (type));
11223 else
11224 ifc = boolean_true_node;
11225 tree stmt = build_call_expr_loc (loc, fn, 2,
11226 build_int_cst (integer_type_node, mask),
11227 ifc);
11228 add_stmt (stmt);
11231 /* Generate GOMP_cancellation_point call for
11232 #pragma omp cancellation point. */
11234 void
11235 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11237 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11238 int mask = 0;
11239 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11240 mask = 1;
11241 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11242 mask = 2;
11243 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11244 mask = 4;
11245 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11246 mask = 8;
11247 else
11249 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11250 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11251 "clauses");
11252 return;
11254 tree stmt = build_call_expr_loc (loc, fn, 1,
11255 build_int_cst (integer_type_node, mask));
11256 add_stmt (stmt);
11259 /* Helper function for handle_omp_array_sections. Called recursively
11260 to handle multiple array-section-subscripts. C is the clause,
11261 T current expression (initially OMP_CLAUSE_DECL), which is either
11262 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11263 expression if specified, TREE_VALUE length expression if specified,
11264 TREE_CHAIN is what it has been specified after, or some decl.
11265 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11266 set to true if any of the array-section-subscript could have length
11267 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11268 first array-section-subscript which is known not to have length
11269 of one. Given say:
11270 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11271 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11272 all are or may have length of 1, array-section-subscript [:2] is the
11273 first one knonwn not to have length 1. For array-section-subscript
11274 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11275 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11276 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11277 case though, as some lengths could be zero. */
11279 static tree
11280 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11281 bool &maybe_zero_len, unsigned int &first_non_one)
11283 tree ret, low_bound, length, type;
11284 if (TREE_CODE (t) != TREE_LIST)
11286 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11287 return error_mark_node;
11288 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11290 if (DECL_P (t))
11291 error_at (OMP_CLAUSE_LOCATION (c),
11292 "%qD is not a variable in %qs clause", t,
11293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11294 else
11295 error_at (OMP_CLAUSE_LOCATION (c),
11296 "%qE is not a variable in %qs clause", t,
11297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11298 return error_mark_node;
11300 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11301 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11303 error_at (OMP_CLAUSE_LOCATION (c),
11304 "%qD is threadprivate variable in %qs clause", t,
11305 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11306 return error_mark_node;
11308 return t;
11311 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11312 maybe_zero_len, first_non_one);
11313 if (ret == error_mark_node || ret == NULL_TREE)
11314 return ret;
11316 type = TREE_TYPE (ret);
11317 low_bound = TREE_PURPOSE (t);
11318 length = TREE_VALUE (t);
11320 if (low_bound == error_mark_node || length == error_mark_node)
11321 return error_mark_node;
11323 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11325 error_at (OMP_CLAUSE_LOCATION (c),
11326 "low bound %qE of array section does not have integral type",
11327 low_bound);
11328 return error_mark_node;
11330 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11332 error_at (OMP_CLAUSE_LOCATION (c),
11333 "length %qE of array section does not have integral type",
11334 length);
11335 return error_mark_node;
11337 if (low_bound
11338 && TREE_CODE (low_bound) == INTEGER_CST
11339 && TYPE_PRECISION (TREE_TYPE (low_bound))
11340 > TYPE_PRECISION (sizetype))
11341 low_bound = fold_convert (sizetype, low_bound);
11342 if (length
11343 && TREE_CODE (length) == INTEGER_CST
11344 && TYPE_PRECISION (TREE_TYPE (length))
11345 > TYPE_PRECISION (sizetype))
11346 length = fold_convert (sizetype, length);
11347 if (low_bound == NULL_TREE)
11348 low_bound = integer_zero_node;
11350 if (length != NULL_TREE)
11352 if (!integer_nonzerop (length))
11353 maybe_zero_len = true;
11354 if (first_non_one == types.length ()
11355 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11356 first_non_one++;
11358 if (TREE_CODE (type) == ARRAY_TYPE)
11360 if (length == NULL_TREE
11361 && (TYPE_DOMAIN (type) == NULL_TREE
11362 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11364 error_at (OMP_CLAUSE_LOCATION (c),
11365 "for unknown bound array type length expression must "
11366 "be specified");
11367 return error_mark_node;
11369 if (TREE_CODE (low_bound) == INTEGER_CST
11370 && tree_int_cst_sgn (low_bound) == -1)
11372 error_at (OMP_CLAUSE_LOCATION (c),
11373 "negative low bound in array section in %qs clause",
11374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11375 return error_mark_node;
11377 if (length != NULL_TREE
11378 && TREE_CODE (length) == INTEGER_CST
11379 && tree_int_cst_sgn (length) == -1)
11381 error_at (OMP_CLAUSE_LOCATION (c),
11382 "negative length in array section in %qs clause",
11383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11384 return error_mark_node;
11386 if (TYPE_DOMAIN (type)
11387 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11388 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11389 == INTEGER_CST)
11391 tree size = size_binop (PLUS_EXPR,
11392 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11393 size_one_node);
11394 if (TREE_CODE (low_bound) == INTEGER_CST)
11396 if (tree_int_cst_lt (size, low_bound))
11398 error_at (OMP_CLAUSE_LOCATION (c),
11399 "low bound %qE above array section size "
11400 "in %qs clause", low_bound,
11401 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11402 return error_mark_node;
11404 if (tree_int_cst_equal (size, low_bound))
11405 maybe_zero_len = true;
11406 else if (length == NULL_TREE
11407 && first_non_one == types.length ()
11408 && tree_int_cst_equal
11409 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11410 low_bound))
11411 first_non_one++;
11413 else if (length == NULL_TREE)
11415 maybe_zero_len = true;
11416 if (first_non_one == types.length ())
11417 first_non_one++;
11419 if (length && TREE_CODE (length) == INTEGER_CST)
11421 if (tree_int_cst_lt (size, length))
11423 error_at (OMP_CLAUSE_LOCATION (c),
11424 "length %qE above array section size "
11425 "in %qs clause", length,
11426 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11427 return error_mark_node;
11429 if (TREE_CODE (low_bound) == INTEGER_CST)
11431 tree lbpluslen
11432 = size_binop (PLUS_EXPR,
11433 fold_convert (sizetype, low_bound),
11434 fold_convert (sizetype, length));
11435 if (TREE_CODE (lbpluslen) == INTEGER_CST
11436 && tree_int_cst_lt (size, lbpluslen))
11438 error_at (OMP_CLAUSE_LOCATION (c),
11439 "high bound %qE above array section size "
11440 "in %qs clause", lbpluslen,
11441 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11442 return error_mark_node;
11447 else if (length == NULL_TREE)
11449 maybe_zero_len = true;
11450 if (first_non_one == types.length ())
11451 first_non_one++;
11454 /* For [lb:] we will need to evaluate lb more than once. */
11455 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11457 tree lb = c_save_expr (low_bound);
11458 if (lb != low_bound)
11460 TREE_PURPOSE (t) = lb;
11461 low_bound = lb;
11465 else if (TREE_CODE (type) == POINTER_TYPE)
11467 if (length == NULL_TREE)
11469 error_at (OMP_CLAUSE_LOCATION (c),
11470 "for pointer type length expression must be specified");
11471 return error_mark_node;
11473 /* If there is a pointer type anywhere but in the very first
11474 array-section-subscript, the array section can't be contiguous. */
11475 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11476 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11478 error_at (OMP_CLAUSE_LOCATION (c),
11479 "array section is not contiguous in %qs clause",
11480 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11481 return error_mark_node;
11484 else
11486 error_at (OMP_CLAUSE_LOCATION (c),
11487 "%qE does not have pointer or array type", ret);
11488 return error_mark_node;
11490 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11491 types.safe_push (TREE_TYPE (ret));
11492 /* We will need to evaluate lb more than once. */
11493 tree lb = c_save_expr (low_bound);
11494 if (lb != low_bound)
11496 TREE_PURPOSE (t) = lb;
11497 low_bound = lb;
11499 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11500 return ret;
11503 /* Handle array sections for clause C. */
11505 static bool
11506 handle_omp_array_sections (tree c)
11508 bool maybe_zero_len = false;
11509 unsigned int first_non_one = 0;
11510 vec<tree> types = vNULL;
11511 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11512 maybe_zero_len, first_non_one);
11513 if (first == error_mark_node)
11515 types.release ();
11516 return true;
11518 if (first == NULL_TREE)
11520 types.release ();
11521 return false;
11523 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11525 tree t = OMP_CLAUSE_DECL (c);
11526 tree tem = NULL_TREE;
11527 types.release ();
11528 /* Need to evaluate side effects in the length expressions
11529 if any. */
11530 while (TREE_CODE (t) == TREE_LIST)
11532 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11534 if (tem == NULL_TREE)
11535 tem = TREE_VALUE (t);
11536 else
11537 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11538 TREE_VALUE (t), tem);
11540 t = TREE_CHAIN (t);
11542 if (tem)
11543 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11544 first = c_fully_fold (first, false, NULL);
11545 OMP_CLAUSE_DECL (c) = first;
11547 else
11549 unsigned int num = types.length (), i;
11550 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11551 tree condition = NULL_TREE;
11553 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11554 maybe_zero_len = true;
11556 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11557 t = TREE_CHAIN (t))
11559 tree low_bound = TREE_PURPOSE (t);
11560 tree length = TREE_VALUE (t);
11562 i--;
11563 if (low_bound
11564 && TREE_CODE (low_bound) == INTEGER_CST
11565 && TYPE_PRECISION (TREE_TYPE (low_bound))
11566 > TYPE_PRECISION (sizetype))
11567 low_bound = fold_convert (sizetype, low_bound);
11568 if (length
11569 && TREE_CODE (length) == INTEGER_CST
11570 && TYPE_PRECISION (TREE_TYPE (length))
11571 > TYPE_PRECISION (sizetype))
11572 length = fold_convert (sizetype, length);
11573 if (low_bound == NULL_TREE)
11574 low_bound = integer_zero_node;
11575 if (!maybe_zero_len && i > first_non_one)
11577 if (integer_nonzerop (low_bound))
11578 goto do_warn_noncontiguous;
11579 if (length != NULL_TREE
11580 && TREE_CODE (length) == INTEGER_CST
11581 && TYPE_DOMAIN (types[i])
11582 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11583 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11584 == INTEGER_CST)
11586 tree size;
11587 size = size_binop (PLUS_EXPR,
11588 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11589 size_one_node);
11590 if (!tree_int_cst_equal (length, size))
11592 do_warn_noncontiguous:
11593 error_at (OMP_CLAUSE_LOCATION (c),
11594 "array section is not contiguous in %qs "
11595 "clause",
11596 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11597 types.release ();
11598 return true;
11601 if (length != NULL_TREE
11602 && TREE_SIDE_EFFECTS (length))
11604 if (side_effects == NULL_TREE)
11605 side_effects = length;
11606 else
11607 side_effects = build2 (COMPOUND_EXPR,
11608 TREE_TYPE (side_effects),
11609 length, side_effects);
11612 else
11614 tree l;
11616 if (i > first_non_one && length && integer_nonzerop (length))
11617 continue;
11618 if (length)
11619 l = fold_convert (sizetype, length);
11620 else
11622 l = size_binop (PLUS_EXPR,
11623 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11624 size_one_node);
11625 l = size_binop (MINUS_EXPR, l,
11626 fold_convert (sizetype, low_bound));
11628 if (i > first_non_one)
11630 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11631 size_zero_node);
11632 if (condition == NULL_TREE)
11633 condition = l;
11634 else
11635 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11636 l, condition);
11638 else if (size == NULL_TREE)
11640 size = size_in_bytes (TREE_TYPE (types[i]));
11641 size = size_binop (MULT_EXPR, size, l);
11642 if (condition)
11643 size = fold_build3 (COND_EXPR, sizetype, condition,
11644 size, size_zero_node);
11646 else
11647 size = size_binop (MULT_EXPR, size, l);
11650 types.release ();
11651 if (side_effects)
11652 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11653 first = c_fully_fold (first, false, NULL);
11654 OMP_CLAUSE_DECL (c) = first;
11655 if (size)
11656 size = c_fully_fold (size, false, NULL);
11657 OMP_CLAUSE_SIZE (c) = size;
11658 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11659 return false;
11660 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11661 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11662 if (!c_mark_addressable (t))
11663 return false;
11664 OMP_CLAUSE_DECL (c2) = t;
11665 t = build_fold_addr_expr (first);
11666 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11667 tree ptr = OMP_CLAUSE_DECL (c2);
11668 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11669 ptr = build_fold_addr_expr (ptr);
11670 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11671 ptrdiff_type_node, t,
11672 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11673 ptrdiff_type_node, ptr));
11674 t = c_fully_fold (t, false, NULL);
11675 OMP_CLAUSE_SIZE (c2) = t;
11676 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11677 OMP_CLAUSE_CHAIN (c) = c2;
11679 return false;
11682 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11683 an inline call. But, remap
11684 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11685 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11687 static tree
11688 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11689 tree decl, tree placeholder)
11691 copy_body_data id;
11692 struct pointer_map_t *decl_map = pointer_map_create ();
11694 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11695 *pointer_map_insert (decl_map, omp_decl2) = decl;
11696 memset (&id, 0, sizeof (id));
11697 id.src_fn = DECL_CONTEXT (omp_decl1);
11698 id.dst_fn = current_function_decl;
11699 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11700 id.decl_map = decl_map;
11702 id.copy_decl = copy_decl_no_change;
11703 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11704 id.transform_new_cfg = true;
11705 id.transform_return_to_modify = false;
11706 id.transform_lang_insert_block = NULL;
11707 id.eh_lp_nr = 0;
11708 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11709 pointer_map_destroy (decl_map);
11710 return stmt;
11713 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11714 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11716 static tree
11717 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11719 if (*tp == (tree) data)
11720 return *tp;
11721 return NULL_TREE;
11724 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11725 Remove any elements from the list that are invalid. */
11727 tree
11728 c_finish_omp_clauses (tree clauses)
11730 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11731 bitmap_head aligned_head;
11732 tree c, t, *pc = &clauses;
11733 bool branch_seen = false;
11734 bool copyprivate_seen = false;
11735 tree *nowait_clause = NULL;
11737 bitmap_obstack_initialize (NULL);
11738 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11739 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11740 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11741 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11743 for (pc = &clauses, c = clauses; c ; c = *pc)
11745 bool remove = false;
11746 bool need_complete = false;
11747 bool need_implicitly_determined = false;
11749 switch (OMP_CLAUSE_CODE (c))
11751 case OMP_CLAUSE_SHARED:
11752 need_implicitly_determined = true;
11753 goto check_dup_generic;
11755 case OMP_CLAUSE_PRIVATE:
11756 need_complete = true;
11757 need_implicitly_determined = true;
11758 goto check_dup_generic;
11760 case OMP_CLAUSE_REDUCTION:
11761 need_implicitly_determined = true;
11762 t = OMP_CLAUSE_DECL (c);
11763 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11764 && (FLOAT_TYPE_P (TREE_TYPE (t))
11765 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11767 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11768 const char *r_name = NULL;
11770 switch (r_code)
11772 case PLUS_EXPR:
11773 case MULT_EXPR:
11774 case MINUS_EXPR:
11775 break;
11776 case MIN_EXPR:
11777 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11778 r_name = "min";
11779 break;
11780 case MAX_EXPR:
11781 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11782 r_name = "max";
11783 break;
11784 case BIT_AND_EXPR:
11785 r_name = "&";
11786 break;
11787 case BIT_XOR_EXPR:
11788 r_name = "^";
11789 break;
11790 case BIT_IOR_EXPR:
11791 r_name = "|";
11792 break;
11793 case TRUTH_ANDIF_EXPR:
11794 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11795 r_name = "&&";
11796 break;
11797 case TRUTH_ORIF_EXPR:
11798 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11799 r_name = "||";
11800 break;
11801 default:
11802 gcc_unreachable ();
11804 if (r_name)
11806 error_at (OMP_CLAUSE_LOCATION (c),
11807 "%qE has invalid type for %<reduction(%s)%>",
11808 t, r_name);
11809 remove = true;
11810 break;
11813 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11815 error_at (OMP_CLAUSE_LOCATION (c),
11816 "user defined reduction not found for %qD", t);
11817 remove = true;
11818 break;
11820 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11822 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11823 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11824 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11825 VAR_DECL, NULL_TREE, type);
11826 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11827 DECL_ARTIFICIAL (placeholder) = 1;
11828 DECL_IGNORED_P (placeholder) = 1;
11829 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11830 c_mark_addressable (placeholder);
11831 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11832 c_mark_addressable (OMP_CLAUSE_DECL (c));
11833 OMP_CLAUSE_REDUCTION_MERGE (c)
11834 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11835 TREE_VEC_ELT (list, 0),
11836 TREE_VEC_ELT (list, 1),
11837 OMP_CLAUSE_DECL (c), placeholder);
11838 OMP_CLAUSE_REDUCTION_MERGE (c)
11839 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11840 void_type_node, NULL_TREE,
11841 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11842 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11843 if (TREE_VEC_LENGTH (list) == 6)
11845 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11846 c_mark_addressable (OMP_CLAUSE_DECL (c));
11847 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11848 c_mark_addressable (placeholder);
11849 tree init = TREE_VEC_ELT (list, 5);
11850 if (init == error_mark_node)
11851 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11852 OMP_CLAUSE_REDUCTION_INIT (c)
11853 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11854 TREE_VEC_ELT (list, 3),
11855 OMP_CLAUSE_DECL (c), placeholder);
11856 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11857 OMP_CLAUSE_REDUCTION_INIT (c)
11858 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11859 OMP_CLAUSE_REDUCTION_INIT (c));
11860 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11861 c_find_omp_placeholder_r,
11862 placeholder, NULL))
11863 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11865 else
11867 tree init;
11868 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11869 init = build_constructor (TREE_TYPE (t), NULL);
11870 else
11871 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11872 OMP_CLAUSE_REDUCTION_INIT (c)
11873 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11875 OMP_CLAUSE_REDUCTION_INIT (c)
11876 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11877 void_type_node, NULL_TREE,
11878 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11879 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11881 goto check_dup_generic;
11883 case OMP_CLAUSE_COPYPRIVATE:
11884 copyprivate_seen = true;
11885 if (nowait_clause)
11887 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11888 "%<nowait%> clause must not be used together "
11889 "with %<copyprivate%>");
11890 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11891 nowait_clause = NULL;
11893 goto check_dup_generic;
11895 case OMP_CLAUSE_COPYIN:
11896 t = OMP_CLAUSE_DECL (c);
11897 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11899 error_at (OMP_CLAUSE_LOCATION (c),
11900 "%qE must be %<threadprivate%> for %<copyin%>", t);
11901 remove = true;
11902 break;
11904 goto check_dup_generic;
11906 case OMP_CLAUSE_LINEAR:
11907 t = OMP_CLAUSE_DECL (c);
11908 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11909 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11911 error_at (OMP_CLAUSE_LOCATION (c),
11912 "linear clause applied to non-integral non-pointer "
11913 "variable with type %qT", TREE_TYPE (t));
11914 remove = true;
11915 break;
11917 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11919 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11920 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11921 OMP_CLAUSE_DECL (c), s);
11922 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11923 sizetype, s, OMP_CLAUSE_DECL (c));
11924 if (s == error_mark_node)
11925 s = size_one_node;
11926 OMP_CLAUSE_LINEAR_STEP (c) = s;
11928 goto check_dup_generic;
11930 check_dup_generic:
11931 t = OMP_CLAUSE_DECL (c);
11932 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11934 error_at (OMP_CLAUSE_LOCATION (c),
11935 "%qE is not a variable in clause %qs", t,
11936 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11937 remove = true;
11939 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11940 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11941 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11943 error_at (OMP_CLAUSE_LOCATION (c),
11944 "%qE appears more than once in data clauses", t);
11945 remove = true;
11947 else
11948 bitmap_set_bit (&generic_head, DECL_UID (t));
11949 break;
11951 case OMP_CLAUSE_FIRSTPRIVATE:
11952 t = OMP_CLAUSE_DECL (c);
11953 need_complete = true;
11954 need_implicitly_determined = true;
11955 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11957 error_at (OMP_CLAUSE_LOCATION (c),
11958 "%qE is not a variable in clause %<firstprivate%>", t);
11959 remove = true;
11961 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11962 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11964 error_at (OMP_CLAUSE_LOCATION (c),
11965 "%qE appears more than once in data clauses", t);
11966 remove = true;
11968 else
11969 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11970 break;
11972 case OMP_CLAUSE_LASTPRIVATE:
11973 t = OMP_CLAUSE_DECL (c);
11974 need_complete = true;
11975 need_implicitly_determined = true;
11976 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11978 error_at (OMP_CLAUSE_LOCATION (c),
11979 "%qE is not a variable in clause %<lastprivate%>", t);
11980 remove = true;
11982 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11983 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11985 error_at (OMP_CLAUSE_LOCATION (c),
11986 "%qE appears more than once in data clauses", t);
11987 remove = true;
11989 else
11990 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11991 break;
11993 case OMP_CLAUSE_ALIGNED:
11994 t = OMP_CLAUSE_DECL (c);
11995 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11997 error_at (OMP_CLAUSE_LOCATION (c),
11998 "%qE is not a variable in %<aligned%> clause", t);
11999 remove = true;
12001 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12002 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12004 error_at (OMP_CLAUSE_LOCATION (c),
12005 "%qE in %<aligned%> clause is neither a pointer nor "
12006 "an array", t);
12007 remove = true;
12009 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12011 error_at (OMP_CLAUSE_LOCATION (c),
12012 "%qE appears more than once in %<aligned%> clauses",
12014 remove = true;
12016 else
12017 bitmap_set_bit (&aligned_head, DECL_UID (t));
12018 break;
12020 case OMP_CLAUSE_DEPEND:
12021 t = OMP_CLAUSE_DECL (c);
12022 if (TREE_CODE (t) == TREE_LIST)
12024 if (handle_omp_array_sections (c))
12025 remove = true;
12026 break;
12028 if (t == error_mark_node)
12029 remove = true;
12030 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12032 error_at (OMP_CLAUSE_LOCATION (c),
12033 "%qE is not a variable in %<depend%> clause", t);
12034 remove = true;
12036 else if (!c_mark_addressable (t))
12037 remove = true;
12038 break;
12040 case OMP_CLAUSE_MAP:
12041 case OMP_CLAUSE_TO:
12042 case OMP_CLAUSE_FROM:
12043 t = OMP_CLAUSE_DECL (c);
12044 if (TREE_CODE (t) == TREE_LIST)
12046 if (handle_omp_array_sections (c))
12047 remove = true;
12048 else
12050 t = OMP_CLAUSE_DECL (c);
12051 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12053 error_at (OMP_CLAUSE_LOCATION (c),
12054 "array section does not have mappable type "
12055 "in %qs clause",
12056 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12057 remove = true;
12060 break;
12062 if (t == error_mark_node)
12063 remove = true;
12064 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12066 error_at (OMP_CLAUSE_LOCATION (c),
12067 "%qE is not a variable in %qs clause", t,
12068 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12069 remove = true;
12071 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12073 error_at (OMP_CLAUSE_LOCATION (c),
12074 "%qD is threadprivate variable in %qs clause", t,
12075 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12076 remove = true;
12078 else if (!c_mark_addressable (t))
12079 remove = true;
12080 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12081 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12082 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12084 error_at (OMP_CLAUSE_LOCATION (c),
12085 "%qD does not have a mappable type in %qs clause", t,
12086 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12087 remove = true;
12089 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12091 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12092 error ("%qD appears more than once in motion clauses", t);
12093 else
12094 error ("%qD appears more than once in map clauses", t);
12095 remove = true;
12097 else
12098 bitmap_set_bit (&generic_head, DECL_UID (t));
12099 break;
12101 case OMP_CLAUSE_UNIFORM:
12102 t = OMP_CLAUSE_DECL (c);
12103 if (TREE_CODE (t) != PARM_DECL)
12105 if (DECL_P (t))
12106 error_at (OMP_CLAUSE_LOCATION (c),
12107 "%qD is not an argument in %<uniform%> clause", t);
12108 else
12109 error_at (OMP_CLAUSE_LOCATION (c),
12110 "%qE is not an argument in %<uniform%> clause", t);
12111 remove = true;
12112 break;
12114 goto check_dup_generic;
12116 case OMP_CLAUSE_NOWAIT:
12117 if (copyprivate_seen)
12119 error_at (OMP_CLAUSE_LOCATION (c),
12120 "%<nowait%> clause must not be used together "
12121 "with %<copyprivate%>");
12122 remove = true;
12123 break;
12125 nowait_clause = pc;
12126 pc = &OMP_CLAUSE_CHAIN (c);
12127 continue;
12129 case OMP_CLAUSE_IF:
12130 case OMP_CLAUSE_NUM_THREADS:
12131 case OMP_CLAUSE_NUM_TEAMS:
12132 case OMP_CLAUSE_THREAD_LIMIT:
12133 case OMP_CLAUSE_SCHEDULE:
12134 case OMP_CLAUSE_ORDERED:
12135 case OMP_CLAUSE_DEFAULT:
12136 case OMP_CLAUSE_UNTIED:
12137 case OMP_CLAUSE_COLLAPSE:
12138 case OMP_CLAUSE_FINAL:
12139 case OMP_CLAUSE_MERGEABLE:
12140 case OMP_CLAUSE_SAFELEN:
12141 case OMP_CLAUSE_SIMDLEN:
12142 case OMP_CLAUSE_DEVICE:
12143 case OMP_CLAUSE_DIST_SCHEDULE:
12144 case OMP_CLAUSE_PARALLEL:
12145 case OMP_CLAUSE_FOR:
12146 case OMP_CLAUSE_SECTIONS:
12147 case OMP_CLAUSE_TASKGROUP:
12148 case OMP_CLAUSE_PROC_BIND:
12149 pc = &OMP_CLAUSE_CHAIN (c);
12150 continue;
12152 case OMP_CLAUSE_INBRANCH:
12153 case OMP_CLAUSE_NOTINBRANCH:
12154 if (branch_seen)
12156 error_at (OMP_CLAUSE_LOCATION (c),
12157 "%<inbranch%> clause is incompatible with "
12158 "%<notinbranch%>");
12159 remove = true;
12160 break;
12162 branch_seen = true;
12163 pc = &OMP_CLAUSE_CHAIN (c);
12164 continue;
12166 default:
12167 gcc_unreachable ();
12170 if (!remove)
12172 t = OMP_CLAUSE_DECL (c);
12174 if (need_complete)
12176 t = require_complete_type (t);
12177 if (t == error_mark_node)
12178 remove = true;
12181 if (need_implicitly_determined)
12183 const char *share_name = NULL;
12185 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12186 share_name = "threadprivate";
12187 else switch (c_omp_predetermined_sharing (t))
12189 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12190 break;
12191 case OMP_CLAUSE_DEFAULT_SHARED:
12192 /* const vars may be specified in firstprivate clause. */
12193 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12194 && TREE_READONLY (t))
12195 break;
12196 share_name = "shared";
12197 break;
12198 case OMP_CLAUSE_DEFAULT_PRIVATE:
12199 share_name = "private";
12200 break;
12201 default:
12202 gcc_unreachable ();
12204 if (share_name)
12206 error_at (OMP_CLAUSE_LOCATION (c),
12207 "%qE is predetermined %qs for %qs",
12208 t, share_name,
12209 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12210 remove = true;
12215 if (remove)
12216 *pc = OMP_CLAUSE_CHAIN (c);
12217 else
12218 pc = &OMP_CLAUSE_CHAIN (c);
12221 bitmap_obstack_release (NULL);
12222 return clauses;
12225 /* Create a transaction node. */
12227 tree
12228 c_finish_transaction (location_t loc, tree block, int flags)
12230 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12231 if (flags & TM_STMT_ATTR_OUTER)
12232 TRANSACTION_EXPR_OUTER (stmt) = 1;
12233 if (flags & TM_STMT_ATTR_RELAXED)
12234 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12235 return add_stmt (stmt);
12238 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12239 down to the element type of an array. */
12241 tree
12242 c_build_qualified_type (tree type, int type_quals)
12244 if (type == error_mark_node)
12245 return type;
12247 if (TREE_CODE (type) == ARRAY_TYPE)
12249 tree t;
12250 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12251 type_quals);
12253 /* See if we already have an identically qualified type. */
12254 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12256 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12257 && TYPE_NAME (t) == TYPE_NAME (type)
12258 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12259 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12260 TYPE_ATTRIBUTES (type)))
12261 break;
12263 if (!t)
12265 tree domain = TYPE_DOMAIN (type);
12267 t = build_variant_type_copy (type);
12268 TREE_TYPE (t) = element_type;
12270 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12271 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12272 SET_TYPE_STRUCTURAL_EQUALITY (t);
12273 else if (TYPE_CANONICAL (element_type) != element_type
12274 || (domain && TYPE_CANONICAL (domain) != domain))
12276 tree unqualified_canon
12277 = build_array_type (TYPE_CANONICAL (element_type),
12278 domain? TYPE_CANONICAL (domain)
12279 : NULL_TREE);
12280 TYPE_CANONICAL (t)
12281 = c_build_qualified_type (unqualified_canon, type_quals);
12283 else
12284 TYPE_CANONICAL (t) = t;
12286 return t;
12289 /* A restrict-qualified pointer type must be a pointer to object or
12290 incomplete type. Note that the use of POINTER_TYPE_P also allows
12291 REFERENCE_TYPEs, which is appropriate for C++. */
12292 if ((type_quals & TYPE_QUAL_RESTRICT)
12293 && (!POINTER_TYPE_P (type)
12294 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12296 error ("invalid use of %<restrict%>");
12297 type_quals &= ~TYPE_QUAL_RESTRICT;
12300 return build_qualified_type (type, type_quals);
12303 /* Build a VA_ARG_EXPR for the C parser. */
12305 tree
12306 c_build_va_arg (location_t loc, tree expr, tree type)
12308 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12309 warning_at (loc, OPT_Wc___compat,
12310 "C++ requires promoted type, not enum type, in %<va_arg%>");
12311 return build_va_arg (loc, expr, type);
12314 /* Return truthvalue of whether T1 is the same tree structure as T2.
12315 Return 1 if they are the same. Return 0 if they are different. */
12317 bool
12318 c_tree_equal (tree t1, tree t2)
12320 enum tree_code code1, code2;
12322 if (t1 == t2)
12323 return true;
12324 if (!t1 || !t2)
12325 return false;
12327 for (code1 = TREE_CODE (t1);
12328 CONVERT_EXPR_CODE_P (code1)
12329 || code1 == NON_LVALUE_EXPR;
12330 code1 = TREE_CODE (t1))
12331 t1 = TREE_OPERAND (t1, 0);
12332 for (code2 = TREE_CODE (t2);
12333 CONVERT_EXPR_CODE_P (code2)
12334 || code2 == NON_LVALUE_EXPR;
12335 code2 = TREE_CODE (t2))
12336 t2 = TREE_OPERAND (t2, 0);
12338 /* They might have become equal now. */
12339 if (t1 == t2)
12340 return true;
12342 if (code1 != code2)
12343 return false;
12345 switch (code1)
12347 case INTEGER_CST:
12348 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12349 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12351 case REAL_CST:
12352 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12354 case STRING_CST:
12355 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12356 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12357 TREE_STRING_LENGTH (t1));
12359 case FIXED_CST:
12360 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12361 TREE_FIXED_CST (t2));
12363 case COMPLEX_CST:
12364 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12365 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12367 case VECTOR_CST:
12368 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12370 case CONSTRUCTOR:
12371 /* We need to do this when determining whether or not two
12372 non-type pointer to member function template arguments
12373 are the same. */
12374 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12375 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12376 return false;
12378 tree field, value;
12379 unsigned int i;
12380 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12382 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12383 if (!c_tree_equal (field, elt2->index)
12384 || !c_tree_equal (value, elt2->value))
12385 return false;
12388 return true;
12390 case TREE_LIST:
12391 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12392 return false;
12393 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12394 return false;
12395 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12397 case SAVE_EXPR:
12398 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12400 case CALL_EXPR:
12402 tree arg1, arg2;
12403 call_expr_arg_iterator iter1, iter2;
12404 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12405 return false;
12406 for (arg1 = first_call_expr_arg (t1, &iter1),
12407 arg2 = first_call_expr_arg (t2, &iter2);
12408 arg1 && arg2;
12409 arg1 = next_call_expr_arg (&iter1),
12410 arg2 = next_call_expr_arg (&iter2))
12411 if (!c_tree_equal (arg1, arg2))
12412 return false;
12413 if (arg1 || arg2)
12414 return false;
12415 return true;
12418 case TARGET_EXPR:
12420 tree o1 = TREE_OPERAND (t1, 0);
12421 tree o2 = TREE_OPERAND (t2, 0);
12423 /* Special case: if either target is an unallocated VAR_DECL,
12424 it means that it's going to be unified with whatever the
12425 TARGET_EXPR is really supposed to initialize, so treat it
12426 as being equivalent to anything. */
12427 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12428 && !DECL_RTL_SET_P (o1))
12429 /*Nop*/;
12430 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12431 && !DECL_RTL_SET_P (o2))
12432 /*Nop*/;
12433 else if (!c_tree_equal (o1, o2))
12434 return false;
12436 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12439 case COMPONENT_REF:
12440 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12441 return false;
12442 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12444 case PARM_DECL:
12445 case VAR_DECL:
12446 case CONST_DECL:
12447 case FIELD_DECL:
12448 case FUNCTION_DECL:
12449 case IDENTIFIER_NODE:
12450 case SSA_NAME:
12451 return false;
12453 case TREE_VEC:
12455 unsigned ix;
12456 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12457 return false;
12458 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12459 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12460 TREE_VEC_ELT (t2, ix)))
12461 return false;
12462 return true;
12465 default:
12466 break;
12469 switch (TREE_CODE_CLASS (code1))
12471 case tcc_unary:
12472 case tcc_binary:
12473 case tcc_comparison:
12474 case tcc_expression:
12475 case tcc_vl_exp:
12476 case tcc_reference:
12477 case tcc_statement:
12479 int i, n = TREE_OPERAND_LENGTH (t1);
12481 switch (code1)
12483 case PREINCREMENT_EXPR:
12484 case PREDECREMENT_EXPR:
12485 case POSTINCREMENT_EXPR:
12486 case POSTDECREMENT_EXPR:
12487 n = 1;
12488 break;
12489 case ARRAY_REF:
12490 n = 2;
12491 break;
12492 default:
12493 break;
12496 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12497 && n != TREE_OPERAND_LENGTH (t2))
12498 return false;
12500 for (i = 0; i < n; ++i)
12501 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12502 return false;
12504 return true;
12507 case tcc_type:
12508 return comptypes (t1, t2);
12509 default:
12510 gcc_unreachable ();
12512 /* We can get here with --disable-checking. */
12513 return false;
12516 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12517 spawn-helper and BODY is the newly created body for FNDECL. */
12519 void
12520 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12522 tree list = alloc_stmt_list ();
12523 tree frame = make_cilk_frame (fndecl);
12524 tree dtor = create_cilk_function_exit (frame, false, true);
12525 add_local_decl (cfun, frame);
12527 DECL_SAVED_TREE (fndecl) = list;
12528 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12529 frame);
12530 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12531 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12533 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12534 append_to_statement_list (detach_expr, &body_list);
12536 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12537 body = fold_build_cleanup_point_expr (void_type_node, body);
12539 append_to_statement_list (body, &body_list);
12540 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12541 body_list, dtor), &list);