Fix warnings occured during profiledboostrap on
[official-gcc.git] / gcc / c / c-typeck.c
bloba3a9c7760c72f8d39300116f9d1d3dcda730cf34
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "hash-set.h"
31 #include "vec.h"
32 #include "symtab.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "double-int.h"
36 #include "machmode.h"
37 #include "inchash.h"
38 #include "real.h"
39 #include "fixed-value.h"
40 #include "tree.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "trans-mem.h"
44 #include "varasm.h"
45 #include "stmt.h"
46 #include "langhooks.h"
47 #include "c-tree.h"
48 #include "c-lang.h"
49 #include "flags.h"
50 #include "intl.h"
51 #include "target.h"
52 #include "tree-iterator.h"
53 #include "bitmap.h"
54 #include "predict.h"
55 #include "vec.h"
56 #include "hashtab.h"
57 #include "hash-set.h"
58 #include "machmode.h"
59 #include "hard-reg-set.h"
60 #include "input.h"
61 #include "function.h"
62 #include "gimple-expr.h"
63 #include "gimplify.h"
64 #include "tree-inline.h"
65 #include "omp-low.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-ubsan.h"
69 #include "cilk.h"
70 #include "wide-int.h"
71 #include "gomp-constants.h"
73 /* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
75 enum impl_conv {
76 ic_argpass,
77 ic_assign,
78 ic_init,
79 ic_return
82 /* The level of nesting inside "__alignof__". */
83 int in_alignof;
85 /* The level of nesting inside "sizeof". */
86 int in_sizeof;
88 /* The level of nesting inside "typeof". */
89 int in_typeof;
91 /* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93 tree c_last_sizeof_arg;
95 /* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97 static int found_missing_braces;
99 static int require_constant_value;
100 static int require_constant_elements;
102 static bool null_pointer_constant_p (const_tree);
103 static tree qualify_type (tree, tree);
104 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
105 bool *);
106 static int comp_target_types (location_t, tree, tree);
107 static int function_types_compatible_p (const_tree, const_tree, bool *,
108 bool *);
109 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
110 static tree lookup_field (tree, tree);
111 static int convert_arguments (location_t, vec<location_t>, tree,
112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
113 tree);
114 static tree pointer_diff (location_t, tree, tree);
115 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
116 enum impl_conv, bool, tree, tree, int);
117 static tree valid_compound_expr_initializer (tree, tree);
118 static void push_string (const char *);
119 static void push_member_name (tree);
120 static int spelling_length (void);
121 static char *print_spelling (char *);
122 static void warning_init (location_t, int, const char *);
123 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
124 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
125 bool, struct obstack *);
126 static void output_pending_init_elements (int, struct obstack *);
127 static int set_designator (location_t, int, struct obstack *);
128 static void push_range_stack (tree, struct obstack *);
129 static void add_pending_init (location_t, tree, tree, tree, bool,
130 struct obstack *);
131 static void set_nonincremental_init (struct obstack *);
132 static void set_nonincremental_init_from_string (tree, struct obstack *);
133 static tree find_init_member (tree, struct obstack *);
134 static void readonly_warning (tree, enum lvalue_use);
135 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
136 static void record_maybe_used_decl (tree);
137 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
139 /* Return true if EXP is a null pointer constant, false otherwise. */
141 static bool
142 null_pointer_constant_p (const_tree expr)
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type = TREE_TYPE (expr);
147 return (TREE_CODE (expr) == INTEGER_CST
148 && !TREE_OVERFLOW (expr)
149 && integer_zerop (expr)
150 && (INTEGRAL_TYPE_P (type)
151 || (TREE_CODE (type) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type))
153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
156 /* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
161 static tree
162 note_integer_operands (tree expr)
164 tree ret;
165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
167 ret = copy_node (expr);
168 TREE_OVERFLOW (ret) = 1;
170 else
172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
175 return ret;
178 /* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
181 expression. */
183 static inline tree
184 remove_c_maybe_const_expr (tree expr)
186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
187 return C_MAYBE_CONST_EXPR_EXPR (expr);
188 else
189 return expr;
192 \f/* This is a cache to hold if two types are compatible or not. */
194 struct tagged_tu_seen_cache {
195 const struct tagged_tu_seen_cache * next;
196 const_tree t1;
197 const_tree t2;
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
200 int val;
203 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
204 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
206 /* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
209 tree
210 require_complete_type (tree value)
212 tree type = TREE_TYPE (value);
214 if (error_operand_p (value))
215 return error_mark_node;
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type))
219 return value;
221 c_incomplete_type_error (value, type);
222 return error_mark_node;
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
229 void
230 c_incomplete_type_error (const_tree value, const_tree type)
232 const char *type_code_string;
234 /* Avoid duplicate error message. */
235 if (TREE_CODE (type) == ERROR_MARK)
236 return;
238 if (value != 0 && (TREE_CODE (value) == VAR_DECL
239 || TREE_CODE (value) == PARM_DECL))
240 error ("%qD has an incomplete type", value);
241 else
243 retry:
244 /* We must print an error message. Be clever about what it says. */
246 switch (TREE_CODE (type))
248 case RECORD_TYPE:
249 type_code_string = "struct";
250 break;
252 case UNION_TYPE:
253 type_code_string = "union";
254 break;
256 case ENUMERAL_TYPE:
257 type_code_string = "enum";
258 break;
260 case VOID_TYPE:
261 error ("invalid use of void expression");
262 return;
264 case ARRAY_TYPE:
265 if (TYPE_DOMAIN (type))
267 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
269 error ("invalid use of flexible array member");
270 return;
272 type = TREE_TYPE (type);
273 goto retry;
275 error ("invalid use of array with unspecified bounds");
276 return;
278 default:
279 gcc_unreachable ();
282 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
283 error ("invalid use of undefined type %<%s %E%>",
284 type_code_string, TYPE_NAME (type));
285 else
286 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
287 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
291 /* Given a type, apply default promotions wrt unnamed function
292 arguments and return the new type. */
294 tree
295 c_type_promotes_to (tree type)
297 tree ret = NULL_TREE;
299 if (TYPE_MAIN_VARIANT (type) == float_type_node)
300 ret = double_type_node;
301 else if (c_promoting_integer_type_p (type))
303 /* Preserve unsignedness if not really getting any wider. */
304 if (TYPE_UNSIGNED (type)
305 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
306 ret = unsigned_type_node;
307 else
308 ret = integer_type_node;
311 if (ret != NULL_TREE)
312 return (TYPE_ATOMIC (type)
313 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
314 : ret);
316 return type;
319 /* Return true if between two named address spaces, whether there is a superset
320 named address space that encompasses both address spaces. If there is a
321 superset, return which address space is the superset. */
323 static bool
324 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
326 if (as1 == as2)
328 *common = as1;
329 return true;
331 else if (targetm.addr_space.subset_p (as1, as2))
333 *common = as2;
334 return true;
336 else if (targetm.addr_space.subset_p (as2, as1))
338 *common = as1;
339 return true;
341 else
342 return false;
345 /* Return a variant of TYPE which has all the type qualifiers of LIKE
346 as well as those of TYPE. */
348 static tree
349 qualify_type (tree type, tree like)
351 addr_space_t as_type = TYPE_ADDR_SPACE (type);
352 addr_space_t as_like = TYPE_ADDR_SPACE (like);
353 addr_space_t as_common;
355 /* If the two named address spaces are different, determine the common
356 superset address space. If there isn't one, raise an error. */
357 if (!addr_space_superset (as_type, as_like, &as_common))
359 as_common = as_type;
360 error ("%qT and %qT are in disjoint named address spaces",
361 type, like);
364 return c_build_qualified_type (type,
365 TYPE_QUALS_NO_ADDR_SPACE (type)
366 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
367 | ENCODE_QUAL_ADDR_SPACE (as_common));
370 /* Return true iff the given tree T is a variable length array. */
372 bool
373 c_vla_type_p (const_tree t)
375 if (TREE_CODE (t) == ARRAY_TYPE
376 && C_TYPE_VARIABLE_SIZE (t))
377 return true;
378 return false;
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
387 tree
388 composite_type (tree t1, tree t2)
390 enum tree_code code1;
391 enum tree_code code2;
392 tree attributes;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
416 return t1;
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
418 return t2;
420 gcc_assert (code1 == code2);
422 switch (code1)
424 case POINTER_TYPE:
425 /* For two pointers, do this recursively on the target type. */
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
429 tree target = composite_type (pointed_to_1, pointed_to_2);
430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
435 case ARRAY_TYPE:
437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
438 int quals;
439 tree unqual_elt;
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
444 bool t1_complete, t2_complete;
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
453 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
468 return build_type_attribute_variant (t1, attributes);
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
471 return build_type_attribute_variant (t2, attributes);
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
482 back at the end. */
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
487 && (d2_variable
488 || d2_zero
489 || !d1_variable))
490 ? t1
491 : t2));
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 t1 = c_build_qualified_type (t1, quals);
502 return build_type_attribute_variant (t1, attributes);
505 case ENUMERAL_TYPE:
506 case RECORD_TYPE:
507 case UNION_TYPE:
508 if (attributes != NULL)
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
512 return t1;
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
514 return t2;
516 return build_type_attribute_variant (t1, attributes);
518 case FUNCTION_TYPE:
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
525 int len;
526 tree newargs, n;
527 int i;
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
531 return build_type_attribute_variant (t1, attributes);
532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
533 return build_type_attribute_variant (t2, attributes);
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == 0)
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
542 if (TYPE_ARG_TYPES (t2) == 0)
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
552 len = list_length (p1);
553 newargs = 0;
555 for (i = 0; i < len; i++)
556 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
558 n = newargs;
560 for (; p1;
561 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
563 /* A null type means arg type is not specified.
564 Take whatever the other function type has. */
565 if (TREE_VALUE (p1) == 0)
567 TREE_VALUE (n) = TREE_VALUE (p2);
568 goto parm_done;
570 if (TREE_VALUE (p2) == 0)
572 TREE_VALUE (n) = TREE_VALUE (p1);
573 goto parm_done;
576 /* Given wait (union {union wait *u; int *i} *)
577 and wait (union wait *),
578 prefer union wait * as type of parm. */
579 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
580 && TREE_VALUE (p1) != TREE_VALUE (p2))
582 tree memb;
583 tree mv2 = TREE_VALUE (p2);
584 if (mv2 && mv2 != error_mark_node
585 && TREE_CODE (mv2) != ARRAY_TYPE)
586 mv2 = TYPE_MAIN_VARIANT (mv2);
587 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
588 memb; memb = DECL_CHAIN (memb))
590 tree mv3 = TREE_TYPE (memb);
591 if (mv3 && mv3 != error_mark_node
592 && TREE_CODE (mv3) != ARRAY_TYPE)
593 mv3 = TYPE_MAIN_VARIANT (mv3);
594 if (comptypes (mv3, mv2))
596 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
597 TREE_VALUE (p2));
598 pedwarn (input_location, OPT_Wpedantic,
599 "function types not truly compatible in ISO C");
600 goto parm_done;
604 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
605 && TREE_VALUE (p2) != TREE_VALUE (p1))
607 tree memb;
608 tree mv1 = TREE_VALUE (p1);
609 if (mv1 && mv1 != error_mark_node
610 && TREE_CODE (mv1) != ARRAY_TYPE)
611 mv1 = TYPE_MAIN_VARIANT (mv1);
612 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
613 memb; memb = DECL_CHAIN (memb))
615 tree mv3 = TREE_TYPE (memb);
616 if (mv3 && mv3 != error_mark_node
617 && TREE_CODE (mv3) != ARRAY_TYPE)
618 mv3 = TYPE_MAIN_VARIANT (mv3);
619 if (comptypes (mv3, mv1))
621 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
622 TREE_VALUE (p1));
623 pedwarn (input_location, OPT_Wpedantic,
624 "function types not truly compatible in ISO C");
625 goto parm_done;
629 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
630 parm_done: ;
633 t1 = build_function_type (valtype, newargs);
634 t1 = qualify_type (t1, t2);
635 /* ... falls through ... */
638 default:
639 return build_type_attribute_variant (t1, attributes);
644 /* Return the type of a conditional expression between pointers to
645 possibly differently qualified versions of compatible types.
647 We assume that comp_target_types has already been done and returned
648 nonzero; if that isn't so, this may crash. */
650 static tree
651 common_pointer_type (tree t1, tree t2)
653 tree attributes;
654 tree pointed_to_1, mv1;
655 tree pointed_to_2, mv2;
656 tree target;
657 unsigned target_quals;
658 addr_space_t as1, as2, as_common;
659 int quals1, quals2;
661 /* Save time if the two types are the same. */
663 if (t1 == t2) return t1;
665 /* If one type is nonsense, use the other. */
666 if (t1 == error_mark_node)
667 return t2;
668 if (t2 == error_mark_node)
669 return t1;
671 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
672 && TREE_CODE (t2) == POINTER_TYPE);
674 /* Merge the attributes. */
675 attributes = targetm.merge_type_attributes (t1, t2);
677 /* Find the composite type of the target types, and combine the
678 qualifiers of the two types' targets. Do not lose qualifiers on
679 array element types by taking the TYPE_MAIN_VARIANT. */
680 mv1 = pointed_to_1 = TREE_TYPE (t1);
681 mv2 = pointed_to_2 = TREE_TYPE (t2);
682 if (TREE_CODE (mv1) != ARRAY_TYPE)
683 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
684 if (TREE_CODE (mv2) != ARRAY_TYPE)
685 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
686 target = composite_type (mv1, mv2);
688 /* Strip array types to get correct qualifier for pointers to arrays */
689 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
690 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
692 /* For function types do not merge const qualifiers, but drop them
693 if used inconsistently. The middle-end uses these to mark const
694 and noreturn functions. */
695 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
696 target_quals = (quals1 & quals2);
697 else
698 target_quals = (quals1 | quals2);
700 /* If the two named address spaces are different, determine the common
701 superset address space. This is guaranteed to exist due to the
702 assumption that comp_target_type returned non-zero. */
703 as1 = TYPE_ADDR_SPACE (pointed_to_1);
704 as2 = TYPE_ADDR_SPACE (pointed_to_2);
705 if (!addr_space_superset (as1, as2, &as_common))
706 gcc_unreachable ();
708 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
710 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
711 return build_type_attribute_variant (t1, attributes);
714 /* Return the common type for two arithmetic types under the usual
715 arithmetic conversions. The default conversions have already been
716 applied, and enumerated types converted to their compatible integer
717 types. The resulting type is unqualified and has no attributes.
719 This is the type for the result of most arithmetic operations
720 if the operands have the given two types. */
722 static tree
723 c_common_type (tree t1, tree t2)
725 enum tree_code code1;
726 enum tree_code code2;
728 /* If one type is nonsense, use the other. */
729 if (t1 == error_mark_node)
730 return t2;
731 if (t2 == error_mark_node)
732 return t1;
734 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
735 t1 = TYPE_MAIN_VARIANT (t1);
737 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
738 t2 = TYPE_MAIN_VARIANT (t2);
740 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
741 t1 = build_type_attribute_variant (t1, NULL_TREE);
743 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
744 t2 = build_type_attribute_variant (t2, NULL_TREE);
746 /* Save time if the two types are the same. */
748 if (t1 == t2) return t1;
750 code1 = TREE_CODE (t1);
751 code2 = TREE_CODE (t2);
753 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
754 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
755 || code1 == INTEGER_TYPE);
756 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
757 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
758 || code2 == INTEGER_TYPE);
760 /* When one operand is a decimal float type, the other operand cannot be
761 a generic float type or a complex type. We also disallow vector types
762 here. */
763 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
764 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
766 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
768 error ("can%'t mix operands of decimal float and vector types");
769 return error_mark_node;
771 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
773 error ("can%'t mix operands of decimal float and complex types");
774 return error_mark_node;
776 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
778 error ("can%'t mix operands of decimal float and other float types");
779 return error_mark_node;
783 /* If one type is a vector type, return that type. (How the usual
784 arithmetic conversions apply to the vector types extension is not
785 precisely specified.) */
786 if (code1 == VECTOR_TYPE)
787 return t1;
789 if (code2 == VECTOR_TYPE)
790 return t2;
792 /* If one type is complex, form the common type of the non-complex
793 components, then make that complex. Use T1 or T2 if it is the
794 required type. */
795 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
797 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
798 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
799 tree subtype = c_common_type (subtype1, subtype2);
801 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
802 return t1;
803 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
804 return t2;
805 else
806 return build_complex_type (subtype);
809 /* If only one is real, use it as the result. */
811 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
812 return t1;
814 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
815 return t2;
817 /* If both are real and either are decimal floating point types, use
818 the decimal floating point type with the greater precision. */
820 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
822 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
823 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
824 return dfloat128_type_node;
825 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
826 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
827 return dfloat64_type_node;
828 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
829 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
830 return dfloat32_type_node;
833 /* Deal with fixed-point types. */
834 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
836 unsigned int unsignedp = 0, satp = 0;
837 machine_mode m1, m2;
838 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
840 m1 = TYPE_MODE (t1);
841 m2 = TYPE_MODE (t2);
843 /* If one input type is saturating, the result type is saturating. */
844 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
845 satp = 1;
847 /* If both fixed-point types are unsigned, the result type is unsigned.
848 When mixing fixed-point and integer types, follow the sign of the
849 fixed-point type.
850 Otherwise, the result type is signed. */
851 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
852 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
853 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
854 && TYPE_UNSIGNED (t1))
855 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
856 && TYPE_UNSIGNED (t2)))
857 unsignedp = 1;
859 /* The result type is signed. */
860 if (unsignedp == 0)
862 /* If the input type is unsigned, we need to convert to the
863 signed type. */
864 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
866 enum mode_class mclass = (enum mode_class) 0;
867 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
868 mclass = MODE_FRACT;
869 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
870 mclass = MODE_ACCUM;
871 else
872 gcc_unreachable ();
873 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
875 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
877 enum mode_class mclass = (enum mode_class) 0;
878 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
879 mclass = MODE_FRACT;
880 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
881 mclass = MODE_ACCUM;
882 else
883 gcc_unreachable ();
884 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
888 if (code1 == FIXED_POINT_TYPE)
890 fbit1 = GET_MODE_FBIT (m1);
891 ibit1 = GET_MODE_IBIT (m1);
893 else
895 fbit1 = 0;
896 /* Signed integers need to subtract one sign bit. */
897 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
900 if (code2 == FIXED_POINT_TYPE)
902 fbit2 = GET_MODE_FBIT (m2);
903 ibit2 = GET_MODE_IBIT (m2);
905 else
907 fbit2 = 0;
908 /* Signed integers need to subtract one sign bit. */
909 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
912 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
913 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
914 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
915 satp);
918 /* Both real or both integers; use the one with greater precision. */
920 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
921 return t1;
922 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
923 return t2;
925 /* Same precision. Prefer long longs to longs to ints when the
926 same precision, following the C99 rules on integer type rank
927 (which are equivalent to the C90 rules for C90 types). */
929 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
931 return long_long_unsigned_type_node;
933 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
936 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
937 return long_long_unsigned_type_node;
938 else
939 return long_long_integer_type_node;
942 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
943 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
944 return long_unsigned_type_node;
946 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
947 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
949 /* But preserve unsignedness from the other type,
950 since long cannot hold all the values of an unsigned int. */
951 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
952 return long_unsigned_type_node;
953 else
954 return long_integer_type_node;
957 /* Likewise, prefer long double to double even if same size. */
958 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
960 return long_double_type_node;
962 /* Likewise, prefer double to float even if same size.
963 We got a couple of embedded targets with 32 bit doubles, and the
964 pdp11 might have 64 bit floats. */
965 if (TYPE_MAIN_VARIANT (t1) == double_type_node
966 || TYPE_MAIN_VARIANT (t2) == double_type_node)
967 return double_type_node;
969 /* Otherwise prefer the unsigned one. */
971 if (TYPE_UNSIGNED (t1))
972 return t1;
973 else
974 return t2;
977 /* Wrapper around c_common_type that is used by c-common.c and other
978 front end optimizations that remove promotions. ENUMERAL_TYPEs
979 are allowed here and are converted to their compatible integer types.
980 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
981 preferably a non-Boolean type as the common type. */
982 tree
983 common_type (tree t1, tree t2)
985 if (TREE_CODE (t1) == ENUMERAL_TYPE)
986 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
987 if (TREE_CODE (t2) == ENUMERAL_TYPE)
988 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
990 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
991 if (TREE_CODE (t1) == BOOLEAN_TYPE
992 && TREE_CODE (t2) == BOOLEAN_TYPE)
993 return boolean_type_node;
995 /* If either type is BOOLEAN_TYPE, then return the other. */
996 if (TREE_CODE (t1) == BOOLEAN_TYPE)
997 return t2;
998 if (TREE_CODE (t2) == BOOLEAN_TYPE)
999 return t1;
1001 return c_common_type (t1, t2);
1004 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1005 or various other operations. Return 2 if they are compatible
1006 but a warning may be needed if you use them together. */
1009 comptypes (tree type1, tree type2)
1011 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1012 int val;
1014 val = comptypes_internal (type1, type2, NULL, NULL);
1015 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1017 return val;
1020 /* Like comptypes, but if it returns non-zero because enum and int are
1021 compatible, it sets *ENUM_AND_INT_P to true. */
1023 static int
1024 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1026 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1027 int val;
1029 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1030 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1032 return val;
1035 /* Like comptypes, but if it returns nonzero for different types, it
1036 sets *DIFFERENT_TYPES_P to true. */
1039 comptypes_check_different_types (tree type1, tree type2,
1040 bool *different_types_p)
1042 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1043 int val;
1045 val = comptypes_internal (type1, type2, NULL, different_types_p);
1046 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1048 return val;
1051 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1052 or various other operations. Return 2 if they are compatible
1053 but a warning may be needed if you use them together. If
1054 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1055 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1056 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1057 NULL, and the types are compatible but different enough not to be
1058 permitted in C11 typedef redeclarations, then this sets
1059 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1060 false, but may or may not be set if the types are incompatible.
1061 This differs from comptypes, in that we don't free the seen
1062 types. */
1064 static int
1065 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1066 bool *different_types_p)
1068 const_tree t1 = type1;
1069 const_tree t2 = type2;
1070 int attrval, val;
1072 /* Suppress errors caused by previously reported errors. */
1074 if (t1 == t2 || !t1 || !t2
1075 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1076 return 1;
1078 /* Enumerated types are compatible with integer types, but this is
1079 not transitive: two enumerated types in the same translation unit
1080 are compatible with each other only if they are the same type. */
1082 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1084 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1085 if (TREE_CODE (t2) != VOID_TYPE)
1087 if (enum_and_int_p != NULL)
1088 *enum_and_int_p = true;
1089 if (different_types_p != NULL)
1090 *different_types_p = true;
1093 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1095 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1096 if (TREE_CODE (t1) != VOID_TYPE)
1098 if (enum_and_int_p != NULL)
1099 *enum_and_int_p = true;
1100 if (different_types_p != NULL)
1101 *different_types_p = true;
1105 if (t1 == t2)
1106 return 1;
1108 /* Different classes of types can't be compatible. */
1110 if (TREE_CODE (t1) != TREE_CODE (t2))
1111 return 0;
1113 /* Qualifiers must match. C99 6.7.3p9 */
1115 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1116 return 0;
1118 /* Allow for two different type nodes which have essentially the same
1119 definition. Note that we already checked for equality of the type
1120 qualifiers (just above). */
1122 if (TREE_CODE (t1) != ARRAY_TYPE
1123 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1124 return 1;
1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1127 if (!(attrval = comp_type_attributes (t1, t2)))
1128 return 0;
1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1131 val = 0;
1133 switch (TREE_CODE (t1))
1135 case POINTER_TYPE:
1136 /* Do not remove mode or aliasing information. */
1137 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1138 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1139 break;
1140 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1141 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1142 enum_and_int_p, different_types_p));
1143 break;
1145 case FUNCTION_TYPE:
1146 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1147 different_types_p);
1148 break;
1150 case ARRAY_TYPE:
1152 tree d1 = TYPE_DOMAIN (t1);
1153 tree d2 = TYPE_DOMAIN (t2);
1154 bool d1_variable, d2_variable;
1155 bool d1_zero, d2_zero;
1156 val = 1;
1158 /* Target types must match incl. qualifiers. */
1159 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1160 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1161 enum_and_int_p,
1162 different_types_p)))
1163 return 0;
1165 if (different_types_p != NULL
1166 && (d1 == 0) != (d2 == 0))
1167 *different_types_p = true;
1168 /* Sizes must match unless one is missing or variable. */
1169 if (d1 == 0 || d2 == 0 || d1 == d2)
1170 break;
1172 d1_zero = !TYPE_MAX_VALUE (d1);
1173 d2_zero = !TYPE_MAX_VALUE (d2);
1175 d1_variable = (!d1_zero
1176 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1177 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1178 d2_variable = (!d2_zero
1179 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1180 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1181 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1182 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1184 if (different_types_p != NULL
1185 && d1_variable != d2_variable)
1186 *different_types_p = true;
1187 if (d1_variable || d2_variable)
1188 break;
1189 if (d1_zero && d2_zero)
1190 break;
1191 if (d1_zero || d2_zero
1192 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1193 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1194 val = 0;
1196 break;
1199 case ENUMERAL_TYPE:
1200 case RECORD_TYPE:
1201 case UNION_TYPE:
1202 if (val != 1 && !same_translation_unit_p (t1, t2))
1204 tree a1 = TYPE_ATTRIBUTES (t1);
1205 tree a2 = TYPE_ATTRIBUTES (t2);
1207 if (! attribute_list_contained (a1, a2)
1208 && ! attribute_list_contained (a2, a1))
1209 break;
1211 if (attrval != 2)
1212 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1213 different_types_p);
1214 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1215 different_types_p);
1217 break;
1219 case VECTOR_TYPE:
1220 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1221 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1222 enum_and_int_p, different_types_p));
1223 break;
1225 default:
1226 break;
1228 return attrval == 2 && val == 1 ? 2 : val;
1231 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1232 their qualifiers, except for named address spaces. If the pointers point to
1233 different named addresses, then we must determine if one address space is a
1234 subset of the other. */
1236 static int
1237 comp_target_types (location_t location, tree ttl, tree ttr)
1239 int val;
1240 int val_ped;
1241 tree mvl = TREE_TYPE (ttl);
1242 tree mvr = TREE_TYPE (ttr);
1243 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1244 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1245 addr_space_t as_common;
1246 bool enum_and_int_p;
1248 /* Fail if pointers point to incompatible address spaces. */
1249 if (!addr_space_superset (asl, asr, &as_common))
1250 return 0;
1252 /* For pedantic record result of comptypes on arrays before losing
1253 qualifiers on the element type below. */
1254 val_ped = 1;
1256 if (TREE_CODE (mvl) == ARRAY_TYPE
1257 && TREE_CODE (mvr) == ARRAY_TYPE)
1258 val_ped = comptypes (mvl, mvr);
1260 /* Qualifiers on element types of array types that are
1261 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1263 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1264 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1265 : TYPE_MAIN_VARIANT (mvl));
1267 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1268 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1269 : TYPE_MAIN_VARIANT (mvr));
1271 enum_and_int_p = false;
1272 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1274 if (val == 1 && val_ped != 1)
1275 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1276 "are incompatible in ISO C");
1278 if (val == 2)
1279 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1281 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1282 warning_at (location, OPT_Wc___compat,
1283 "pointer target types incompatible in C++");
1285 return val;
1288 /* Subroutines of `comptypes'. */
1290 /* Determine whether two trees derive from the same translation unit.
1291 If the CONTEXT chain ends in a null, that tree's context is still
1292 being parsed, so if two trees have context chains ending in null,
1293 they're in the same translation unit. */
1295 same_translation_unit_p (const_tree t1, const_tree t2)
1297 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1298 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1300 case tcc_declaration:
1301 t1 = DECL_CONTEXT (t1); break;
1302 case tcc_type:
1303 t1 = TYPE_CONTEXT (t1); break;
1304 case tcc_exceptional:
1305 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1306 default: gcc_unreachable ();
1309 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1310 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1312 case tcc_declaration:
1313 t2 = DECL_CONTEXT (t2); break;
1314 case tcc_type:
1315 t2 = TYPE_CONTEXT (t2); break;
1316 case tcc_exceptional:
1317 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1318 default: gcc_unreachable ();
1321 return t1 == t2;
1324 /* Allocate the seen two types, assuming that they are compatible. */
1326 static struct tagged_tu_seen_cache *
1327 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1329 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1330 tu->next = tagged_tu_seen_base;
1331 tu->t1 = t1;
1332 tu->t2 = t2;
1334 tagged_tu_seen_base = tu;
1336 /* The C standard says that two structures in different translation
1337 units are compatible with each other only if the types of their
1338 fields are compatible (among other things). We assume that they
1339 are compatible until proven otherwise when building the cache.
1340 An example where this can occur is:
1341 struct a
1343 struct a *next;
1345 If we are comparing this against a similar struct in another TU,
1346 and did not assume they were compatible, we end up with an infinite
1347 loop. */
1348 tu->val = 1;
1349 return tu;
1352 /* Free the seen types until we get to TU_TIL. */
1354 static void
1355 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1357 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1358 while (tu != tu_til)
1360 const struct tagged_tu_seen_cache *const tu1
1361 = (const struct tagged_tu_seen_cache *) tu;
1362 tu = tu1->next;
1363 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1365 tagged_tu_seen_base = tu_til;
1368 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1369 compatible. If the two types are not the same (which has been
1370 checked earlier), this can only happen when multiple translation
1371 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1372 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1373 comptypes_internal. */
1375 static int
1376 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1377 bool *enum_and_int_p, bool *different_types_p)
1379 tree s1, s2;
1380 bool needs_warning = false;
1382 /* We have to verify that the tags of the types are the same. This
1383 is harder than it looks because this may be a typedef, so we have
1384 to go look at the original type. It may even be a typedef of a
1385 typedef...
1386 In the case of compiler-created builtin structs the TYPE_DECL
1387 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1388 while (TYPE_NAME (t1)
1389 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1390 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1391 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1393 while (TYPE_NAME (t2)
1394 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1395 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1396 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1398 /* C90 didn't have the requirement that the two tags be the same. */
1399 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1400 return 0;
1402 /* C90 didn't say what happened if one or both of the types were
1403 incomplete; we choose to follow C99 rules here, which is that they
1404 are compatible. */
1405 if (TYPE_SIZE (t1) == NULL
1406 || TYPE_SIZE (t2) == NULL)
1407 return 1;
1410 const struct tagged_tu_seen_cache * tts_i;
1411 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1412 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1413 return tts_i->val;
1416 switch (TREE_CODE (t1))
1418 case ENUMERAL_TYPE:
1420 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1421 /* Speed up the case where the type values are in the same order. */
1422 tree tv1 = TYPE_VALUES (t1);
1423 tree tv2 = TYPE_VALUES (t2);
1425 if (tv1 == tv2)
1427 return 1;
1430 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1432 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1433 break;
1434 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1436 tu->val = 0;
1437 return 0;
1441 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1443 return 1;
1445 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1447 tu->val = 0;
1448 return 0;
1451 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1453 tu->val = 0;
1454 return 0;
1457 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1459 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1460 if (s2 == NULL
1461 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1463 tu->val = 0;
1464 return 0;
1467 return 1;
1470 case UNION_TYPE:
1472 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1473 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1475 tu->val = 0;
1476 return 0;
1479 /* Speed up the common case where the fields are in the same order. */
1480 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1481 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1483 int result;
1485 if (DECL_NAME (s1) != DECL_NAME (s2))
1486 break;
1487 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1488 enum_and_int_p, different_types_p);
1490 if (result != 1 && !DECL_NAME (s1))
1491 break;
1492 if (result == 0)
1494 tu->val = 0;
1495 return 0;
1497 if (result == 2)
1498 needs_warning = true;
1500 if (TREE_CODE (s1) == FIELD_DECL
1501 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1502 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1504 tu->val = 0;
1505 return 0;
1508 if (!s1 && !s2)
1510 tu->val = needs_warning ? 2 : 1;
1511 return tu->val;
1514 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1516 bool ok = false;
1518 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1519 if (DECL_NAME (s1) == DECL_NAME (s2))
1521 int result;
1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1524 enum_and_int_p,
1525 different_types_p);
1527 if (result != 1 && !DECL_NAME (s1))
1528 continue;
1529 if (result == 0)
1531 tu->val = 0;
1532 return 0;
1534 if (result == 2)
1535 needs_warning = true;
1537 if (TREE_CODE (s1) == FIELD_DECL
1538 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1539 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1540 break;
1542 ok = true;
1543 break;
1545 if (!ok)
1547 tu->val = 0;
1548 return 0;
1551 tu->val = needs_warning ? 2 : 10;
1552 return tu->val;
1555 case RECORD_TYPE:
1557 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1559 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1560 s1 && s2;
1561 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1563 int result;
1564 if (TREE_CODE (s1) != TREE_CODE (s2)
1565 || DECL_NAME (s1) != DECL_NAME (s2))
1566 break;
1567 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1568 enum_and_int_p, different_types_p);
1569 if (result == 0)
1570 break;
1571 if (result == 2)
1572 needs_warning = true;
1574 if (TREE_CODE (s1) == FIELD_DECL
1575 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1576 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1577 break;
1579 if (s1 && s2)
1580 tu->val = 0;
1581 else
1582 tu->val = needs_warning ? 2 : 1;
1583 return tu->val;
1586 default:
1587 gcc_unreachable ();
1591 /* Return 1 if two function types F1 and F2 are compatible.
1592 If either type specifies no argument types,
1593 the other must specify a fixed number of self-promoting arg types.
1594 Otherwise, if one type specifies only the number of arguments,
1595 the other must specify that number of self-promoting arg types.
1596 Otherwise, the argument types must match.
1597 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1599 static int
1600 function_types_compatible_p (const_tree f1, const_tree f2,
1601 bool *enum_and_int_p, bool *different_types_p)
1603 tree args1, args2;
1604 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1605 int val = 1;
1606 int val1;
1607 tree ret1, ret2;
1609 ret1 = TREE_TYPE (f1);
1610 ret2 = TREE_TYPE (f2);
1612 /* 'volatile' qualifiers on a function's return type used to mean
1613 the function is noreturn. */
1614 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1615 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1616 if (TYPE_VOLATILE (ret1))
1617 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1618 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1619 if (TYPE_VOLATILE (ret2))
1620 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1621 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1622 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1623 if (val == 0)
1624 return 0;
1626 args1 = TYPE_ARG_TYPES (f1);
1627 args2 = TYPE_ARG_TYPES (f2);
1629 if (different_types_p != NULL
1630 && (args1 == 0) != (args2 == 0))
1631 *different_types_p = true;
1633 /* An unspecified parmlist matches any specified parmlist
1634 whose argument types don't need default promotions. */
1636 if (args1 == 0)
1638 if (!self_promoting_args_p (args2))
1639 return 0;
1640 /* If one of these types comes from a non-prototype fn definition,
1641 compare that with the other type's arglist.
1642 If they don't match, ask for a warning (but no error). */
1643 if (TYPE_ACTUAL_ARG_TYPES (f1)
1644 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1645 enum_and_int_p, different_types_p))
1646 val = 2;
1647 return val;
1649 if (args2 == 0)
1651 if (!self_promoting_args_p (args1))
1652 return 0;
1653 if (TYPE_ACTUAL_ARG_TYPES (f2)
1654 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1655 enum_and_int_p, different_types_p))
1656 val = 2;
1657 return val;
1660 /* Both types have argument lists: compare them and propagate results. */
1661 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1662 different_types_p);
1663 return val1 != 1 ? val1 : val;
1666 /* Check two lists of types for compatibility, returning 0 for
1667 incompatible, 1 for compatible, or 2 for compatible with
1668 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1669 comptypes_internal. */
1671 static int
1672 type_lists_compatible_p (const_tree args1, const_tree args2,
1673 bool *enum_and_int_p, bool *different_types_p)
1675 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1676 int val = 1;
1677 int newval = 0;
1679 while (1)
1681 tree a1, mv1, a2, mv2;
1682 if (args1 == 0 && args2 == 0)
1683 return val;
1684 /* If one list is shorter than the other,
1685 they fail to match. */
1686 if (args1 == 0 || args2 == 0)
1687 return 0;
1688 mv1 = a1 = TREE_VALUE (args1);
1689 mv2 = a2 = TREE_VALUE (args2);
1690 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1691 mv1 = (TYPE_ATOMIC (mv1)
1692 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1693 TYPE_QUAL_ATOMIC)
1694 : TYPE_MAIN_VARIANT (mv1));
1695 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1696 mv2 = (TYPE_ATOMIC (mv2)
1697 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1698 TYPE_QUAL_ATOMIC)
1699 : TYPE_MAIN_VARIANT (mv2));
1700 /* A null pointer instead of a type
1701 means there is supposed to be an argument
1702 but nothing is specified about what type it has.
1703 So match anything that self-promotes. */
1704 if (different_types_p != NULL
1705 && (a1 == 0) != (a2 == 0))
1706 *different_types_p = true;
1707 if (a1 == 0)
1709 if (c_type_promotes_to (a2) != a2)
1710 return 0;
1712 else if (a2 == 0)
1714 if (c_type_promotes_to (a1) != a1)
1715 return 0;
1717 /* If one of the lists has an error marker, ignore this arg. */
1718 else if (TREE_CODE (a1) == ERROR_MARK
1719 || TREE_CODE (a2) == ERROR_MARK)
1721 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1722 different_types_p)))
1724 if (different_types_p != NULL)
1725 *different_types_p = true;
1726 /* Allow wait (union {union wait *u; int *i} *)
1727 and wait (union wait *) to be compatible. */
1728 if (TREE_CODE (a1) == UNION_TYPE
1729 && (TYPE_NAME (a1) == 0
1730 || TYPE_TRANSPARENT_AGGR (a1))
1731 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1732 && tree_int_cst_equal (TYPE_SIZE (a1),
1733 TYPE_SIZE (a2)))
1735 tree memb;
1736 for (memb = TYPE_FIELDS (a1);
1737 memb; memb = DECL_CHAIN (memb))
1739 tree mv3 = TREE_TYPE (memb);
1740 if (mv3 && mv3 != error_mark_node
1741 && TREE_CODE (mv3) != ARRAY_TYPE)
1742 mv3 = (TYPE_ATOMIC (mv3)
1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1744 TYPE_QUAL_ATOMIC)
1745 : TYPE_MAIN_VARIANT (mv3));
1746 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1747 different_types_p))
1748 break;
1750 if (memb == 0)
1751 return 0;
1753 else if (TREE_CODE (a2) == UNION_TYPE
1754 && (TYPE_NAME (a2) == 0
1755 || TYPE_TRANSPARENT_AGGR (a2))
1756 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1757 && tree_int_cst_equal (TYPE_SIZE (a2),
1758 TYPE_SIZE (a1)))
1760 tree memb;
1761 for (memb = TYPE_FIELDS (a2);
1762 memb; memb = DECL_CHAIN (memb))
1764 tree mv3 = TREE_TYPE (memb);
1765 if (mv3 && mv3 != error_mark_node
1766 && TREE_CODE (mv3) != ARRAY_TYPE)
1767 mv3 = (TYPE_ATOMIC (mv3)
1768 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1769 TYPE_QUAL_ATOMIC)
1770 : TYPE_MAIN_VARIANT (mv3));
1771 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1772 different_types_p))
1773 break;
1775 if (memb == 0)
1776 return 0;
1778 else
1779 return 0;
1782 /* comptypes said ok, but record if it said to warn. */
1783 if (newval > val)
1784 val = newval;
1786 args1 = TREE_CHAIN (args1);
1787 args2 = TREE_CHAIN (args2);
1791 /* Compute the size to increment a pointer by. When a function type or void
1792 type or incomplete type is passed, size_one_node is returned.
1793 This function does not emit any diagnostics; the caller is responsible
1794 for that. */
1796 static tree
1797 c_size_in_bytes (const_tree type)
1799 enum tree_code code = TREE_CODE (type);
1801 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1802 || !COMPLETE_TYPE_P (type))
1803 return size_one_node;
1805 /* Convert in case a char is more than one unit. */
1806 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1807 size_int (TYPE_PRECISION (char_type_node)
1808 / BITS_PER_UNIT));
1811 /* Return either DECL or its known constant value (if it has one). */
1813 tree
1814 decl_constant_value (tree decl)
1816 if (/* Don't change a variable array bound or initial value to a constant
1817 in a place where a variable is invalid. Note that DECL_INITIAL
1818 isn't valid for a PARM_DECL. */
1819 current_function_decl != 0
1820 && TREE_CODE (decl) != PARM_DECL
1821 && !TREE_THIS_VOLATILE (decl)
1822 && TREE_READONLY (decl)
1823 && DECL_INITIAL (decl) != 0
1824 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1825 /* This is invalid if initial value is not constant.
1826 If it has either a function call, a memory reference,
1827 or a variable, then re-evaluating it could give different results. */
1828 && TREE_CONSTANT (DECL_INITIAL (decl))
1829 /* Check for cases where this is sub-optimal, even though valid. */
1830 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1831 return DECL_INITIAL (decl);
1832 return decl;
1835 /* Convert the array expression EXP to a pointer. */
1836 static tree
1837 array_to_pointer_conversion (location_t loc, tree exp)
1839 tree orig_exp = exp;
1840 tree type = TREE_TYPE (exp);
1841 tree adr;
1842 tree restype = TREE_TYPE (type);
1843 tree ptrtype;
1845 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1847 STRIP_TYPE_NOPS (exp);
1849 if (TREE_NO_WARNING (orig_exp))
1850 TREE_NO_WARNING (exp) = 1;
1852 ptrtype = build_pointer_type (restype);
1854 if (TREE_CODE (exp) == INDIRECT_REF)
1855 return convert (ptrtype, TREE_OPERAND (exp, 0));
1857 /* In C++ array compound literals are temporary objects unless they are
1858 const or appear in namespace scope, so they are destroyed too soon
1859 to use them for much of anything (c++/53220). */
1860 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1862 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1863 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1864 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1865 "converting an array compound literal to a pointer "
1866 "is ill-formed in C++");
1869 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1870 return convert (ptrtype, adr);
1873 /* Convert the function expression EXP to a pointer. */
1874 static tree
1875 function_to_pointer_conversion (location_t loc, tree exp)
1877 tree orig_exp = exp;
1879 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1881 STRIP_TYPE_NOPS (exp);
1883 if (TREE_NO_WARNING (orig_exp))
1884 TREE_NO_WARNING (exp) = 1;
1886 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1889 /* Mark EXP as read, not just set, for set but not used -Wunused
1890 warning purposes. */
1892 void
1893 mark_exp_read (tree exp)
1895 switch (TREE_CODE (exp))
1897 case VAR_DECL:
1898 case PARM_DECL:
1899 DECL_READ_P (exp) = 1;
1900 break;
1901 case ARRAY_REF:
1902 case COMPONENT_REF:
1903 case MODIFY_EXPR:
1904 case REALPART_EXPR:
1905 case IMAGPART_EXPR:
1906 CASE_CONVERT:
1907 case ADDR_EXPR:
1908 mark_exp_read (TREE_OPERAND (exp, 0));
1909 break;
1910 case COMPOUND_EXPR:
1911 case C_MAYBE_CONST_EXPR:
1912 mark_exp_read (TREE_OPERAND (exp, 1));
1913 break;
1914 default:
1915 break;
1919 /* Perform the default conversion of arrays and functions to pointers.
1920 Return the result of converting EXP. For any other expression, just
1921 return EXP.
1923 LOC is the location of the expression. */
1925 struct c_expr
1926 default_function_array_conversion (location_t loc, struct c_expr exp)
1928 tree orig_exp = exp.value;
1929 tree type = TREE_TYPE (exp.value);
1930 enum tree_code code = TREE_CODE (type);
1932 switch (code)
1934 case ARRAY_TYPE:
1936 bool not_lvalue = false;
1937 bool lvalue_array_p;
1939 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1940 || CONVERT_EXPR_P (exp.value))
1941 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1943 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1944 not_lvalue = true;
1945 exp.value = TREE_OPERAND (exp.value, 0);
1948 if (TREE_NO_WARNING (orig_exp))
1949 TREE_NO_WARNING (exp.value) = 1;
1951 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1952 if (!flag_isoc99 && !lvalue_array_p)
1954 /* Before C99, non-lvalue arrays do not decay to pointers.
1955 Normally, using such an array would be invalid; but it can
1956 be used correctly inside sizeof or as a statement expression.
1957 Thus, do not give an error here; an error will result later. */
1958 return exp;
1961 exp.value = array_to_pointer_conversion (loc, exp.value);
1963 break;
1964 case FUNCTION_TYPE:
1965 exp.value = function_to_pointer_conversion (loc, exp.value);
1966 break;
1967 default:
1968 break;
1971 return exp;
1974 struct c_expr
1975 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1977 mark_exp_read (exp.value);
1978 return default_function_array_conversion (loc, exp);
1981 /* Return whether EXPR should be treated as an atomic lvalue for the
1982 purposes of load and store handling. */
1984 static bool
1985 really_atomic_lvalue (tree expr)
1987 if (error_operand_p (expr))
1988 return false;
1989 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1990 return false;
1991 if (!lvalue_p (expr))
1992 return false;
1994 /* Ignore _Atomic on register variables, since their addresses can't
1995 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1996 sequences wouldn't work. Ignore _Atomic on structures containing
1997 bit-fields, since accessing elements of atomic structures or
1998 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1999 it's undefined at translation time or execution time, and the
2000 normal atomic sequences again wouldn't work. */
2001 while (handled_component_p (expr))
2003 if (TREE_CODE (expr) == COMPONENT_REF
2004 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2005 return false;
2006 expr = TREE_OPERAND (expr, 0);
2008 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2009 return false;
2010 return true;
2013 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2014 including converting functions and arrays to pointers if CONVERT_P.
2015 If READ_P, also mark the expression as having been read. */
2017 struct c_expr
2018 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2019 bool convert_p, bool read_p)
2021 if (read_p)
2022 mark_exp_read (exp.value);
2023 if (convert_p)
2024 exp = default_function_array_conversion (loc, exp);
2025 if (really_atomic_lvalue (exp.value))
2027 vec<tree, va_gc> *params;
2028 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2029 tree expr_type = TREE_TYPE (exp.value);
2030 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2031 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2033 gcc_assert (TYPE_ATOMIC (expr_type));
2035 /* Expansion of a generic atomic load may require an addition
2036 element, so allocate enough to prevent a resize. */
2037 vec_alloc (params, 4);
2039 /* Remove the qualifiers for the rest of the expressions and
2040 create the VAL temp variable to hold the RHS. */
2041 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2042 tmp = create_tmp_var (nonatomic_type);
2043 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2044 TREE_ADDRESSABLE (tmp) = 1;
2045 TREE_NO_WARNING (tmp) = 1;
2047 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2048 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2049 params->quick_push (expr_addr);
2050 params->quick_push (tmp_addr);
2051 params->quick_push (seq_cst);
2052 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2054 /* EXPR is always read. */
2055 mark_exp_read (exp.value);
2057 /* Return tmp which contains the value loaded. */
2058 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2060 return exp;
2063 /* EXP is an expression of integer type. Apply the integer promotions
2064 to it and return the promoted value. */
2066 tree
2067 perform_integral_promotions (tree exp)
2069 tree type = TREE_TYPE (exp);
2070 enum tree_code code = TREE_CODE (type);
2072 gcc_assert (INTEGRAL_TYPE_P (type));
2074 /* Normally convert enums to int,
2075 but convert wide enums to something wider. */
2076 if (code == ENUMERAL_TYPE)
2078 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2079 TYPE_PRECISION (integer_type_node)),
2080 ((TYPE_PRECISION (type)
2081 >= TYPE_PRECISION (integer_type_node))
2082 && TYPE_UNSIGNED (type)));
2084 return convert (type, exp);
2087 /* ??? This should no longer be needed now bit-fields have their
2088 proper types. */
2089 if (TREE_CODE (exp) == COMPONENT_REF
2090 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2091 /* If it's thinner than an int, promote it like a
2092 c_promoting_integer_type_p, otherwise leave it alone. */
2093 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2094 TYPE_PRECISION (integer_type_node)))
2095 return convert (integer_type_node, exp);
2097 if (c_promoting_integer_type_p (type))
2099 /* Preserve unsignedness if not really getting any wider. */
2100 if (TYPE_UNSIGNED (type)
2101 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2102 return convert (unsigned_type_node, exp);
2104 return convert (integer_type_node, exp);
2107 return exp;
2111 /* Perform default promotions for C data used in expressions.
2112 Enumeral types or short or char are converted to int.
2113 In addition, manifest constants symbols are replaced by their values. */
2115 tree
2116 default_conversion (tree exp)
2118 tree orig_exp;
2119 tree type = TREE_TYPE (exp);
2120 enum tree_code code = TREE_CODE (type);
2121 tree promoted_type;
2123 mark_exp_read (exp);
2125 /* Functions and arrays have been converted during parsing. */
2126 gcc_assert (code != FUNCTION_TYPE);
2127 if (code == ARRAY_TYPE)
2128 return exp;
2130 /* Constants can be used directly unless they're not loadable. */
2131 if (TREE_CODE (exp) == CONST_DECL)
2132 exp = DECL_INITIAL (exp);
2134 /* Strip no-op conversions. */
2135 orig_exp = exp;
2136 STRIP_TYPE_NOPS (exp);
2138 if (TREE_NO_WARNING (orig_exp))
2139 TREE_NO_WARNING (exp) = 1;
2141 if (code == VOID_TYPE)
2143 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2144 "void value not ignored as it ought to be");
2145 return error_mark_node;
2148 exp = require_complete_type (exp);
2149 if (exp == error_mark_node)
2150 return error_mark_node;
2152 promoted_type = targetm.promoted_type (type);
2153 if (promoted_type)
2154 return convert (promoted_type, exp);
2156 if (INTEGRAL_TYPE_P (type))
2157 return perform_integral_promotions (exp);
2159 return exp;
2162 /* Look up COMPONENT in a structure or union TYPE.
2164 If the component name is not found, returns NULL_TREE. Otherwise,
2165 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2166 stepping down the chain to the component, which is in the last
2167 TREE_VALUE of the list. Normally the list is of length one, but if
2168 the component is embedded within (nested) anonymous structures or
2169 unions, the list steps down the chain to the component. */
2171 static tree
2172 lookup_field (tree type, tree component)
2174 tree field;
2176 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2177 to the field elements. Use a binary search on this array to quickly
2178 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2179 will always be set for structures which have many elements. */
2181 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2183 int bot, top, half;
2184 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2186 field = TYPE_FIELDS (type);
2187 bot = 0;
2188 top = TYPE_LANG_SPECIFIC (type)->s->len;
2189 while (top - bot > 1)
2191 half = (top - bot + 1) >> 1;
2192 field = field_array[bot+half];
2194 if (DECL_NAME (field) == NULL_TREE)
2196 /* Step through all anon unions in linear fashion. */
2197 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2199 field = field_array[bot++];
2200 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2201 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2203 tree anon = lookup_field (TREE_TYPE (field), component);
2205 if (anon)
2206 return tree_cons (NULL_TREE, field, anon);
2208 /* The Plan 9 compiler permits referring
2209 directly to an anonymous struct/union field
2210 using a typedef name. */
2211 if (flag_plan9_extensions
2212 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2213 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2214 == TYPE_DECL)
2215 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2216 == component))
2217 break;
2221 /* Entire record is only anon unions. */
2222 if (bot > top)
2223 return NULL_TREE;
2225 /* Restart the binary search, with new lower bound. */
2226 continue;
2229 if (DECL_NAME (field) == component)
2230 break;
2231 if (DECL_NAME (field) < component)
2232 bot += half;
2233 else
2234 top = bot + half;
2237 if (DECL_NAME (field_array[bot]) == component)
2238 field = field_array[bot];
2239 else if (DECL_NAME (field) != component)
2240 return NULL_TREE;
2242 else
2244 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2246 if (DECL_NAME (field) == NULL_TREE
2247 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2248 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2250 tree anon = lookup_field (TREE_TYPE (field), component);
2252 if (anon)
2253 return tree_cons (NULL_TREE, field, anon);
2255 /* The Plan 9 compiler permits referring directly to an
2256 anonymous struct/union field using a typedef
2257 name. */
2258 if (flag_plan9_extensions
2259 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2260 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2261 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2262 == component))
2263 break;
2266 if (DECL_NAME (field) == component)
2267 break;
2270 if (field == NULL_TREE)
2271 return NULL_TREE;
2274 return tree_cons (NULL_TREE, field, NULL_TREE);
2277 /* Make an expression to refer to the COMPONENT field of structure or
2278 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2279 location of the COMPONENT_REF. */
2281 tree
2282 build_component_ref (location_t loc, tree datum, tree component)
2284 tree type = TREE_TYPE (datum);
2285 enum tree_code code = TREE_CODE (type);
2286 tree field = NULL;
2287 tree ref;
2288 bool datum_lvalue = lvalue_p (datum);
2290 if (!objc_is_public (datum, component))
2291 return error_mark_node;
2293 /* Detect Objective-C property syntax object.property. */
2294 if (c_dialect_objc ()
2295 && (ref = objc_maybe_build_component_ref (datum, component)))
2296 return ref;
2298 /* See if there is a field or component with name COMPONENT. */
2300 if (code == RECORD_TYPE || code == UNION_TYPE)
2302 if (!COMPLETE_TYPE_P (type))
2304 c_incomplete_type_error (NULL_TREE, type);
2305 return error_mark_node;
2308 field = lookup_field (type, component);
2310 if (!field)
2312 error_at (loc, "%qT has no member named %qE", type, component);
2313 return error_mark_node;
2316 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2317 This might be better solved in future the way the C++ front
2318 end does it - by giving the anonymous entities each a
2319 separate name and type, and then have build_component_ref
2320 recursively call itself. We can't do that here. */
2323 tree subdatum = TREE_VALUE (field);
2324 int quals;
2325 tree subtype;
2326 bool use_datum_quals;
2328 if (TREE_TYPE (subdatum) == error_mark_node)
2329 return error_mark_node;
2331 /* If this is an rvalue, it does not have qualifiers in C
2332 standard terms and we must avoid propagating such
2333 qualifiers down to a non-lvalue array that is then
2334 converted to a pointer. */
2335 use_datum_quals = (datum_lvalue
2336 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2338 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2339 if (use_datum_quals)
2340 quals |= TYPE_QUALS (TREE_TYPE (datum));
2341 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2343 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2344 NULL_TREE);
2345 SET_EXPR_LOCATION (ref, loc);
2346 if (TREE_READONLY (subdatum)
2347 || (use_datum_quals && TREE_READONLY (datum)))
2348 TREE_READONLY (ref) = 1;
2349 if (TREE_THIS_VOLATILE (subdatum)
2350 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2351 TREE_THIS_VOLATILE (ref) = 1;
2353 if (TREE_DEPRECATED (subdatum))
2354 warn_deprecated_use (subdatum, NULL_TREE);
2356 datum = ref;
2358 field = TREE_CHAIN (field);
2360 while (field);
2362 return ref;
2364 else if (code != ERROR_MARK)
2365 error_at (loc,
2366 "request for member %qE in something not a structure or union",
2367 component);
2369 return error_mark_node;
2372 /* Given an expression PTR for a pointer, return an expression
2373 for the value pointed to.
2374 ERRORSTRING is the name of the operator to appear in error messages.
2376 LOC is the location to use for the generated tree. */
2378 tree
2379 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2381 tree pointer = default_conversion (ptr);
2382 tree type = TREE_TYPE (pointer);
2383 tree ref;
2385 if (TREE_CODE (type) == POINTER_TYPE)
2387 if (CONVERT_EXPR_P (pointer)
2388 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2390 /* If a warning is issued, mark it to avoid duplicates from
2391 the backend. This only needs to be done at
2392 warn_strict_aliasing > 2. */
2393 if (warn_strict_aliasing > 2)
2394 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2395 type, TREE_OPERAND (pointer, 0)))
2396 TREE_NO_WARNING (pointer) = 1;
2399 if (TREE_CODE (pointer) == ADDR_EXPR
2400 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2401 == TREE_TYPE (type)))
2403 ref = TREE_OPERAND (pointer, 0);
2404 protected_set_expr_location (ref, loc);
2405 return ref;
2407 else
2409 tree t = TREE_TYPE (type);
2411 ref = build1 (INDIRECT_REF, t, pointer);
2413 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2415 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2417 error_at (loc, "dereferencing pointer to incomplete type "
2418 "%qT", t);
2419 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2421 return error_mark_node;
2423 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2424 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2426 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2427 so that we get the proper error message if the result is used
2428 to assign to. Also, &* is supposed to be a no-op.
2429 And ANSI C seems to specify that the type of the result
2430 should be the const type. */
2431 /* A de-reference of a pointer to const is not a const. It is valid
2432 to change it via some other pointer. */
2433 TREE_READONLY (ref) = TYPE_READONLY (t);
2434 TREE_SIDE_EFFECTS (ref)
2435 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2436 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2437 protected_set_expr_location (ref, loc);
2438 return ref;
2441 else if (TREE_CODE (pointer) != ERROR_MARK)
2442 invalid_indirection_error (loc, type, errstring);
2444 return error_mark_node;
2447 /* This handles expressions of the form "a[i]", which denotes
2448 an array reference.
2450 This is logically equivalent in C to *(a+i), but we may do it differently.
2451 If A is a variable or a member, we generate a primitive ARRAY_REF.
2452 This avoids forcing the array out of registers, and can work on
2453 arrays that are not lvalues (for example, members of structures returned
2454 by functions).
2456 For vector types, allow vector[i] but not i[vector], and create
2457 *(((type*)&vectortype) + i) for the expression.
2459 LOC is the location to use for the returned expression. */
2461 tree
2462 build_array_ref (location_t loc, tree array, tree index)
2464 tree ret;
2465 bool swapped = false;
2466 if (TREE_TYPE (array) == error_mark_node
2467 || TREE_TYPE (index) == error_mark_node)
2468 return error_mark_node;
2470 if (flag_cilkplus && contains_array_notation_expr (index))
2472 size_t rank = 0;
2473 if (!find_rank (loc, index, index, true, &rank))
2474 return error_mark_node;
2475 if (rank > 1)
2477 error_at (loc, "rank of the array's index is greater than 1");
2478 return error_mark_node;
2481 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2482 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2483 /* Allow vector[index] but not index[vector]. */
2484 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2486 tree temp;
2487 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2488 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2490 error_at (loc,
2491 "subscripted value is neither array nor pointer nor vector");
2493 return error_mark_node;
2495 temp = array;
2496 array = index;
2497 index = temp;
2498 swapped = true;
2501 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2503 error_at (loc, "array subscript is not an integer");
2504 return error_mark_node;
2507 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2509 error_at (loc, "subscripted value is pointer to function");
2510 return error_mark_node;
2513 /* ??? Existing practice has been to warn only when the char
2514 index is syntactically the index, not for char[array]. */
2515 if (!swapped)
2516 warn_array_subscript_with_type_char (loc, index);
2518 /* Apply default promotions *after* noticing character types. */
2519 index = default_conversion (index);
2520 if (index == error_mark_node)
2521 return error_mark_node;
2523 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2525 bool non_lvalue
2526 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2528 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2530 tree rval, type;
2532 /* An array that is indexed by a non-constant
2533 cannot be stored in a register; we must be able to do
2534 address arithmetic on its address.
2535 Likewise an array of elements of variable size. */
2536 if (TREE_CODE (index) != INTEGER_CST
2537 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2538 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2540 if (!c_mark_addressable (array))
2541 return error_mark_node;
2543 /* An array that is indexed by a constant value which is not within
2544 the array bounds cannot be stored in a register either; because we
2545 would get a crash in store_bit_field/extract_bit_field when trying
2546 to access a non-existent part of the register. */
2547 if (TREE_CODE (index) == INTEGER_CST
2548 && TYPE_DOMAIN (TREE_TYPE (array))
2549 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2551 if (!c_mark_addressable (array))
2552 return error_mark_node;
2555 if (pedantic || warn_c90_c99_compat)
2557 tree foo = array;
2558 while (TREE_CODE (foo) == COMPONENT_REF)
2559 foo = TREE_OPERAND (foo, 0);
2560 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2561 pedwarn (loc, OPT_Wpedantic,
2562 "ISO C forbids subscripting %<register%> array");
2563 else if (!lvalue_p (foo))
2564 pedwarn_c90 (loc, OPT_Wpedantic,
2565 "ISO C90 forbids subscripting non-lvalue "
2566 "array");
2569 type = TREE_TYPE (TREE_TYPE (array));
2570 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2571 /* Array ref is const/volatile if the array elements are
2572 or if the array is. */
2573 TREE_READONLY (rval)
2574 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2575 | TREE_READONLY (array));
2576 TREE_SIDE_EFFECTS (rval)
2577 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2578 | TREE_SIDE_EFFECTS (array));
2579 TREE_THIS_VOLATILE (rval)
2580 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2581 /* This was added by rms on 16 Nov 91.
2582 It fixes vol struct foo *a; a->elts[1]
2583 in an inline function.
2584 Hope it doesn't break something else. */
2585 | TREE_THIS_VOLATILE (array));
2586 ret = require_complete_type (rval);
2587 protected_set_expr_location (ret, loc);
2588 if (non_lvalue)
2589 ret = non_lvalue_loc (loc, ret);
2590 return ret;
2592 else
2594 tree ar = default_conversion (array);
2596 if (ar == error_mark_node)
2597 return ar;
2599 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2600 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2602 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2603 index, 0),
2604 RO_ARRAY_INDEXING);
2605 if (non_lvalue)
2606 ret = non_lvalue_loc (loc, ret);
2607 return ret;
2611 /* Build an external reference to identifier ID. FUN indicates
2612 whether this will be used for a function call. LOC is the source
2613 location of the identifier. This sets *TYPE to the type of the
2614 identifier, which is not the same as the type of the returned value
2615 for CONST_DECLs defined as enum constants. If the type of the
2616 identifier is not available, *TYPE is set to NULL. */
2617 tree
2618 build_external_ref (location_t loc, tree id, int fun, tree *type)
2620 tree ref;
2621 tree decl = lookup_name (id);
2623 /* In Objective-C, an instance variable (ivar) may be preferred to
2624 whatever lookup_name() found. */
2625 decl = objc_lookup_ivar (decl, id);
2627 *type = NULL;
2628 if (decl && decl != error_mark_node)
2630 ref = decl;
2631 *type = TREE_TYPE (ref);
2633 else if (fun)
2634 /* Implicit function declaration. */
2635 ref = implicitly_declare (loc, id);
2636 else if (decl == error_mark_node)
2637 /* Don't complain about something that's already been
2638 complained about. */
2639 return error_mark_node;
2640 else
2642 undeclared_variable (loc, id);
2643 return error_mark_node;
2646 if (TREE_TYPE (ref) == error_mark_node)
2647 return error_mark_node;
2649 if (TREE_DEPRECATED (ref))
2650 warn_deprecated_use (ref, NULL_TREE);
2652 /* Recursive call does not count as usage. */
2653 if (ref != current_function_decl)
2655 TREE_USED (ref) = 1;
2658 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2660 if (!in_sizeof && !in_typeof)
2661 C_DECL_USED (ref) = 1;
2662 else if (DECL_INITIAL (ref) == 0
2663 && DECL_EXTERNAL (ref)
2664 && !TREE_PUBLIC (ref))
2665 record_maybe_used_decl (ref);
2668 if (TREE_CODE (ref) == CONST_DECL)
2670 used_types_insert (TREE_TYPE (ref));
2672 if (warn_cxx_compat
2673 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2674 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2676 warning_at (loc, OPT_Wc___compat,
2677 ("enum constant defined in struct or union "
2678 "is not visible in C++"));
2679 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2682 ref = DECL_INITIAL (ref);
2683 TREE_CONSTANT (ref) = 1;
2685 else if (current_function_decl != 0
2686 && !DECL_FILE_SCOPE_P (current_function_decl)
2687 && (TREE_CODE (ref) == VAR_DECL
2688 || TREE_CODE (ref) == PARM_DECL
2689 || TREE_CODE (ref) == FUNCTION_DECL))
2691 tree context = decl_function_context (ref);
2693 if (context != 0 && context != current_function_decl)
2694 DECL_NONLOCAL (ref) = 1;
2696 /* C99 6.7.4p3: An inline definition of a function with external
2697 linkage ... shall not contain a reference to an identifier with
2698 internal linkage. */
2699 else if (current_function_decl != 0
2700 && DECL_DECLARED_INLINE_P (current_function_decl)
2701 && DECL_EXTERNAL (current_function_decl)
2702 && VAR_OR_FUNCTION_DECL_P (ref)
2703 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2704 && ! TREE_PUBLIC (ref)
2705 && DECL_CONTEXT (ref) != current_function_decl)
2706 record_inline_static (loc, current_function_decl, ref,
2707 csi_internal);
2709 return ref;
2712 /* Record details of decls possibly used inside sizeof or typeof. */
2713 struct maybe_used_decl
2715 /* The decl. */
2716 tree decl;
2717 /* The level seen at (in_sizeof + in_typeof). */
2718 int level;
2719 /* The next one at this level or above, or NULL. */
2720 struct maybe_used_decl *next;
2723 static struct maybe_used_decl *maybe_used_decls;
2725 /* Record that DECL, an undefined static function reference seen
2726 inside sizeof or typeof, might be used if the operand of sizeof is
2727 a VLA type or the operand of typeof is a variably modified
2728 type. */
2730 static void
2731 record_maybe_used_decl (tree decl)
2733 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2734 t->decl = decl;
2735 t->level = in_sizeof + in_typeof;
2736 t->next = maybe_used_decls;
2737 maybe_used_decls = t;
2740 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2741 USED is false, just discard them. If it is true, mark them used
2742 (if no longer inside sizeof or typeof) or move them to the next
2743 level up (if still inside sizeof or typeof). */
2745 void
2746 pop_maybe_used (bool used)
2748 struct maybe_used_decl *p = maybe_used_decls;
2749 int cur_level = in_sizeof + in_typeof;
2750 while (p && p->level > cur_level)
2752 if (used)
2754 if (cur_level == 0)
2755 C_DECL_USED (p->decl) = 1;
2756 else
2757 p->level = cur_level;
2759 p = p->next;
2761 if (!used || cur_level == 0)
2762 maybe_used_decls = p;
2765 /* Return the result of sizeof applied to EXPR. */
2767 struct c_expr
2768 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2770 struct c_expr ret;
2771 if (expr.value == error_mark_node)
2773 ret.value = error_mark_node;
2774 ret.original_code = ERROR_MARK;
2775 ret.original_type = NULL;
2776 pop_maybe_used (false);
2778 else
2780 bool expr_const_operands = true;
2782 if (TREE_CODE (expr.value) == PARM_DECL
2783 && C_ARRAY_PARAMETER (expr.value))
2785 if (warning_at (loc, OPT_Wsizeof_array_argument,
2786 "%<sizeof%> on array function parameter %qE will "
2787 "return size of %qT", expr.value,
2788 expr.original_type))
2789 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2791 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2792 &expr_const_operands);
2793 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2794 c_last_sizeof_arg = expr.value;
2795 ret.original_code = SIZEOF_EXPR;
2796 ret.original_type = NULL;
2797 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2799 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2800 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2801 folded_expr, ret.value);
2802 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2803 SET_EXPR_LOCATION (ret.value, loc);
2805 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2807 return ret;
2810 /* Return the result of sizeof applied to T, a structure for the type
2811 name passed to sizeof (rather than the type itself). LOC is the
2812 location of the original expression. */
2814 struct c_expr
2815 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2817 tree type;
2818 struct c_expr ret;
2819 tree type_expr = NULL_TREE;
2820 bool type_expr_const = true;
2821 type = groktypename (t, &type_expr, &type_expr_const);
2822 ret.value = c_sizeof (loc, type);
2823 c_last_sizeof_arg = type;
2824 ret.original_code = SIZEOF_EXPR;
2825 ret.original_type = NULL;
2826 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2827 && c_vla_type_p (type))
2829 /* If the type is a [*] array, it is a VLA but is represented as
2830 having a size of zero. In such a case we must ensure that
2831 the result of sizeof does not get folded to a constant by
2832 c_fully_fold, because if the size is evaluated the result is
2833 not constant and so constraints on zero or negative size
2834 arrays must not be applied when this sizeof call is inside
2835 another array declarator. */
2836 if (!type_expr)
2837 type_expr = integer_zero_node;
2838 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2839 type_expr, ret.value);
2840 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2842 pop_maybe_used (type != error_mark_node
2843 ? C_TYPE_VARIABLE_SIZE (type) : false);
2844 return ret;
2847 /* Build a function call to function FUNCTION with parameters PARAMS.
2848 The function call is at LOC.
2849 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2850 TREE_VALUE of each node is a parameter-expression.
2851 FUNCTION's data type may be a function type or a pointer-to-function. */
2853 tree
2854 build_function_call (location_t loc, tree function, tree params)
2856 vec<tree, va_gc> *v;
2857 tree ret;
2859 vec_alloc (v, list_length (params));
2860 for (; params; params = TREE_CHAIN (params))
2861 v->quick_push (TREE_VALUE (params));
2862 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2863 vec_free (v);
2864 return ret;
2867 /* Give a note about the location of the declaration of DECL. */
2869 static void inform_declaration (tree decl)
2871 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2872 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2875 /* Build a function call to function FUNCTION with parameters PARAMS.
2876 ORIGTYPES, if not NULL, is a vector of types; each element is
2877 either NULL or the original type of the corresponding element in
2878 PARAMS. The original type may differ from TREE_TYPE of the
2879 parameter for enums. FUNCTION's data type may be a function type
2880 or pointer-to-function. This function changes the elements of
2881 PARAMS. */
2883 tree
2884 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2885 tree function, vec<tree, va_gc> *params,
2886 vec<tree, va_gc> *origtypes)
2888 tree fntype, fundecl = 0;
2889 tree name = NULL_TREE, result;
2890 tree tem;
2891 int nargs;
2892 tree *argarray;
2895 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2896 STRIP_TYPE_NOPS (function);
2898 /* Convert anything with function type to a pointer-to-function. */
2899 if (TREE_CODE (function) == FUNCTION_DECL)
2901 name = DECL_NAME (function);
2903 if (flag_tm)
2904 tm_malloc_replacement (function);
2905 fundecl = function;
2906 /* Atomic functions have type checking/casting already done. They are
2907 often rewritten and don't match the original parameter list. */
2908 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2909 origtypes = NULL;
2911 if (flag_cilkplus
2912 && is_cilkplus_reduce_builtin (function))
2913 origtypes = NULL;
2915 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2916 function = function_to_pointer_conversion (loc, function);
2918 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2919 expressions, like those used for ObjC messenger dispatches. */
2920 if (params && !params->is_empty ())
2921 function = objc_rewrite_function_call (function, (*params)[0]);
2923 function = c_fully_fold (function, false, NULL);
2925 fntype = TREE_TYPE (function);
2927 if (TREE_CODE (fntype) == ERROR_MARK)
2928 return error_mark_node;
2930 if (!(TREE_CODE (fntype) == POINTER_TYPE
2931 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2933 if (!flag_diagnostics_show_caret)
2934 error_at (loc,
2935 "called object %qE is not a function or function pointer",
2936 function);
2937 else if (DECL_P (function))
2939 error_at (loc,
2940 "called object %qD is not a function or function pointer",
2941 function);
2942 inform_declaration (function);
2944 else
2945 error_at (loc,
2946 "called object is not a function or function pointer");
2947 return error_mark_node;
2950 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2951 current_function_returns_abnormally = 1;
2953 /* fntype now gets the type of function pointed to. */
2954 fntype = TREE_TYPE (fntype);
2956 /* Convert the parameters to the types declared in the
2957 function prototype, or apply default promotions. */
2959 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2960 origtypes, function, fundecl);
2961 if (nargs < 0)
2962 return error_mark_node;
2964 /* Check that the function is called through a compatible prototype.
2965 If it is not, warn. */
2966 if (CONVERT_EXPR_P (function)
2967 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2968 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2969 && !comptypes (fntype, TREE_TYPE (tem)))
2971 tree return_type = TREE_TYPE (fntype);
2973 /* This situation leads to run-time undefined behavior. We can't,
2974 therefore, simply error unless we can prove that all possible
2975 executions of the program must execute the code. */
2976 warning_at (loc, 0, "function called through a non-compatible type");
2978 if (VOID_TYPE_P (return_type)
2979 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2980 pedwarn (loc, 0,
2981 "function with qualified void return type called");
2984 argarray = vec_safe_address (params);
2986 /* Check that arguments to builtin functions match the expectations. */
2987 if (fundecl
2988 && DECL_BUILT_IN (fundecl)
2989 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2990 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2991 return error_mark_node;
2993 /* Check that the arguments to the function are valid. */
2994 check_function_arguments (fntype, nargs, argarray);
2996 if (name != NULL_TREE
2997 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2999 if (require_constant_value)
3000 result =
3001 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3002 function, nargs, argarray);
3003 else
3004 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3005 function, nargs, argarray);
3006 if (TREE_CODE (result) == NOP_EXPR
3007 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3008 STRIP_TYPE_NOPS (result);
3010 else
3011 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3012 function, nargs, argarray);
3014 if (VOID_TYPE_P (TREE_TYPE (result)))
3016 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3017 pedwarn (loc, 0,
3018 "function with qualified void return type called");
3019 return result;
3021 return require_complete_type (result);
3024 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3026 tree
3027 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3028 tree function, vec<tree, va_gc> *params,
3029 vec<tree, va_gc> *origtypes)
3031 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3032 STRIP_TYPE_NOPS (function);
3034 /* Convert anything with function type to a pointer-to-function. */
3035 if (TREE_CODE (function) == FUNCTION_DECL)
3037 /* Implement type-directed function overloading for builtins.
3038 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3039 handle all the type checking. The result is a complete expression
3040 that implements this function call. */
3041 tree tem = resolve_overloaded_builtin (loc, function, params);
3042 if (tem)
3043 return tem;
3045 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3048 /* Convert the argument expressions in the vector VALUES
3049 to the types in the list TYPELIST.
3051 If TYPELIST is exhausted, or when an element has NULL as its type,
3052 perform the default conversions.
3054 ORIGTYPES is the original types of the expressions in VALUES. This
3055 holds the type of enum values which have been converted to integral
3056 types. It may be NULL.
3058 FUNCTION is a tree for the called function. It is used only for
3059 error messages, where it is formatted with %qE.
3061 This is also where warnings about wrong number of args are generated.
3063 ARG_LOC are locations of function arguments (if any).
3065 Returns the actual number of arguments processed (which may be less
3066 than the length of VALUES in some error situations), or -1 on
3067 failure. */
3069 static int
3070 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3071 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3072 tree function, tree fundecl)
3074 tree typetail, val;
3075 unsigned int parmnum;
3076 bool error_args = false;
3077 const bool type_generic = fundecl
3078 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3079 bool type_generic_remove_excess_precision = false;
3080 tree selector;
3082 /* Change pointer to function to the function itself for
3083 diagnostics. */
3084 if (TREE_CODE (function) == ADDR_EXPR
3085 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3086 function = TREE_OPERAND (function, 0);
3088 /* Handle an ObjC selector specially for diagnostics. */
3089 selector = objc_message_selector ();
3091 /* For type-generic built-in functions, determine whether excess
3092 precision should be removed (classification) or not
3093 (comparison). */
3094 if (type_generic
3095 && DECL_BUILT_IN (fundecl)
3096 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3098 switch (DECL_FUNCTION_CODE (fundecl))
3100 case BUILT_IN_ISFINITE:
3101 case BUILT_IN_ISINF:
3102 case BUILT_IN_ISINF_SIGN:
3103 case BUILT_IN_ISNAN:
3104 case BUILT_IN_ISNORMAL:
3105 case BUILT_IN_FPCLASSIFY:
3106 type_generic_remove_excess_precision = true;
3107 break;
3109 default:
3110 type_generic_remove_excess_precision = false;
3111 break;
3114 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3115 return vec_safe_length (values);
3117 /* Scan the given expressions and types, producing individual
3118 converted arguments. */
3120 for (typetail = typelist, parmnum = 0;
3121 values && values->iterate (parmnum, &val);
3122 ++parmnum)
3124 tree type = typetail ? TREE_VALUE (typetail) : 0;
3125 tree valtype = TREE_TYPE (val);
3126 tree rname = function;
3127 int argnum = parmnum + 1;
3128 const char *invalid_func_diag;
3129 bool excess_precision = false;
3130 bool npc;
3131 tree parmval;
3132 /* Some __atomic_* builtins have additional hidden argument at
3133 position 0. */
3134 location_t ploc
3135 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3136 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3137 : input_location;
3139 if (type == void_type_node)
3141 if (selector)
3142 error_at (loc, "too many arguments to method %qE", selector);
3143 else
3144 error_at (loc, "too many arguments to function %qE", function);
3145 inform_declaration (fundecl);
3146 return error_args ? -1 : (int) parmnum;
3149 if (selector && argnum > 2)
3151 rname = selector;
3152 argnum -= 2;
3155 npc = null_pointer_constant_p (val);
3157 /* If there is excess precision and a prototype, convert once to
3158 the required type rather than converting via the semantic
3159 type. Likewise without a prototype a float value represented
3160 as long double should be converted once to double. But for
3161 type-generic classification functions excess precision must
3162 be removed here. */
3163 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3164 && (type || !type_generic || !type_generic_remove_excess_precision))
3166 val = TREE_OPERAND (val, 0);
3167 excess_precision = true;
3169 val = c_fully_fold (val, false, NULL);
3170 STRIP_TYPE_NOPS (val);
3172 val = require_complete_type (val);
3174 if (type != 0)
3176 /* Formal parm type is specified by a function prototype. */
3178 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3180 error_at (ploc, "type of formal parameter %d is incomplete",
3181 parmnum + 1);
3182 parmval = val;
3184 else
3186 tree origtype;
3188 /* Optionally warn about conversions that
3189 differ from the default conversions. */
3190 if (warn_traditional_conversion || warn_traditional)
3192 unsigned int formal_prec = TYPE_PRECISION (type);
3194 if (INTEGRAL_TYPE_P (type)
3195 && TREE_CODE (valtype) == REAL_TYPE)
3196 warning_at (ploc, OPT_Wtraditional_conversion,
3197 "passing argument %d of %qE as integer rather "
3198 "than floating due to prototype",
3199 argnum, rname);
3200 if (INTEGRAL_TYPE_P (type)
3201 && TREE_CODE (valtype) == COMPLEX_TYPE)
3202 warning_at (ploc, OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE as integer rather "
3204 "than complex due to prototype",
3205 argnum, rname);
3206 else if (TREE_CODE (type) == COMPLEX_TYPE
3207 && TREE_CODE (valtype) == REAL_TYPE)
3208 warning_at (ploc, OPT_Wtraditional_conversion,
3209 "passing argument %d of %qE as complex rather "
3210 "than floating due to prototype",
3211 argnum, rname);
3212 else if (TREE_CODE (type) == REAL_TYPE
3213 && INTEGRAL_TYPE_P (valtype))
3214 warning_at (ploc, OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE as floating rather "
3216 "than integer due to prototype",
3217 argnum, rname);
3218 else if (TREE_CODE (type) == COMPLEX_TYPE
3219 && INTEGRAL_TYPE_P (valtype))
3220 warning_at (ploc, OPT_Wtraditional_conversion,
3221 "passing argument %d of %qE as complex rather "
3222 "than integer due to prototype",
3223 argnum, rname);
3224 else if (TREE_CODE (type) == REAL_TYPE
3225 && TREE_CODE (valtype) == COMPLEX_TYPE)
3226 warning_at (ploc, OPT_Wtraditional_conversion,
3227 "passing argument %d of %qE as floating rather "
3228 "than complex due to prototype",
3229 argnum, rname);
3230 /* ??? At some point, messages should be written about
3231 conversions between complex types, but that's too messy
3232 to do now. */
3233 else if (TREE_CODE (type) == REAL_TYPE
3234 && TREE_CODE (valtype) == REAL_TYPE)
3236 /* Warn if any argument is passed as `float',
3237 since without a prototype it would be `double'. */
3238 if (formal_prec == TYPE_PRECISION (float_type_node)
3239 && type != dfloat32_type_node)
3240 warning_at (ploc, 0,
3241 "passing argument %d of %qE as %<float%> "
3242 "rather than %<double%> due to prototype",
3243 argnum, rname);
3245 /* Warn if mismatch between argument and prototype
3246 for decimal float types. Warn of conversions with
3247 binary float types and of precision narrowing due to
3248 prototype. */
3249 else if (type != valtype
3250 && (type == dfloat32_type_node
3251 || type == dfloat64_type_node
3252 || type == dfloat128_type_node
3253 || valtype == dfloat32_type_node
3254 || valtype == dfloat64_type_node
3255 || valtype == dfloat128_type_node)
3256 && (formal_prec
3257 <= TYPE_PRECISION (valtype)
3258 || (type == dfloat128_type_node
3259 && (valtype
3260 != dfloat64_type_node
3261 && (valtype
3262 != dfloat32_type_node)))
3263 || (type == dfloat64_type_node
3264 && (valtype
3265 != dfloat32_type_node))))
3266 warning_at (ploc, 0,
3267 "passing argument %d of %qE as %qT "
3268 "rather than %qT due to prototype",
3269 argnum, rname, type, valtype);
3272 /* Detect integer changing in width or signedness.
3273 These warnings are only activated with
3274 -Wtraditional-conversion, not with -Wtraditional. */
3275 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3276 && INTEGRAL_TYPE_P (valtype))
3278 tree would_have_been = default_conversion (val);
3279 tree type1 = TREE_TYPE (would_have_been);
3281 if (TREE_CODE (type) == ENUMERAL_TYPE
3282 && (TYPE_MAIN_VARIANT (type)
3283 == TYPE_MAIN_VARIANT (valtype)))
3284 /* No warning if function asks for enum
3285 and the actual arg is that enum type. */
3287 else if (formal_prec != TYPE_PRECISION (type1))
3288 warning_at (ploc, OPT_Wtraditional_conversion,
3289 "passing argument %d of %qE "
3290 "with different width due to prototype",
3291 argnum, rname);
3292 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3294 /* Don't complain if the formal parameter type
3295 is an enum, because we can't tell now whether
3296 the value was an enum--even the same enum. */
3297 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3299 else if (TREE_CODE (val) == INTEGER_CST
3300 && int_fits_type_p (val, type))
3301 /* Change in signedness doesn't matter
3302 if a constant value is unaffected. */
3304 /* If the value is extended from a narrower
3305 unsigned type, it doesn't matter whether we
3306 pass it as signed or unsigned; the value
3307 certainly is the same either way. */
3308 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3309 && TYPE_UNSIGNED (valtype))
3311 else if (TYPE_UNSIGNED (type))
3312 warning_at (ploc, OPT_Wtraditional_conversion,
3313 "passing argument %d of %qE "
3314 "as unsigned due to prototype",
3315 argnum, rname);
3316 else
3317 warning_at (ploc, OPT_Wtraditional_conversion,
3318 "passing argument %d of %qE "
3319 "as signed due to prototype",
3320 argnum, rname);
3324 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3325 sake of better warnings from convert_and_check. */
3326 if (excess_precision)
3327 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3328 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3329 parmval = convert_for_assignment (loc, ploc, type,
3330 val, origtype, ic_argpass,
3331 npc, fundecl, function,
3332 parmnum + 1);
3334 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3335 && INTEGRAL_TYPE_P (type)
3336 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3337 parmval = default_conversion (parmval);
3340 else if (TREE_CODE (valtype) == REAL_TYPE
3341 && (TYPE_PRECISION (valtype)
3342 <= TYPE_PRECISION (double_type_node))
3343 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3344 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3345 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3347 if (type_generic)
3348 parmval = val;
3349 else
3351 /* Convert `float' to `double'. */
3352 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3353 warning_at (ploc, OPT_Wdouble_promotion,
3354 "implicit conversion from %qT to %qT when passing "
3355 "argument to function",
3356 valtype, double_type_node);
3357 parmval = convert (double_type_node, val);
3360 else if (excess_precision && !type_generic)
3361 /* A "double" argument with excess precision being passed
3362 without a prototype or in variable arguments. */
3363 parmval = convert (valtype, val);
3364 else if ((invalid_func_diag =
3365 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3367 error (invalid_func_diag);
3368 return -1;
3370 else
3371 /* Convert `short' and `char' to full-size `int'. */
3372 parmval = default_conversion (val);
3374 (*values)[parmnum] = parmval;
3375 if (parmval == error_mark_node)
3376 error_args = true;
3378 if (typetail)
3379 typetail = TREE_CHAIN (typetail);
3382 gcc_assert (parmnum == vec_safe_length (values));
3384 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3386 error_at (loc, "too few arguments to function %qE", function);
3387 inform_declaration (fundecl);
3388 return -1;
3391 return error_args ? -1 : (int) parmnum;
3394 /* This is the entry point used by the parser to build unary operators
3395 in the input. CODE, a tree_code, specifies the unary operator, and
3396 ARG is the operand. For unary plus, the C parser currently uses
3397 CONVERT_EXPR for code.
3399 LOC is the location to use for the tree generated.
3402 struct c_expr
3403 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3405 struct c_expr result;
3407 result.value = build_unary_op (loc, code, arg.value, 0);
3408 result.original_code = code;
3409 result.original_type = NULL;
3411 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3412 overflow_warning (loc, result.value);
3414 return result;
3417 /* This is the entry point used by the parser to build binary operators
3418 in the input. CODE, a tree_code, specifies the binary operator, and
3419 ARG1 and ARG2 are the operands. In addition to constructing the
3420 expression, we check for operands that were written with other binary
3421 operators in a way that is likely to confuse the user.
3423 LOCATION is the location of the binary operator. */
3425 struct c_expr
3426 parser_build_binary_op (location_t location, enum tree_code code,
3427 struct c_expr arg1, struct c_expr arg2)
3429 struct c_expr result;
3431 enum tree_code code1 = arg1.original_code;
3432 enum tree_code code2 = arg2.original_code;
3433 tree type1 = (arg1.original_type
3434 ? arg1.original_type
3435 : TREE_TYPE (arg1.value));
3436 tree type2 = (arg2.original_type
3437 ? arg2.original_type
3438 : TREE_TYPE (arg2.value));
3440 result.value = build_binary_op (location, code,
3441 arg1.value, arg2.value, 1);
3442 result.original_code = code;
3443 result.original_type = NULL;
3445 if (TREE_CODE (result.value) == ERROR_MARK)
3446 return result;
3448 if (location != UNKNOWN_LOCATION)
3449 protected_set_expr_location (result.value, location);
3451 /* Check for cases such as x+y<<z which users are likely
3452 to misinterpret. */
3453 if (warn_parentheses)
3454 warn_about_parentheses (location, code, code1, arg1.value, code2,
3455 arg2.value);
3457 if (warn_logical_op)
3458 warn_logical_operator (location, code, TREE_TYPE (result.value),
3459 code1, arg1.value, code2, arg2.value);
3461 if (warn_logical_not_paren
3462 && code1 == TRUTH_NOT_EXPR
3463 && code2 != TRUTH_NOT_EXPR)
3464 warn_logical_not_parentheses (location, code, arg2.value);
3466 /* Warn about comparisons against string literals, with the exception
3467 of testing for equality or inequality of a string literal with NULL. */
3468 if (code == EQ_EXPR || code == NE_EXPR)
3470 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3471 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3472 warning_at (location, OPT_Waddress,
3473 "comparison with string literal results in unspecified behavior");
3475 else if (TREE_CODE_CLASS (code) == tcc_comparison
3476 && (code1 == STRING_CST || code2 == STRING_CST))
3477 warning_at (location, OPT_Waddress,
3478 "comparison with string literal results in unspecified behavior");
3480 if (TREE_OVERFLOW_P (result.value)
3481 && !TREE_OVERFLOW_P (arg1.value)
3482 && !TREE_OVERFLOW_P (arg2.value))
3483 overflow_warning (location, result.value);
3485 /* Warn about comparisons of different enum types. */
3486 if (warn_enum_compare
3487 && TREE_CODE_CLASS (code) == tcc_comparison
3488 && TREE_CODE (type1) == ENUMERAL_TYPE
3489 && TREE_CODE (type2) == ENUMERAL_TYPE
3490 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3491 warning_at (location, OPT_Wenum_compare,
3492 "comparison between %qT and %qT",
3493 type1, type2);
3495 return result;
3498 /* Return a tree for the difference of pointers OP0 and OP1.
3499 The resulting tree has type int. */
3501 static tree
3502 pointer_diff (location_t loc, tree op0, tree op1)
3504 tree restype = ptrdiff_type_node;
3505 tree result, inttype;
3507 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3508 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3509 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3510 tree orig_op1 = op1;
3512 /* If the operands point into different address spaces, we need to
3513 explicitly convert them to pointers into the common address space
3514 before we can subtract the numerical address values. */
3515 if (as0 != as1)
3517 addr_space_t as_common;
3518 tree common_type;
3520 /* Determine the common superset address space. This is guaranteed
3521 to exist because the caller verified that comp_target_types
3522 returned non-zero. */
3523 if (!addr_space_superset (as0, as1, &as_common))
3524 gcc_unreachable ();
3526 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3527 op0 = convert (common_type, op0);
3528 op1 = convert (common_type, op1);
3531 /* Determine integer type to perform computations in. This will usually
3532 be the same as the result type (ptrdiff_t), but may need to be a wider
3533 type if pointers for the address space are wider than ptrdiff_t. */
3534 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3535 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3536 else
3537 inttype = restype;
3539 if (TREE_CODE (target_type) == VOID_TYPE)
3540 pedwarn (loc, OPT_Wpointer_arith,
3541 "pointer of type %<void *%> used in subtraction");
3542 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3543 pedwarn (loc, OPT_Wpointer_arith,
3544 "pointer to a function used in subtraction");
3546 /* First do the subtraction as integers;
3547 then drop through to build the divide operator.
3548 Do not do default conversions on the minus operator
3549 in case restype is a short type. */
3551 op0 = build_binary_op (loc,
3552 MINUS_EXPR, convert (inttype, op0),
3553 convert (inttype, op1), 0);
3554 /* This generates an error if op1 is pointer to incomplete type. */
3555 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3556 error_at (loc, "arithmetic on pointer to an incomplete type");
3558 op1 = c_size_in_bytes (target_type);
3560 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3561 error_at (loc, "arithmetic on pointer to an empty aggregate");
3563 /* Divide by the size, in easiest possible way. */
3564 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3565 op0, convert (inttype, op1));
3567 /* Convert to final result type if necessary. */
3568 return convert (restype, result);
3571 /* Expand atomic compound assignments into an approriate sequence as
3572 specified by the C11 standard section 6.5.16.2.
3573 given
3574 _Atomic T1 E1
3575 T2 E2
3576 E1 op= E2
3578 This sequence is used for all types for which these operations are
3579 supported.
3581 In addition, built-in versions of the 'fe' prefixed routines may
3582 need to be invoked for floating point (real, complex or vector) when
3583 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3585 T1 newval;
3586 T1 old;
3587 T1 *addr
3588 T2 val
3589 fenv_t fenv
3591 addr = &E1;
3592 val = (E2);
3593 __atomic_load (addr, &old, SEQ_CST);
3594 feholdexcept (&fenv);
3595 loop:
3596 newval = old op val;
3597 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3598 SEQ_CST))
3599 goto done;
3600 feclearexcept (FE_ALL_EXCEPT);
3601 goto loop:
3602 done:
3603 feupdateenv (&fenv);
3605 Also note that the compiler is simply issuing the generic form of
3606 the atomic operations. This requires temp(s) and has their address
3607 taken. The atomic processing is smart enough to figure out when the
3608 size of an object can utilize a lock-free version, and convert the
3609 built-in call to the appropriate lock-free routine. The optimizers
3610 will then dispose of any temps that are no longer required, and
3611 lock-free implementations are utilized as long as there is target
3612 support for the required size.
3614 If the operator is NOP_EXPR, then this is a simple assignment, and
3615 an __atomic_store is issued to perform the assignment rather than
3616 the above loop.
3620 /* Build an atomic assignment at LOC, expanding into the proper
3621 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3622 the result of the operation, unless RETURN_OLD_P in which case
3623 return the old value of LHS (this is only for postincrement and
3624 postdecrement). */
3625 static tree
3626 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3627 tree rhs, bool return_old_p)
3629 tree fndecl, func_call;
3630 vec<tree, va_gc> *params;
3631 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3632 tree old, old_addr;
3633 tree compound_stmt;
3634 tree stmt, goto_stmt;
3635 tree loop_label, loop_decl, done_label, done_decl;
3637 tree lhs_type = TREE_TYPE (lhs);
3638 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3639 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3640 tree rhs_type = TREE_TYPE (rhs);
3642 gcc_assert (TYPE_ATOMIC (lhs_type));
3644 if (return_old_p)
3645 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3647 /* Allocate enough vector items for a compare_exchange. */
3648 vec_alloc (params, 6);
3650 /* Create a compound statement to hold the sequence of statements
3651 with a loop. */
3652 compound_stmt = c_begin_compound_stmt (false);
3654 /* Fold the RHS if it hasn't already been folded. */
3655 if (modifycode != NOP_EXPR)
3656 rhs = c_fully_fold (rhs, false, NULL);
3658 /* Remove the qualifiers for the rest of the expressions and create
3659 the VAL temp variable to hold the RHS. */
3660 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3661 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3662 val = create_tmp_var (nonatomic_rhs_type);
3663 TREE_ADDRESSABLE (val) = 1;
3664 TREE_NO_WARNING (val) = 1;
3665 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3666 SET_EXPR_LOCATION (rhs, loc);
3667 add_stmt (rhs);
3669 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3670 an atomic_store. */
3671 if (modifycode == NOP_EXPR)
3673 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3674 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3675 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3676 params->quick_push (lhs_addr);
3677 params->quick_push (rhs);
3678 params->quick_push (seq_cst);
3679 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3680 add_stmt (func_call);
3682 /* Finish the compound statement. */
3683 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3685 /* VAL is the value which was stored, return a COMPOUND_STMT of
3686 the statement and that value. */
3687 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3690 /* Create the variables and labels required for the op= form. */
3691 old = create_tmp_var (nonatomic_lhs_type);
3692 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3693 TREE_ADDRESSABLE (old) = 1;
3694 TREE_NO_WARNING (old) = 1;
3696 newval = create_tmp_var (nonatomic_lhs_type);
3697 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3698 TREE_ADDRESSABLE (newval) = 1;
3700 loop_decl = create_artificial_label (loc);
3701 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3703 done_decl = create_artificial_label (loc);
3704 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3706 /* __atomic_load (addr, &old, SEQ_CST). */
3707 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3708 params->quick_push (lhs_addr);
3709 params->quick_push (old_addr);
3710 params->quick_push (seq_cst);
3711 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3712 add_stmt (func_call);
3713 params->truncate (0);
3715 /* Create the expressions for floating-point environment
3716 manipulation, if required. */
3717 bool need_fenv = (flag_trapping_math
3718 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3719 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3720 if (need_fenv)
3721 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3723 if (hold_call)
3724 add_stmt (hold_call);
3726 /* loop: */
3727 add_stmt (loop_label);
3729 /* newval = old + val; */
3730 rhs = build_binary_op (loc, modifycode, old, val, 1);
3731 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3732 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3733 NULL_TREE, 0);
3734 if (rhs != error_mark_node)
3736 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3737 SET_EXPR_LOCATION (rhs, loc);
3738 add_stmt (rhs);
3741 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3742 goto done; */
3743 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3744 params->quick_push (lhs_addr);
3745 params->quick_push (old_addr);
3746 params->quick_push (newval_addr);
3747 params->quick_push (integer_zero_node);
3748 params->quick_push (seq_cst);
3749 params->quick_push (seq_cst);
3750 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3752 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3753 SET_EXPR_LOCATION (goto_stmt, loc);
3755 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3756 SET_EXPR_LOCATION (stmt, loc);
3757 add_stmt (stmt);
3759 if (clear_call)
3760 add_stmt (clear_call);
3762 /* goto loop; */
3763 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3764 SET_EXPR_LOCATION (goto_stmt, loc);
3765 add_stmt (goto_stmt);
3767 /* done: */
3768 add_stmt (done_label);
3770 if (update_call)
3771 add_stmt (update_call);
3773 /* Finish the compound statement. */
3774 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3776 /* NEWVAL is the value that was successfully stored, return a
3777 COMPOUND_EXPR of the statement and the appropriate value. */
3778 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3779 return_old_p ? old : newval);
3782 /* Construct and perhaps optimize a tree representation
3783 for a unary operation. CODE, a tree_code, specifies the operation
3784 and XARG is the operand.
3785 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3786 the default promotions (such as from short to int).
3787 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3788 allows non-lvalues; this is only used to handle conversion of non-lvalue
3789 arrays to pointers in C99.
3791 LOCATION is the location of the operator. */
3793 tree
3794 build_unary_op (location_t location,
3795 enum tree_code code, tree xarg, int flag)
3797 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3798 tree arg = xarg;
3799 tree argtype = 0;
3800 enum tree_code typecode;
3801 tree val;
3802 tree ret = error_mark_node;
3803 tree eptype = NULL_TREE;
3804 int noconvert = flag;
3805 const char *invalid_op_diag;
3806 bool int_operands;
3808 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3809 if (int_operands)
3810 arg = remove_c_maybe_const_expr (arg);
3812 if (code != ADDR_EXPR)
3813 arg = require_complete_type (arg);
3815 typecode = TREE_CODE (TREE_TYPE (arg));
3816 if (typecode == ERROR_MARK)
3817 return error_mark_node;
3818 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3819 typecode = INTEGER_TYPE;
3821 if ((invalid_op_diag
3822 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3824 error_at (location, invalid_op_diag);
3825 return error_mark_node;
3828 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3830 eptype = TREE_TYPE (arg);
3831 arg = TREE_OPERAND (arg, 0);
3834 switch (code)
3836 case CONVERT_EXPR:
3837 /* This is used for unary plus, because a CONVERT_EXPR
3838 is enough to prevent anybody from looking inside for
3839 associativity, but won't generate any code. */
3840 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3841 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3842 || typecode == VECTOR_TYPE))
3844 error_at (location, "wrong type argument to unary plus");
3845 return error_mark_node;
3847 else if (!noconvert)
3848 arg = default_conversion (arg);
3849 arg = non_lvalue_loc (location, arg);
3850 break;
3852 case NEGATE_EXPR:
3853 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3854 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3855 || typecode == VECTOR_TYPE))
3857 error_at (location, "wrong type argument to unary minus");
3858 return error_mark_node;
3860 else if (!noconvert)
3861 arg = default_conversion (arg);
3862 break;
3864 case BIT_NOT_EXPR:
3865 /* ~ works on integer types and non float vectors. */
3866 if (typecode == INTEGER_TYPE
3867 || (typecode == VECTOR_TYPE
3868 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3870 if (!noconvert)
3871 arg = default_conversion (arg);
3873 else if (typecode == COMPLEX_TYPE)
3875 code = CONJ_EXPR;
3876 pedwarn (location, OPT_Wpedantic,
3877 "ISO C does not support %<~%> for complex conjugation");
3878 if (!noconvert)
3879 arg = default_conversion (arg);
3881 else
3883 error_at (location, "wrong type argument to bit-complement");
3884 return error_mark_node;
3886 break;
3888 case ABS_EXPR:
3889 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3891 error_at (location, "wrong type argument to abs");
3892 return error_mark_node;
3894 else if (!noconvert)
3895 arg = default_conversion (arg);
3896 break;
3898 case CONJ_EXPR:
3899 /* Conjugating a real value is a no-op, but allow it anyway. */
3900 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3901 || typecode == COMPLEX_TYPE))
3903 error_at (location, "wrong type argument to conjugation");
3904 return error_mark_node;
3906 else if (!noconvert)
3907 arg = default_conversion (arg);
3908 break;
3910 case TRUTH_NOT_EXPR:
3911 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3912 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3913 && typecode != COMPLEX_TYPE)
3915 error_at (location,
3916 "wrong type argument to unary exclamation mark");
3917 return error_mark_node;
3919 if (int_operands)
3921 arg = c_objc_common_truthvalue_conversion (location, xarg);
3922 arg = remove_c_maybe_const_expr (arg);
3924 else
3925 arg = c_objc_common_truthvalue_conversion (location, arg);
3926 ret = invert_truthvalue_loc (location, arg);
3927 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3928 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3929 location = EXPR_LOCATION (ret);
3930 goto return_build_unary_op;
3932 case REALPART_EXPR:
3933 case IMAGPART_EXPR:
3934 ret = build_real_imag_expr (location, code, arg);
3935 if (ret == error_mark_node)
3936 return error_mark_node;
3937 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3938 eptype = TREE_TYPE (eptype);
3939 goto return_build_unary_op;
3941 case PREINCREMENT_EXPR:
3942 case POSTINCREMENT_EXPR:
3943 case PREDECREMENT_EXPR:
3944 case POSTDECREMENT_EXPR:
3946 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3948 tree inner = build_unary_op (location, code,
3949 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3950 if (inner == error_mark_node)
3951 return error_mark_node;
3952 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3953 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3954 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3955 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3956 goto return_build_unary_op;
3959 /* Complain about anything that is not a true lvalue. In
3960 Objective-C, skip this check for property_refs. */
3961 if (!objc_is_property_ref (arg)
3962 && !lvalue_or_else (location,
3963 arg, ((code == PREINCREMENT_EXPR
3964 || code == POSTINCREMENT_EXPR)
3965 ? lv_increment
3966 : lv_decrement)))
3967 return error_mark_node;
3969 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3971 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3972 warning_at (location, OPT_Wc___compat,
3973 "increment of enumeration value is invalid in C++");
3974 else
3975 warning_at (location, OPT_Wc___compat,
3976 "decrement of enumeration value is invalid in C++");
3979 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3980 arg = c_fully_fold (arg, false, NULL);
3982 bool atomic_op;
3983 atomic_op = really_atomic_lvalue (arg);
3985 /* Increment or decrement the real part of the value,
3986 and don't change the imaginary part. */
3987 if (typecode == COMPLEX_TYPE)
3989 tree real, imag;
3991 pedwarn (location, OPT_Wpedantic,
3992 "ISO C does not support %<++%> and %<--%> on complex types");
3994 if (!atomic_op)
3996 arg = stabilize_reference (arg);
3997 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3998 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3999 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4000 if (real == error_mark_node || imag == error_mark_node)
4001 return error_mark_node;
4002 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4003 real, imag);
4004 goto return_build_unary_op;
4008 /* Report invalid types. */
4010 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4011 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4012 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4014 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4015 error_at (location, "wrong type argument to increment");
4016 else
4017 error_at (location, "wrong type argument to decrement");
4019 return error_mark_node;
4023 tree inc;
4025 argtype = TREE_TYPE (arg);
4027 /* Compute the increment. */
4029 if (typecode == POINTER_TYPE)
4031 /* If pointer target is an incomplete type,
4032 we just cannot know how to do the arithmetic. */
4033 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4035 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4036 error_at (location,
4037 "increment of pointer to an incomplete type %qT",
4038 TREE_TYPE (argtype));
4039 else
4040 error_at (location,
4041 "decrement of pointer to an incomplete type %qT",
4042 TREE_TYPE (argtype));
4044 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4045 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4047 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4048 pedwarn (location, OPT_Wpointer_arith,
4049 "wrong type argument to increment");
4050 else
4051 pedwarn (location, OPT_Wpointer_arith,
4052 "wrong type argument to decrement");
4055 inc = c_size_in_bytes (TREE_TYPE (argtype));
4056 inc = convert_to_ptrofftype_loc (location, inc);
4058 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4060 /* For signed fract types, we invert ++ to -- or
4061 -- to ++, and change inc from 1 to -1, because
4062 it is not possible to represent 1 in signed fract constants.
4063 For unsigned fract types, the result always overflows and
4064 we get an undefined (original) or the maximum value. */
4065 if (code == PREINCREMENT_EXPR)
4066 code = PREDECREMENT_EXPR;
4067 else if (code == PREDECREMENT_EXPR)
4068 code = PREINCREMENT_EXPR;
4069 else if (code == POSTINCREMENT_EXPR)
4070 code = POSTDECREMENT_EXPR;
4071 else /* code == POSTDECREMENT_EXPR */
4072 code = POSTINCREMENT_EXPR;
4074 inc = integer_minus_one_node;
4075 inc = convert (argtype, inc);
4077 else
4079 inc = VECTOR_TYPE_P (argtype)
4080 ? build_one_cst (argtype)
4081 : integer_one_node;
4082 inc = convert (argtype, inc);
4085 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4086 need to ask Objective-C to build the increment or decrement
4087 expression for it. */
4088 if (objc_is_property_ref (arg))
4089 return objc_build_incr_expr_for_property_ref (location, code,
4090 arg, inc);
4092 /* Report a read-only lvalue. */
4093 if (TYPE_READONLY (argtype))
4095 readonly_error (location, arg,
4096 ((code == PREINCREMENT_EXPR
4097 || code == POSTINCREMENT_EXPR)
4098 ? lv_increment : lv_decrement));
4099 return error_mark_node;
4101 else if (TREE_READONLY (arg))
4102 readonly_warning (arg,
4103 ((code == PREINCREMENT_EXPR
4104 || code == POSTINCREMENT_EXPR)
4105 ? lv_increment : lv_decrement));
4107 /* If the argument is atomic, use the special code sequences for
4108 atomic compound assignment. */
4109 if (atomic_op)
4111 arg = stabilize_reference (arg);
4112 ret = build_atomic_assign (location, arg,
4113 ((code == PREINCREMENT_EXPR
4114 || code == POSTINCREMENT_EXPR)
4115 ? PLUS_EXPR
4116 : MINUS_EXPR),
4117 (FRACT_MODE_P (TYPE_MODE (argtype))
4118 ? inc
4119 : integer_one_node),
4120 (code == POSTINCREMENT_EXPR
4121 || code == POSTDECREMENT_EXPR));
4122 goto return_build_unary_op;
4125 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4126 val = boolean_increment (code, arg);
4127 else
4128 val = build2 (code, TREE_TYPE (arg), arg, inc);
4129 TREE_SIDE_EFFECTS (val) = 1;
4130 if (TREE_CODE (val) != code)
4131 TREE_NO_WARNING (val) = 1;
4132 ret = val;
4133 goto return_build_unary_op;
4136 case ADDR_EXPR:
4137 /* Note that this operation never does default_conversion. */
4139 /* The operand of unary '&' must be an lvalue (which excludes
4140 expressions of type void), or, in C99, the result of a [] or
4141 unary '*' operator. */
4142 if (VOID_TYPE_P (TREE_TYPE (arg))
4143 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4144 && (TREE_CODE (arg) != INDIRECT_REF
4145 || !flag_isoc99))
4146 pedwarn (location, 0, "taking address of expression of type %<void%>");
4148 /* Let &* cancel out to simplify resulting code. */
4149 if (TREE_CODE (arg) == INDIRECT_REF)
4151 /* Don't let this be an lvalue. */
4152 if (lvalue_p (TREE_OPERAND (arg, 0)))
4153 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4154 ret = TREE_OPERAND (arg, 0);
4155 goto return_build_unary_op;
4158 /* For &x[y], return x+y */
4159 if (TREE_CODE (arg) == ARRAY_REF)
4161 tree op0 = TREE_OPERAND (arg, 0);
4162 if (!c_mark_addressable (op0))
4163 return error_mark_node;
4166 /* Anything not already handled and not a true memory reference
4167 or a non-lvalue array is an error. */
4168 else if (typecode != FUNCTION_TYPE && !flag
4169 && !lvalue_or_else (location, arg, lv_addressof))
4170 return error_mark_node;
4172 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4173 folding later. */
4174 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4176 tree inner = build_unary_op (location, code,
4177 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4178 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4179 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4180 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4181 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4182 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4183 goto return_build_unary_op;
4186 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4187 argtype = TREE_TYPE (arg);
4189 /* If the lvalue is const or volatile, merge that into the type
4190 to which the address will point. This is only needed
4191 for function types. */
4192 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4193 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4194 && TREE_CODE (argtype) == FUNCTION_TYPE)
4196 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4197 int quals = orig_quals;
4199 if (TREE_READONLY (arg))
4200 quals |= TYPE_QUAL_CONST;
4201 if (TREE_THIS_VOLATILE (arg))
4202 quals |= TYPE_QUAL_VOLATILE;
4204 argtype = c_build_qualified_type (argtype, quals);
4207 if (!c_mark_addressable (arg))
4208 return error_mark_node;
4210 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4211 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4213 argtype = build_pointer_type (argtype);
4215 /* ??? Cope with user tricks that amount to offsetof. Delete this
4216 when we have proper support for integer constant expressions. */
4217 val = get_base_address (arg);
4218 if (val && TREE_CODE (val) == INDIRECT_REF
4219 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4221 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4222 goto return_build_unary_op;
4225 val = build1 (ADDR_EXPR, argtype, arg);
4227 ret = val;
4228 goto return_build_unary_op;
4230 default:
4231 gcc_unreachable ();
4234 if (argtype == 0)
4235 argtype = TREE_TYPE (arg);
4236 if (TREE_CODE (arg) == INTEGER_CST)
4237 ret = (require_constant_value
4238 ? fold_build1_initializer_loc (location, code, argtype, arg)
4239 : fold_build1_loc (location, code, argtype, arg));
4240 else
4241 ret = build1 (code, argtype, arg);
4242 return_build_unary_op:
4243 gcc_assert (ret != error_mark_node);
4244 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4245 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4246 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4247 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4248 ret = note_integer_operands (ret);
4249 if (eptype)
4250 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4251 protected_set_expr_location (ret, location);
4252 return ret;
4255 /* Return nonzero if REF is an lvalue valid for this language.
4256 Lvalues can be assigned, unless their type has TYPE_READONLY.
4257 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4259 bool
4260 lvalue_p (const_tree ref)
4262 const enum tree_code code = TREE_CODE (ref);
4264 switch (code)
4266 case REALPART_EXPR:
4267 case IMAGPART_EXPR:
4268 case COMPONENT_REF:
4269 return lvalue_p (TREE_OPERAND (ref, 0));
4271 case C_MAYBE_CONST_EXPR:
4272 return lvalue_p (TREE_OPERAND (ref, 1));
4274 case COMPOUND_LITERAL_EXPR:
4275 case STRING_CST:
4276 return 1;
4278 case INDIRECT_REF:
4279 case ARRAY_REF:
4280 case ARRAY_NOTATION_REF:
4281 case VAR_DECL:
4282 case PARM_DECL:
4283 case RESULT_DECL:
4284 case ERROR_MARK:
4285 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4286 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4288 case BIND_EXPR:
4289 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4291 default:
4292 return 0;
4296 /* Give a warning for storing in something that is read-only in GCC
4297 terms but not const in ISO C terms. */
4299 static void
4300 readonly_warning (tree arg, enum lvalue_use use)
4302 switch (use)
4304 case lv_assign:
4305 warning (0, "assignment of read-only location %qE", arg);
4306 break;
4307 case lv_increment:
4308 warning (0, "increment of read-only location %qE", arg);
4309 break;
4310 case lv_decrement:
4311 warning (0, "decrement of read-only location %qE", arg);
4312 break;
4313 default:
4314 gcc_unreachable ();
4316 return;
4320 /* Return nonzero if REF is an lvalue valid for this language;
4321 otherwise, print an error message and return zero. USE says
4322 how the lvalue is being used and so selects the error message.
4323 LOCATION is the location at which any error should be reported. */
4325 static int
4326 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4328 int win = lvalue_p (ref);
4330 if (!win)
4331 lvalue_error (loc, use);
4333 return win;
4336 /* Mark EXP saying that we need to be able to take the
4337 address of it; it should not be allocated in a register.
4338 Returns true if successful. */
4340 bool
4341 c_mark_addressable (tree exp)
4343 tree x = exp;
4345 while (1)
4346 switch (TREE_CODE (x))
4348 case COMPONENT_REF:
4349 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4351 error
4352 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4353 return false;
4356 /* ... fall through ... */
4358 case ADDR_EXPR:
4359 case ARRAY_REF:
4360 case REALPART_EXPR:
4361 case IMAGPART_EXPR:
4362 x = TREE_OPERAND (x, 0);
4363 break;
4365 case COMPOUND_LITERAL_EXPR:
4366 case CONSTRUCTOR:
4367 TREE_ADDRESSABLE (x) = 1;
4368 return true;
4370 case VAR_DECL:
4371 case CONST_DECL:
4372 case PARM_DECL:
4373 case RESULT_DECL:
4374 if (C_DECL_REGISTER (x)
4375 && DECL_NONLOCAL (x))
4377 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4379 error
4380 ("global register variable %qD used in nested function", x);
4381 return false;
4383 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4385 else if (C_DECL_REGISTER (x))
4387 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4388 error ("address of global register variable %qD requested", x);
4389 else
4390 error ("address of register variable %qD requested", x);
4391 return false;
4394 /* drops in */
4395 case FUNCTION_DECL:
4396 TREE_ADDRESSABLE (x) = 1;
4397 /* drops out */
4398 default:
4399 return true;
4403 /* Convert EXPR to TYPE, warning about conversion problems with
4404 constants. SEMANTIC_TYPE is the type this conversion would use
4405 without excess precision. If SEMANTIC_TYPE is NULL, this function
4406 is equivalent to convert_and_check. This function is a wrapper that
4407 handles conversions that may be different than
4408 the usual ones because of excess precision. */
4410 static tree
4411 ep_convert_and_check (location_t loc, tree type, tree expr,
4412 tree semantic_type)
4414 if (TREE_TYPE (expr) == type)
4415 return expr;
4417 if (!semantic_type)
4418 return convert_and_check (loc, type, expr);
4420 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4421 && TREE_TYPE (expr) != semantic_type)
4423 /* For integers, we need to check the real conversion, not
4424 the conversion to the excess precision type. */
4425 expr = convert_and_check (loc, semantic_type, expr);
4427 /* Result type is the excess precision type, which should be
4428 large enough, so do not check. */
4429 return convert (type, expr);
4432 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4433 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4434 if folded to an integer constant then the unselected half may
4435 contain arbitrary operations not normally permitted in constant
4436 expressions. Set the location of the expression to LOC. */
4438 tree
4439 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4440 tree op1, tree op1_original_type, tree op2,
4441 tree op2_original_type)
4443 tree type1;
4444 tree type2;
4445 enum tree_code code1;
4446 enum tree_code code2;
4447 tree result_type = NULL;
4448 tree semantic_result_type = NULL;
4449 tree orig_op1 = op1, orig_op2 = op2;
4450 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4451 bool ifexp_int_operands;
4452 tree ret;
4454 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4455 if (op1_int_operands)
4456 op1 = remove_c_maybe_const_expr (op1);
4457 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4458 if (op2_int_operands)
4459 op2 = remove_c_maybe_const_expr (op2);
4460 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4461 if (ifexp_int_operands)
4462 ifexp = remove_c_maybe_const_expr (ifexp);
4464 /* Promote both alternatives. */
4466 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4467 op1 = default_conversion (op1);
4468 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4469 op2 = default_conversion (op2);
4471 if (TREE_CODE (ifexp) == ERROR_MARK
4472 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4473 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4474 return error_mark_node;
4476 type1 = TREE_TYPE (op1);
4477 code1 = TREE_CODE (type1);
4478 type2 = TREE_TYPE (op2);
4479 code2 = TREE_CODE (type2);
4481 /* C90 does not permit non-lvalue arrays in conditional expressions.
4482 In C99 they will be pointers by now. */
4483 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4485 error_at (colon_loc, "non-lvalue array in conditional expression");
4486 return error_mark_node;
4489 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4490 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4491 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4492 || code1 == COMPLEX_TYPE)
4493 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4494 || code2 == COMPLEX_TYPE))
4496 semantic_result_type = c_common_type (type1, type2);
4497 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4499 op1 = TREE_OPERAND (op1, 0);
4500 type1 = TREE_TYPE (op1);
4501 gcc_assert (TREE_CODE (type1) == code1);
4503 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4505 op2 = TREE_OPERAND (op2, 0);
4506 type2 = TREE_TYPE (op2);
4507 gcc_assert (TREE_CODE (type2) == code2);
4511 if (warn_cxx_compat)
4513 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4514 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4516 if (TREE_CODE (t1) == ENUMERAL_TYPE
4517 && TREE_CODE (t2) == ENUMERAL_TYPE
4518 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4519 warning_at (colon_loc, OPT_Wc___compat,
4520 ("different enum types in conditional is "
4521 "invalid in C++: %qT vs %qT"),
4522 t1, t2);
4525 /* Quickly detect the usual case where op1 and op2 have the same type
4526 after promotion. */
4527 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4529 if (type1 == type2)
4530 result_type = type1;
4531 else
4532 result_type = TYPE_MAIN_VARIANT (type1);
4534 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4535 || code1 == COMPLEX_TYPE)
4536 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4537 || code2 == COMPLEX_TYPE))
4539 result_type = c_common_type (type1, type2);
4540 do_warn_double_promotion (result_type, type1, type2,
4541 "implicit conversion from %qT to %qT to "
4542 "match other result of conditional",
4543 colon_loc);
4545 /* If -Wsign-compare, warn here if type1 and type2 have
4546 different signedness. We'll promote the signed to unsigned
4547 and later code won't know it used to be different.
4548 Do this check on the original types, so that explicit casts
4549 will be considered, but default promotions won't. */
4550 if (c_inhibit_evaluation_warnings == 0)
4552 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4553 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4555 if (unsigned_op1 ^ unsigned_op2)
4557 bool ovf;
4559 /* Do not warn if the result type is signed, since the
4560 signed type will only be chosen if it can represent
4561 all the values of the unsigned type. */
4562 if (!TYPE_UNSIGNED (result_type))
4563 /* OK */;
4564 else
4566 bool op1_maybe_const = true;
4567 bool op2_maybe_const = true;
4569 /* Do not warn if the signed quantity is an
4570 unsuffixed integer literal (or some static
4571 constant expression involving such literals) and
4572 it is non-negative. This warning requires the
4573 operands to be folded for best results, so do
4574 that folding in this case even without
4575 warn_sign_compare to avoid warning options
4576 possibly affecting code generation. */
4577 c_inhibit_evaluation_warnings
4578 += (ifexp == truthvalue_false_node);
4579 op1 = c_fully_fold (op1, require_constant_value,
4580 &op1_maybe_const);
4581 c_inhibit_evaluation_warnings
4582 -= (ifexp == truthvalue_false_node);
4584 c_inhibit_evaluation_warnings
4585 += (ifexp == truthvalue_true_node);
4586 op2 = c_fully_fold (op2, require_constant_value,
4587 &op2_maybe_const);
4588 c_inhibit_evaluation_warnings
4589 -= (ifexp == truthvalue_true_node);
4591 if (warn_sign_compare)
4593 if ((unsigned_op2
4594 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4595 || (unsigned_op1
4596 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4597 /* OK */;
4598 else
4599 warning_at (colon_loc, OPT_Wsign_compare,
4600 ("signed and unsigned type in "
4601 "conditional expression"));
4603 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4604 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4605 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4606 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4611 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4613 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4614 pedwarn (colon_loc, OPT_Wpedantic,
4615 "ISO C forbids conditional expr with only one void side");
4616 result_type = void_type_node;
4618 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4620 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4621 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4622 addr_space_t as_common;
4624 if (comp_target_types (colon_loc, type1, type2))
4625 result_type = common_pointer_type (type1, type2);
4626 else if (null_pointer_constant_p (orig_op1))
4627 result_type = type2;
4628 else if (null_pointer_constant_p (orig_op2))
4629 result_type = type1;
4630 else if (!addr_space_superset (as1, as2, &as_common))
4632 error_at (colon_loc, "pointers to disjoint address spaces "
4633 "used in conditional expression");
4634 return error_mark_node;
4636 else if (VOID_TYPE_P (TREE_TYPE (type1))
4637 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4639 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4640 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4641 & ~TYPE_QUALS (TREE_TYPE (type1))))
4642 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4643 "pointer to array loses qualifier "
4644 "in conditional expression");
4646 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4647 pedwarn (colon_loc, OPT_Wpedantic,
4648 "ISO C forbids conditional expr between "
4649 "%<void *%> and function pointer");
4650 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4651 TREE_TYPE (type2)));
4653 else if (VOID_TYPE_P (TREE_TYPE (type2))
4654 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4656 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4657 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4658 & ~TYPE_QUALS (TREE_TYPE (type2))))
4659 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4660 "pointer to array loses qualifier "
4661 "in conditional expression");
4663 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4664 pedwarn (colon_loc, OPT_Wpedantic,
4665 "ISO C forbids conditional expr between "
4666 "%<void *%> and function pointer");
4667 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4668 TREE_TYPE (type1)));
4670 /* Objective-C pointer comparisons are a bit more lenient. */
4671 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4672 result_type = objc_common_type (type1, type2);
4673 else
4675 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4677 pedwarn (colon_loc, 0,
4678 "pointer type mismatch in conditional expression");
4679 result_type = build_pointer_type
4680 (build_qualified_type (void_type_node, qual));
4683 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4685 if (!null_pointer_constant_p (orig_op2))
4686 pedwarn (colon_loc, 0,
4687 "pointer/integer type mismatch in conditional expression");
4688 else
4690 op2 = null_pointer_node;
4692 result_type = type1;
4694 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4696 if (!null_pointer_constant_p (orig_op1))
4697 pedwarn (colon_loc, 0,
4698 "pointer/integer type mismatch in conditional expression");
4699 else
4701 op1 = null_pointer_node;
4703 result_type = type2;
4706 if (!result_type)
4708 if (flag_cond_mismatch)
4709 result_type = void_type_node;
4710 else
4712 error_at (colon_loc, "type mismatch in conditional expression");
4713 return error_mark_node;
4717 /* Merge const and volatile flags of the incoming types. */
4718 result_type
4719 = build_type_variant (result_type,
4720 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4721 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4723 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4724 semantic_result_type);
4725 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4726 semantic_result_type);
4728 if (ifexp_bcp && ifexp == truthvalue_true_node)
4730 op2_int_operands = true;
4731 op1 = c_fully_fold (op1, require_constant_value, NULL);
4733 if (ifexp_bcp && ifexp == truthvalue_false_node)
4735 op1_int_operands = true;
4736 op2 = c_fully_fold (op2, require_constant_value, NULL);
4738 int_const = int_operands = (ifexp_int_operands
4739 && op1_int_operands
4740 && op2_int_operands);
4741 if (int_operands)
4743 int_const = ((ifexp == truthvalue_true_node
4744 && TREE_CODE (orig_op1) == INTEGER_CST
4745 && !TREE_OVERFLOW (orig_op1))
4746 || (ifexp == truthvalue_false_node
4747 && TREE_CODE (orig_op2) == INTEGER_CST
4748 && !TREE_OVERFLOW (orig_op2)));
4750 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4751 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4752 else
4754 if (int_operands)
4756 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4757 nested inside of the expression. */
4758 op1 = c_fully_fold (op1, false, NULL);
4759 op2 = c_fully_fold (op2, false, NULL);
4761 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4762 if (int_operands)
4763 ret = note_integer_operands (ret);
4765 if (semantic_result_type)
4766 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4768 protected_set_expr_location (ret, colon_loc);
4769 return ret;
4772 /* Return a compound expression that performs two expressions and
4773 returns the value of the second of them.
4775 LOC is the location of the COMPOUND_EXPR. */
4777 tree
4778 build_compound_expr (location_t loc, tree expr1, tree expr2)
4780 bool expr1_int_operands, expr2_int_operands;
4781 tree eptype = NULL_TREE;
4782 tree ret;
4784 if (flag_cilkplus
4785 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4786 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4788 error_at (loc,
4789 "spawned function call cannot be part of a comma expression");
4790 return error_mark_node;
4792 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4793 if (expr1_int_operands)
4794 expr1 = remove_c_maybe_const_expr (expr1);
4795 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4796 if (expr2_int_operands)
4797 expr2 = remove_c_maybe_const_expr (expr2);
4799 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4800 expr1 = TREE_OPERAND (expr1, 0);
4801 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4803 eptype = TREE_TYPE (expr2);
4804 expr2 = TREE_OPERAND (expr2, 0);
4807 if (!TREE_SIDE_EFFECTS (expr1))
4809 /* The left-hand operand of a comma expression is like an expression
4810 statement: with -Wunused, we should warn if it doesn't have
4811 any side-effects, unless it was explicitly cast to (void). */
4812 if (warn_unused_value)
4814 if (VOID_TYPE_P (TREE_TYPE (expr1))
4815 && CONVERT_EXPR_P (expr1))
4816 ; /* (void) a, b */
4817 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4818 && TREE_CODE (expr1) == COMPOUND_EXPR
4819 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4820 ; /* (void) a, (void) b, c */
4821 else
4822 warning_at (loc, OPT_Wunused_value,
4823 "left-hand operand of comma expression has no effect");
4826 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4827 && warn_unused_value)
4829 tree r = expr1;
4830 location_t cloc = loc;
4831 while (TREE_CODE (r) == COMPOUND_EXPR)
4833 if (EXPR_HAS_LOCATION (r))
4834 cloc = EXPR_LOCATION (r);
4835 r = TREE_OPERAND (r, 1);
4837 if (!TREE_SIDE_EFFECTS (r)
4838 && !VOID_TYPE_P (TREE_TYPE (r))
4839 && !CONVERT_EXPR_P (r))
4840 warning_at (cloc, OPT_Wunused_value,
4841 "right-hand operand of comma expression has no effect");
4844 /* With -Wunused, we should also warn if the left-hand operand does have
4845 side-effects, but computes a value which is not used. For example, in
4846 `foo() + bar(), baz()' the result of the `+' operator is not used,
4847 so we should issue a warning. */
4848 else if (warn_unused_value)
4849 warn_if_unused_value (expr1, loc);
4851 if (expr2 == error_mark_node)
4852 return error_mark_node;
4854 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4856 if (flag_isoc99
4857 && expr1_int_operands
4858 && expr2_int_operands)
4859 ret = note_integer_operands (ret);
4861 if (eptype)
4862 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4864 protected_set_expr_location (ret, loc);
4865 return ret;
4868 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4869 which we are casting. OTYPE is the type of the expression being
4870 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4871 of the cast. -Wcast-qual appeared on the command line. Named
4872 address space qualifiers are not handled here, because they result
4873 in different warnings. */
4875 static void
4876 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4878 tree in_type = type;
4879 tree in_otype = otype;
4880 int added = 0;
4881 int discarded = 0;
4882 bool is_const;
4884 /* Check that the qualifiers on IN_TYPE are a superset of the
4885 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4886 nodes is uninteresting and we stop as soon as we hit a
4887 non-POINTER_TYPE node on either type. */
4890 in_otype = TREE_TYPE (in_otype);
4891 in_type = TREE_TYPE (in_type);
4893 /* GNU C allows cv-qualified function types. 'const' means the
4894 function is very pure, 'volatile' means it can't return. We
4895 need to warn when such qualifiers are added, not when they're
4896 taken away. */
4897 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4898 && TREE_CODE (in_type) == FUNCTION_TYPE)
4899 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4900 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4901 else
4902 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4903 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4905 while (TREE_CODE (in_type) == POINTER_TYPE
4906 && TREE_CODE (in_otype) == POINTER_TYPE);
4908 if (added)
4909 warning_at (loc, OPT_Wcast_qual,
4910 "cast adds %q#v qualifier to function type", added);
4912 if (discarded)
4913 /* There are qualifiers present in IN_OTYPE that are not present
4914 in IN_TYPE. */
4915 warning_at (loc, OPT_Wcast_qual,
4916 "cast discards %qv qualifier from pointer target type",
4917 discarded);
4919 if (added || discarded)
4920 return;
4922 /* A cast from **T to const **T is unsafe, because it can cause a
4923 const value to be changed with no additional warning. We only
4924 issue this warning if T is the same on both sides, and we only
4925 issue the warning if there are the same number of pointers on
4926 both sides, as otherwise the cast is clearly unsafe anyhow. A
4927 cast is unsafe when a qualifier is added at one level and const
4928 is not present at all outer levels.
4930 To issue this warning, we check at each level whether the cast
4931 adds new qualifiers not already seen. We don't need to special
4932 case function types, as they won't have the same
4933 TYPE_MAIN_VARIANT. */
4935 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4936 return;
4937 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4938 return;
4940 in_type = type;
4941 in_otype = otype;
4942 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4945 in_type = TREE_TYPE (in_type);
4946 in_otype = TREE_TYPE (in_otype);
4947 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4948 && !is_const)
4950 warning_at (loc, OPT_Wcast_qual,
4951 "to be safe all intermediate pointers in cast from "
4952 "%qT to %qT must be %<const%> qualified",
4953 otype, type);
4954 break;
4956 if (is_const)
4957 is_const = TYPE_READONLY (in_type);
4959 while (TREE_CODE (in_type) == POINTER_TYPE);
4962 /* Build an expression representing a cast to type TYPE of expression EXPR.
4963 LOC is the location of the cast-- typically the open paren of the cast. */
4965 tree
4966 build_c_cast (location_t loc, tree type, tree expr)
4968 tree value;
4970 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4971 expr = TREE_OPERAND (expr, 0);
4973 value = expr;
4975 if (type == error_mark_node || expr == error_mark_node)
4976 return error_mark_node;
4978 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4979 only in <protocol> qualifications. But when constructing cast expressions,
4980 the protocols do matter and must be kept around. */
4981 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4982 return build1 (NOP_EXPR, type, expr);
4984 type = TYPE_MAIN_VARIANT (type);
4986 if (TREE_CODE (type) == ARRAY_TYPE)
4988 error_at (loc, "cast specifies array type");
4989 return error_mark_node;
4992 if (TREE_CODE (type) == FUNCTION_TYPE)
4994 error_at (loc, "cast specifies function type");
4995 return error_mark_node;
4998 if (!VOID_TYPE_P (type))
5000 value = require_complete_type (value);
5001 if (value == error_mark_node)
5002 return error_mark_node;
5005 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5007 if (TREE_CODE (type) == RECORD_TYPE
5008 || TREE_CODE (type) == UNION_TYPE)
5009 pedwarn (loc, OPT_Wpedantic,
5010 "ISO C forbids casting nonscalar to the same type");
5012 /* Convert to remove any qualifiers from VALUE's type. */
5013 value = convert (type, value);
5015 else if (TREE_CODE (type) == UNION_TYPE)
5017 tree field;
5019 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5020 if (TREE_TYPE (field) != error_mark_node
5021 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5022 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5023 break;
5025 if (field)
5027 tree t;
5028 bool maybe_const = true;
5030 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5031 t = c_fully_fold (value, false, &maybe_const);
5032 t = build_constructor_single (type, field, t);
5033 if (!maybe_const)
5034 t = c_wrap_maybe_const (t, true);
5035 t = digest_init (loc, type, t,
5036 NULL_TREE, false, true, 0);
5037 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5038 return t;
5040 error_at (loc, "cast to union type from type not present in union");
5041 return error_mark_node;
5043 else
5045 tree otype, ovalue;
5047 if (type == void_type_node)
5049 tree t = build1 (CONVERT_EXPR, type, value);
5050 SET_EXPR_LOCATION (t, loc);
5051 return t;
5054 otype = TREE_TYPE (value);
5056 /* Optionally warn about potentially worrisome casts. */
5057 if (warn_cast_qual
5058 && TREE_CODE (type) == POINTER_TYPE
5059 && TREE_CODE (otype) == POINTER_TYPE)
5060 handle_warn_cast_qual (loc, type, otype);
5062 /* Warn about conversions between pointers to disjoint
5063 address spaces. */
5064 if (TREE_CODE (type) == POINTER_TYPE
5065 && TREE_CODE (otype) == POINTER_TYPE
5066 && !null_pointer_constant_p (value))
5068 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5069 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5070 addr_space_t as_common;
5072 if (!addr_space_superset (as_to, as_from, &as_common))
5074 if (ADDR_SPACE_GENERIC_P (as_from))
5075 warning_at (loc, 0, "cast to %s address space pointer "
5076 "from disjoint generic address space pointer",
5077 c_addr_space_name (as_to));
5079 else if (ADDR_SPACE_GENERIC_P (as_to))
5080 warning_at (loc, 0, "cast to generic address space pointer "
5081 "from disjoint %s address space pointer",
5082 c_addr_space_name (as_from));
5084 else
5085 warning_at (loc, 0, "cast to %s address space pointer "
5086 "from disjoint %s address space pointer",
5087 c_addr_space_name (as_to),
5088 c_addr_space_name (as_from));
5092 /* Warn about possible alignment problems. */
5093 if (STRICT_ALIGNMENT
5094 && TREE_CODE (type) == POINTER_TYPE
5095 && TREE_CODE (otype) == POINTER_TYPE
5096 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5097 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5098 /* Don't warn about opaque types, where the actual alignment
5099 restriction is unknown. */
5100 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5101 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5102 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5103 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5104 warning_at (loc, OPT_Wcast_align,
5105 "cast increases required alignment of target type");
5107 if (TREE_CODE (type) == INTEGER_TYPE
5108 && TREE_CODE (otype) == POINTER_TYPE
5109 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5110 /* Unlike conversion of integers to pointers, where the
5111 warning is disabled for converting constants because
5112 of cases such as SIG_*, warn about converting constant
5113 pointers to integers. In some cases it may cause unwanted
5114 sign extension, and a warning is appropriate. */
5115 warning_at (loc, OPT_Wpointer_to_int_cast,
5116 "cast from pointer to integer of different size");
5118 if (TREE_CODE (value) == CALL_EXPR
5119 && TREE_CODE (type) != TREE_CODE (otype))
5120 warning_at (loc, OPT_Wbad_function_cast,
5121 "cast from function call of type %qT "
5122 "to non-matching type %qT", otype, type);
5124 if (TREE_CODE (type) == POINTER_TYPE
5125 && TREE_CODE (otype) == INTEGER_TYPE
5126 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5127 /* Don't warn about converting any constant. */
5128 && !TREE_CONSTANT (value))
5129 warning_at (loc,
5130 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5131 "of different size");
5133 if (warn_strict_aliasing <= 2)
5134 strict_aliasing_warning (otype, type, expr);
5136 /* If pedantic, warn for conversions between function and object
5137 pointer types, except for converting a null pointer constant
5138 to function pointer type. */
5139 if (pedantic
5140 && TREE_CODE (type) == POINTER_TYPE
5141 && TREE_CODE (otype) == POINTER_TYPE
5142 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5143 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5144 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5145 "conversion of function pointer to object pointer type");
5147 if (pedantic
5148 && TREE_CODE (type) == POINTER_TYPE
5149 && TREE_CODE (otype) == POINTER_TYPE
5150 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5151 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5152 && !null_pointer_constant_p (value))
5153 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5154 "conversion of object pointer to function pointer type");
5156 ovalue = value;
5157 value = convert (type, value);
5159 /* Ignore any integer overflow caused by the cast. */
5160 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5162 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5164 if (!TREE_OVERFLOW (value))
5166 /* Avoid clobbering a shared constant. */
5167 value = copy_node (value);
5168 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5171 else if (TREE_OVERFLOW (value))
5172 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5173 value = wide_int_to_tree (TREE_TYPE (value), value);
5177 /* Don't let a cast be an lvalue. */
5178 if (value == expr)
5179 value = non_lvalue_loc (loc, value);
5181 /* Don't allow the results of casting to floating-point or complex
5182 types be confused with actual constants, or casts involving
5183 integer and pointer types other than direct integer-to-integer
5184 and integer-to-pointer be confused with integer constant
5185 expressions and null pointer constants. */
5186 if (TREE_CODE (value) == REAL_CST
5187 || TREE_CODE (value) == COMPLEX_CST
5188 || (TREE_CODE (value) == INTEGER_CST
5189 && !((TREE_CODE (expr) == INTEGER_CST
5190 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5191 || TREE_CODE (expr) == REAL_CST
5192 || TREE_CODE (expr) == COMPLEX_CST)))
5193 value = build1 (NOP_EXPR, type, value);
5195 if (CAN_HAVE_LOCATION_P (value))
5196 SET_EXPR_LOCATION (value, loc);
5197 return value;
5200 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5201 location of the open paren of the cast, or the position of the cast
5202 expr. */
5203 tree
5204 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5206 tree type;
5207 tree type_expr = NULL_TREE;
5208 bool type_expr_const = true;
5209 tree ret;
5210 int saved_wsp = warn_strict_prototypes;
5212 /* This avoids warnings about unprototyped casts on
5213 integers. E.g. "#define SIG_DFL (void(*)())0". */
5214 if (TREE_CODE (expr) == INTEGER_CST)
5215 warn_strict_prototypes = 0;
5216 type = groktypename (type_name, &type_expr, &type_expr_const);
5217 warn_strict_prototypes = saved_wsp;
5219 ret = build_c_cast (loc, type, expr);
5220 if (type_expr)
5222 bool inner_expr_const = true;
5223 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5224 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5225 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5226 && inner_expr_const);
5227 SET_EXPR_LOCATION (ret, loc);
5230 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5231 SET_EXPR_LOCATION (ret, loc);
5233 /* C++ does not permits types to be defined in a cast, but it
5234 allows references to incomplete types. */
5235 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5236 warning_at (loc, OPT_Wc___compat,
5237 "defining a type in a cast is invalid in C++");
5239 return ret;
5242 /* Build an assignment expression of lvalue LHS from value RHS.
5243 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5244 may differ from TREE_TYPE (LHS) for an enum bitfield.
5245 MODIFYCODE is the code for a binary operator that we use
5246 to combine the old value of LHS with RHS to get the new value.
5247 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5248 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5249 which may differ from TREE_TYPE (RHS) for an enum value.
5251 LOCATION is the location of the MODIFYCODE operator.
5252 RHS_LOC is the location of the RHS. */
5254 tree
5255 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5256 enum tree_code modifycode,
5257 location_t rhs_loc, tree rhs, tree rhs_origtype)
5259 tree result;
5260 tree newrhs;
5261 tree rhseval = NULL_TREE;
5262 tree rhs_semantic_type = NULL_TREE;
5263 tree lhstype = TREE_TYPE (lhs);
5264 tree olhstype = lhstype;
5265 bool npc;
5266 bool is_atomic_op;
5268 /* Types that aren't fully specified cannot be used in assignments. */
5269 lhs = require_complete_type (lhs);
5271 /* Avoid duplicate error messages from operands that had errors. */
5272 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5273 return error_mark_node;
5275 /* Ensure an error for assigning a non-lvalue array to an array in
5276 C90. */
5277 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5279 error_at (location, "assignment to expression with array type");
5280 return error_mark_node;
5283 /* For ObjC properties, defer this check. */
5284 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5285 return error_mark_node;
5287 is_atomic_op = really_atomic_lvalue (lhs);
5289 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5291 rhs_semantic_type = TREE_TYPE (rhs);
5292 rhs = TREE_OPERAND (rhs, 0);
5295 newrhs = rhs;
5297 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5299 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5300 lhs_origtype, modifycode, rhs_loc, rhs,
5301 rhs_origtype);
5302 if (inner == error_mark_node)
5303 return error_mark_node;
5304 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5305 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5306 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5307 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5308 protected_set_expr_location (result, location);
5309 return result;
5312 /* If a binary op has been requested, combine the old LHS value with the RHS
5313 producing the value we should actually store into the LHS. */
5315 if (modifycode != NOP_EXPR)
5317 lhs = c_fully_fold (lhs, false, NULL);
5318 lhs = stabilize_reference (lhs);
5320 /* Construct the RHS for any non-atomic compound assignemnt. */
5321 if (!is_atomic_op)
5323 /* If in LHS op= RHS the RHS has side-effects, ensure they
5324 are preevaluated before the rest of the assignment expression's
5325 side-effects, because RHS could contain e.g. function calls
5326 that modify LHS. */
5327 if (TREE_SIDE_EFFECTS (rhs))
5329 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5330 rhseval = newrhs;
5332 newrhs = build_binary_op (location,
5333 modifycode, lhs, newrhs, 1);
5335 /* The original type of the right hand side is no longer
5336 meaningful. */
5337 rhs_origtype = NULL_TREE;
5341 if (c_dialect_objc ())
5343 /* Check if we are modifying an Objective-C property reference;
5344 if so, we need to generate setter calls. */
5345 result = objc_maybe_build_modify_expr (lhs, newrhs);
5346 if (result)
5347 goto return_result;
5349 /* Else, do the check that we postponed for Objective-C. */
5350 if (!lvalue_or_else (location, lhs, lv_assign))
5351 return error_mark_node;
5354 /* Give an error for storing in something that is 'const'. */
5356 if (TYPE_READONLY (lhstype)
5357 || ((TREE_CODE (lhstype) == RECORD_TYPE
5358 || TREE_CODE (lhstype) == UNION_TYPE)
5359 && C_TYPE_FIELDS_READONLY (lhstype)))
5361 readonly_error (location, lhs, lv_assign);
5362 return error_mark_node;
5364 else if (TREE_READONLY (lhs))
5365 readonly_warning (lhs, lv_assign);
5367 /* If storing into a structure or union member,
5368 it has probably been given type `int'.
5369 Compute the type that would go with
5370 the actual amount of storage the member occupies. */
5372 if (TREE_CODE (lhs) == COMPONENT_REF
5373 && (TREE_CODE (lhstype) == INTEGER_TYPE
5374 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5375 || TREE_CODE (lhstype) == REAL_TYPE
5376 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5377 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5379 /* If storing in a field that is in actuality a short or narrower than one,
5380 we must store in the field in its actual type. */
5382 if (lhstype != TREE_TYPE (lhs))
5384 lhs = copy_node (lhs);
5385 TREE_TYPE (lhs) = lhstype;
5388 /* Issue -Wc++-compat warnings about an assignment to an enum type
5389 when LHS does not have its original type. This happens for,
5390 e.g., an enum bitfield in a struct. */
5391 if (warn_cxx_compat
5392 && lhs_origtype != NULL_TREE
5393 && lhs_origtype != lhstype
5394 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5396 tree checktype = (rhs_origtype != NULL_TREE
5397 ? rhs_origtype
5398 : TREE_TYPE (rhs));
5399 if (checktype != error_mark_node
5400 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5401 || (is_atomic_op && modifycode != NOP_EXPR)))
5402 warning_at (location, OPT_Wc___compat,
5403 "enum conversion in assignment is invalid in C++");
5406 /* If the lhs is atomic, remove that qualifier. */
5407 if (is_atomic_op)
5409 lhstype = build_qualified_type (lhstype,
5410 (TYPE_QUALS (lhstype)
5411 & ~TYPE_QUAL_ATOMIC));
5412 olhstype = build_qualified_type (olhstype,
5413 (TYPE_QUALS (lhstype)
5414 & ~TYPE_QUAL_ATOMIC));
5417 /* Convert new value to destination type. Fold it first, then
5418 restore any excess precision information, for the sake of
5419 conversion warnings. */
5421 if (!(is_atomic_op && modifycode != NOP_EXPR))
5423 npc = null_pointer_constant_p (newrhs);
5424 newrhs = c_fully_fold (newrhs, false, NULL);
5425 if (rhs_semantic_type)
5426 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5427 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5428 rhs_origtype, ic_assign, npc,
5429 NULL_TREE, NULL_TREE, 0);
5430 if (TREE_CODE (newrhs) == ERROR_MARK)
5431 return error_mark_node;
5434 /* Emit ObjC write barrier, if necessary. */
5435 if (c_dialect_objc () && flag_objc_gc)
5437 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5438 if (result)
5440 protected_set_expr_location (result, location);
5441 goto return_result;
5445 /* Scan operands. */
5447 if (is_atomic_op)
5448 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5449 else
5451 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5452 TREE_SIDE_EFFECTS (result) = 1;
5453 protected_set_expr_location (result, location);
5456 /* If we got the LHS in a different type for storing in,
5457 convert the result back to the nominal type of LHS
5458 so that the value we return always has the same type
5459 as the LHS argument. */
5461 if (olhstype == TREE_TYPE (result))
5462 goto return_result;
5464 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5465 rhs_origtype, ic_assign, false, NULL_TREE,
5466 NULL_TREE, 0);
5467 protected_set_expr_location (result, location);
5469 return_result:
5470 if (rhseval)
5471 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5472 return result;
5475 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5476 This is used to implement -fplan9-extensions. */
5478 static bool
5479 find_anonymous_field_with_type (tree struct_type, tree type)
5481 tree field;
5482 bool found;
5484 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5485 || TREE_CODE (struct_type) == UNION_TYPE);
5486 found = false;
5487 for (field = TYPE_FIELDS (struct_type);
5488 field != NULL_TREE;
5489 field = TREE_CHAIN (field))
5491 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5492 ? c_build_qualified_type (TREE_TYPE (field),
5493 TYPE_QUAL_ATOMIC)
5494 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5495 if (DECL_NAME (field) == NULL
5496 && comptypes (type, fieldtype))
5498 if (found)
5499 return false;
5500 found = true;
5502 else if (DECL_NAME (field) == NULL
5503 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5504 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5505 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5507 if (found)
5508 return false;
5509 found = true;
5512 return found;
5515 /* RHS is an expression whose type is pointer to struct. If there is
5516 an anonymous field in RHS with type TYPE, then return a pointer to
5517 that field in RHS. This is used with -fplan9-extensions. This
5518 returns NULL if no conversion could be found. */
5520 static tree
5521 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5523 tree rhs_struct_type, lhs_main_type;
5524 tree field, found_field;
5525 bool found_sub_field;
5526 tree ret;
5528 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5529 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5530 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5531 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5533 gcc_assert (POINTER_TYPE_P (type));
5534 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5535 ? c_build_qualified_type (TREE_TYPE (type),
5536 TYPE_QUAL_ATOMIC)
5537 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5539 found_field = NULL_TREE;
5540 found_sub_field = false;
5541 for (field = TYPE_FIELDS (rhs_struct_type);
5542 field != NULL_TREE;
5543 field = TREE_CHAIN (field))
5545 if (DECL_NAME (field) != NULL_TREE
5546 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5547 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5548 continue;
5549 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5550 ? c_build_qualified_type (TREE_TYPE (field),
5551 TYPE_QUAL_ATOMIC)
5552 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5553 if (comptypes (lhs_main_type, fieldtype))
5555 if (found_field != NULL_TREE)
5556 return NULL_TREE;
5557 found_field = field;
5559 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5560 lhs_main_type))
5562 if (found_field != NULL_TREE)
5563 return NULL_TREE;
5564 found_field = field;
5565 found_sub_field = true;
5569 if (found_field == NULL_TREE)
5570 return NULL_TREE;
5572 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5573 build_fold_indirect_ref (rhs), found_field,
5574 NULL_TREE);
5575 ret = build_fold_addr_expr_loc (location, ret);
5577 if (found_sub_field)
5579 ret = convert_to_anonymous_field (location, type, ret);
5580 gcc_assert (ret != NULL_TREE);
5583 return ret;
5586 /* Issue an error message for a bad initializer component.
5587 GMSGID identifies the message.
5588 The component name is taken from the spelling stack. */
5590 static void
5591 error_init (location_t loc, const char *gmsgid)
5593 char *ofwhat;
5595 /* The gmsgid may be a format string with %< and %>. */
5596 error_at (loc, gmsgid);
5597 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5598 if (*ofwhat)
5599 inform (loc, "(near initialization for %qs)", ofwhat);
5602 /* Issue a pedantic warning for a bad initializer component. OPT is
5603 the option OPT_* (from options.h) controlling this warning or 0 if
5604 it is unconditionally given. GMSGID identifies the message. The
5605 component name is taken from the spelling stack. */
5607 static void
5608 pedwarn_init (location_t location, int opt, const char *gmsgid)
5610 char *ofwhat;
5611 bool warned;
5613 /* The gmsgid may be a format string with %< and %>. */
5614 warned = pedwarn (location, opt, gmsgid);
5615 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5616 if (*ofwhat && warned)
5617 inform (location, "(near initialization for %qs)", ofwhat);
5620 /* Issue a warning for a bad initializer component.
5622 OPT is the OPT_W* value corresponding to the warning option that
5623 controls this warning. GMSGID identifies the message. The
5624 component name is taken from the spelling stack. */
5626 static void
5627 warning_init (location_t loc, int opt, const char *gmsgid)
5629 char *ofwhat;
5630 bool warned;
5632 /* The gmsgid may be a format string with %< and %>. */
5633 warned = warning_at (loc, opt, gmsgid);
5634 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5635 if (*ofwhat && warned)
5636 inform (loc, "(near initialization for %qs)", ofwhat);
5639 /* If TYPE is an array type and EXPR is a parenthesized string
5640 constant, warn if pedantic that EXPR is being used to initialize an
5641 object of type TYPE. */
5643 void
5644 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5646 if (pedantic
5647 && TREE_CODE (type) == ARRAY_TYPE
5648 && TREE_CODE (expr.value) == STRING_CST
5649 && expr.original_code != STRING_CST)
5650 pedwarn_init (loc, OPT_Wpedantic,
5651 "array initialized from parenthesized string constant");
5654 /* Convert value RHS to type TYPE as preparation for an assignment to
5655 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5656 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5657 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5658 constant before any folding.
5659 The real work of conversion is done by `convert'.
5660 The purpose of this function is to generate error messages
5661 for assignments that are not allowed in C.
5662 ERRTYPE says whether it is argument passing, assignment,
5663 initialization or return.
5665 LOCATION is the location of the assignment, EXPR_LOC is the location of
5666 the RHS or, for a function, location of an argument.
5667 FUNCTION is a tree for the function being called.
5668 PARMNUM is the number of the argument, for printing in error messages. */
5670 static tree
5671 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5672 tree rhs, tree origtype, enum impl_conv errtype,
5673 bool null_pointer_constant, tree fundecl,
5674 tree function, int parmnum)
5676 enum tree_code codel = TREE_CODE (type);
5677 tree orig_rhs = rhs;
5678 tree rhstype;
5679 enum tree_code coder;
5680 tree rname = NULL_TREE;
5681 bool objc_ok = false;
5683 if (errtype == ic_argpass)
5685 tree selector;
5686 /* Change pointer to function to the function itself for
5687 diagnostics. */
5688 if (TREE_CODE (function) == ADDR_EXPR
5689 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5690 function = TREE_OPERAND (function, 0);
5692 /* Handle an ObjC selector specially for diagnostics. */
5693 selector = objc_message_selector ();
5694 rname = function;
5695 if (selector && parmnum > 2)
5697 rname = selector;
5698 parmnum -= 2;
5702 /* This macro is used to emit diagnostics to ensure that all format
5703 strings are complete sentences, visible to gettext and checked at
5704 compile time. */
5705 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5706 do { \
5707 switch (errtype) \
5709 case ic_argpass: \
5710 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5711 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5712 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5713 "expected %qT but argument is of type %qT", \
5714 type, rhstype); \
5715 break; \
5716 case ic_assign: \
5717 pedwarn (LOCATION, OPT, AS); \
5718 break; \
5719 case ic_init: \
5720 pedwarn_init (LOCATION, OPT, IN); \
5721 break; \
5722 case ic_return: \
5723 pedwarn (LOCATION, OPT, RE); \
5724 break; \
5725 default: \
5726 gcc_unreachable (); \
5728 } while (0)
5730 /* This macro is used to emit diagnostics to ensure that all format
5731 strings are complete sentences, visible to gettext and checked at
5732 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5733 extra parameter to enumerate qualifiers. */
5734 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5735 do { \
5736 switch (errtype) \
5738 case ic_argpass: \
5739 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5740 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5741 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5742 "expected %qT but argument is of type %qT", \
5743 type, rhstype); \
5744 break; \
5745 case ic_assign: \
5746 pedwarn (LOCATION, OPT, AS, QUALS); \
5747 break; \
5748 case ic_init: \
5749 pedwarn (LOCATION, OPT, IN, QUALS); \
5750 break; \
5751 case ic_return: \
5752 pedwarn (LOCATION, OPT, RE, QUALS); \
5753 break; \
5754 default: \
5755 gcc_unreachable (); \
5757 } while (0)
5759 /* This macro is used to emit diagnostics to ensure that all format
5760 strings are complete sentences, visible to gettext and checked at
5761 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5762 warning_at instead of pedwarn. */
5763 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5764 do { \
5765 switch (errtype) \
5767 case ic_argpass: \
5768 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5769 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5770 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5771 "expected %qT but argument is of type %qT", \
5772 type, rhstype); \
5773 break; \
5774 case ic_assign: \
5775 warning_at (LOCATION, OPT, AS, QUALS); \
5776 break; \
5777 case ic_init: \
5778 warning_at (LOCATION, OPT, IN, QUALS); \
5779 break; \
5780 case ic_return: \
5781 warning_at (LOCATION, OPT, RE, QUALS); \
5782 break; \
5783 default: \
5784 gcc_unreachable (); \
5786 } while (0)
5788 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5789 rhs = TREE_OPERAND (rhs, 0);
5791 rhstype = TREE_TYPE (rhs);
5792 coder = TREE_CODE (rhstype);
5794 if (coder == ERROR_MARK)
5795 return error_mark_node;
5797 if (c_dialect_objc ())
5799 int parmno;
5801 switch (errtype)
5803 case ic_return:
5804 parmno = 0;
5805 break;
5807 case ic_assign:
5808 parmno = -1;
5809 break;
5811 case ic_init:
5812 parmno = -2;
5813 break;
5815 default:
5816 parmno = parmnum;
5817 break;
5820 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5823 if (warn_cxx_compat)
5825 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5826 if (checktype != error_mark_node
5827 && TREE_CODE (type) == ENUMERAL_TYPE
5828 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5830 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5831 G_("enum conversion when passing argument "
5832 "%d of %qE is invalid in C++"),
5833 G_("enum conversion in assignment is "
5834 "invalid in C++"),
5835 G_("enum conversion in initialization is "
5836 "invalid in C++"),
5837 G_("enum conversion in return is "
5838 "invalid in C++"));
5842 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5843 return rhs;
5845 if (coder == VOID_TYPE)
5847 /* Except for passing an argument to an unprototyped function,
5848 this is a constraint violation. When passing an argument to
5849 an unprototyped function, it is compile-time undefined;
5850 making it a constraint in that case was rejected in
5851 DR#252. */
5852 error_at (location, "void value not ignored as it ought to be");
5853 return error_mark_node;
5855 rhs = require_complete_type (rhs);
5856 if (rhs == error_mark_node)
5857 return error_mark_node;
5858 /* A non-reference type can convert to a reference. This handles
5859 va_start, va_copy and possibly port built-ins. */
5860 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5862 if (!lvalue_p (rhs))
5864 error_at (location, "cannot pass rvalue to reference parameter");
5865 return error_mark_node;
5867 if (!c_mark_addressable (rhs))
5868 return error_mark_node;
5869 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5870 SET_EXPR_LOCATION (rhs, location);
5872 rhs = convert_for_assignment (location, expr_loc,
5873 build_pointer_type (TREE_TYPE (type)),
5874 rhs, origtype, errtype,
5875 null_pointer_constant, fundecl, function,
5876 parmnum);
5877 if (rhs == error_mark_node)
5878 return error_mark_node;
5880 rhs = build1 (NOP_EXPR, type, rhs);
5881 SET_EXPR_LOCATION (rhs, location);
5882 return rhs;
5884 /* Some types can interconvert without explicit casts. */
5885 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5886 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5887 return convert (type, rhs);
5888 /* Arithmetic types all interconvert, and enum is treated like int. */
5889 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5890 || codel == FIXED_POINT_TYPE
5891 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5892 || codel == BOOLEAN_TYPE)
5893 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5894 || coder == FIXED_POINT_TYPE
5895 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5896 || coder == BOOLEAN_TYPE))
5898 tree ret;
5899 bool save = in_late_binary_op;
5900 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5901 || (coder == REAL_TYPE
5902 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5903 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5904 in_late_binary_op = true;
5905 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5906 ? expr_loc : location, type, orig_rhs);
5907 in_late_binary_op = save;
5908 return ret;
5911 /* Aggregates in different TUs might need conversion. */
5912 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5913 && codel == coder
5914 && comptypes (type, rhstype))
5915 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5916 ? expr_loc : location, type, rhs);
5918 /* Conversion to a transparent union or record from its member types.
5919 This applies only to function arguments. */
5920 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5921 && TYPE_TRANSPARENT_AGGR (type))
5922 && errtype == ic_argpass)
5924 tree memb, marginal_memb = NULL_TREE;
5926 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5928 tree memb_type = TREE_TYPE (memb);
5930 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5931 TYPE_MAIN_VARIANT (rhstype)))
5932 break;
5934 if (TREE_CODE (memb_type) != POINTER_TYPE)
5935 continue;
5937 if (coder == POINTER_TYPE)
5939 tree ttl = TREE_TYPE (memb_type);
5940 tree ttr = TREE_TYPE (rhstype);
5942 /* Any non-function converts to a [const][volatile] void *
5943 and vice versa; otherwise, targets must be the same.
5944 Meanwhile, the lhs target must have all the qualifiers of
5945 the rhs. */
5946 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5947 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5948 || comp_target_types (location, memb_type, rhstype))
5950 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5951 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5952 /* If this type won't generate any warnings, use it. */
5953 if (lquals == rquals
5954 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5955 && TREE_CODE (ttl) == FUNCTION_TYPE)
5956 ? ((lquals | rquals) == rquals)
5957 : ((lquals | rquals) == lquals)))
5958 break;
5960 /* Keep looking for a better type, but remember this one. */
5961 if (!marginal_memb)
5962 marginal_memb = memb;
5966 /* Can convert integer zero to any pointer type. */
5967 if (null_pointer_constant)
5969 rhs = null_pointer_node;
5970 break;
5974 if (memb || marginal_memb)
5976 if (!memb)
5978 /* We have only a marginally acceptable member type;
5979 it needs a warning. */
5980 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5981 tree ttr = TREE_TYPE (rhstype);
5983 /* Const and volatile mean something different for function
5984 types, so the usual warnings are not appropriate. */
5985 if (TREE_CODE (ttr) == FUNCTION_TYPE
5986 && TREE_CODE (ttl) == FUNCTION_TYPE)
5988 /* Because const and volatile on functions are
5989 restrictions that say the function will not do
5990 certain things, it is okay to use a const or volatile
5991 function where an ordinary one is wanted, but not
5992 vice-versa. */
5993 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5994 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5995 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5996 OPT_Wdiscarded_qualifiers,
5997 G_("passing argument %d of %qE "
5998 "makes %q#v qualified function "
5999 "pointer from unqualified"),
6000 G_("assignment makes %q#v qualified "
6001 "function pointer from "
6002 "unqualified"),
6003 G_("initialization makes %q#v qualified "
6004 "function pointer from "
6005 "unqualified"),
6006 G_("return makes %q#v qualified function "
6007 "pointer from unqualified"),
6008 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6010 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6011 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6012 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6013 OPT_Wdiscarded_qualifiers,
6014 G_("passing argument %d of %qE discards "
6015 "%qv qualifier from pointer target type"),
6016 G_("assignment discards %qv qualifier "
6017 "from pointer target type"),
6018 G_("initialization discards %qv qualifier "
6019 "from pointer target type"),
6020 G_("return discards %qv qualifier from "
6021 "pointer target type"),
6022 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6024 memb = marginal_memb;
6027 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6028 pedwarn (location, OPT_Wpedantic,
6029 "ISO C prohibits argument conversion to union type");
6031 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6032 return build_constructor_single (type, memb, rhs);
6036 /* Conversions among pointers */
6037 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6038 && (coder == codel))
6040 tree ttl = TREE_TYPE (type);
6041 tree ttr = TREE_TYPE (rhstype);
6042 tree mvl = ttl;
6043 tree mvr = ttr;
6044 bool is_opaque_pointer;
6045 int target_cmp = 0; /* Cache comp_target_types () result. */
6046 addr_space_t asl;
6047 addr_space_t asr;
6049 if (TREE_CODE (mvl) != ARRAY_TYPE)
6050 mvl = (TYPE_ATOMIC (mvl)
6051 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6052 TYPE_QUAL_ATOMIC)
6053 : TYPE_MAIN_VARIANT (mvl));
6054 if (TREE_CODE (mvr) != ARRAY_TYPE)
6055 mvr = (TYPE_ATOMIC (mvr)
6056 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6057 TYPE_QUAL_ATOMIC)
6058 : TYPE_MAIN_VARIANT (mvr));
6059 /* Opaque pointers are treated like void pointers. */
6060 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6062 /* The Plan 9 compiler permits a pointer to a struct to be
6063 automatically converted into a pointer to an anonymous field
6064 within the struct. */
6065 if (flag_plan9_extensions
6066 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6067 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6068 && mvl != mvr)
6070 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6071 if (new_rhs != NULL_TREE)
6073 rhs = new_rhs;
6074 rhstype = TREE_TYPE (rhs);
6075 coder = TREE_CODE (rhstype);
6076 ttr = TREE_TYPE (rhstype);
6077 mvr = TYPE_MAIN_VARIANT (ttr);
6081 /* C++ does not allow the implicit conversion void* -> T*. However,
6082 for the purpose of reducing the number of false positives, we
6083 tolerate the special case of
6085 int *p = NULL;
6087 where NULL is typically defined in C to be '(void *) 0'. */
6088 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6089 warning_at (errtype == ic_argpass ? expr_loc : location,
6090 OPT_Wc___compat,
6091 "request for implicit conversion "
6092 "from %qT to %qT not permitted in C++", rhstype, type);
6094 /* See if the pointers point to incompatible address spaces. */
6095 asl = TYPE_ADDR_SPACE (ttl);
6096 asr = TYPE_ADDR_SPACE (ttr);
6097 if (!null_pointer_constant_p (rhs)
6098 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6100 switch (errtype)
6102 case ic_argpass:
6103 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6104 "non-enclosed address space", parmnum, rname);
6105 break;
6106 case ic_assign:
6107 error_at (location, "assignment from pointer to "
6108 "non-enclosed address space");
6109 break;
6110 case ic_init:
6111 error_at (location, "initialization from pointer to "
6112 "non-enclosed address space");
6113 break;
6114 case ic_return:
6115 error_at (location, "return from pointer to "
6116 "non-enclosed address space");
6117 break;
6118 default:
6119 gcc_unreachable ();
6121 return error_mark_node;
6124 /* Check if the right-hand side has a format attribute but the
6125 left-hand side doesn't. */
6126 if (warn_suggest_attribute_format
6127 && check_missing_format_attribute (type, rhstype))
6129 switch (errtype)
6131 case ic_argpass:
6132 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6133 "argument %d of %qE might be "
6134 "a candidate for a format attribute",
6135 parmnum, rname);
6136 break;
6137 case ic_assign:
6138 warning_at (location, OPT_Wsuggest_attribute_format,
6139 "assignment left-hand side might be "
6140 "a candidate for a format attribute");
6141 break;
6142 case ic_init:
6143 warning_at (location, OPT_Wsuggest_attribute_format,
6144 "initialization left-hand side might be "
6145 "a candidate for a format attribute");
6146 break;
6147 case ic_return:
6148 warning_at (location, OPT_Wsuggest_attribute_format,
6149 "return type might be "
6150 "a candidate for a format attribute");
6151 break;
6152 default:
6153 gcc_unreachable ();
6157 /* Any non-function converts to a [const][volatile] void *
6158 and vice versa; otherwise, targets must be the same.
6159 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6160 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6161 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6162 || (target_cmp = comp_target_types (location, type, rhstype))
6163 || is_opaque_pointer
6164 || ((c_common_unsigned_type (mvl)
6165 == c_common_unsigned_type (mvr))
6166 && (c_common_signed_type (mvl)
6167 == c_common_signed_type (mvr))
6168 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6170 /* Warn about loss of qualifers from pointers to arrays with
6171 qualifiers on the element type. */
6172 if (TREE_CODE (ttr) == ARRAY_TYPE)
6174 ttr = strip_array_types (ttr);
6175 ttl = strip_array_types (ttl);
6177 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6178 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6179 WARNING_FOR_QUALIFIERS (location, expr_loc,
6180 OPT_Wdiscarded_array_qualifiers,
6181 G_("passing argument %d of %qE discards "
6182 "%qv qualifier from pointer target type"),
6183 G_("assignment discards %qv qualifier "
6184 "from pointer target type"),
6185 G_("initialization discards %qv qualifier "
6186 "from pointer target type"),
6187 G_("return discards %qv qualifier from "
6188 "pointer target type"),
6189 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6191 else if (pedantic
6192 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6194 (VOID_TYPE_P (ttr)
6195 && !null_pointer_constant
6196 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6197 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6198 G_("ISO C forbids passing argument %d of "
6199 "%qE between function pointer "
6200 "and %<void *%>"),
6201 G_("ISO C forbids assignment between "
6202 "function pointer and %<void *%>"),
6203 G_("ISO C forbids initialization between "
6204 "function pointer and %<void *%>"),
6205 G_("ISO C forbids return between function "
6206 "pointer and %<void *%>"));
6207 /* Const and volatile mean something different for function types,
6208 so the usual warnings are not appropriate. */
6209 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6210 && TREE_CODE (ttl) != FUNCTION_TYPE)
6212 /* Don't warn about loss of qualifier for conversions from
6213 qualified void* to pointers to arrays with corresponding
6214 qualifier on the element type. */
6215 if (!pedantic)
6216 ttl = strip_array_types (ttl);
6218 /* Assignments between atomic and non-atomic objects are OK. */
6219 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6220 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6222 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6223 OPT_Wdiscarded_qualifiers,
6224 G_("passing argument %d of %qE discards "
6225 "%qv qualifier from pointer target type"),
6226 G_("assignment discards %qv qualifier "
6227 "from pointer target type"),
6228 G_("initialization discards %qv qualifier "
6229 "from pointer target type"),
6230 G_("return discards %qv qualifier from "
6231 "pointer target type"),
6232 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6234 /* If this is not a case of ignoring a mismatch in signedness,
6235 no warning. */
6236 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6237 || target_cmp)
6239 /* If there is a mismatch, do warn. */
6240 else if (warn_pointer_sign)
6241 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6242 G_("pointer targets in passing argument "
6243 "%d of %qE differ in signedness"),
6244 G_("pointer targets in assignment "
6245 "differ in signedness"),
6246 G_("pointer targets in initialization "
6247 "differ in signedness"),
6248 G_("pointer targets in return differ "
6249 "in signedness"));
6251 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6252 && TREE_CODE (ttr) == FUNCTION_TYPE)
6254 /* Because const and volatile on functions are restrictions
6255 that say the function will not do certain things,
6256 it is okay to use a const or volatile function
6257 where an ordinary one is wanted, but not vice-versa. */
6258 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6259 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6260 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6261 OPT_Wdiscarded_qualifiers,
6262 G_("passing argument %d of %qE makes "
6263 "%q#v qualified function pointer "
6264 "from unqualified"),
6265 G_("assignment makes %q#v qualified function "
6266 "pointer from unqualified"),
6267 G_("initialization makes %q#v qualified "
6268 "function pointer from unqualified"),
6269 G_("return makes %q#v qualified function "
6270 "pointer from unqualified"),
6271 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6274 else
6275 /* Avoid warning about the volatile ObjC EH puts on decls. */
6276 if (!objc_ok)
6277 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6278 OPT_Wincompatible_pointer_types,
6279 G_("passing argument %d of %qE from "
6280 "incompatible pointer type"),
6281 G_("assignment from incompatible pointer type"),
6282 G_("initialization from incompatible "
6283 "pointer type"),
6284 G_("return from incompatible pointer type"));
6286 return convert (type, rhs);
6288 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6290 /* ??? This should not be an error when inlining calls to
6291 unprototyped functions. */
6292 error_at (location, "invalid use of non-lvalue array");
6293 return error_mark_node;
6295 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6297 /* An explicit constant 0 can convert to a pointer,
6298 or one that results from arithmetic, even including
6299 a cast to integer type. */
6300 if (!null_pointer_constant)
6301 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6302 OPT_Wint_conversion,
6303 G_("passing argument %d of %qE makes "
6304 "pointer from integer without a cast"),
6305 G_("assignment makes pointer from integer "
6306 "without a cast"),
6307 G_("initialization makes pointer from "
6308 "integer without a cast"),
6309 G_("return makes pointer from integer "
6310 "without a cast"));
6312 return convert (type, rhs);
6314 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6316 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6317 OPT_Wint_conversion,
6318 G_("passing argument %d of %qE makes integer "
6319 "from pointer without a cast"),
6320 G_("assignment makes integer from pointer "
6321 "without a cast"),
6322 G_("initialization makes integer from pointer "
6323 "without a cast"),
6324 G_("return makes integer from pointer "
6325 "without a cast"));
6326 return convert (type, rhs);
6328 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6330 tree ret;
6331 bool save = in_late_binary_op;
6332 in_late_binary_op = true;
6333 ret = convert (type, rhs);
6334 in_late_binary_op = save;
6335 return ret;
6338 switch (errtype)
6340 case ic_argpass:
6341 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6342 rname);
6343 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6344 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6345 "expected %qT but argument is of type %qT", type, rhstype);
6346 break;
6347 case ic_assign:
6348 error_at (location, "incompatible types when assigning to type %qT from "
6349 "type %qT", type, rhstype);
6350 break;
6351 case ic_init:
6352 error_at (location,
6353 "incompatible types when initializing type %qT using type %qT",
6354 type, rhstype);
6355 break;
6356 case ic_return:
6357 error_at (location,
6358 "incompatible types when returning type %qT but %qT was "
6359 "expected", rhstype, type);
6360 break;
6361 default:
6362 gcc_unreachable ();
6365 return error_mark_node;
6368 /* If VALUE is a compound expr all of whose expressions are constant, then
6369 return its value. Otherwise, return error_mark_node.
6371 This is for handling COMPOUND_EXPRs as initializer elements
6372 which is allowed with a warning when -pedantic is specified. */
6374 static tree
6375 valid_compound_expr_initializer (tree value, tree endtype)
6377 if (TREE_CODE (value) == COMPOUND_EXPR)
6379 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6380 == error_mark_node)
6381 return error_mark_node;
6382 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6383 endtype);
6385 else if (!initializer_constant_valid_p (value, endtype))
6386 return error_mark_node;
6387 else
6388 return value;
6391 /* Perform appropriate conversions on the initial value of a variable,
6392 store it in the declaration DECL,
6393 and print any error messages that are appropriate.
6394 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6395 If the init is invalid, store an ERROR_MARK.
6397 INIT_LOC is the location of the initial value. */
6399 void
6400 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6402 tree value, type;
6403 bool npc = false;
6405 /* If variable's type was invalidly declared, just ignore it. */
6407 type = TREE_TYPE (decl);
6408 if (TREE_CODE (type) == ERROR_MARK)
6409 return;
6411 /* Digest the specified initializer into an expression. */
6413 if (init)
6414 npc = null_pointer_constant_p (init);
6415 value = digest_init (init_loc, type, init, origtype, npc,
6416 true, TREE_STATIC (decl));
6418 /* Store the expression if valid; else report error. */
6420 if (!in_system_header_at (input_location)
6421 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6422 warning (OPT_Wtraditional, "traditional C rejects automatic "
6423 "aggregate initialization");
6425 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6426 DECL_INITIAL (decl) = value;
6428 /* ANSI wants warnings about out-of-range constant initializers. */
6429 STRIP_TYPE_NOPS (value);
6430 if (TREE_STATIC (decl))
6431 constant_expression_warning (value);
6433 /* Check if we need to set array size from compound literal size. */
6434 if (TREE_CODE (type) == ARRAY_TYPE
6435 && TYPE_DOMAIN (type) == 0
6436 && value != error_mark_node)
6438 tree inside_init = init;
6440 STRIP_TYPE_NOPS (inside_init);
6441 inside_init = fold (inside_init);
6443 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6445 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6447 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6449 /* For int foo[] = (int [3]){1}; we need to set array size
6450 now since later on array initializer will be just the
6451 brace enclosed list of the compound literal. */
6452 tree etype = strip_array_types (TREE_TYPE (decl));
6453 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6454 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6455 layout_type (type);
6456 layout_decl (cldecl, 0);
6457 TREE_TYPE (decl)
6458 = c_build_qualified_type (type, TYPE_QUALS (etype));
6464 /* Methods for storing and printing names for error messages. */
6466 /* Implement a spelling stack that allows components of a name to be pushed
6467 and popped. Each element on the stack is this structure. */
6469 struct spelling
6471 int kind;
6472 union
6474 unsigned HOST_WIDE_INT i;
6475 const char *s;
6476 } u;
6479 #define SPELLING_STRING 1
6480 #define SPELLING_MEMBER 2
6481 #define SPELLING_BOUNDS 3
6483 static struct spelling *spelling; /* Next stack element (unused). */
6484 static struct spelling *spelling_base; /* Spelling stack base. */
6485 static int spelling_size; /* Size of the spelling stack. */
6487 /* Macros to save and restore the spelling stack around push_... functions.
6488 Alternative to SAVE_SPELLING_STACK. */
6490 #define SPELLING_DEPTH() (spelling - spelling_base)
6491 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6493 /* Push an element on the spelling stack with type KIND and assign VALUE
6494 to MEMBER. */
6496 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6498 int depth = SPELLING_DEPTH (); \
6500 if (depth >= spelling_size) \
6502 spelling_size += 10; \
6503 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6504 spelling_size); \
6505 RESTORE_SPELLING_DEPTH (depth); \
6508 spelling->kind = (KIND); \
6509 spelling->MEMBER = (VALUE); \
6510 spelling++; \
6513 /* Push STRING on the stack. Printed literally. */
6515 static void
6516 push_string (const char *string)
6518 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6521 /* Push a member name on the stack. Printed as '.' STRING. */
6523 static void
6524 push_member_name (tree decl)
6526 const char *const string
6527 = (DECL_NAME (decl)
6528 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6529 : _("<anonymous>"));
6530 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6533 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6535 static void
6536 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6538 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6541 /* Compute the maximum size in bytes of the printed spelling. */
6543 static int
6544 spelling_length (void)
6546 int size = 0;
6547 struct spelling *p;
6549 for (p = spelling_base; p < spelling; p++)
6551 if (p->kind == SPELLING_BOUNDS)
6552 size += 25;
6553 else
6554 size += strlen (p->u.s) + 1;
6557 return size;
6560 /* Print the spelling to BUFFER and return it. */
6562 static char *
6563 print_spelling (char *buffer)
6565 char *d = buffer;
6566 struct spelling *p;
6568 for (p = spelling_base; p < spelling; p++)
6569 if (p->kind == SPELLING_BOUNDS)
6571 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6572 d += strlen (d);
6574 else
6576 const char *s;
6577 if (p->kind == SPELLING_MEMBER)
6578 *d++ = '.';
6579 for (s = p->u.s; (*d = *s++); d++)
6582 *d++ = '\0';
6583 return buffer;
6586 /* Digest the parser output INIT as an initializer for type TYPE.
6587 Return a C expression of type TYPE to represent the initial value.
6589 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6591 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6593 If INIT is a string constant, STRICT_STRING is true if it is
6594 unparenthesized or we should not warn here for it being parenthesized.
6595 For other types of INIT, STRICT_STRING is not used.
6597 INIT_LOC is the location of the INIT.
6599 REQUIRE_CONSTANT requests an error if non-constant initializers or
6600 elements are seen. */
6602 static tree
6603 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6604 bool null_pointer_constant, bool strict_string,
6605 int require_constant)
6607 enum tree_code code = TREE_CODE (type);
6608 tree inside_init = init;
6609 tree semantic_type = NULL_TREE;
6610 bool maybe_const = true;
6612 if (type == error_mark_node
6613 || !init
6614 || error_operand_p (init))
6615 return error_mark_node;
6617 STRIP_TYPE_NOPS (inside_init);
6619 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6621 semantic_type = TREE_TYPE (inside_init);
6622 inside_init = TREE_OPERAND (inside_init, 0);
6624 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6625 inside_init = decl_constant_value_for_optimization (inside_init);
6627 /* Initialization of an array of chars from a string constant
6628 optionally enclosed in braces. */
6630 if (code == ARRAY_TYPE && inside_init
6631 && TREE_CODE (inside_init) == STRING_CST)
6633 tree typ1
6634 = (TYPE_ATOMIC (TREE_TYPE (type))
6635 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6636 TYPE_QUAL_ATOMIC)
6637 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6638 /* Note that an array could be both an array of character type
6639 and an array of wchar_t if wchar_t is signed char or unsigned
6640 char. */
6641 bool char_array = (typ1 == char_type_node
6642 || typ1 == signed_char_type_node
6643 || typ1 == unsigned_char_type_node);
6644 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6645 bool char16_array = !!comptypes (typ1, char16_type_node);
6646 bool char32_array = !!comptypes (typ1, char32_type_node);
6648 if (char_array || wchar_array || char16_array || char32_array)
6650 struct c_expr expr;
6651 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6652 expr.value = inside_init;
6653 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6654 expr.original_type = NULL;
6655 maybe_warn_string_init (init_loc, type, expr);
6657 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6658 pedwarn_init (init_loc, OPT_Wpedantic,
6659 "initialization of a flexible array member");
6661 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6662 TYPE_MAIN_VARIANT (type)))
6663 return inside_init;
6665 if (char_array)
6667 if (typ2 != char_type_node)
6669 error_init (init_loc, "char-array initialized from wide "
6670 "string");
6671 return error_mark_node;
6674 else
6676 if (typ2 == char_type_node)
6678 error_init (init_loc, "wide character array initialized "
6679 "from non-wide string");
6680 return error_mark_node;
6682 else if (!comptypes(typ1, typ2))
6684 error_init (init_loc, "wide character array initialized "
6685 "from incompatible wide string");
6686 return error_mark_node;
6690 TREE_TYPE (inside_init) = type;
6691 if (TYPE_DOMAIN (type) != 0
6692 && TYPE_SIZE (type) != 0
6693 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6695 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6697 /* Subtract the size of a single (possibly wide) character
6698 because it's ok to ignore the terminating null char
6699 that is counted in the length of the constant. */
6700 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6701 (len
6702 - (TYPE_PRECISION (typ1)
6703 / BITS_PER_UNIT))))
6704 pedwarn_init (init_loc, 0,
6705 ("initializer-string for array of chars "
6706 "is too long"));
6707 else if (warn_cxx_compat
6708 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6709 warning_at (init_loc, OPT_Wc___compat,
6710 ("initializer-string for array chars "
6711 "is too long for C++"));
6714 return inside_init;
6716 else if (INTEGRAL_TYPE_P (typ1))
6718 error_init (init_loc, "array of inappropriate type initialized "
6719 "from string constant");
6720 return error_mark_node;
6724 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6725 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6726 below and handle as a constructor. */
6727 if (code == VECTOR_TYPE
6728 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6729 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6730 && TREE_CONSTANT (inside_init))
6732 if (TREE_CODE (inside_init) == VECTOR_CST
6733 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6734 TYPE_MAIN_VARIANT (type)))
6735 return inside_init;
6737 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6739 unsigned HOST_WIDE_INT ix;
6740 tree value;
6741 bool constant_p = true;
6743 /* Iterate through elements and check if all constructor
6744 elements are *_CSTs. */
6745 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6746 if (!CONSTANT_CLASS_P (value))
6748 constant_p = false;
6749 break;
6752 if (constant_p)
6753 return build_vector_from_ctor (type,
6754 CONSTRUCTOR_ELTS (inside_init));
6758 if (warn_sequence_point)
6759 verify_sequence_points (inside_init);
6761 /* Any type can be initialized
6762 from an expression of the same type, optionally with braces. */
6764 if (inside_init && TREE_TYPE (inside_init) != 0
6765 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6766 TYPE_MAIN_VARIANT (type))
6767 || (code == ARRAY_TYPE
6768 && comptypes (TREE_TYPE (inside_init), type))
6769 || (code == VECTOR_TYPE
6770 && comptypes (TREE_TYPE (inside_init), type))
6771 || (code == POINTER_TYPE
6772 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6773 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6774 TREE_TYPE (type)))))
6776 if (code == POINTER_TYPE)
6778 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6780 if (TREE_CODE (inside_init) == STRING_CST
6781 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6782 inside_init = array_to_pointer_conversion
6783 (init_loc, inside_init);
6784 else
6786 error_init (init_loc, "invalid use of non-lvalue array");
6787 return error_mark_node;
6792 if (code == VECTOR_TYPE)
6793 /* Although the types are compatible, we may require a
6794 conversion. */
6795 inside_init = convert (type, inside_init);
6797 if (require_constant
6798 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6800 /* As an extension, allow initializing objects with static storage
6801 duration with compound literals (which are then treated just as
6802 the brace enclosed list they contain). Also allow this for
6803 vectors, as we can only assign them with compound literals. */
6804 if (flag_isoc99 && code != VECTOR_TYPE)
6805 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6806 "is not constant");
6807 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6808 inside_init = DECL_INITIAL (decl);
6811 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6812 && TREE_CODE (inside_init) != CONSTRUCTOR)
6814 error_init (init_loc, "array initialized from non-constant array "
6815 "expression");
6816 return error_mark_node;
6819 /* Compound expressions can only occur here if -Wpedantic or
6820 -pedantic-errors is specified. In the later case, we always want
6821 an error. In the former case, we simply want a warning. */
6822 if (require_constant && pedantic
6823 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6825 inside_init
6826 = valid_compound_expr_initializer (inside_init,
6827 TREE_TYPE (inside_init));
6828 if (inside_init == error_mark_node)
6829 error_init (init_loc, "initializer element is not constant");
6830 else
6831 pedwarn_init (init_loc, OPT_Wpedantic,
6832 "initializer element is not constant");
6833 if (flag_pedantic_errors)
6834 inside_init = error_mark_node;
6836 else if (require_constant
6837 && !initializer_constant_valid_p (inside_init,
6838 TREE_TYPE (inside_init)))
6840 error_init (init_loc, "initializer element is not constant");
6841 inside_init = error_mark_node;
6843 else if (require_constant && !maybe_const)
6844 pedwarn_init (init_loc, 0,
6845 "initializer element is not a constant expression");
6847 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6848 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6849 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6850 type, inside_init, origtype,
6851 ic_init, null_pointer_constant,
6852 NULL_TREE, NULL_TREE, 0);
6853 return inside_init;
6856 /* Handle scalar types, including conversions. */
6858 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6859 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6860 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6862 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6863 && (TREE_CODE (init) == STRING_CST
6864 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6865 inside_init = init = array_to_pointer_conversion (init_loc, init);
6866 if (semantic_type)
6867 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6868 inside_init);
6869 inside_init
6870 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6871 inside_init, origtype, ic_init,
6872 null_pointer_constant, NULL_TREE, NULL_TREE,
6875 /* Check to see if we have already given an error message. */
6876 if (inside_init == error_mark_node)
6878 else if (require_constant && !TREE_CONSTANT (inside_init))
6880 error_init (init_loc, "initializer element is not constant");
6881 inside_init = error_mark_node;
6883 else if (require_constant
6884 && !initializer_constant_valid_p (inside_init,
6885 TREE_TYPE (inside_init)))
6887 error_init (init_loc, "initializer element is not computable at "
6888 "load time");
6889 inside_init = error_mark_node;
6891 else if (require_constant && !maybe_const)
6892 pedwarn_init (init_loc, 0,
6893 "initializer element is not a constant expression");
6895 return inside_init;
6898 /* Come here only for records and arrays. */
6900 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6902 error_init (init_loc, "variable-sized object may not be initialized");
6903 return error_mark_node;
6906 error_init (init_loc, "invalid initializer");
6907 return error_mark_node;
6910 /* Handle initializers that use braces. */
6912 /* Type of object we are accumulating a constructor for.
6913 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6914 static tree constructor_type;
6916 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6917 left to fill. */
6918 static tree constructor_fields;
6920 /* For an ARRAY_TYPE, this is the specified index
6921 at which to store the next element we get. */
6922 static tree constructor_index;
6924 /* For an ARRAY_TYPE, this is the maximum index. */
6925 static tree constructor_max_index;
6927 /* For a RECORD_TYPE, this is the first field not yet written out. */
6928 static tree constructor_unfilled_fields;
6930 /* For an ARRAY_TYPE, this is the index of the first element
6931 not yet written out. */
6932 static tree constructor_unfilled_index;
6934 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6935 This is so we can generate gaps between fields, when appropriate. */
6936 static tree constructor_bit_index;
6938 /* If we are saving up the elements rather than allocating them,
6939 this is the list of elements so far (in reverse order,
6940 most recent first). */
6941 static vec<constructor_elt, va_gc> *constructor_elements;
6943 /* 1 if constructor should be incrementally stored into a constructor chain,
6944 0 if all the elements should be kept in AVL tree. */
6945 static int constructor_incremental;
6947 /* 1 if so far this constructor's elements are all compile-time constants. */
6948 static int constructor_constant;
6950 /* 1 if so far this constructor's elements are all valid address constants. */
6951 static int constructor_simple;
6953 /* 1 if this constructor has an element that cannot be part of a
6954 constant expression. */
6955 static int constructor_nonconst;
6957 /* 1 if this constructor is erroneous so far. */
6958 static int constructor_erroneous;
6960 /* 1 if this constructor is the universal zero initializer { 0 }. */
6961 static int constructor_zeroinit;
6963 /* Structure for managing pending initializer elements, organized as an
6964 AVL tree. */
6966 struct init_node
6968 struct init_node *left, *right;
6969 struct init_node *parent;
6970 int balance;
6971 tree purpose;
6972 tree value;
6973 tree origtype;
6976 /* Tree of pending elements at this constructor level.
6977 These are elements encountered out of order
6978 which belong at places we haven't reached yet in actually
6979 writing the output.
6980 Will never hold tree nodes across GC runs. */
6981 static struct init_node *constructor_pending_elts;
6983 /* The SPELLING_DEPTH of this constructor. */
6984 static int constructor_depth;
6986 /* DECL node for which an initializer is being read.
6987 0 means we are reading a constructor expression
6988 such as (struct foo) {...}. */
6989 static tree constructor_decl;
6991 /* Nonzero if this is an initializer for a top-level decl. */
6992 static int constructor_top_level;
6994 /* Nonzero if there were any member designators in this initializer. */
6995 static int constructor_designated;
6997 /* Nesting depth of designator list. */
6998 static int designator_depth;
7000 /* Nonzero if there were diagnosed errors in this designator list. */
7001 static int designator_erroneous;
7004 /* This stack has a level for each implicit or explicit level of
7005 structuring in the initializer, including the outermost one. It
7006 saves the values of most of the variables above. */
7008 struct constructor_range_stack;
7010 struct constructor_stack
7012 struct constructor_stack *next;
7013 tree type;
7014 tree fields;
7015 tree index;
7016 tree max_index;
7017 tree unfilled_index;
7018 tree unfilled_fields;
7019 tree bit_index;
7020 vec<constructor_elt, va_gc> *elements;
7021 struct init_node *pending_elts;
7022 int offset;
7023 int depth;
7024 /* If value nonzero, this value should replace the entire
7025 constructor at this level. */
7026 struct c_expr replacement_value;
7027 struct constructor_range_stack *range_stack;
7028 char constant;
7029 char simple;
7030 char nonconst;
7031 char implicit;
7032 char erroneous;
7033 char outer;
7034 char incremental;
7035 char designated;
7036 int designator_depth;
7039 static struct constructor_stack *constructor_stack;
7041 /* This stack represents designators from some range designator up to
7042 the last designator in the list. */
7044 struct constructor_range_stack
7046 struct constructor_range_stack *next, *prev;
7047 struct constructor_stack *stack;
7048 tree range_start;
7049 tree index;
7050 tree range_end;
7051 tree fields;
7054 static struct constructor_range_stack *constructor_range_stack;
7056 /* This stack records separate initializers that are nested.
7057 Nested initializers can't happen in ANSI C, but GNU C allows them
7058 in cases like { ... (struct foo) { ... } ... }. */
7060 struct initializer_stack
7062 struct initializer_stack *next;
7063 tree decl;
7064 struct constructor_stack *constructor_stack;
7065 struct constructor_range_stack *constructor_range_stack;
7066 vec<constructor_elt, va_gc> *elements;
7067 struct spelling *spelling;
7068 struct spelling *spelling_base;
7069 int spelling_size;
7070 char top_level;
7071 char require_constant_value;
7072 char require_constant_elements;
7075 static struct initializer_stack *initializer_stack;
7077 /* Prepare to parse and output the initializer for variable DECL. */
7079 void
7080 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7082 const char *locus;
7083 struct initializer_stack *p = XNEW (struct initializer_stack);
7085 p->decl = constructor_decl;
7086 p->require_constant_value = require_constant_value;
7087 p->require_constant_elements = require_constant_elements;
7088 p->constructor_stack = constructor_stack;
7089 p->constructor_range_stack = constructor_range_stack;
7090 p->elements = constructor_elements;
7091 p->spelling = spelling;
7092 p->spelling_base = spelling_base;
7093 p->spelling_size = spelling_size;
7094 p->top_level = constructor_top_level;
7095 p->next = initializer_stack;
7096 initializer_stack = p;
7098 constructor_decl = decl;
7099 constructor_designated = 0;
7100 constructor_top_level = top_level;
7102 if (decl != 0 && decl != error_mark_node)
7104 require_constant_value = TREE_STATIC (decl);
7105 require_constant_elements
7106 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7107 /* For a scalar, you can always use any value to initialize,
7108 even within braces. */
7109 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7110 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7111 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7112 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7113 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7115 else
7117 require_constant_value = 0;
7118 require_constant_elements = 0;
7119 locus = _("(anonymous)");
7122 constructor_stack = 0;
7123 constructor_range_stack = 0;
7125 found_missing_braces = 0;
7127 spelling_base = 0;
7128 spelling_size = 0;
7129 RESTORE_SPELLING_DEPTH (0);
7131 if (locus)
7132 push_string (locus);
7135 void
7136 finish_init (void)
7138 struct initializer_stack *p = initializer_stack;
7140 /* Free the whole constructor stack of this initializer. */
7141 while (constructor_stack)
7143 struct constructor_stack *q = constructor_stack;
7144 constructor_stack = q->next;
7145 free (q);
7148 gcc_assert (!constructor_range_stack);
7150 /* Pop back to the data of the outer initializer (if any). */
7151 free (spelling_base);
7153 constructor_decl = p->decl;
7154 require_constant_value = p->require_constant_value;
7155 require_constant_elements = p->require_constant_elements;
7156 constructor_stack = p->constructor_stack;
7157 constructor_range_stack = p->constructor_range_stack;
7158 constructor_elements = p->elements;
7159 spelling = p->spelling;
7160 spelling_base = p->spelling_base;
7161 spelling_size = p->spelling_size;
7162 constructor_top_level = p->top_level;
7163 initializer_stack = p->next;
7164 free (p);
7167 /* Call here when we see the initializer is surrounded by braces.
7168 This is instead of a call to push_init_level;
7169 it is matched by a call to pop_init_level.
7171 TYPE is the type to initialize, for a constructor expression.
7172 For an initializer for a decl, TYPE is zero. */
7174 void
7175 really_start_incremental_init (tree type)
7177 struct constructor_stack *p = XNEW (struct constructor_stack);
7179 if (type == 0)
7180 type = TREE_TYPE (constructor_decl);
7182 if (TREE_CODE (type) == VECTOR_TYPE
7183 && TYPE_VECTOR_OPAQUE (type))
7184 error ("opaque vector types cannot be initialized");
7186 p->type = constructor_type;
7187 p->fields = constructor_fields;
7188 p->index = constructor_index;
7189 p->max_index = constructor_max_index;
7190 p->unfilled_index = constructor_unfilled_index;
7191 p->unfilled_fields = constructor_unfilled_fields;
7192 p->bit_index = constructor_bit_index;
7193 p->elements = constructor_elements;
7194 p->constant = constructor_constant;
7195 p->simple = constructor_simple;
7196 p->nonconst = constructor_nonconst;
7197 p->erroneous = constructor_erroneous;
7198 p->pending_elts = constructor_pending_elts;
7199 p->depth = constructor_depth;
7200 p->replacement_value.value = 0;
7201 p->replacement_value.original_code = ERROR_MARK;
7202 p->replacement_value.original_type = NULL;
7203 p->implicit = 0;
7204 p->range_stack = 0;
7205 p->outer = 0;
7206 p->incremental = constructor_incremental;
7207 p->designated = constructor_designated;
7208 p->designator_depth = designator_depth;
7209 p->next = 0;
7210 constructor_stack = p;
7212 constructor_constant = 1;
7213 constructor_simple = 1;
7214 constructor_nonconst = 0;
7215 constructor_depth = SPELLING_DEPTH ();
7216 constructor_elements = NULL;
7217 constructor_pending_elts = 0;
7218 constructor_type = type;
7219 constructor_incremental = 1;
7220 constructor_designated = 0;
7221 constructor_zeroinit = 1;
7222 designator_depth = 0;
7223 designator_erroneous = 0;
7225 if (TREE_CODE (constructor_type) == RECORD_TYPE
7226 || TREE_CODE (constructor_type) == UNION_TYPE)
7228 constructor_fields = TYPE_FIELDS (constructor_type);
7229 /* Skip any nameless bit fields at the beginning. */
7230 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7231 && DECL_NAME (constructor_fields) == 0)
7232 constructor_fields = DECL_CHAIN (constructor_fields);
7234 constructor_unfilled_fields = constructor_fields;
7235 constructor_bit_index = bitsize_zero_node;
7237 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7239 if (TYPE_DOMAIN (constructor_type))
7241 constructor_max_index
7242 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7244 /* Detect non-empty initializations of zero-length arrays. */
7245 if (constructor_max_index == NULL_TREE
7246 && TYPE_SIZE (constructor_type))
7247 constructor_max_index = integer_minus_one_node;
7249 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7250 to initialize VLAs will cause a proper error; avoid tree
7251 checking errors as well by setting a safe value. */
7252 if (constructor_max_index
7253 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7254 constructor_max_index = integer_minus_one_node;
7256 constructor_index
7257 = convert (bitsizetype,
7258 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7260 else
7262 constructor_index = bitsize_zero_node;
7263 constructor_max_index = NULL_TREE;
7266 constructor_unfilled_index = constructor_index;
7268 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7270 /* Vectors are like simple fixed-size arrays. */
7271 constructor_max_index =
7272 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7273 constructor_index = bitsize_zero_node;
7274 constructor_unfilled_index = constructor_index;
7276 else
7278 /* Handle the case of int x = {5}; */
7279 constructor_fields = constructor_type;
7280 constructor_unfilled_fields = constructor_type;
7284 /* Push down into a subobject, for initialization.
7285 If this is for an explicit set of braces, IMPLICIT is 0.
7286 If it is because the next element belongs at a lower level,
7287 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7289 void
7290 push_init_level (location_t loc, int implicit,
7291 struct obstack *braced_init_obstack)
7293 struct constructor_stack *p;
7294 tree value = NULL_TREE;
7296 /* If we've exhausted any levels that didn't have braces,
7297 pop them now. If implicit == 1, this will have been done in
7298 process_init_element; do not repeat it here because in the case
7299 of excess initializers for an empty aggregate this leads to an
7300 infinite cycle of popping a level and immediately recreating
7301 it. */
7302 if (implicit != 1)
7304 while (constructor_stack->implicit)
7306 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7307 || TREE_CODE (constructor_type) == UNION_TYPE)
7308 && constructor_fields == 0)
7309 process_init_element (input_location,
7310 pop_init_level (loc, 1, braced_init_obstack),
7311 true, braced_init_obstack);
7312 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7313 && constructor_max_index
7314 && tree_int_cst_lt (constructor_max_index,
7315 constructor_index))
7316 process_init_element (input_location,
7317 pop_init_level (loc, 1, braced_init_obstack),
7318 true, braced_init_obstack);
7319 else
7320 break;
7324 /* Unless this is an explicit brace, we need to preserve previous
7325 content if any. */
7326 if (implicit)
7328 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7329 || TREE_CODE (constructor_type) == UNION_TYPE)
7330 && constructor_fields)
7331 value = find_init_member (constructor_fields, braced_init_obstack);
7332 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7333 value = find_init_member (constructor_index, braced_init_obstack);
7336 p = XNEW (struct constructor_stack);
7337 p->type = constructor_type;
7338 p->fields = constructor_fields;
7339 p->index = constructor_index;
7340 p->max_index = constructor_max_index;
7341 p->unfilled_index = constructor_unfilled_index;
7342 p->unfilled_fields = constructor_unfilled_fields;
7343 p->bit_index = constructor_bit_index;
7344 p->elements = constructor_elements;
7345 p->constant = constructor_constant;
7346 p->simple = constructor_simple;
7347 p->nonconst = constructor_nonconst;
7348 p->erroneous = constructor_erroneous;
7349 p->pending_elts = constructor_pending_elts;
7350 p->depth = constructor_depth;
7351 p->replacement_value.value = 0;
7352 p->replacement_value.original_code = ERROR_MARK;
7353 p->replacement_value.original_type = NULL;
7354 p->implicit = implicit;
7355 p->outer = 0;
7356 p->incremental = constructor_incremental;
7357 p->designated = constructor_designated;
7358 p->designator_depth = designator_depth;
7359 p->next = constructor_stack;
7360 p->range_stack = 0;
7361 constructor_stack = p;
7363 constructor_constant = 1;
7364 constructor_simple = 1;
7365 constructor_nonconst = 0;
7366 constructor_depth = SPELLING_DEPTH ();
7367 constructor_elements = NULL;
7368 constructor_incremental = 1;
7369 constructor_designated = 0;
7370 constructor_pending_elts = 0;
7371 if (!implicit)
7373 p->range_stack = constructor_range_stack;
7374 constructor_range_stack = 0;
7375 designator_depth = 0;
7376 designator_erroneous = 0;
7379 /* Don't die if an entire brace-pair level is superfluous
7380 in the containing level. */
7381 if (constructor_type == 0)
7383 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7384 || TREE_CODE (constructor_type) == UNION_TYPE)
7386 /* Don't die if there are extra init elts at the end. */
7387 if (constructor_fields == 0)
7388 constructor_type = 0;
7389 else
7391 constructor_type = TREE_TYPE (constructor_fields);
7392 push_member_name (constructor_fields);
7393 constructor_depth++;
7395 /* If upper initializer is designated, then mark this as
7396 designated too to prevent bogus warnings. */
7397 constructor_designated = p->designated;
7399 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7401 constructor_type = TREE_TYPE (constructor_type);
7402 push_array_bounds (tree_to_uhwi (constructor_index));
7403 constructor_depth++;
7406 if (constructor_type == 0)
7408 error_init (loc, "extra brace group at end of initializer");
7409 constructor_fields = 0;
7410 constructor_unfilled_fields = 0;
7411 return;
7414 if (value && TREE_CODE (value) == CONSTRUCTOR)
7416 constructor_constant = TREE_CONSTANT (value);
7417 constructor_simple = TREE_STATIC (value);
7418 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7419 constructor_elements = CONSTRUCTOR_ELTS (value);
7420 if (!vec_safe_is_empty (constructor_elements)
7421 && (TREE_CODE (constructor_type) == RECORD_TYPE
7422 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7423 set_nonincremental_init (braced_init_obstack);
7426 if (implicit == 1)
7427 found_missing_braces = 1;
7429 if (TREE_CODE (constructor_type) == RECORD_TYPE
7430 || TREE_CODE (constructor_type) == UNION_TYPE)
7432 constructor_fields = TYPE_FIELDS (constructor_type);
7433 /* Skip any nameless bit fields at the beginning. */
7434 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7435 && DECL_NAME (constructor_fields) == 0)
7436 constructor_fields = DECL_CHAIN (constructor_fields);
7438 constructor_unfilled_fields = constructor_fields;
7439 constructor_bit_index = bitsize_zero_node;
7441 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7443 /* Vectors are like simple fixed-size arrays. */
7444 constructor_max_index =
7445 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7446 constructor_index = bitsize_int (0);
7447 constructor_unfilled_index = constructor_index;
7449 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7451 if (TYPE_DOMAIN (constructor_type))
7453 constructor_max_index
7454 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7456 /* Detect non-empty initializations of zero-length arrays. */
7457 if (constructor_max_index == NULL_TREE
7458 && TYPE_SIZE (constructor_type))
7459 constructor_max_index = integer_minus_one_node;
7461 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7462 to initialize VLAs will cause a proper error; avoid tree
7463 checking errors as well by setting a safe value. */
7464 if (constructor_max_index
7465 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7466 constructor_max_index = integer_minus_one_node;
7468 constructor_index
7469 = convert (bitsizetype,
7470 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7472 else
7473 constructor_index = bitsize_zero_node;
7475 constructor_unfilled_index = constructor_index;
7476 if (value && TREE_CODE (value) == STRING_CST)
7478 /* We need to split the char/wchar array into individual
7479 characters, so that we don't have to special case it
7480 everywhere. */
7481 set_nonincremental_init_from_string (value, braced_init_obstack);
7484 else
7486 if (constructor_type != error_mark_node)
7487 warning_init (input_location, 0, "braces around scalar initializer");
7488 constructor_fields = constructor_type;
7489 constructor_unfilled_fields = constructor_type;
7493 /* At the end of an implicit or explicit brace level,
7494 finish up that level of constructor. If a single expression
7495 with redundant braces initialized that level, return the
7496 c_expr structure for that expression. Otherwise, the original_code
7497 element is set to ERROR_MARK.
7498 If we were outputting the elements as they are read, return 0 as the value
7499 from inner levels (process_init_element ignores that),
7500 but return error_mark_node as the value from the outermost level
7501 (that's what we want to put in DECL_INITIAL).
7502 Otherwise, return a CONSTRUCTOR expression as the value. */
7504 struct c_expr
7505 pop_init_level (location_t loc, int implicit,
7506 struct obstack *braced_init_obstack)
7508 struct constructor_stack *p;
7509 struct c_expr ret;
7510 ret.value = 0;
7511 ret.original_code = ERROR_MARK;
7512 ret.original_type = NULL;
7514 if (implicit == 0)
7516 /* When we come to an explicit close brace,
7517 pop any inner levels that didn't have explicit braces. */
7518 while (constructor_stack->implicit)
7519 process_init_element (input_location,
7520 pop_init_level (loc, 1, braced_init_obstack),
7521 true, braced_init_obstack);
7522 gcc_assert (!constructor_range_stack);
7525 /* Now output all pending elements. */
7526 constructor_incremental = 1;
7527 output_pending_init_elements (1, braced_init_obstack);
7529 p = constructor_stack;
7531 /* Error for initializing a flexible array member, or a zero-length
7532 array member in an inappropriate context. */
7533 if (constructor_type && constructor_fields
7534 && TREE_CODE (constructor_type) == ARRAY_TYPE
7535 && TYPE_DOMAIN (constructor_type)
7536 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7538 /* Silently discard empty initializations. The parser will
7539 already have pedwarned for empty brackets. */
7540 if (integer_zerop (constructor_unfilled_index))
7541 constructor_type = NULL_TREE;
7542 else
7544 gcc_assert (!TYPE_SIZE (constructor_type));
7546 if (constructor_depth > 2)
7547 error_init (loc, "initialization of flexible array member in a nested context");
7548 else
7549 pedwarn_init (loc, OPT_Wpedantic,
7550 "initialization of a flexible array member");
7552 /* We have already issued an error message for the existence
7553 of a flexible array member not at the end of the structure.
7554 Discard the initializer so that we do not die later. */
7555 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7556 constructor_type = NULL_TREE;
7560 switch (vec_safe_length (constructor_elements))
7562 case 0:
7563 /* Initialization with { } counts as zeroinit. */
7564 constructor_zeroinit = 1;
7565 break;
7566 case 1:
7567 /* This might be zeroinit as well. */
7568 if (integer_zerop ((*constructor_elements)[0].value))
7569 constructor_zeroinit = 1;
7570 break;
7571 default:
7572 /* If the constructor has more than one element, it can't be { 0 }. */
7573 constructor_zeroinit = 0;
7574 break;
7577 /* Warn when some structs are initialized with direct aggregation. */
7578 if (!implicit && found_missing_braces && warn_missing_braces
7579 && !constructor_zeroinit)
7580 warning_init (loc, OPT_Wmissing_braces,
7581 "missing braces around initializer");
7583 /* Warn when some struct elements are implicitly initialized to zero. */
7584 if (warn_missing_field_initializers
7585 && constructor_type
7586 && TREE_CODE (constructor_type) == RECORD_TYPE
7587 && constructor_unfilled_fields)
7589 /* Do not warn for flexible array members or zero-length arrays. */
7590 while (constructor_unfilled_fields
7591 && (!DECL_SIZE (constructor_unfilled_fields)
7592 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7593 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7595 if (constructor_unfilled_fields
7596 /* Do not warn if this level of the initializer uses member
7597 designators; it is likely to be deliberate. */
7598 && !constructor_designated
7599 /* Do not warn about initializing with { 0 } or with { }. */
7600 && !constructor_zeroinit)
7602 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7603 "missing initializer for field %qD of %qT",
7604 constructor_unfilled_fields,
7605 constructor_type))
7606 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7607 "%qD declared here", constructor_unfilled_fields);
7611 /* Pad out the end of the structure. */
7612 if (p->replacement_value.value)
7613 /* If this closes a superfluous brace pair,
7614 just pass out the element between them. */
7615 ret = p->replacement_value;
7616 else if (constructor_type == 0)
7618 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7619 && TREE_CODE (constructor_type) != UNION_TYPE
7620 && TREE_CODE (constructor_type) != ARRAY_TYPE
7621 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7623 /* A nonincremental scalar initializer--just return
7624 the element, after verifying there is just one. */
7625 if (vec_safe_is_empty (constructor_elements))
7627 if (!constructor_erroneous)
7628 error_init (loc, "empty scalar initializer");
7629 ret.value = error_mark_node;
7631 else if (vec_safe_length (constructor_elements) != 1)
7633 error_init (loc, "extra elements in scalar initializer");
7634 ret.value = (*constructor_elements)[0].value;
7636 else
7637 ret.value = (*constructor_elements)[0].value;
7639 else
7641 if (constructor_erroneous)
7642 ret.value = error_mark_node;
7643 else
7645 ret.value = build_constructor (constructor_type,
7646 constructor_elements);
7647 if (constructor_constant)
7648 TREE_CONSTANT (ret.value) = 1;
7649 if (constructor_constant && constructor_simple)
7650 TREE_STATIC (ret.value) = 1;
7651 if (constructor_nonconst)
7652 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7656 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7658 if (constructor_nonconst)
7659 ret.original_code = C_MAYBE_CONST_EXPR;
7660 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7661 ret.original_code = ERROR_MARK;
7664 constructor_type = p->type;
7665 constructor_fields = p->fields;
7666 constructor_index = p->index;
7667 constructor_max_index = p->max_index;
7668 constructor_unfilled_index = p->unfilled_index;
7669 constructor_unfilled_fields = p->unfilled_fields;
7670 constructor_bit_index = p->bit_index;
7671 constructor_elements = p->elements;
7672 constructor_constant = p->constant;
7673 constructor_simple = p->simple;
7674 constructor_nonconst = p->nonconst;
7675 constructor_erroneous = p->erroneous;
7676 constructor_incremental = p->incremental;
7677 constructor_designated = p->designated;
7678 designator_depth = p->designator_depth;
7679 constructor_pending_elts = p->pending_elts;
7680 constructor_depth = p->depth;
7681 if (!p->implicit)
7682 constructor_range_stack = p->range_stack;
7683 RESTORE_SPELLING_DEPTH (constructor_depth);
7685 constructor_stack = p->next;
7686 free (p);
7688 if (ret.value == 0 && constructor_stack == 0)
7689 ret.value = error_mark_node;
7690 return ret;
7693 /* Common handling for both array range and field name designators.
7694 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7696 static int
7697 set_designator (location_t loc, int array,
7698 struct obstack *braced_init_obstack)
7700 tree subtype;
7701 enum tree_code subcode;
7703 /* Don't die if an entire brace-pair level is superfluous
7704 in the containing level. */
7705 if (constructor_type == 0)
7706 return 1;
7708 /* If there were errors in this designator list already, bail out
7709 silently. */
7710 if (designator_erroneous)
7711 return 1;
7713 if (!designator_depth)
7715 gcc_assert (!constructor_range_stack);
7717 /* Designator list starts at the level of closest explicit
7718 braces. */
7719 while (constructor_stack->implicit)
7720 process_init_element (input_location,
7721 pop_init_level (loc, 1, braced_init_obstack),
7722 true, braced_init_obstack);
7723 constructor_designated = 1;
7724 return 0;
7727 switch (TREE_CODE (constructor_type))
7729 case RECORD_TYPE:
7730 case UNION_TYPE:
7731 subtype = TREE_TYPE (constructor_fields);
7732 if (subtype != error_mark_node)
7733 subtype = TYPE_MAIN_VARIANT (subtype);
7734 break;
7735 case ARRAY_TYPE:
7736 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7737 break;
7738 default:
7739 gcc_unreachable ();
7742 subcode = TREE_CODE (subtype);
7743 if (array && subcode != ARRAY_TYPE)
7745 error_init (loc, "array index in non-array initializer");
7746 return 1;
7748 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7750 error_init (loc, "field name not in record or union initializer");
7751 return 1;
7754 constructor_designated = 1;
7755 push_init_level (loc, 2, braced_init_obstack);
7756 return 0;
7759 /* If there are range designators in designator list, push a new designator
7760 to constructor_range_stack. RANGE_END is end of such stack range or
7761 NULL_TREE if there is no range designator at this level. */
7763 static void
7764 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7766 struct constructor_range_stack *p;
7768 p = (struct constructor_range_stack *)
7769 obstack_alloc (braced_init_obstack,
7770 sizeof (struct constructor_range_stack));
7771 p->prev = constructor_range_stack;
7772 p->next = 0;
7773 p->fields = constructor_fields;
7774 p->range_start = constructor_index;
7775 p->index = constructor_index;
7776 p->stack = constructor_stack;
7777 p->range_end = range_end;
7778 if (constructor_range_stack)
7779 constructor_range_stack->next = p;
7780 constructor_range_stack = p;
7783 /* Within an array initializer, specify the next index to be initialized.
7784 FIRST is that index. If LAST is nonzero, then initialize a range
7785 of indices, running from FIRST through LAST. */
7787 void
7788 set_init_index (location_t loc, tree first, tree last,
7789 struct obstack *braced_init_obstack)
7791 if (set_designator (loc, 1, braced_init_obstack))
7792 return;
7794 designator_erroneous = 1;
7796 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7797 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7799 error_init (loc, "array index in initializer not of integer type");
7800 return;
7803 if (TREE_CODE (first) != INTEGER_CST)
7805 first = c_fully_fold (first, false, NULL);
7806 if (TREE_CODE (first) == INTEGER_CST)
7807 pedwarn_init (loc, OPT_Wpedantic,
7808 "array index in initializer is not "
7809 "an integer constant expression");
7812 if (last && TREE_CODE (last) != INTEGER_CST)
7814 last = c_fully_fold (last, false, NULL);
7815 if (TREE_CODE (last) == INTEGER_CST)
7816 pedwarn_init (loc, OPT_Wpedantic,
7817 "array index in initializer is not "
7818 "an integer constant expression");
7821 if (TREE_CODE (first) != INTEGER_CST)
7822 error_init (loc, "nonconstant array index in initializer");
7823 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7824 error_init (loc, "nonconstant array index in initializer");
7825 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7826 error_init (loc, "array index in non-array initializer");
7827 else if (tree_int_cst_sgn (first) == -1)
7828 error_init (loc, "array index in initializer exceeds array bounds");
7829 else if (constructor_max_index
7830 && tree_int_cst_lt (constructor_max_index, first))
7831 error_init (loc, "array index in initializer exceeds array bounds");
7832 else
7834 constant_expression_warning (first);
7835 if (last)
7836 constant_expression_warning (last);
7837 constructor_index = convert (bitsizetype, first);
7838 if (tree_int_cst_lt (constructor_index, first))
7840 constructor_index = copy_node (constructor_index);
7841 TREE_OVERFLOW (constructor_index) = 1;
7844 if (last)
7846 if (tree_int_cst_equal (first, last))
7847 last = 0;
7848 else if (tree_int_cst_lt (last, first))
7850 error_init (loc, "empty index range in initializer");
7851 last = 0;
7853 else
7855 last = convert (bitsizetype, last);
7856 if (constructor_max_index != 0
7857 && tree_int_cst_lt (constructor_max_index, last))
7859 error_init (loc, "array index range in initializer exceeds "
7860 "array bounds");
7861 last = 0;
7866 designator_depth++;
7867 designator_erroneous = 0;
7868 if (constructor_range_stack || last)
7869 push_range_stack (last, braced_init_obstack);
7873 /* Within a struct initializer, specify the next field to be initialized. */
7875 void
7876 set_init_label (location_t loc, tree fieldname,
7877 struct obstack *braced_init_obstack)
7879 tree field;
7881 if (set_designator (loc, 0, braced_init_obstack))
7882 return;
7884 designator_erroneous = 1;
7886 if (TREE_CODE (constructor_type) != RECORD_TYPE
7887 && TREE_CODE (constructor_type) != UNION_TYPE)
7889 error_init (loc, "field name not in record or union initializer");
7890 return;
7893 field = lookup_field (constructor_type, fieldname);
7895 if (field == 0)
7896 error ("unknown field %qE specified in initializer", fieldname);
7897 else
7900 constructor_fields = TREE_VALUE (field);
7901 designator_depth++;
7902 designator_erroneous = 0;
7903 if (constructor_range_stack)
7904 push_range_stack (NULL_TREE, braced_init_obstack);
7905 field = TREE_CHAIN (field);
7906 if (field)
7908 if (set_designator (loc, 0, braced_init_obstack))
7909 return;
7912 while (field != NULL_TREE);
7915 /* Add a new initializer to the tree of pending initializers. PURPOSE
7916 identifies the initializer, either array index or field in a structure.
7917 VALUE is the value of that index or field. If ORIGTYPE is not
7918 NULL_TREE, it is the original type of VALUE.
7920 IMPLICIT is true if value comes from pop_init_level (1),
7921 the new initializer has been merged with the existing one
7922 and thus no warnings should be emitted about overriding an
7923 existing initializer. */
7925 static void
7926 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7927 bool implicit, struct obstack *braced_init_obstack)
7929 struct init_node *p, **q, *r;
7931 q = &constructor_pending_elts;
7932 p = 0;
7934 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7936 while (*q != 0)
7938 p = *q;
7939 if (tree_int_cst_lt (purpose, p->purpose))
7940 q = &p->left;
7941 else if (tree_int_cst_lt (p->purpose, purpose))
7942 q = &p->right;
7943 else
7945 if (!implicit)
7947 if (TREE_SIDE_EFFECTS (p->value))
7948 warning_init (loc, 0,
7949 "initialized field with side-effects "
7950 "overwritten");
7951 else if (warn_override_init)
7952 warning_init (loc, OPT_Woverride_init,
7953 "initialized field overwritten");
7955 p->value = value;
7956 p->origtype = origtype;
7957 return;
7961 else
7963 tree bitpos;
7965 bitpos = bit_position (purpose);
7966 while (*q != NULL)
7968 p = *q;
7969 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7970 q = &p->left;
7971 else if (p->purpose != purpose)
7972 q = &p->right;
7973 else
7975 if (!implicit)
7977 if (TREE_SIDE_EFFECTS (p->value))
7978 warning_init (loc, 0,
7979 "initialized field with side-effects "
7980 "overwritten");
7981 else if (warn_override_init)
7982 warning_init (loc, OPT_Woverride_init,
7983 "initialized field overwritten");
7985 p->value = value;
7986 p->origtype = origtype;
7987 return;
7992 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7993 sizeof (struct init_node));
7994 r->purpose = purpose;
7995 r->value = value;
7996 r->origtype = origtype;
7998 *q = r;
7999 r->parent = p;
8000 r->left = 0;
8001 r->right = 0;
8002 r->balance = 0;
8004 while (p)
8006 struct init_node *s;
8008 if (r == p->left)
8010 if (p->balance == 0)
8011 p->balance = -1;
8012 else if (p->balance < 0)
8014 if (r->balance < 0)
8016 /* L rotation. */
8017 p->left = r->right;
8018 if (p->left)
8019 p->left->parent = p;
8020 r->right = p;
8022 p->balance = 0;
8023 r->balance = 0;
8025 s = p->parent;
8026 p->parent = r;
8027 r->parent = s;
8028 if (s)
8030 if (s->left == p)
8031 s->left = r;
8032 else
8033 s->right = r;
8035 else
8036 constructor_pending_elts = r;
8038 else
8040 /* LR rotation. */
8041 struct init_node *t = r->right;
8043 r->right = t->left;
8044 if (r->right)
8045 r->right->parent = r;
8046 t->left = r;
8048 p->left = t->right;
8049 if (p->left)
8050 p->left->parent = p;
8051 t->right = p;
8053 p->balance = t->balance < 0;
8054 r->balance = -(t->balance > 0);
8055 t->balance = 0;
8057 s = p->parent;
8058 p->parent = t;
8059 r->parent = t;
8060 t->parent = s;
8061 if (s)
8063 if (s->left == p)
8064 s->left = t;
8065 else
8066 s->right = t;
8068 else
8069 constructor_pending_elts = t;
8071 break;
8073 else
8075 /* p->balance == +1; growth of left side balances the node. */
8076 p->balance = 0;
8077 break;
8080 else /* r == p->right */
8082 if (p->balance == 0)
8083 /* Growth propagation from right side. */
8084 p->balance++;
8085 else if (p->balance > 0)
8087 if (r->balance > 0)
8089 /* R rotation. */
8090 p->right = r->left;
8091 if (p->right)
8092 p->right->parent = p;
8093 r->left = p;
8095 p->balance = 0;
8096 r->balance = 0;
8098 s = p->parent;
8099 p->parent = r;
8100 r->parent = s;
8101 if (s)
8103 if (s->left == p)
8104 s->left = r;
8105 else
8106 s->right = r;
8108 else
8109 constructor_pending_elts = r;
8111 else /* r->balance == -1 */
8113 /* RL rotation */
8114 struct init_node *t = r->left;
8116 r->left = t->right;
8117 if (r->left)
8118 r->left->parent = r;
8119 t->right = r;
8121 p->right = t->left;
8122 if (p->right)
8123 p->right->parent = p;
8124 t->left = p;
8126 r->balance = (t->balance < 0);
8127 p->balance = -(t->balance > 0);
8128 t->balance = 0;
8130 s = p->parent;
8131 p->parent = t;
8132 r->parent = t;
8133 t->parent = s;
8134 if (s)
8136 if (s->left == p)
8137 s->left = t;
8138 else
8139 s->right = t;
8141 else
8142 constructor_pending_elts = t;
8144 break;
8146 else
8148 /* p->balance == -1; growth of right side balances the node. */
8149 p->balance = 0;
8150 break;
8154 r = p;
8155 p = p->parent;
8159 /* Build AVL tree from a sorted chain. */
8161 static void
8162 set_nonincremental_init (struct obstack * braced_init_obstack)
8164 unsigned HOST_WIDE_INT ix;
8165 tree index, value;
8167 if (TREE_CODE (constructor_type) != RECORD_TYPE
8168 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8169 return;
8171 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8172 add_pending_init (input_location, index, value, NULL_TREE, true,
8173 braced_init_obstack);
8174 constructor_elements = NULL;
8175 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8177 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8178 /* Skip any nameless bit fields at the beginning. */
8179 while (constructor_unfilled_fields != 0
8180 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8181 && DECL_NAME (constructor_unfilled_fields) == 0)
8182 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8185 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8187 if (TYPE_DOMAIN (constructor_type))
8188 constructor_unfilled_index
8189 = convert (bitsizetype,
8190 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8191 else
8192 constructor_unfilled_index = bitsize_zero_node;
8194 constructor_incremental = 0;
8197 /* Build AVL tree from a string constant. */
8199 static void
8200 set_nonincremental_init_from_string (tree str,
8201 struct obstack * braced_init_obstack)
8203 tree value, purpose, type;
8204 HOST_WIDE_INT val[2];
8205 const char *p, *end;
8206 int byte, wchar_bytes, charwidth, bitpos;
8208 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8210 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8211 charwidth = TYPE_PRECISION (char_type_node);
8212 type = TREE_TYPE (constructor_type);
8213 p = TREE_STRING_POINTER (str);
8214 end = p + TREE_STRING_LENGTH (str);
8216 for (purpose = bitsize_zero_node;
8217 p < end
8218 && !(constructor_max_index
8219 && tree_int_cst_lt (constructor_max_index, purpose));
8220 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8222 if (wchar_bytes == 1)
8224 val[0] = (unsigned char) *p++;
8225 val[1] = 0;
8227 else
8229 val[1] = 0;
8230 val[0] = 0;
8231 for (byte = 0; byte < wchar_bytes; byte++)
8233 if (BYTES_BIG_ENDIAN)
8234 bitpos = (wchar_bytes - byte - 1) * charwidth;
8235 else
8236 bitpos = byte * charwidth;
8237 val[bitpos % HOST_BITS_PER_WIDE_INT]
8238 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8239 << (bitpos % HOST_BITS_PER_WIDE_INT);
8243 if (!TYPE_UNSIGNED (type))
8245 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8246 if (bitpos < HOST_BITS_PER_WIDE_INT)
8248 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8250 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8251 val[1] = -1;
8254 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8256 if (val[0] < 0)
8257 val[1] = -1;
8259 else if (val[1] & (((HOST_WIDE_INT) 1)
8260 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8261 val[1] |= ((HOST_WIDE_INT) -1)
8262 << (bitpos - HOST_BITS_PER_WIDE_INT);
8265 value = wide_int_to_tree (type,
8266 wide_int::from_array (val, 2,
8267 HOST_BITS_PER_WIDE_INT * 2));
8268 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8269 braced_init_obstack);
8272 constructor_incremental = 0;
8275 /* Return value of FIELD in pending initializer or zero if the field was
8276 not initialized yet. */
8278 static tree
8279 find_init_member (tree field, struct obstack * braced_init_obstack)
8281 struct init_node *p;
8283 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8285 if (constructor_incremental
8286 && tree_int_cst_lt (field, constructor_unfilled_index))
8287 set_nonincremental_init (braced_init_obstack);
8289 p = constructor_pending_elts;
8290 while (p)
8292 if (tree_int_cst_lt (field, p->purpose))
8293 p = p->left;
8294 else if (tree_int_cst_lt (p->purpose, field))
8295 p = p->right;
8296 else
8297 return p->value;
8300 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8302 tree bitpos = bit_position (field);
8304 if (constructor_incremental
8305 && (!constructor_unfilled_fields
8306 || tree_int_cst_lt (bitpos,
8307 bit_position (constructor_unfilled_fields))))
8308 set_nonincremental_init (braced_init_obstack);
8310 p = constructor_pending_elts;
8311 while (p)
8313 if (field == p->purpose)
8314 return p->value;
8315 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8316 p = p->left;
8317 else
8318 p = p->right;
8321 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8323 if (!vec_safe_is_empty (constructor_elements)
8324 && (constructor_elements->last ().index == field))
8325 return constructor_elements->last ().value;
8327 return 0;
8330 /* "Output" the next constructor element.
8331 At top level, really output it to assembler code now.
8332 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8333 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8334 TYPE is the data type that the containing data type wants here.
8335 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8336 If VALUE is a string constant, STRICT_STRING is true if it is
8337 unparenthesized or we should not warn here for it being parenthesized.
8338 For other types of VALUE, STRICT_STRING is not used.
8340 PENDING if non-nil means output pending elements that belong
8341 right after this element. (PENDING is normally 1;
8342 it is 0 while outputting pending elements, to avoid recursion.)
8344 IMPLICIT is true if value comes from pop_init_level (1),
8345 the new initializer has been merged with the existing one
8346 and thus no warnings should be emitted about overriding an
8347 existing initializer. */
8349 static void
8350 output_init_element (location_t loc, tree value, tree origtype,
8351 bool strict_string, tree type, tree field, int pending,
8352 bool implicit, struct obstack * braced_init_obstack)
8354 tree semantic_type = NULL_TREE;
8355 bool maybe_const = true;
8356 bool npc;
8358 if (type == error_mark_node || value == error_mark_node)
8360 constructor_erroneous = 1;
8361 return;
8363 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8364 && (TREE_CODE (value) == STRING_CST
8365 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8366 && !(TREE_CODE (value) == STRING_CST
8367 && TREE_CODE (type) == ARRAY_TYPE
8368 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8369 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8370 TYPE_MAIN_VARIANT (type)))
8371 value = array_to_pointer_conversion (input_location, value);
8373 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8374 && require_constant_value && pending)
8376 /* As an extension, allow initializing objects with static storage
8377 duration with compound literals (which are then treated just as
8378 the brace enclosed list they contain). */
8379 if (flag_isoc99)
8380 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8381 "constant");
8382 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8383 value = DECL_INITIAL (decl);
8386 npc = null_pointer_constant_p (value);
8387 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8389 semantic_type = TREE_TYPE (value);
8390 value = TREE_OPERAND (value, 0);
8392 value = c_fully_fold (value, require_constant_value, &maybe_const);
8394 if (value == error_mark_node)
8395 constructor_erroneous = 1;
8396 else if (!TREE_CONSTANT (value))
8397 constructor_constant = 0;
8398 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8399 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8400 || TREE_CODE (constructor_type) == UNION_TYPE)
8401 && DECL_C_BIT_FIELD (field)
8402 && TREE_CODE (value) != INTEGER_CST))
8403 constructor_simple = 0;
8404 if (!maybe_const)
8405 constructor_nonconst = 1;
8407 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8409 if (require_constant_value)
8411 error_init (loc, "initializer element is not constant");
8412 value = error_mark_node;
8414 else if (require_constant_elements)
8415 pedwarn (loc, OPT_Wpedantic,
8416 "initializer element is not computable at load time");
8418 else if (!maybe_const
8419 && (require_constant_value || require_constant_elements))
8420 pedwarn_init (loc, OPT_Wpedantic,
8421 "initializer element is not a constant expression");
8423 /* Issue -Wc++-compat warnings about initializing a bitfield with
8424 enum type. */
8425 if (warn_cxx_compat
8426 && field != NULL_TREE
8427 && TREE_CODE (field) == FIELD_DECL
8428 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8429 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8430 != TYPE_MAIN_VARIANT (type))
8431 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8433 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8434 if (checktype != error_mark_node
8435 && (TYPE_MAIN_VARIANT (checktype)
8436 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8437 warning_init (loc, OPT_Wc___compat,
8438 "enum conversion in initialization is invalid in C++");
8441 /* If this field is empty (and not at the end of structure),
8442 don't do anything other than checking the initializer. */
8443 if (field
8444 && (TREE_TYPE (field) == error_mark_node
8445 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8446 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8447 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8448 || DECL_CHAIN (field)))))
8449 return;
8451 if (semantic_type)
8452 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8453 value = digest_init (loc, type, value, origtype, npc, strict_string,
8454 require_constant_value);
8455 if (value == error_mark_node)
8457 constructor_erroneous = 1;
8458 return;
8460 if (require_constant_value || require_constant_elements)
8461 constant_expression_warning (value);
8463 /* If this element doesn't come next in sequence,
8464 put it on constructor_pending_elts. */
8465 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8466 && (!constructor_incremental
8467 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8469 if (constructor_incremental
8470 && tree_int_cst_lt (field, constructor_unfilled_index))
8471 set_nonincremental_init (braced_init_obstack);
8473 add_pending_init (loc, field, value, origtype, implicit,
8474 braced_init_obstack);
8475 return;
8477 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8478 && (!constructor_incremental
8479 || field != constructor_unfilled_fields))
8481 /* We do this for records but not for unions. In a union,
8482 no matter which field is specified, it can be initialized
8483 right away since it starts at the beginning of the union. */
8484 if (constructor_incremental)
8486 if (!constructor_unfilled_fields)
8487 set_nonincremental_init (braced_init_obstack);
8488 else
8490 tree bitpos, unfillpos;
8492 bitpos = bit_position (field);
8493 unfillpos = bit_position (constructor_unfilled_fields);
8495 if (tree_int_cst_lt (bitpos, unfillpos))
8496 set_nonincremental_init (braced_init_obstack);
8500 add_pending_init (loc, field, value, origtype, implicit,
8501 braced_init_obstack);
8502 return;
8504 else if (TREE_CODE (constructor_type) == UNION_TYPE
8505 && !vec_safe_is_empty (constructor_elements))
8507 if (!implicit)
8509 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8510 warning_init (loc, 0,
8511 "initialized field with side-effects overwritten");
8512 else if (warn_override_init)
8513 warning_init (loc, OPT_Woverride_init,
8514 "initialized field overwritten");
8517 /* We can have just one union field set. */
8518 constructor_elements = NULL;
8521 /* Otherwise, output this element either to
8522 constructor_elements or to the assembler file. */
8524 constructor_elt celt = {field, value};
8525 vec_safe_push (constructor_elements, celt);
8527 /* Advance the variable that indicates sequential elements output. */
8528 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8529 constructor_unfilled_index
8530 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8531 bitsize_one_node);
8532 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8534 constructor_unfilled_fields
8535 = DECL_CHAIN (constructor_unfilled_fields);
8537 /* Skip any nameless bit fields. */
8538 while (constructor_unfilled_fields != 0
8539 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8540 && DECL_NAME (constructor_unfilled_fields) == 0)
8541 constructor_unfilled_fields =
8542 DECL_CHAIN (constructor_unfilled_fields);
8544 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8545 constructor_unfilled_fields = 0;
8547 /* Now output any pending elements which have become next. */
8548 if (pending)
8549 output_pending_init_elements (0, braced_init_obstack);
8552 /* Output any pending elements which have become next.
8553 As we output elements, constructor_unfilled_{fields,index}
8554 advances, which may cause other elements to become next;
8555 if so, they too are output.
8557 If ALL is 0, we return when there are
8558 no more pending elements to output now.
8560 If ALL is 1, we output space as necessary so that
8561 we can output all the pending elements. */
8562 static void
8563 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8565 struct init_node *elt = constructor_pending_elts;
8566 tree next;
8568 retry:
8570 /* Look through the whole pending tree.
8571 If we find an element that should be output now,
8572 output it. Otherwise, set NEXT to the element
8573 that comes first among those still pending. */
8575 next = 0;
8576 while (elt)
8578 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8580 if (tree_int_cst_equal (elt->purpose,
8581 constructor_unfilled_index))
8582 output_init_element (input_location, elt->value, elt->origtype,
8583 true, TREE_TYPE (constructor_type),
8584 constructor_unfilled_index, 0, false,
8585 braced_init_obstack);
8586 else if (tree_int_cst_lt (constructor_unfilled_index,
8587 elt->purpose))
8589 /* Advance to the next smaller node. */
8590 if (elt->left)
8591 elt = elt->left;
8592 else
8594 /* We have reached the smallest node bigger than the
8595 current unfilled index. Fill the space first. */
8596 next = elt->purpose;
8597 break;
8600 else
8602 /* Advance to the next bigger node. */
8603 if (elt->right)
8604 elt = elt->right;
8605 else
8607 /* We have reached the biggest node in a subtree. Find
8608 the parent of it, which is the next bigger node. */
8609 while (elt->parent && elt->parent->right == elt)
8610 elt = elt->parent;
8611 elt = elt->parent;
8612 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8613 elt->purpose))
8615 next = elt->purpose;
8616 break;
8621 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8622 || TREE_CODE (constructor_type) == UNION_TYPE)
8624 tree ctor_unfilled_bitpos, elt_bitpos;
8626 /* If the current record is complete we are done. */
8627 if (constructor_unfilled_fields == 0)
8628 break;
8630 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8631 elt_bitpos = bit_position (elt->purpose);
8632 /* We can't compare fields here because there might be empty
8633 fields in between. */
8634 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8636 constructor_unfilled_fields = elt->purpose;
8637 output_init_element (input_location, elt->value, elt->origtype,
8638 true, TREE_TYPE (elt->purpose),
8639 elt->purpose, 0, false,
8640 braced_init_obstack);
8642 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8644 /* Advance to the next smaller node. */
8645 if (elt->left)
8646 elt = elt->left;
8647 else
8649 /* We have reached the smallest node bigger than the
8650 current unfilled field. Fill the space first. */
8651 next = elt->purpose;
8652 break;
8655 else
8657 /* Advance to the next bigger node. */
8658 if (elt->right)
8659 elt = elt->right;
8660 else
8662 /* We have reached the biggest node in a subtree. Find
8663 the parent of it, which is the next bigger node. */
8664 while (elt->parent && elt->parent->right == elt)
8665 elt = elt->parent;
8666 elt = elt->parent;
8667 if (elt
8668 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8669 bit_position (elt->purpose))))
8671 next = elt->purpose;
8672 break;
8679 /* Ordinarily return, but not if we want to output all
8680 and there are elements left. */
8681 if (!(all && next != 0))
8682 return;
8684 /* If it's not incremental, just skip over the gap, so that after
8685 jumping to retry we will output the next successive element. */
8686 if (TREE_CODE (constructor_type) == RECORD_TYPE
8687 || TREE_CODE (constructor_type) == UNION_TYPE)
8688 constructor_unfilled_fields = next;
8689 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8690 constructor_unfilled_index = next;
8692 /* ELT now points to the node in the pending tree with the next
8693 initializer to output. */
8694 goto retry;
8697 /* Add one non-braced element to the current constructor level.
8698 This adjusts the current position within the constructor's type.
8699 This may also start or terminate implicit levels
8700 to handle a partly-braced initializer.
8702 Once this has found the correct level for the new element,
8703 it calls output_init_element.
8705 IMPLICIT is true if value comes from pop_init_level (1),
8706 the new initializer has been merged with the existing one
8707 and thus no warnings should be emitted about overriding an
8708 existing initializer. */
8710 void
8711 process_init_element (location_t loc, struct c_expr value, bool implicit,
8712 struct obstack * braced_init_obstack)
8714 tree orig_value = value.value;
8715 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8716 bool strict_string = value.original_code == STRING_CST;
8717 bool was_designated = designator_depth != 0;
8719 designator_depth = 0;
8720 designator_erroneous = 0;
8722 if (!implicit && value.value && !integer_zerop (value.value))
8723 constructor_zeroinit = 0;
8725 /* Handle superfluous braces around string cst as in
8726 char x[] = {"foo"}; */
8727 if (string_flag
8728 && constructor_type
8729 && !was_designated
8730 && TREE_CODE (constructor_type) == ARRAY_TYPE
8731 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8732 && integer_zerop (constructor_unfilled_index))
8734 if (constructor_stack->replacement_value.value)
8735 error_init (loc, "excess elements in char array initializer");
8736 constructor_stack->replacement_value = value;
8737 return;
8740 if (constructor_stack->replacement_value.value != 0)
8742 error_init (loc, "excess elements in struct initializer");
8743 return;
8746 /* Ignore elements of a brace group if it is entirely superfluous
8747 and has already been diagnosed. */
8748 if (constructor_type == 0)
8749 return;
8751 if (!implicit && warn_designated_init && !was_designated
8752 && TREE_CODE (constructor_type) == RECORD_TYPE
8753 && lookup_attribute ("designated_init",
8754 TYPE_ATTRIBUTES (constructor_type)))
8755 warning_init (loc,
8756 OPT_Wdesignated_init,
8757 "positional initialization of field "
8758 "in %<struct%> declared with %<designated_init%> attribute");
8760 /* If we've exhausted any levels that didn't have braces,
8761 pop them now. */
8762 while (constructor_stack->implicit)
8764 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8765 || TREE_CODE (constructor_type) == UNION_TYPE)
8766 && constructor_fields == 0)
8767 process_init_element (loc,
8768 pop_init_level (loc, 1, braced_init_obstack),
8769 true, braced_init_obstack);
8770 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8771 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8772 && constructor_max_index
8773 && tree_int_cst_lt (constructor_max_index,
8774 constructor_index))
8775 process_init_element (loc,
8776 pop_init_level (loc, 1, braced_init_obstack),
8777 true, braced_init_obstack);
8778 else
8779 break;
8782 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8783 if (constructor_range_stack)
8785 /* If value is a compound literal and we'll be just using its
8786 content, don't put it into a SAVE_EXPR. */
8787 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8788 || !require_constant_value)
8790 tree semantic_type = NULL_TREE;
8791 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8793 semantic_type = TREE_TYPE (value.value);
8794 value.value = TREE_OPERAND (value.value, 0);
8796 value.value = c_save_expr (value.value);
8797 if (semantic_type)
8798 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8799 value.value);
8803 while (1)
8805 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8807 tree fieldtype;
8808 enum tree_code fieldcode;
8810 if (constructor_fields == 0)
8812 pedwarn_init (loc, 0, "excess elements in struct initializer");
8813 break;
8816 fieldtype = TREE_TYPE (constructor_fields);
8817 if (fieldtype != error_mark_node)
8818 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8819 fieldcode = TREE_CODE (fieldtype);
8821 /* Error for non-static initialization of a flexible array member. */
8822 if (fieldcode == ARRAY_TYPE
8823 && !require_constant_value
8824 && TYPE_SIZE (fieldtype) == NULL_TREE
8825 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8827 error_init (loc, "non-static initialization of a flexible "
8828 "array member");
8829 break;
8832 /* Error for initialization of a flexible array member with
8833 a string constant if the structure is in an array. E.g.:
8834 struct S { int x; char y[]; };
8835 struct S s[] = { { 1, "foo" } };
8836 is invalid. */
8837 if (string_flag
8838 && fieldcode == ARRAY_TYPE
8839 && constructor_depth > 1
8840 && TYPE_SIZE (fieldtype) == NULL_TREE
8841 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8843 bool in_array_p = false;
8844 for (struct constructor_stack *p = constructor_stack;
8845 p && p->type; p = p->next)
8846 if (TREE_CODE (p->type) == ARRAY_TYPE)
8848 in_array_p = true;
8849 break;
8851 if (in_array_p)
8853 error_init (loc, "initialization of flexible array "
8854 "member in a nested context");
8855 break;
8859 /* Accept a string constant to initialize a subarray. */
8860 if (value.value != 0
8861 && fieldcode == ARRAY_TYPE
8862 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8863 && string_flag)
8864 value.value = orig_value;
8865 /* Otherwise, if we have come to a subaggregate,
8866 and we don't have an element of its type, push into it. */
8867 else if (value.value != 0
8868 && value.value != error_mark_node
8869 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8870 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8871 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8873 push_init_level (loc, 1, braced_init_obstack);
8874 continue;
8877 if (value.value)
8879 push_member_name (constructor_fields);
8880 output_init_element (loc, value.value, value.original_type,
8881 strict_string, fieldtype,
8882 constructor_fields, 1, implicit,
8883 braced_init_obstack);
8884 RESTORE_SPELLING_DEPTH (constructor_depth);
8886 else
8887 /* Do the bookkeeping for an element that was
8888 directly output as a constructor. */
8890 /* For a record, keep track of end position of last field. */
8891 if (DECL_SIZE (constructor_fields))
8892 constructor_bit_index
8893 = size_binop_loc (input_location, PLUS_EXPR,
8894 bit_position (constructor_fields),
8895 DECL_SIZE (constructor_fields));
8897 /* If the current field was the first one not yet written out,
8898 it isn't now, so update. */
8899 if (constructor_unfilled_fields == constructor_fields)
8901 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8902 /* Skip any nameless bit fields. */
8903 while (constructor_unfilled_fields != 0
8904 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8905 && DECL_NAME (constructor_unfilled_fields) == 0)
8906 constructor_unfilled_fields =
8907 DECL_CHAIN (constructor_unfilled_fields);
8911 constructor_fields = DECL_CHAIN (constructor_fields);
8912 /* Skip any nameless bit fields at the beginning. */
8913 while (constructor_fields != 0
8914 && DECL_C_BIT_FIELD (constructor_fields)
8915 && DECL_NAME (constructor_fields) == 0)
8916 constructor_fields = DECL_CHAIN (constructor_fields);
8918 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8920 tree fieldtype;
8921 enum tree_code fieldcode;
8923 if (constructor_fields == 0)
8925 pedwarn_init (loc, 0,
8926 "excess elements in union initializer");
8927 break;
8930 fieldtype = TREE_TYPE (constructor_fields);
8931 if (fieldtype != error_mark_node)
8932 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8933 fieldcode = TREE_CODE (fieldtype);
8935 /* Warn that traditional C rejects initialization of unions.
8936 We skip the warning if the value is zero. This is done
8937 under the assumption that the zero initializer in user
8938 code appears conditioned on e.g. __STDC__ to avoid
8939 "missing initializer" warnings and relies on default
8940 initialization to zero in the traditional C case.
8941 We also skip the warning if the initializer is designated,
8942 again on the assumption that this must be conditional on
8943 __STDC__ anyway (and we've already complained about the
8944 member-designator already). */
8945 if (!in_system_header_at (input_location) && !constructor_designated
8946 && !(value.value && (integer_zerop (value.value)
8947 || real_zerop (value.value))))
8948 warning (OPT_Wtraditional, "traditional C rejects initialization "
8949 "of unions");
8951 /* Accept a string constant to initialize a subarray. */
8952 if (value.value != 0
8953 && fieldcode == ARRAY_TYPE
8954 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8955 && string_flag)
8956 value.value = orig_value;
8957 /* Otherwise, if we have come to a subaggregate,
8958 and we don't have an element of its type, push into it. */
8959 else if (value.value != 0
8960 && value.value != error_mark_node
8961 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8962 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8963 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8965 push_init_level (loc, 1, braced_init_obstack);
8966 continue;
8969 if (value.value)
8971 push_member_name (constructor_fields);
8972 output_init_element (loc, value.value, value.original_type,
8973 strict_string, fieldtype,
8974 constructor_fields, 1, implicit,
8975 braced_init_obstack);
8976 RESTORE_SPELLING_DEPTH (constructor_depth);
8978 else
8979 /* Do the bookkeeping for an element that was
8980 directly output as a constructor. */
8982 constructor_bit_index = DECL_SIZE (constructor_fields);
8983 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8986 constructor_fields = 0;
8988 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8990 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8991 enum tree_code eltcode = TREE_CODE (elttype);
8993 /* Accept a string constant to initialize a subarray. */
8994 if (value.value != 0
8995 && eltcode == ARRAY_TYPE
8996 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8997 && string_flag)
8998 value.value = orig_value;
8999 /* Otherwise, if we have come to a subaggregate,
9000 and we don't have an element of its type, push into it. */
9001 else if (value.value != 0
9002 && value.value != error_mark_node
9003 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9004 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9005 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9007 push_init_level (loc, 1, braced_init_obstack);
9008 continue;
9011 if (constructor_max_index != 0
9012 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9013 || integer_all_onesp (constructor_max_index)))
9015 pedwarn_init (loc, 0,
9016 "excess elements in array initializer");
9017 break;
9020 /* Now output the actual element. */
9021 if (value.value)
9023 push_array_bounds (tree_to_uhwi (constructor_index));
9024 output_init_element (loc, value.value, value.original_type,
9025 strict_string, elttype,
9026 constructor_index, 1, implicit,
9027 braced_init_obstack);
9028 RESTORE_SPELLING_DEPTH (constructor_depth);
9031 constructor_index
9032 = size_binop_loc (input_location, PLUS_EXPR,
9033 constructor_index, bitsize_one_node);
9035 if (!value.value)
9036 /* If we are doing the bookkeeping for an element that was
9037 directly output as a constructor, we must update
9038 constructor_unfilled_index. */
9039 constructor_unfilled_index = constructor_index;
9041 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9043 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9045 /* Do a basic check of initializer size. Note that vectors
9046 always have a fixed size derived from their type. */
9047 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9049 pedwarn_init (loc, 0,
9050 "excess elements in vector initializer");
9051 break;
9054 /* Now output the actual element. */
9055 if (value.value)
9057 if (TREE_CODE (value.value) == VECTOR_CST)
9058 elttype = TYPE_MAIN_VARIANT (constructor_type);
9059 output_init_element (loc, value.value, value.original_type,
9060 strict_string, elttype,
9061 constructor_index, 1, implicit,
9062 braced_init_obstack);
9065 constructor_index
9066 = size_binop_loc (input_location,
9067 PLUS_EXPR, constructor_index, bitsize_one_node);
9069 if (!value.value)
9070 /* If we are doing the bookkeeping for an element that was
9071 directly output as a constructor, we must update
9072 constructor_unfilled_index. */
9073 constructor_unfilled_index = constructor_index;
9076 /* Handle the sole element allowed in a braced initializer
9077 for a scalar variable. */
9078 else if (constructor_type != error_mark_node
9079 && constructor_fields == 0)
9081 pedwarn_init (loc, 0,
9082 "excess elements in scalar initializer");
9083 break;
9085 else
9087 if (value.value)
9088 output_init_element (loc, value.value, value.original_type,
9089 strict_string, constructor_type,
9090 NULL_TREE, 1, implicit,
9091 braced_init_obstack);
9092 constructor_fields = 0;
9095 /* Handle range initializers either at this level or anywhere higher
9096 in the designator stack. */
9097 if (constructor_range_stack)
9099 struct constructor_range_stack *p, *range_stack;
9100 int finish = 0;
9102 range_stack = constructor_range_stack;
9103 constructor_range_stack = 0;
9104 while (constructor_stack != range_stack->stack)
9106 gcc_assert (constructor_stack->implicit);
9107 process_init_element (loc,
9108 pop_init_level (loc, 1,
9109 braced_init_obstack),
9110 true, braced_init_obstack);
9112 for (p = range_stack;
9113 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9114 p = p->prev)
9116 gcc_assert (constructor_stack->implicit);
9117 process_init_element (loc,
9118 pop_init_level (loc, 1,
9119 braced_init_obstack),
9120 true, braced_init_obstack);
9123 p->index = size_binop_loc (input_location,
9124 PLUS_EXPR, p->index, bitsize_one_node);
9125 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9126 finish = 1;
9128 while (1)
9130 constructor_index = p->index;
9131 constructor_fields = p->fields;
9132 if (finish && p->range_end && p->index == p->range_start)
9134 finish = 0;
9135 p->prev = 0;
9137 p = p->next;
9138 if (!p)
9139 break;
9140 push_init_level (loc, 2, braced_init_obstack);
9141 p->stack = constructor_stack;
9142 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9143 p->index = p->range_start;
9146 if (!finish)
9147 constructor_range_stack = range_stack;
9148 continue;
9151 break;
9154 constructor_range_stack = 0;
9157 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9158 (guaranteed to be 'volatile' or null) and ARGS (represented using
9159 an ASM_EXPR node). */
9160 tree
9161 build_asm_stmt (tree cv_qualifier, tree args)
9163 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9164 ASM_VOLATILE_P (args) = 1;
9165 return add_stmt (args);
9168 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9169 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9170 SIMPLE indicates whether there was anything at all after the
9171 string in the asm expression -- asm("blah") and asm("blah" : )
9172 are subtly different. We use a ASM_EXPR node to represent this. */
9173 tree
9174 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9175 tree clobbers, tree labels, bool simple)
9177 tree tail;
9178 tree args;
9179 int i;
9180 const char *constraint;
9181 const char **oconstraints;
9182 bool allows_mem, allows_reg, is_inout;
9183 int ninputs, noutputs;
9185 ninputs = list_length (inputs);
9186 noutputs = list_length (outputs);
9187 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9189 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9191 /* Remove output conversions that change the type but not the mode. */
9192 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9194 tree output = TREE_VALUE (tail);
9196 output = c_fully_fold (output, false, NULL);
9198 /* ??? Really, this should not be here. Users should be using a
9199 proper lvalue, dammit. But there's a long history of using casts
9200 in the output operands. In cases like longlong.h, this becomes a
9201 primitive form of typechecking -- if the cast can be removed, then
9202 the output operand had a type of the proper width; otherwise we'll
9203 get an error. Gross, but ... */
9204 STRIP_NOPS (output);
9206 if (!lvalue_or_else (loc, output, lv_asm))
9207 output = error_mark_node;
9209 if (output != error_mark_node
9210 && (TREE_READONLY (output)
9211 || TYPE_READONLY (TREE_TYPE (output))
9212 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9213 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9214 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9215 readonly_error (loc, output, lv_asm);
9217 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9218 oconstraints[i] = constraint;
9220 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9221 &allows_mem, &allows_reg, &is_inout))
9223 /* If the operand is going to end up in memory,
9224 mark it addressable. */
9225 if (!allows_reg && !c_mark_addressable (output))
9226 output = error_mark_node;
9227 if (!(!allows_reg && allows_mem)
9228 && output != error_mark_node
9229 && VOID_TYPE_P (TREE_TYPE (output)))
9231 error_at (loc, "invalid use of void expression");
9232 output = error_mark_node;
9235 else
9236 output = error_mark_node;
9238 TREE_VALUE (tail) = output;
9241 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9243 tree input;
9245 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9246 input = TREE_VALUE (tail);
9248 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9249 oconstraints, &allows_mem, &allows_reg))
9251 /* If the operand is going to end up in memory,
9252 mark it addressable. */
9253 if (!allows_reg && allows_mem)
9255 input = c_fully_fold (input, false, NULL);
9257 /* Strip the nops as we allow this case. FIXME, this really
9258 should be rejected or made deprecated. */
9259 STRIP_NOPS (input);
9260 if (!c_mark_addressable (input))
9261 input = error_mark_node;
9263 else
9265 struct c_expr expr;
9266 memset (&expr, 0, sizeof (expr));
9267 expr.value = input;
9268 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9269 input = c_fully_fold (expr.value, false, NULL);
9271 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9273 error_at (loc, "invalid use of void expression");
9274 input = error_mark_node;
9278 else
9279 input = error_mark_node;
9281 TREE_VALUE (tail) = input;
9284 /* ASMs with labels cannot have outputs. This should have been
9285 enforced by the parser. */
9286 gcc_assert (outputs == NULL || labels == NULL);
9288 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9290 /* asm statements without outputs, including simple ones, are treated
9291 as volatile. */
9292 ASM_INPUT_P (args) = simple;
9293 ASM_VOLATILE_P (args) = (noutputs == 0);
9295 return args;
9298 /* Generate a goto statement to LABEL. LOC is the location of the
9299 GOTO. */
9301 tree
9302 c_finish_goto_label (location_t loc, tree label)
9304 tree decl = lookup_label_for_goto (loc, label);
9305 if (!decl)
9306 return NULL_TREE;
9307 TREE_USED (decl) = 1;
9309 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9310 SET_EXPR_LOCATION (t, loc);
9311 return add_stmt (t);
9315 /* Generate a computed goto statement to EXPR. LOC is the location of
9316 the GOTO. */
9318 tree
9319 c_finish_goto_ptr (location_t loc, tree expr)
9321 tree t;
9322 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9323 expr = c_fully_fold (expr, false, NULL);
9324 expr = convert (ptr_type_node, expr);
9325 t = build1 (GOTO_EXPR, void_type_node, expr);
9326 SET_EXPR_LOCATION (t, loc);
9327 return add_stmt (t);
9330 /* Generate a C `return' statement. RETVAL is the expression for what
9331 to return, or a null pointer for `return;' with no value. LOC is
9332 the location of the return statement, or the location of the expression,
9333 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9334 is the original type of RETVAL. */
9336 tree
9337 c_finish_return (location_t loc, tree retval, tree origtype)
9339 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9340 bool no_warning = false;
9341 bool npc = false;
9342 size_t rank = 0;
9344 if (TREE_THIS_VOLATILE (current_function_decl))
9345 warning_at (loc, 0,
9346 "function declared %<noreturn%> has a %<return%> statement");
9348 if (flag_cilkplus && contains_array_notation_expr (retval))
9350 /* Array notations are allowed in a return statement if it is inside a
9351 built-in array notation reduction function. */
9352 if (!find_rank (loc, retval, retval, false, &rank))
9353 return error_mark_node;
9354 if (rank >= 1)
9356 error_at (loc, "array notation expression cannot be used as a "
9357 "return value");
9358 return error_mark_node;
9361 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9363 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9364 "allowed");
9365 return error_mark_node;
9367 if (retval)
9369 tree semantic_type = NULL_TREE;
9370 npc = null_pointer_constant_p (retval);
9371 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9373 semantic_type = TREE_TYPE (retval);
9374 retval = TREE_OPERAND (retval, 0);
9376 retval = c_fully_fold (retval, false, NULL);
9377 if (semantic_type)
9378 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9381 if (!retval)
9383 current_function_returns_null = 1;
9384 if ((warn_return_type || flag_isoc99)
9385 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9387 if (flag_isoc99)
9388 pedwarn (loc, 0, "%<return%> with no value, in "
9389 "function returning non-void");
9390 else
9391 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9392 "in function returning non-void");
9393 no_warning = true;
9396 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9398 current_function_returns_null = 1;
9399 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9400 pedwarn (loc, 0,
9401 "%<return%> with a value, in function returning void");
9402 else
9403 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9404 "%<return%> with expression, in function returning void");
9406 else
9408 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9409 retval, origtype, ic_return,
9410 npc, NULL_TREE, NULL_TREE, 0);
9411 tree res = DECL_RESULT (current_function_decl);
9412 tree inner;
9413 bool save;
9415 current_function_returns_value = 1;
9416 if (t == error_mark_node)
9417 return NULL_TREE;
9419 save = in_late_binary_op;
9420 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9421 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9422 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9423 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9424 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9425 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9426 in_late_binary_op = true;
9427 inner = t = convert (TREE_TYPE (res), t);
9428 in_late_binary_op = save;
9430 /* Strip any conversions, additions, and subtractions, and see if
9431 we are returning the address of a local variable. Warn if so. */
9432 while (1)
9434 switch (TREE_CODE (inner))
9436 CASE_CONVERT:
9437 case NON_LVALUE_EXPR:
9438 case PLUS_EXPR:
9439 case POINTER_PLUS_EXPR:
9440 inner = TREE_OPERAND (inner, 0);
9441 continue;
9443 case MINUS_EXPR:
9444 /* If the second operand of the MINUS_EXPR has a pointer
9445 type (or is converted from it), this may be valid, so
9446 don't give a warning. */
9448 tree op1 = TREE_OPERAND (inner, 1);
9450 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9451 && (CONVERT_EXPR_P (op1)
9452 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9453 op1 = TREE_OPERAND (op1, 0);
9455 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9456 break;
9458 inner = TREE_OPERAND (inner, 0);
9459 continue;
9462 case ADDR_EXPR:
9463 inner = TREE_OPERAND (inner, 0);
9465 while (REFERENCE_CLASS_P (inner)
9466 && TREE_CODE (inner) != INDIRECT_REF)
9467 inner = TREE_OPERAND (inner, 0);
9469 if (DECL_P (inner)
9470 && !DECL_EXTERNAL (inner)
9471 && !TREE_STATIC (inner)
9472 && DECL_CONTEXT (inner) == current_function_decl)
9474 if (TREE_CODE (inner) == LABEL_DECL)
9475 warning_at (loc, OPT_Wreturn_local_addr,
9476 "function returns address of label");
9477 else
9479 warning_at (loc, OPT_Wreturn_local_addr,
9480 "function returns address of local variable");
9481 tree zero = build_zero_cst (TREE_TYPE (res));
9482 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9485 break;
9487 default:
9488 break;
9491 break;
9494 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9495 SET_EXPR_LOCATION (retval, loc);
9497 if (warn_sequence_point)
9498 verify_sequence_points (retval);
9501 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9502 TREE_NO_WARNING (ret_stmt) |= no_warning;
9503 return add_stmt (ret_stmt);
9506 struct c_switch {
9507 /* The SWITCH_EXPR being built. */
9508 tree switch_expr;
9510 /* The original type of the testing expression, i.e. before the
9511 default conversion is applied. */
9512 tree orig_type;
9514 /* A splay-tree mapping the low element of a case range to the high
9515 element, or NULL_TREE if there is no high element. Used to
9516 determine whether or not a new case label duplicates an old case
9517 label. We need a tree, rather than simply a hash table, because
9518 of the GNU case range extension. */
9519 splay_tree cases;
9521 /* The bindings at the point of the switch. This is used for
9522 warnings crossing decls when branching to a case label. */
9523 struct c_spot_bindings *bindings;
9525 /* The next node on the stack. */
9526 struct c_switch *next;
9529 /* A stack of the currently active switch statements. The innermost
9530 switch statement is on the top of the stack. There is no need to
9531 mark the stack for garbage collection because it is only active
9532 during the processing of the body of a function, and we never
9533 collect at that point. */
9535 struct c_switch *c_switch_stack;
9537 /* Start a C switch statement, testing expression EXP. Return the new
9538 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9539 SWITCH_COND_LOC is the location of the switch's condition.
9540 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9542 tree
9543 c_start_case (location_t switch_loc,
9544 location_t switch_cond_loc,
9545 tree exp, bool explicit_cast_p)
9547 tree orig_type = error_mark_node;
9548 struct c_switch *cs;
9550 if (exp != error_mark_node)
9552 orig_type = TREE_TYPE (exp);
9554 if (!INTEGRAL_TYPE_P (orig_type))
9556 if (orig_type != error_mark_node)
9558 error_at (switch_cond_loc, "switch quantity not an integer");
9559 orig_type = error_mark_node;
9561 exp = integer_zero_node;
9563 else
9565 tree type = TYPE_MAIN_VARIANT (orig_type);
9566 tree e = exp;
9568 /* Warn if the condition has boolean value. */
9569 while (TREE_CODE (e) == COMPOUND_EXPR)
9570 e = TREE_OPERAND (e, 1);
9572 if ((TREE_CODE (type) == BOOLEAN_TYPE
9573 || truth_value_p (TREE_CODE (e)))
9574 /* Explicit cast to int suppresses this warning. */
9575 && !(TREE_CODE (type) == INTEGER_TYPE
9576 && explicit_cast_p))
9577 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9578 "switch condition has boolean value");
9580 if (!in_system_header_at (input_location)
9581 && (type == long_integer_type_node
9582 || type == long_unsigned_type_node))
9583 warning_at (switch_cond_loc,
9584 OPT_Wtraditional, "%<long%> switch expression not "
9585 "converted to %<int%> in ISO C");
9587 exp = c_fully_fold (exp, false, NULL);
9588 exp = default_conversion (exp);
9590 if (warn_sequence_point)
9591 verify_sequence_points (exp);
9595 /* Add this new SWITCH_EXPR to the stack. */
9596 cs = XNEW (struct c_switch);
9597 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9598 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9599 cs->orig_type = orig_type;
9600 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9601 cs->bindings = c_get_switch_bindings ();
9602 cs->next = c_switch_stack;
9603 c_switch_stack = cs;
9605 return add_stmt (cs->switch_expr);
9608 /* Process a case label at location LOC. */
9610 tree
9611 do_case (location_t loc, tree low_value, tree high_value)
9613 tree label = NULL_TREE;
9615 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9617 low_value = c_fully_fold (low_value, false, NULL);
9618 if (TREE_CODE (low_value) == INTEGER_CST)
9619 pedwarn (loc, OPT_Wpedantic,
9620 "case label is not an integer constant expression");
9623 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9625 high_value = c_fully_fold (high_value, false, NULL);
9626 if (TREE_CODE (high_value) == INTEGER_CST)
9627 pedwarn (input_location, OPT_Wpedantic,
9628 "case label is not an integer constant expression");
9631 if (c_switch_stack == NULL)
9633 if (low_value)
9634 error_at (loc, "case label not within a switch statement");
9635 else
9636 error_at (loc, "%<default%> label not within a switch statement");
9637 return NULL_TREE;
9640 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9641 EXPR_LOCATION (c_switch_stack->switch_expr),
9642 loc))
9643 return NULL_TREE;
9645 label = c_add_case_label (loc, c_switch_stack->cases,
9646 SWITCH_COND (c_switch_stack->switch_expr),
9647 c_switch_stack->orig_type,
9648 low_value, high_value);
9649 if (label == error_mark_node)
9650 label = NULL_TREE;
9651 return label;
9654 /* Finish the switch statement. TYPE is the original type of the
9655 controlling expression of the switch, or NULL_TREE. */
9657 void
9658 c_finish_case (tree body, tree type)
9660 struct c_switch *cs = c_switch_stack;
9661 location_t switch_location;
9663 SWITCH_BODY (cs->switch_expr) = body;
9665 /* Emit warnings as needed. */
9666 switch_location = EXPR_LOCATION (cs->switch_expr);
9667 c_do_switch_warnings (cs->cases, switch_location,
9668 type ? type : TREE_TYPE (cs->switch_expr),
9669 SWITCH_COND (cs->switch_expr));
9671 /* Pop the stack. */
9672 c_switch_stack = cs->next;
9673 splay_tree_delete (cs->cases);
9674 c_release_switch_bindings (cs->bindings);
9675 XDELETE (cs);
9678 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9679 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9680 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9681 statement, and was not surrounded with parenthesis. */
9683 void
9684 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9685 tree else_block, bool nested_if)
9687 tree stmt;
9689 /* If the condition has array notations, then the rank of the then_block and
9690 else_block must be either 0 or be equal to the rank of the condition. If
9691 the condition does not have array notations then break them up as it is
9692 broken up in a normal expression. */
9693 if (flag_cilkplus && contains_array_notation_expr (cond))
9695 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9696 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9697 return;
9698 if (then_block
9699 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9700 return;
9701 if (else_block
9702 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9703 return;
9704 if (cond_rank != then_rank && then_rank != 0)
9706 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9707 " and the then-block");
9708 return;
9710 else if (cond_rank != else_rank && else_rank != 0)
9712 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9713 " and the else-block");
9714 return;
9717 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9718 if (warn_parentheses && nested_if && else_block == NULL)
9720 tree inner_if = then_block;
9722 /* We know from the grammar productions that there is an IF nested
9723 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9724 it might not be exactly THEN_BLOCK, but should be the last
9725 non-container statement within. */
9726 while (1)
9727 switch (TREE_CODE (inner_if))
9729 case COND_EXPR:
9730 goto found;
9731 case BIND_EXPR:
9732 inner_if = BIND_EXPR_BODY (inner_if);
9733 break;
9734 case STATEMENT_LIST:
9735 inner_if = expr_last (then_block);
9736 break;
9737 case TRY_FINALLY_EXPR:
9738 case TRY_CATCH_EXPR:
9739 inner_if = TREE_OPERAND (inner_if, 0);
9740 break;
9741 default:
9742 gcc_unreachable ();
9744 found:
9746 if (COND_EXPR_ELSE (inner_if))
9747 warning_at (if_locus, OPT_Wparentheses,
9748 "suggest explicit braces to avoid ambiguous %<else%>");
9751 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9752 SET_EXPR_LOCATION (stmt, if_locus);
9753 add_stmt (stmt);
9756 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9757 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9758 is false for DO loops. INCR is the FOR increment expression. BODY is
9759 the statement controlled by the loop. BLAB is the break label. CLAB is
9760 the continue label. Everything is allowed to be NULL. */
9762 void
9763 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9764 tree blab, tree clab, bool cond_is_first)
9766 tree entry = NULL, exit = NULL, t;
9768 /* In theory could forbid cilk spawn for loop increment expression,
9769 but it should work just fine. */
9771 /* If the condition is zero don't generate a loop construct. */
9772 if (cond && integer_zerop (cond))
9774 if (cond_is_first)
9776 t = build_and_jump (&blab);
9777 SET_EXPR_LOCATION (t, start_locus);
9778 add_stmt (t);
9781 else
9783 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9785 /* If we have an exit condition, then we build an IF with gotos either
9786 out of the loop, or to the top of it. If there's no exit condition,
9787 then we just build a jump back to the top. */
9788 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9790 if (cond && !integer_nonzerop (cond))
9792 /* Canonicalize the loop condition to the end. This means
9793 generating a branch to the loop condition. Reuse the
9794 continue label, if possible. */
9795 if (cond_is_first)
9797 if (incr || !clab)
9799 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9800 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9802 else
9803 t = build1 (GOTO_EXPR, void_type_node, clab);
9804 SET_EXPR_LOCATION (t, start_locus);
9805 add_stmt (t);
9808 t = build_and_jump (&blab);
9809 if (cond_is_first)
9810 exit = fold_build3_loc (start_locus,
9811 COND_EXPR, void_type_node, cond, exit, t);
9812 else
9813 exit = fold_build3_loc (input_location,
9814 COND_EXPR, void_type_node, cond, exit, t);
9817 add_stmt (top);
9820 if (body)
9821 add_stmt (body);
9822 if (clab)
9823 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9824 if (incr)
9825 add_stmt (incr);
9826 if (entry)
9827 add_stmt (entry);
9828 if (exit)
9829 add_stmt (exit);
9830 if (blab)
9831 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9834 tree
9835 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9837 bool skip;
9838 tree label = *label_p;
9840 /* In switch statements break is sometimes stylistically used after
9841 a return statement. This can lead to spurious warnings about
9842 control reaching the end of a non-void function when it is
9843 inlined. Note that we are calling block_may_fallthru with
9844 language specific tree nodes; this works because
9845 block_may_fallthru returns true when given something it does not
9846 understand. */
9847 skip = !block_may_fallthru (cur_stmt_list);
9849 if (!label)
9851 if (!skip)
9852 *label_p = label = create_artificial_label (loc);
9854 else if (TREE_CODE (label) == LABEL_DECL)
9856 else switch (TREE_INT_CST_LOW (label))
9858 case 0:
9859 if (is_break)
9860 error_at (loc, "break statement not within loop or switch");
9861 else
9862 error_at (loc, "continue statement not within a loop");
9863 return NULL_TREE;
9865 case 1:
9866 gcc_assert (is_break);
9867 error_at (loc, "break statement used with OpenMP for loop");
9868 return NULL_TREE;
9870 case 2:
9871 if (is_break)
9872 error ("break statement within %<#pragma simd%> loop body");
9873 else
9874 error ("continue statement within %<#pragma simd%> loop body");
9875 return NULL_TREE;
9877 default:
9878 gcc_unreachable ();
9881 if (skip)
9882 return NULL_TREE;
9884 if (!is_break)
9885 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9887 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9890 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9892 static void
9893 emit_side_effect_warnings (location_t loc, tree expr)
9895 if (expr == error_mark_node)
9897 else if (!TREE_SIDE_EFFECTS (expr))
9899 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9900 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9902 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9904 tree r = expr;
9905 location_t cloc = loc;
9906 while (TREE_CODE (r) == COMPOUND_EXPR)
9908 if (EXPR_HAS_LOCATION (r))
9909 cloc = EXPR_LOCATION (r);
9910 r = TREE_OPERAND (r, 1);
9912 if (!TREE_SIDE_EFFECTS (r)
9913 && !VOID_TYPE_P (TREE_TYPE (r))
9914 && !CONVERT_EXPR_P (r)
9915 && !TREE_NO_WARNING (r)
9916 && !TREE_NO_WARNING (expr))
9917 warning_at (cloc, OPT_Wunused_value,
9918 "right-hand operand of comma expression has no effect");
9920 else
9921 warn_if_unused_value (expr, loc);
9924 /* Process an expression as if it were a complete statement. Emit
9925 diagnostics, but do not call ADD_STMT. LOC is the location of the
9926 statement. */
9928 tree
9929 c_process_expr_stmt (location_t loc, tree expr)
9931 tree exprv;
9933 if (!expr)
9934 return NULL_TREE;
9936 expr = c_fully_fold (expr, false, NULL);
9938 if (warn_sequence_point)
9939 verify_sequence_points (expr);
9941 if (TREE_TYPE (expr) != error_mark_node
9942 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9943 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9944 error_at (loc, "expression statement has incomplete type");
9946 /* If we're not processing a statement expression, warn about unused values.
9947 Warnings for statement expressions will be emitted later, once we figure
9948 out which is the result. */
9949 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9950 && warn_unused_value)
9951 emit_side_effect_warnings (loc, expr);
9953 exprv = expr;
9954 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9955 exprv = TREE_OPERAND (exprv, 1);
9956 while (CONVERT_EXPR_P (exprv))
9957 exprv = TREE_OPERAND (exprv, 0);
9958 if (DECL_P (exprv)
9959 || handled_component_p (exprv)
9960 || TREE_CODE (exprv) == ADDR_EXPR)
9961 mark_exp_read (exprv);
9963 /* If the expression is not of a type to which we cannot assign a line
9964 number, wrap the thing in a no-op NOP_EXPR. */
9965 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9967 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9968 SET_EXPR_LOCATION (expr, loc);
9971 return expr;
9974 /* Emit an expression as a statement. LOC is the location of the
9975 expression. */
9977 tree
9978 c_finish_expr_stmt (location_t loc, tree expr)
9980 if (expr)
9981 return add_stmt (c_process_expr_stmt (loc, expr));
9982 else
9983 return NULL;
9986 /* Do the opposite and emit a statement as an expression. To begin,
9987 create a new binding level and return it. */
9989 tree
9990 c_begin_stmt_expr (void)
9992 tree ret;
9994 /* We must force a BLOCK for this level so that, if it is not expanded
9995 later, there is a way to turn off the entire subtree of blocks that
9996 are contained in it. */
9997 keep_next_level ();
9998 ret = c_begin_compound_stmt (true);
10000 c_bindings_start_stmt_expr (c_switch_stack == NULL
10001 ? NULL
10002 : c_switch_stack->bindings);
10004 /* Mark the current statement list as belonging to a statement list. */
10005 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10007 return ret;
10010 /* LOC is the location of the compound statement to which this body
10011 belongs. */
10013 tree
10014 c_finish_stmt_expr (location_t loc, tree body)
10016 tree last, type, tmp, val;
10017 tree *last_p;
10019 body = c_end_compound_stmt (loc, body, true);
10021 c_bindings_end_stmt_expr (c_switch_stack == NULL
10022 ? NULL
10023 : c_switch_stack->bindings);
10025 /* Locate the last statement in BODY. See c_end_compound_stmt
10026 about always returning a BIND_EXPR. */
10027 last_p = &BIND_EXPR_BODY (body);
10028 last = BIND_EXPR_BODY (body);
10030 continue_searching:
10031 if (TREE_CODE (last) == STATEMENT_LIST)
10033 tree_stmt_iterator i;
10035 /* This can happen with degenerate cases like ({ }). No value. */
10036 if (!TREE_SIDE_EFFECTS (last))
10037 return body;
10039 /* If we're supposed to generate side effects warnings, process
10040 all of the statements except the last. */
10041 if (warn_unused_value)
10043 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10045 location_t tloc;
10046 tree t = tsi_stmt (i);
10048 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10049 emit_side_effect_warnings (tloc, t);
10052 else
10053 i = tsi_last (last);
10054 last_p = tsi_stmt_ptr (i);
10055 last = *last_p;
10058 /* If the end of the list is exception related, then the list was split
10059 by a call to push_cleanup. Continue searching. */
10060 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10061 || TREE_CODE (last) == TRY_CATCH_EXPR)
10063 last_p = &TREE_OPERAND (last, 0);
10064 last = *last_p;
10065 goto continue_searching;
10068 if (last == error_mark_node)
10069 return last;
10071 /* In the case that the BIND_EXPR is not necessary, return the
10072 expression out from inside it. */
10073 if (last == BIND_EXPR_BODY (body)
10074 && BIND_EXPR_VARS (body) == NULL)
10076 /* Even if this looks constant, do not allow it in a constant
10077 expression. */
10078 last = c_wrap_maybe_const (last, true);
10079 /* Do not warn if the return value of a statement expression is
10080 unused. */
10081 TREE_NO_WARNING (last) = 1;
10082 return last;
10085 /* Extract the type of said expression. */
10086 type = TREE_TYPE (last);
10088 /* If we're not returning a value at all, then the BIND_EXPR that
10089 we already have is a fine expression to return. */
10090 if (!type || VOID_TYPE_P (type))
10091 return body;
10093 /* Now that we've located the expression containing the value, it seems
10094 silly to make voidify_wrapper_expr repeat the process. Create a
10095 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10096 tmp = create_tmp_var_raw (type);
10098 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10099 tree_expr_nonnegative_p giving up immediately. */
10100 val = last;
10101 if (TREE_CODE (val) == NOP_EXPR
10102 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10103 val = TREE_OPERAND (val, 0);
10105 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10106 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10109 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10110 SET_EXPR_LOCATION (t, loc);
10111 return t;
10115 /* Begin and end compound statements. This is as simple as pushing
10116 and popping new statement lists from the tree. */
10118 tree
10119 c_begin_compound_stmt (bool do_scope)
10121 tree stmt = push_stmt_list ();
10122 if (do_scope)
10123 push_scope ();
10124 return stmt;
10127 /* End a compound statement. STMT is the statement. LOC is the
10128 location of the compound statement-- this is usually the location
10129 of the opening brace. */
10131 tree
10132 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10134 tree block = NULL;
10136 if (do_scope)
10138 if (c_dialect_objc ())
10139 objc_clear_super_receiver ();
10140 block = pop_scope ();
10143 stmt = pop_stmt_list (stmt);
10144 stmt = c_build_bind_expr (loc, block, stmt);
10146 /* If this compound statement is nested immediately inside a statement
10147 expression, then force a BIND_EXPR to be created. Otherwise we'll
10148 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10149 STATEMENT_LISTs merge, and thus we can lose track of what statement
10150 was really last. */
10151 if (building_stmt_list_p ()
10152 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10153 && TREE_CODE (stmt) != BIND_EXPR)
10155 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10156 TREE_SIDE_EFFECTS (stmt) = 1;
10157 SET_EXPR_LOCATION (stmt, loc);
10160 return stmt;
10163 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10164 when the current scope is exited. EH_ONLY is true when this is not
10165 meant to apply to normal control flow transfer. */
10167 void
10168 push_cleanup (tree decl, tree cleanup, bool eh_only)
10170 enum tree_code code;
10171 tree stmt, list;
10172 bool stmt_expr;
10174 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10175 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10176 add_stmt (stmt);
10177 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10178 list = push_stmt_list ();
10179 TREE_OPERAND (stmt, 0) = list;
10180 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10183 /* Build a binary-operation expression without default conversions.
10184 CODE is the kind of expression to build.
10185 LOCATION is the operator's location.
10186 This function differs from `build' in several ways:
10187 the data type of the result is computed and recorded in it,
10188 warnings are generated if arg data types are invalid,
10189 special handling for addition and subtraction of pointers is known,
10190 and some optimization is done (operations on narrow ints
10191 are done in the narrower type when that gives the same result).
10192 Constant folding is also done before the result is returned.
10194 Note that the operands will never have enumeral types, or function
10195 or array types, because either they will have the default conversions
10196 performed or they have both just been converted to some other type in which
10197 the arithmetic is to be done. */
10199 tree
10200 build_binary_op (location_t location, enum tree_code code,
10201 tree orig_op0, tree orig_op1, int convert_p)
10203 tree type0, type1, orig_type0, orig_type1;
10204 tree eptype;
10205 enum tree_code code0, code1;
10206 tree op0, op1;
10207 tree ret = error_mark_node;
10208 const char *invalid_op_diag;
10209 bool op0_int_operands, op1_int_operands;
10210 bool int_const, int_const_or_overflow, int_operands;
10212 /* Expression code to give to the expression when it is built.
10213 Normally this is CODE, which is what the caller asked for,
10214 but in some special cases we change it. */
10215 enum tree_code resultcode = code;
10217 /* Data type in which the computation is to be performed.
10218 In the simplest cases this is the common type of the arguments. */
10219 tree result_type = NULL;
10221 /* When the computation is in excess precision, the type of the
10222 final EXCESS_PRECISION_EXPR. */
10223 tree semantic_result_type = NULL;
10225 /* Nonzero means operands have already been type-converted
10226 in whatever way is necessary.
10227 Zero means they need to be converted to RESULT_TYPE. */
10228 int converted = 0;
10230 /* Nonzero means create the expression with this type, rather than
10231 RESULT_TYPE. */
10232 tree build_type = 0;
10234 /* Nonzero means after finally constructing the expression
10235 convert it to this type. */
10236 tree final_type = 0;
10238 /* Nonzero if this is an operation like MIN or MAX which can
10239 safely be computed in short if both args are promoted shorts.
10240 Also implies COMMON.
10241 -1 indicates a bitwise operation; this makes a difference
10242 in the exact conditions for when it is safe to do the operation
10243 in a narrower mode. */
10244 int shorten = 0;
10246 /* Nonzero if this is a comparison operation;
10247 if both args are promoted shorts, compare the original shorts.
10248 Also implies COMMON. */
10249 int short_compare = 0;
10251 /* Nonzero if this is a right-shift operation, which can be computed on the
10252 original short and then promoted if the operand is a promoted short. */
10253 int short_shift = 0;
10255 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10256 int common = 0;
10258 /* True means types are compatible as far as ObjC is concerned. */
10259 bool objc_ok;
10261 /* True means this is an arithmetic operation that may need excess
10262 precision. */
10263 bool may_need_excess_precision;
10265 /* True means this is a boolean operation that converts both its
10266 operands to truth-values. */
10267 bool boolean_op = false;
10269 /* Remember whether we're doing / or %. */
10270 bool doing_div_or_mod = false;
10272 /* Remember whether we're doing << or >>. */
10273 bool doing_shift = false;
10275 /* Tree holding instrumentation expression. */
10276 tree instrument_expr = NULL;
10278 if (location == UNKNOWN_LOCATION)
10279 location = input_location;
10281 op0 = orig_op0;
10282 op1 = orig_op1;
10284 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10285 if (op0_int_operands)
10286 op0 = remove_c_maybe_const_expr (op0);
10287 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10288 if (op1_int_operands)
10289 op1 = remove_c_maybe_const_expr (op1);
10290 int_operands = (op0_int_operands && op1_int_operands);
10291 if (int_operands)
10293 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10294 && TREE_CODE (orig_op1) == INTEGER_CST);
10295 int_const = (int_const_or_overflow
10296 && !TREE_OVERFLOW (orig_op0)
10297 && !TREE_OVERFLOW (orig_op1));
10299 else
10300 int_const = int_const_or_overflow = false;
10302 /* Do not apply default conversion in mixed vector/scalar expression. */
10303 if (convert_p
10304 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10305 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10307 op0 = default_conversion (op0);
10308 op1 = default_conversion (op1);
10311 /* When Cilk Plus is enabled and there are array notations inside op0, then
10312 we check to see if there are builtin array notation functions. If
10313 so, then we take on the type of the array notation inside it. */
10314 if (flag_cilkplus && contains_array_notation_expr (op0))
10315 orig_type0 = type0 = find_correct_array_notation_type (op0);
10316 else
10317 orig_type0 = type0 = TREE_TYPE (op0);
10319 if (flag_cilkplus && contains_array_notation_expr (op1))
10320 orig_type1 = type1 = find_correct_array_notation_type (op1);
10321 else
10322 orig_type1 = type1 = TREE_TYPE (op1);
10324 /* The expression codes of the data types of the arguments tell us
10325 whether the arguments are integers, floating, pointers, etc. */
10326 code0 = TREE_CODE (type0);
10327 code1 = TREE_CODE (type1);
10329 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10330 STRIP_TYPE_NOPS (op0);
10331 STRIP_TYPE_NOPS (op1);
10333 /* If an error was already reported for one of the arguments,
10334 avoid reporting another error. */
10336 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10337 return error_mark_node;
10339 if ((invalid_op_diag
10340 = targetm.invalid_binary_op (code, type0, type1)))
10342 error_at (location, invalid_op_diag);
10343 return error_mark_node;
10346 switch (code)
10348 case PLUS_EXPR:
10349 case MINUS_EXPR:
10350 case MULT_EXPR:
10351 case TRUNC_DIV_EXPR:
10352 case CEIL_DIV_EXPR:
10353 case FLOOR_DIV_EXPR:
10354 case ROUND_DIV_EXPR:
10355 case EXACT_DIV_EXPR:
10356 may_need_excess_precision = true;
10357 break;
10358 default:
10359 may_need_excess_precision = false;
10360 break;
10362 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10364 op0 = TREE_OPERAND (op0, 0);
10365 type0 = TREE_TYPE (op0);
10367 else if (may_need_excess_precision
10368 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10370 type0 = eptype;
10371 op0 = convert (eptype, op0);
10373 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10375 op1 = TREE_OPERAND (op1, 0);
10376 type1 = TREE_TYPE (op1);
10378 else if (may_need_excess_precision
10379 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10381 type1 = eptype;
10382 op1 = convert (eptype, op1);
10385 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10387 /* In case when one of the operands of the binary operation is
10388 a vector and another is a scalar -- convert scalar to vector. */
10389 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10391 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10392 true);
10394 switch (convert_flag)
10396 case stv_error:
10397 return error_mark_node;
10398 case stv_firstarg:
10400 bool maybe_const = true;
10401 tree sc;
10402 sc = c_fully_fold (op0, false, &maybe_const);
10403 sc = save_expr (sc);
10404 sc = convert (TREE_TYPE (type1), sc);
10405 op0 = build_vector_from_val (type1, sc);
10406 if (!maybe_const)
10407 op0 = c_wrap_maybe_const (op0, true);
10408 orig_type0 = type0 = TREE_TYPE (op0);
10409 code0 = TREE_CODE (type0);
10410 converted = 1;
10411 break;
10413 case stv_secondarg:
10415 bool maybe_const = true;
10416 tree sc;
10417 sc = c_fully_fold (op1, false, &maybe_const);
10418 sc = save_expr (sc);
10419 sc = convert (TREE_TYPE (type0), sc);
10420 op1 = build_vector_from_val (type0, sc);
10421 if (!maybe_const)
10422 op1 = c_wrap_maybe_const (op1, true);
10423 orig_type1 = type1 = TREE_TYPE (op1);
10424 code1 = TREE_CODE (type1);
10425 converted = 1;
10426 break;
10428 default:
10429 break;
10433 switch (code)
10435 case PLUS_EXPR:
10436 /* Handle the pointer + int case. */
10437 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10439 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10440 goto return_build_binary_op;
10442 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10444 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10445 goto return_build_binary_op;
10447 else
10448 common = 1;
10449 break;
10451 case MINUS_EXPR:
10452 /* Subtraction of two similar pointers.
10453 We must subtract them as integers, then divide by object size. */
10454 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10455 && comp_target_types (location, type0, type1))
10457 ret = pointer_diff (location, op0, op1);
10458 goto return_build_binary_op;
10460 /* Handle pointer minus int. Just like pointer plus int. */
10461 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10463 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10464 goto return_build_binary_op;
10466 else
10467 common = 1;
10468 break;
10470 case MULT_EXPR:
10471 common = 1;
10472 break;
10474 case TRUNC_DIV_EXPR:
10475 case CEIL_DIV_EXPR:
10476 case FLOOR_DIV_EXPR:
10477 case ROUND_DIV_EXPR:
10478 case EXACT_DIV_EXPR:
10479 doing_div_or_mod = true;
10480 warn_for_div_by_zero (location, op1);
10482 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10483 || code0 == FIXED_POINT_TYPE
10484 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10485 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10486 || code1 == FIXED_POINT_TYPE
10487 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10489 enum tree_code tcode0 = code0, tcode1 = code1;
10491 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10492 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10493 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10494 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10496 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10497 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10498 resultcode = RDIV_EXPR;
10499 else
10500 /* Although it would be tempting to shorten always here, that
10501 loses on some targets, since the modulo instruction is
10502 undefined if the quotient can't be represented in the
10503 computation mode. We shorten only if unsigned or if
10504 dividing by something we know != -1. */
10505 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10506 || (TREE_CODE (op1) == INTEGER_CST
10507 && !integer_all_onesp (op1)));
10508 common = 1;
10510 break;
10512 case BIT_AND_EXPR:
10513 case BIT_IOR_EXPR:
10514 case BIT_XOR_EXPR:
10515 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10516 shorten = -1;
10517 /* Allow vector types which are not floating point types. */
10518 else if (code0 == VECTOR_TYPE
10519 && code1 == VECTOR_TYPE
10520 && !VECTOR_FLOAT_TYPE_P (type0)
10521 && !VECTOR_FLOAT_TYPE_P (type1))
10522 common = 1;
10523 break;
10525 case TRUNC_MOD_EXPR:
10526 case FLOOR_MOD_EXPR:
10527 doing_div_or_mod = true;
10528 warn_for_div_by_zero (location, op1);
10530 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10531 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10532 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10533 common = 1;
10534 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10536 /* Although it would be tempting to shorten always here, that loses
10537 on some targets, since the modulo instruction is undefined if the
10538 quotient can't be represented in the computation mode. We shorten
10539 only if unsigned or if dividing by something we know != -1. */
10540 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10541 || (TREE_CODE (op1) == INTEGER_CST
10542 && !integer_all_onesp (op1)));
10543 common = 1;
10545 break;
10547 case TRUTH_ANDIF_EXPR:
10548 case TRUTH_ORIF_EXPR:
10549 case TRUTH_AND_EXPR:
10550 case TRUTH_OR_EXPR:
10551 case TRUTH_XOR_EXPR:
10552 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10553 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10554 || code0 == FIXED_POINT_TYPE)
10555 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10556 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10557 || code1 == FIXED_POINT_TYPE))
10559 /* Result of these operations is always an int,
10560 but that does not mean the operands should be
10561 converted to ints! */
10562 result_type = integer_type_node;
10563 if (op0_int_operands)
10565 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10566 op0 = remove_c_maybe_const_expr (op0);
10568 else
10569 op0 = c_objc_common_truthvalue_conversion (location, op0);
10570 if (op1_int_operands)
10572 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10573 op1 = remove_c_maybe_const_expr (op1);
10575 else
10576 op1 = c_objc_common_truthvalue_conversion (location, op1);
10577 converted = 1;
10578 boolean_op = true;
10580 if (code == TRUTH_ANDIF_EXPR)
10582 int_const_or_overflow = (int_operands
10583 && TREE_CODE (orig_op0) == INTEGER_CST
10584 && (op0 == truthvalue_false_node
10585 || TREE_CODE (orig_op1) == INTEGER_CST));
10586 int_const = (int_const_or_overflow
10587 && !TREE_OVERFLOW (orig_op0)
10588 && (op0 == truthvalue_false_node
10589 || !TREE_OVERFLOW (orig_op1)));
10591 else if (code == TRUTH_ORIF_EXPR)
10593 int_const_or_overflow = (int_operands
10594 && TREE_CODE (orig_op0) == INTEGER_CST
10595 && (op0 == truthvalue_true_node
10596 || TREE_CODE (orig_op1) == INTEGER_CST));
10597 int_const = (int_const_or_overflow
10598 && !TREE_OVERFLOW (orig_op0)
10599 && (op0 == truthvalue_true_node
10600 || !TREE_OVERFLOW (orig_op1)));
10602 break;
10604 /* Shift operations: result has same type as first operand;
10605 always convert second operand to int.
10606 Also set SHORT_SHIFT if shifting rightward. */
10608 case RSHIFT_EXPR:
10609 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10610 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10612 result_type = type0;
10613 converted = 1;
10615 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10616 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10617 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10618 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10620 result_type = type0;
10621 converted = 1;
10623 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10624 && code1 == INTEGER_TYPE)
10626 doing_shift = true;
10627 if (TREE_CODE (op1) == INTEGER_CST)
10629 if (tree_int_cst_sgn (op1) < 0)
10631 int_const = false;
10632 if (c_inhibit_evaluation_warnings == 0)
10633 warning_at (location, OPT_Wshift_count_negative,
10634 "right shift count is negative");
10636 else
10638 if (!integer_zerop (op1))
10639 short_shift = 1;
10641 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10643 int_const = false;
10644 if (c_inhibit_evaluation_warnings == 0)
10645 warning_at (location, OPT_Wshift_count_overflow,
10646 "right shift count >= width of type");
10651 /* Use the type of the value to be shifted. */
10652 result_type = type0;
10653 /* Avoid converting op1 to result_type later. */
10654 converted = 1;
10656 break;
10658 case LSHIFT_EXPR:
10659 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10660 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10662 result_type = type0;
10663 converted = 1;
10665 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10666 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10667 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10668 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10670 result_type = type0;
10671 converted = 1;
10673 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10674 && code1 == INTEGER_TYPE)
10676 doing_shift = true;
10677 if (TREE_CODE (op1) == INTEGER_CST)
10679 if (tree_int_cst_sgn (op1) < 0)
10681 int_const = false;
10682 if (c_inhibit_evaluation_warnings == 0)
10683 warning_at (location, OPT_Wshift_count_negative,
10684 "left shift count is negative");
10687 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10689 int_const = false;
10690 if (c_inhibit_evaluation_warnings == 0)
10691 warning_at (location, OPT_Wshift_count_overflow,
10692 "left shift count >= width of type");
10696 /* Use the type of the value to be shifted. */
10697 result_type = type0;
10698 /* Avoid converting op1 to result_type later. */
10699 converted = 1;
10701 break;
10703 case EQ_EXPR:
10704 case NE_EXPR:
10705 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10707 tree intt;
10708 if (!vector_types_compatible_elements_p (type0, type1))
10710 error_at (location, "comparing vectors with different "
10711 "element types");
10712 return error_mark_node;
10715 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10717 error_at (location, "comparing vectors with different "
10718 "number of elements");
10719 return error_mark_node;
10722 /* Always construct signed integer vector type. */
10723 intt = c_common_type_for_size (GET_MODE_BITSIZE
10724 (TYPE_MODE (TREE_TYPE (type0))), 0);
10725 result_type = build_opaque_vector_type (intt,
10726 TYPE_VECTOR_SUBPARTS (type0));
10727 converted = 1;
10728 break;
10730 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10731 warning_at (location,
10732 OPT_Wfloat_equal,
10733 "comparing floating point with == or != is unsafe");
10734 /* Result of comparison is always int,
10735 but don't convert the args to int! */
10736 build_type = integer_type_node;
10737 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10738 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10739 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10740 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10741 short_compare = 1;
10742 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10744 if (TREE_CODE (op0) == ADDR_EXPR
10745 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10747 if (code == EQ_EXPR)
10748 warning_at (location,
10749 OPT_Waddress,
10750 "the comparison will always evaluate as %<false%> "
10751 "for the address of %qD will never be NULL",
10752 TREE_OPERAND (op0, 0));
10753 else
10754 warning_at (location,
10755 OPT_Waddress,
10756 "the comparison will always evaluate as %<true%> "
10757 "for the address of %qD will never be NULL",
10758 TREE_OPERAND (op0, 0));
10760 result_type = type0;
10762 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10764 if (TREE_CODE (op1) == ADDR_EXPR
10765 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10767 if (code == EQ_EXPR)
10768 warning_at (location,
10769 OPT_Waddress,
10770 "the comparison will always evaluate as %<false%> "
10771 "for the address of %qD will never be NULL",
10772 TREE_OPERAND (op1, 0));
10773 else
10774 warning_at (location,
10775 OPT_Waddress,
10776 "the comparison will always evaluate as %<true%> "
10777 "for the address of %qD will never be NULL",
10778 TREE_OPERAND (op1, 0));
10780 result_type = type1;
10782 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10784 tree tt0 = TREE_TYPE (type0);
10785 tree tt1 = TREE_TYPE (type1);
10786 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10787 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10788 addr_space_t as_common = ADDR_SPACE_GENERIC;
10790 /* Anything compares with void *. void * compares with anything.
10791 Otherwise, the targets must be compatible
10792 and both must be object or both incomplete. */
10793 if (comp_target_types (location, type0, type1))
10794 result_type = common_pointer_type (type0, type1);
10795 else if (!addr_space_superset (as0, as1, &as_common))
10797 error_at (location, "comparison of pointers to "
10798 "disjoint address spaces");
10799 return error_mark_node;
10801 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10803 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10804 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10805 "comparison of %<void *%> with function pointer");
10807 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10809 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10810 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10811 "comparison of %<void *%> with function pointer");
10813 else
10814 /* Avoid warning about the volatile ObjC EH puts on decls. */
10815 if (!objc_ok)
10816 pedwarn (location, 0,
10817 "comparison of distinct pointer types lacks a cast");
10819 if (result_type == NULL_TREE)
10821 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10822 result_type = build_pointer_type
10823 (build_qualified_type (void_type_node, qual));
10826 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10828 result_type = type0;
10829 pedwarn (location, 0, "comparison between pointer and integer");
10831 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10833 result_type = type1;
10834 pedwarn (location, 0, "comparison between pointer and integer");
10836 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10837 || truth_value_p (TREE_CODE (orig_op0)))
10838 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10839 || truth_value_p (TREE_CODE (orig_op1))))
10840 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10841 break;
10843 case LE_EXPR:
10844 case GE_EXPR:
10845 case LT_EXPR:
10846 case GT_EXPR:
10847 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10849 tree intt;
10850 if (!vector_types_compatible_elements_p (type0, type1))
10852 error_at (location, "comparing vectors with different "
10853 "element types");
10854 return error_mark_node;
10857 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10859 error_at (location, "comparing vectors with different "
10860 "number of elements");
10861 return error_mark_node;
10864 /* Always construct signed integer vector type. */
10865 intt = c_common_type_for_size (GET_MODE_BITSIZE
10866 (TYPE_MODE (TREE_TYPE (type0))), 0);
10867 result_type = build_opaque_vector_type (intt,
10868 TYPE_VECTOR_SUBPARTS (type0));
10869 converted = 1;
10870 break;
10872 build_type = integer_type_node;
10873 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10874 || code0 == FIXED_POINT_TYPE)
10875 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10876 || code1 == FIXED_POINT_TYPE))
10877 short_compare = 1;
10878 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10880 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10881 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10882 addr_space_t as_common;
10884 if (comp_target_types (location, type0, type1))
10886 result_type = common_pointer_type (type0, type1);
10887 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10888 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10889 pedwarn (location, 0,
10890 "comparison of complete and incomplete pointers");
10891 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10892 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10893 "ordered comparisons of pointers to functions");
10894 else if (null_pointer_constant_p (orig_op0)
10895 || null_pointer_constant_p (orig_op1))
10896 warning_at (location, OPT_Wextra,
10897 "ordered comparison of pointer with null pointer");
10900 else if (!addr_space_superset (as0, as1, &as_common))
10902 error_at (location, "comparison of pointers to "
10903 "disjoint address spaces");
10904 return error_mark_node;
10906 else
10908 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10909 result_type = build_pointer_type
10910 (build_qualified_type (void_type_node, qual));
10911 pedwarn (location, 0,
10912 "comparison of distinct pointer types lacks a cast");
10915 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10917 result_type = type0;
10918 if (pedantic)
10919 pedwarn (location, OPT_Wpedantic,
10920 "ordered comparison of pointer with integer zero");
10921 else if (extra_warnings)
10922 warning_at (location, OPT_Wextra,
10923 "ordered comparison of pointer with integer zero");
10925 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10927 result_type = type1;
10928 if (pedantic)
10929 pedwarn (location, OPT_Wpedantic,
10930 "ordered comparison of pointer with integer zero");
10931 else if (extra_warnings)
10932 warning_at (location, OPT_Wextra,
10933 "ordered comparison of pointer with integer zero");
10935 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10937 result_type = type0;
10938 pedwarn (location, 0, "comparison between pointer and integer");
10940 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10942 result_type = type1;
10943 pedwarn (location, 0, "comparison between pointer and integer");
10945 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10946 || truth_value_p (TREE_CODE (orig_op0)))
10947 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10948 || truth_value_p (TREE_CODE (orig_op1))))
10949 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10950 break;
10952 default:
10953 gcc_unreachable ();
10956 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10957 return error_mark_node;
10959 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10960 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10961 || !vector_types_compatible_elements_p (type0, type1)))
10963 binary_op_error (location, code, type0, type1);
10964 return error_mark_node;
10967 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10968 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10970 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10971 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10973 bool first_complex = (code0 == COMPLEX_TYPE);
10974 bool second_complex = (code1 == COMPLEX_TYPE);
10975 int none_complex = (!first_complex && !second_complex);
10977 if (shorten || common || short_compare)
10979 result_type = c_common_type (type0, type1);
10980 do_warn_double_promotion (result_type, type0, type1,
10981 "implicit conversion from %qT to %qT "
10982 "to match other operand of binary "
10983 "expression",
10984 location);
10985 if (result_type == error_mark_node)
10986 return error_mark_node;
10989 if (first_complex != second_complex
10990 && (code == PLUS_EXPR
10991 || code == MINUS_EXPR
10992 || code == MULT_EXPR
10993 || (code == TRUNC_DIV_EXPR && first_complex))
10994 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10995 && flag_signed_zeros)
10997 /* An operation on mixed real/complex operands must be
10998 handled specially, but the language-independent code can
10999 more easily optimize the plain complex arithmetic if
11000 -fno-signed-zeros. */
11001 tree real_type = TREE_TYPE (result_type);
11002 tree real, imag;
11003 if (type0 != orig_type0 || type1 != orig_type1)
11005 gcc_assert (may_need_excess_precision && common);
11006 semantic_result_type = c_common_type (orig_type0, orig_type1);
11008 if (first_complex)
11010 if (TREE_TYPE (op0) != result_type)
11011 op0 = convert_and_check (location, result_type, op0);
11012 if (TREE_TYPE (op1) != real_type)
11013 op1 = convert_and_check (location, real_type, op1);
11015 else
11017 if (TREE_TYPE (op0) != real_type)
11018 op0 = convert_and_check (location, real_type, op0);
11019 if (TREE_TYPE (op1) != result_type)
11020 op1 = convert_and_check (location, result_type, op1);
11022 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11023 return error_mark_node;
11024 if (first_complex)
11026 op0 = c_save_expr (op0);
11027 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11028 op0, 1);
11029 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11030 op0, 1);
11031 switch (code)
11033 case MULT_EXPR:
11034 case TRUNC_DIV_EXPR:
11035 op1 = c_save_expr (op1);
11036 imag = build2 (resultcode, real_type, imag, op1);
11037 /* Fall through. */
11038 case PLUS_EXPR:
11039 case MINUS_EXPR:
11040 real = build2 (resultcode, real_type, real, op1);
11041 break;
11042 default:
11043 gcc_unreachable();
11046 else
11048 op1 = c_save_expr (op1);
11049 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11050 op1, 1);
11051 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11052 op1, 1);
11053 switch (code)
11055 case MULT_EXPR:
11056 op0 = c_save_expr (op0);
11057 imag = build2 (resultcode, real_type, op0, imag);
11058 /* Fall through. */
11059 case PLUS_EXPR:
11060 real = build2 (resultcode, real_type, op0, real);
11061 break;
11062 case MINUS_EXPR:
11063 real = build2 (resultcode, real_type, op0, real);
11064 imag = build1 (NEGATE_EXPR, real_type, imag);
11065 break;
11066 default:
11067 gcc_unreachable();
11070 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11071 goto return_build_binary_op;
11074 /* For certain operations (which identify themselves by shorten != 0)
11075 if both args were extended from the same smaller type,
11076 do the arithmetic in that type and then extend.
11078 shorten !=0 and !=1 indicates a bitwise operation.
11079 For them, this optimization is safe only if
11080 both args are zero-extended or both are sign-extended.
11081 Otherwise, we might change the result.
11082 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11083 but calculated in (unsigned short) it would be (unsigned short)-1. */
11085 if (shorten && none_complex)
11087 final_type = result_type;
11088 result_type = shorten_binary_op (result_type, op0, op1,
11089 shorten == -1);
11092 /* Shifts can be shortened if shifting right. */
11094 if (short_shift)
11096 int unsigned_arg;
11097 tree arg0 = get_narrower (op0, &unsigned_arg);
11099 final_type = result_type;
11101 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11102 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11104 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11105 && tree_int_cst_sgn (op1) > 0
11106 /* We can shorten only if the shift count is less than the
11107 number of bits in the smaller type size. */
11108 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11109 /* We cannot drop an unsigned shift after sign-extension. */
11110 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11112 /* Do an unsigned shift if the operand was zero-extended. */
11113 result_type
11114 = c_common_signed_or_unsigned_type (unsigned_arg,
11115 TREE_TYPE (arg0));
11116 /* Convert value-to-be-shifted to that type. */
11117 if (TREE_TYPE (op0) != result_type)
11118 op0 = convert (result_type, op0);
11119 converted = 1;
11123 /* Comparison operations are shortened too but differently.
11124 They identify themselves by setting short_compare = 1. */
11126 if (short_compare)
11128 /* Don't write &op0, etc., because that would prevent op0
11129 from being kept in a register.
11130 Instead, make copies of the our local variables and
11131 pass the copies by reference, then copy them back afterward. */
11132 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11133 enum tree_code xresultcode = resultcode;
11134 tree val
11135 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11136 &xresultcode);
11138 if (val != 0)
11140 ret = val;
11141 goto return_build_binary_op;
11144 op0 = xop0, op1 = xop1;
11145 converted = 1;
11146 resultcode = xresultcode;
11148 if (c_inhibit_evaluation_warnings == 0)
11150 bool op0_maybe_const = true;
11151 bool op1_maybe_const = true;
11152 tree orig_op0_folded, orig_op1_folded;
11154 if (in_late_binary_op)
11156 orig_op0_folded = orig_op0;
11157 orig_op1_folded = orig_op1;
11159 else
11161 /* Fold for the sake of possible warnings, as in
11162 build_conditional_expr. This requires the
11163 "original" values to be folded, not just op0 and
11164 op1. */
11165 c_inhibit_evaluation_warnings++;
11166 op0 = c_fully_fold (op0, require_constant_value,
11167 &op0_maybe_const);
11168 op1 = c_fully_fold (op1, require_constant_value,
11169 &op1_maybe_const);
11170 c_inhibit_evaluation_warnings--;
11171 orig_op0_folded = c_fully_fold (orig_op0,
11172 require_constant_value,
11173 NULL);
11174 orig_op1_folded = c_fully_fold (orig_op1,
11175 require_constant_value,
11176 NULL);
11179 if (warn_sign_compare)
11180 warn_for_sign_compare (location, orig_op0_folded,
11181 orig_op1_folded, op0, op1,
11182 result_type, resultcode);
11183 if (!in_late_binary_op && !int_operands)
11185 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11186 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11187 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11188 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11194 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11195 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11196 Then the expression will be built.
11197 It will be given type FINAL_TYPE if that is nonzero;
11198 otherwise, it will be given type RESULT_TYPE. */
11200 if (!result_type)
11202 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11203 return error_mark_node;
11206 if (build_type == NULL_TREE)
11208 build_type = result_type;
11209 if ((type0 != orig_type0 || type1 != orig_type1)
11210 && !boolean_op)
11212 gcc_assert (may_need_excess_precision && common);
11213 semantic_result_type = c_common_type (orig_type0, orig_type1);
11217 if (!converted)
11219 op0 = ep_convert_and_check (location, result_type, op0,
11220 semantic_result_type);
11221 op1 = ep_convert_and_check (location, result_type, op1,
11222 semantic_result_type);
11224 /* This can happen if one operand has a vector type, and the other
11225 has a different type. */
11226 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11227 return error_mark_node;
11230 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11231 | SANITIZE_FLOAT_DIVIDE))
11232 && current_function_decl != 0
11233 && !lookup_attribute ("no_sanitize_undefined",
11234 DECL_ATTRIBUTES (current_function_decl))
11235 && (doing_div_or_mod || doing_shift))
11237 /* OP0 and/or OP1 might have side-effects. */
11238 op0 = c_save_expr (op0);
11239 op1 = c_save_expr (op1);
11240 op0 = c_fully_fold (op0, false, NULL);
11241 op1 = c_fully_fold (op1, false, NULL);
11242 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11243 | SANITIZE_FLOAT_DIVIDE)))
11244 instrument_expr = ubsan_instrument_division (location, op0, op1);
11245 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11246 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11249 /* Treat expressions in initializers specially as they can't trap. */
11250 if (int_const_or_overflow)
11251 ret = (require_constant_value
11252 ? fold_build2_initializer_loc (location, resultcode, build_type,
11253 op0, op1)
11254 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11255 else
11256 ret = build2 (resultcode, build_type, op0, op1);
11257 if (final_type != 0)
11258 ret = convert (final_type, ret);
11260 return_build_binary_op:
11261 gcc_assert (ret != error_mark_node);
11262 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11263 ret = (int_operands
11264 ? note_integer_operands (ret)
11265 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11266 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11267 && !in_late_binary_op)
11268 ret = note_integer_operands (ret);
11269 if (semantic_result_type)
11270 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11271 protected_set_expr_location (ret, location);
11273 if (instrument_expr != NULL)
11274 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11275 instrument_expr, ret);
11277 return ret;
11281 /* Convert EXPR to be a truth-value, validating its type for this
11282 purpose. LOCATION is the source location for the expression. */
11284 tree
11285 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11287 bool int_const, int_operands;
11289 switch (TREE_CODE (TREE_TYPE (expr)))
11291 case ARRAY_TYPE:
11292 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11293 return error_mark_node;
11295 case RECORD_TYPE:
11296 error_at (location, "used struct type value where scalar is required");
11297 return error_mark_node;
11299 case UNION_TYPE:
11300 error_at (location, "used union type value where scalar is required");
11301 return error_mark_node;
11303 case VOID_TYPE:
11304 error_at (location, "void value not ignored as it ought to be");
11305 return error_mark_node;
11307 case FUNCTION_TYPE:
11308 gcc_unreachable ();
11310 case VECTOR_TYPE:
11311 error_at (location, "used vector type where scalar is required");
11312 return error_mark_node;
11314 default:
11315 break;
11318 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11319 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11320 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11322 expr = remove_c_maybe_const_expr (expr);
11323 expr = build2 (NE_EXPR, integer_type_node, expr,
11324 convert (TREE_TYPE (expr), integer_zero_node));
11325 expr = note_integer_operands (expr);
11327 else
11328 /* ??? Should we also give an error for vectors rather than leaving
11329 those to give errors later? */
11330 expr = c_common_truthvalue_conversion (location, expr);
11332 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11334 if (TREE_OVERFLOW (expr))
11335 return expr;
11336 else
11337 return note_integer_operands (expr);
11339 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11340 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11341 return expr;
11345 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11346 required. */
11348 tree
11349 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11351 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11353 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11354 /* Executing a compound literal inside a function reinitializes
11355 it. */
11356 if (!TREE_STATIC (decl))
11357 *se = true;
11358 return decl;
11360 else
11361 return expr;
11364 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11365 statement. LOC is the location of the OACC_PARALLEL. */
11367 tree
11368 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11370 tree stmt;
11372 block = c_end_compound_stmt (loc, block, true);
11374 stmt = make_node (OACC_PARALLEL);
11375 TREE_TYPE (stmt) = void_type_node;
11376 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11377 OACC_PARALLEL_BODY (stmt) = block;
11378 SET_EXPR_LOCATION (stmt, loc);
11380 return add_stmt (stmt);
11383 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11384 statement. LOC is the location of the OACC_KERNELS. */
11386 tree
11387 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11389 tree stmt;
11391 block = c_end_compound_stmt (loc, block, true);
11393 stmt = make_node (OACC_KERNELS);
11394 TREE_TYPE (stmt) = void_type_node;
11395 OACC_KERNELS_CLAUSES (stmt) = clauses;
11396 OACC_KERNELS_BODY (stmt) = block;
11397 SET_EXPR_LOCATION (stmt, loc);
11399 return add_stmt (stmt);
11402 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11403 statement. LOC is the location of the OACC_DATA. */
11405 tree
11406 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11408 tree stmt;
11410 block = c_end_compound_stmt (loc, block, true);
11412 stmt = make_node (OACC_DATA);
11413 TREE_TYPE (stmt) = void_type_node;
11414 OACC_DATA_CLAUSES (stmt) = clauses;
11415 OACC_DATA_BODY (stmt) = block;
11416 SET_EXPR_LOCATION (stmt, loc);
11418 return add_stmt (stmt);
11421 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11423 tree
11424 c_begin_omp_parallel (void)
11426 tree block;
11428 keep_next_level ();
11429 block = c_begin_compound_stmt (true);
11431 return block;
11434 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11435 statement. LOC is the location of the OMP_PARALLEL. */
11437 tree
11438 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11440 tree stmt;
11442 block = c_end_compound_stmt (loc, block, true);
11444 stmt = make_node (OMP_PARALLEL);
11445 TREE_TYPE (stmt) = void_type_node;
11446 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11447 OMP_PARALLEL_BODY (stmt) = block;
11448 SET_EXPR_LOCATION (stmt, loc);
11450 return add_stmt (stmt);
11453 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11455 tree
11456 c_begin_omp_task (void)
11458 tree block;
11460 keep_next_level ();
11461 block = c_begin_compound_stmt (true);
11463 return block;
11466 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11467 statement. LOC is the location of the #pragma. */
11469 tree
11470 c_finish_omp_task (location_t loc, tree clauses, tree block)
11472 tree stmt;
11474 block = c_end_compound_stmt (loc, block, true);
11476 stmt = make_node (OMP_TASK);
11477 TREE_TYPE (stmt) = void_type_node;
11478 OMP_TASK_CLAUSES (stmt) = clauses;
11479 OMP_TASK_BODY (stmt) = block;
11480 SET_EXPR_LOCATION (stmt, loc);
11482 return add_stmt (stmt);
11485 /* Generate GOMP_cancel call for #pragma omp cancel. */
11487 void
11488 c_finish_omp_cancel (location_t loc, tree clauses)
11490 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11491 int mask = 0;
11492 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11493 mask = 1;
11494 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11495 mask = 2;
11496 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11497 mask = 4;
11498 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11499 mask = 8;
11500 else
11502 error_at (loc, "%<#pragma omp cancel must specify one of "
11503 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11504 "clauses");
11505 return;
11507 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11508 if (ifc != NULL_TREE)
11510 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11511 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11512 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11513 build_zero_cst (type));
11515 else
11516 ifc = boolean_true_node;
11517 tree stmt = build_call_expr_loc (loc, fn, 2,
11518 build_int_cst (integer_type_node, mask),
11519 ifc);
11520 add_stmt (stmt);
11523 /* Generate GOMP_cancellation_point call for
11524 #pragma omp cancellation point. */
11526 void
11527 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11529 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11530 int mask = 0;
11531 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11532 mask = 1;
11533 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11534 mask = 2;
11535 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11536 mask = 4;
11537 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11538 mask = 8;
11539 else
11541 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11542 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11543 "clauses");
11544 return;
11546 tree stmt = build_call_expr_loc (loc, fn, 1,
11547 build_int_cst (integer_type_node, mask));
11548 add_stmt (stmt);
11551 /* Helper function for handle_omp_array_sections. Called recursively
11552 to handle multiple array-section-subscripts. C is the clause,
11553 T current expression (initially OMP_CLAUSE_DECL), which is either
11554 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11555 expression if specified, TREE_VALUE length expression if specified,
11556 TREE_CHAIN is what it has been specified after, or some decl.
11557 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11558 set to true if any of the array-section-subscript could have length
11559 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11560 first array-section-subscript which is known not to have length
11561 of one. Given say:
11562 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11563 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11564 all are or may have length of 1, array-section-subscript [:2] is the
11565 first one knonwn not to have length 1. For array-section-subscript
11566 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11567 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11568 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11569 case though, as some lengths could be zero. */
11571 static tree
11572 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11573 bool &maybe_zero_len, unsigned int &first_non_one)
11575 tree ret, low_bound, length, type;
11576 if (TREE_CODE (t) != TREE_LIST)
11578 if (error_operand_p (t))
11579 return error_mark_node;
11580 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11582 if (DECL_P (t))
11583 error_at (OMP_CLAUSE_LOCATION (c),
11584 "%qD is not a variable in %qs clause", t,
11585 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11586 else
11587 error_at (OMP_CLAUSE_LOCATION (c),
11588 "%qE is not a variable in %qs clause", t,
11589 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11590 return error_mark_node;
11592 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11593 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11595 error_at (OMP_CLAUSE_LOCATION (c),
11596 "%qD is threadprivate variable in %qs clause", t,
11597 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11598 return error_mark_node;
11600 return t;
11603 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11604 maybe_zero_len, first_non_one);
11605 if (ret == error_mark_node || ret == NULL_TREE)
11606 return ret;
11608 type = TREE_TYPE (ret);
11609 low_bound = TREE_PURPOSE (t);
11610 length = TREE_VALUE (t);
11612 if (low_bound == error_mark_node || length == error_mark_node)
11613 return error_mark_node;
11615 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11617 error_at (OMP_CLAUSE_LOCATION (c),
11618 "low bound %qE of array section does not have integral type",
11619 low_bound);
11620 return error_mark_node;
11622 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11624 error_at (OMP_CLAUSE_LOCATION (c),
11625 "length %qE of array section does not have integral type",
11626 length);
11627 return error_mark_node;
11629 if (low_bound
11630 && TREE_CODE (low_bound) == INTEGER_CST
11631 && TYPE_PRECISION (TREE_TYPE (low_bound))
11632 > TYPE_PRECISION (sizetype))
11633 low_bound = fold_convert (sizetype, low_bound);
11634 if (length
11635 && TREE_CODE (length) == INTEGER_CST
11636 && TYPE_PRECISION (TREE_TYPE (length))
11637 > TYPE_PRECISION (sizetype))
11638 length = fold_convert (sizetype, length);
11639 if (low_bound == NULL_TREE)
11640 low_bound = integer_zero_node;
11642 if (length != NULL_TREE)
11644 if (!integer_nonzerop (length))
11645 maybe_zero_len = true;
11646 if (first_non_one == types.length ()
11647 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11648 first_non_one++;
11650 if (TREE_CODE (type) == ARRAY_TYPE)
11652 if (length == NULL_TREE
11653 && (TYPE_DOMAIN (type) == NULL_TREE
11654 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11656 error_at (OMP_CLAUSE_LOCATION (c),
11657 "for unknown bound array type length expression must "
11658 "be specified");
11659 return error_mark_node;
11661 if (TREE_CODE (low_bound) == INTEGER_CST
11662 && tree_int_cst_sgn (low_bound) == -1)
11664 error_at (OMP_CLAUSE_LOCATION (c),
11665 "negative low bound in array section in %qs clause",
11666 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11667 return error_mark_node;
11669 if (length != NULL_TREE
11670 && TREE_CODE (length) == INTEGER_CST
11671 && tree_int_cst_sgn (length) == -1)
11673 error_at (OMP_CLAUSE_LOCATION (c),
11674 "negative length in array section in %qs clause",
11675 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11676 return error_mark_node;
11678 if (TYPE_DOMAIN (type)
11679 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11680 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11681 == INTEGER_CST)
11683 tree size = size_binop (PLUS_EXPR,
11684 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11685 size_one_node);
11686 if (TREE_CODE (low_bound) == INTEGER_CST)
11688 if (tree_int_cst_lt (size, low_bound))
11690 error_at (OMP_CLAUSE_LOCATION (c),
11691 "low bound %qE above array section size "
11692 "in %qs clause", low_bound,
11693 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11694 return error_mark_node;
11696 if (tree_int_cst_equal (size, low_bound))
11697 maybe_zero_len = true;
11698 else if (length == NULL_TREE
11699 && first_non_one == types.length ()
11700 && tree_int_cst_equal
11701 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11702 low_bound))
11703 first_non_one++;
11705 else if (length == NULL_TREE)
11707 maybe_zero_len = true;
11708 if (first_non_one == types.length ())
11709 first_non_one++;
11711 if (length && TREE_CODE (length) == INTEGER_CST)
11713 if (tree_int_cst_lt (size, length))
11715 error_at (OMP_CLAUSE_LOCATION (c),
11716 "length %qE above array section size "
11717 "in %qs clause", length,
11718 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11719 return error_mark_node;
11721 if (TREE_CODE (low_bound) == INTEGER_CST)
11723 tree lbpluslen
11724 = size_binop (PLUS_EXPR,
11725 fold_convert (sizetype, low_bound),
11726 fold_convert (sizetype, length));
11727 if (TREE_CODE (lbpluslen) == INTEGER_CST
11728 && tree_int_cst_lt (size, lbpluslen))
11730 error_at (OMP_CLAUSE_LOCATION (c),
11731 "high bound %qE above array section size "
11732 "in %qs clause", lbpluslen,
11733 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11734 return error_mark_node;
11739 else if (length == NULL_TREE)
11741 maybe_zero_len = true;
11742 if (first_non_one == types.length ())
11743 first_non_one++;
11746 /* For [lb:] we will need to evaluate lb more than once. */
11747 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11749 tree lb = c_save_expr (low_bound);
11750 if (lb != low_bound)
11752 TREE_PURPOSE (t) = lb;
11753 low_bound = lb;
11757 else if (TREE_CODE (type) == POINTER_TYPE)
11759 if (length == NULL_TREE)
11761 error_at (OMP_CLAUSE_LOCATION (c),
11762 "for pointer type length expression must be specified");
11763 return error_mark_node;
11765 /* If there is a pointer type anywhere but in the very first
11766 array-section-subscript, the array section can't be contiguous. */
11767 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11768 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11770 error_at (OMP_CLAUSE_LOCATION (c),
11771 "array section is not contiguous in %qs clause",
11772 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11773 return error_mark_node;
11776 else
11778 error_at (OMP_CLAUSE_LOCATION (c),
11779 "%qE does not have pointer or array type", ret);
11780 return error_mark_node;
11782 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11783 types.safe_push (TREE_TYPE (ret));
11784 /* We will need to evaluate lb more than once. */
11785 tree lb = c_save_expr (low_bound);
11786 if (lb != low_bound)
11788 TREE_PURPOSE (t) = lb;
11789 low_bound = lb;
11791 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11792 return ret;
11795 /* Handle array sections for clause C. */
11797 static bool
11798 handle_omp_array_sections (tree c)
11800 bool maybe_zero_len = false;
11801 unsigned int first_non_one = 0;
11802 vec<tree> types = vNULL;
11803 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11804 maybe_zero_len, first_non_one);
11805 if (first == error_mark_node)
11807 types.release ();
11808 return true;
11810 if (first == NULL_TREE)
11812 types.release ();
11813 return false;
11815 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11817 tree t = OMP_CLAUSE_DECL (c);
11818 tree tem = NULL_TREE;
11819 types.release ();
11820 /* Need to evaluate side effects in the length expressions
11821 if any. */
11822 while (TREE_CODE (t) == TREE_LIST)
11824 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11826 if (tem == NULL_TREE)
11827 tem = TREE_VALUE (t);
11828 else
11829 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11830 TREE_VALUE (t), tem);
11832 t = TREE_CHAIN (t);
11834 if (tem)
11835 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11836 first = c_fully_fold (first, false, NULL);
11837 OMP_CLAUSE_DECL (c) = first;
11839 else
11841 unsigned int num = types.length (), i;
11842 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11843 tree condition = NULL_TREE;
11845 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11846 maybe_zero_len = true;
11848 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11849 t = TREE_CHAIN (t))
11851 tree low_bound = TREE_PURPOSE (t);
11852 tree length = TREE_VALUE (t);
11854 i--;
11855 if (low_bound
11856 && TREE_CODE (low_bound) == INTEGER_CST
11857 && TYPE_PRECISION (TREE_TYPE (low_bound))
11858 > TYPE_PRECISION (sizetype))
11859 low_bound = fold_convert (sizetype, low_bound);
11860 if (length
11861 && TREE_CODE (length) == INTEGER_CST
11862 && TYPE_PRECISION (TREE_TYPE (length))
11863 > TYPE_PRECISION (sizetype))
11864 length = fold_convert (sizetype, length);
11865 if (low_bound == NULL_TREE)
11866 low_bound = integer_zero_node;
11867 if (!maybe_zero_len && i > first_non_one)
11869 if (integer_nonzerop (low_bound))
11870 goto do_warn_noncontiguous;
11871 if (length != NULL_TREE
11872 && TREE_CODE (length) == INTEGER_CST
11873 && TYPE_DOMAIN (types[i])
11874 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11875 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11876 == INTEGER_CST)
11878 tree size;
11879 size = size_binop (PLUS_EXPR,
11880 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11881 size_one_node);
11882 if (!tree_int_cst_equal (length, size))
11884 do_warn_noncontiguous:
11885 error_at (OMP_CLAUSE_LOCATION (c),
11886 "array section is not contiguous in %qs "
11887 "clause",
11888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11889 types.release ();
11890 return true;
11893 if (length != NULL_TREE
11894 && TREE_SIDE_EFFECTS (length))
11896 if (side_effects == NULL_TREE)
11897 side_effects = length;
11898 else
11899 side_effects = build2 (COMPOUND_EXPR,
11900 TREE_TYPE (side_effects),
11901 length, side_effects);
11904 else
11906 tree l;
11908 if (i > first_non_one && length && integer_nonzerop (length))
11909 continue;
11910 if (length)
11911 l = fold_convert (sizetype, length);
11912 else
11914 l = size_binop (PLUS_EXPR,
11915 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11916 size_one_node);
11917 l = size_binop (MINUS_EXPR, l,
11918 fold_convert (sizetype, low_bound));
11920 if (i > first_non_one)
11922 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11923 size_zero_node);
11924 if (condition == NULL_TREE)
11925 condition = l;
11926 else
11927 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11928 l, condition);
11930 else if (size == NULL_TREE)
11932 size = size_in_bytes (TREE_TYPE (types[i]));
11933 size = size_binop (MULT_EXPR, size, l);
11934 if (condition)
11935 size = fold_build3 (COND_EXPR, sizetype, condition,
11936 size, size_zero_node);
11938 else
11939 size = size_binop (MULT_EXPR, size, l);
11942 types.release ();
11943 if (side_effects)
11944 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11945 first = c_fully_fold (first, false, NULL);
11946 OMP_CLAUSE_DECL (c) = first;
11947 if (size)
11948 size = c_fully_fold (size, false, NULL);
11949 OMP_CLAUSE_SIZE (c) = size;
11950 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11951 return false;
11952 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11953 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11954 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
11955 if (!c_mark_addressable (t))
11956 return false;
11957 OMP_CLAUSE_DECL (c2) = t;
11958 t = build_fold_addr_expr (first);
11959 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11960 tree ptr = OMP_CLAUSE_DECL (c2);
11961 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11962 ptr = build_fold_addr_expr (ptr);
11963 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11964 ptrdiff_type_node, t,
11965 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11966 ptrdiff_type_node, ptr));
11967 t = c_fully_fold (t, false, NULL);
11968 OMP_CLAUSE_SIZE (c2) = t;
11969 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11970 OMP_CLAUSE_CHAIN (c) = c2;
11972 return false;
11975 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11976 an inline call. But, remap
11977 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11978 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11980 static tree
11981 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11982 tree decl, tree placeholder)
11984 copy_body_data id;
11985 hash_map<tree, tree> decl_map;
11987 decl_map.put (omp_decl1, placeholder);
11988 decl_map.put (omp_decl2, decl);
11989 memset (&id, 0, sizeof (id));
11990 id.src_fn = DECL_CONTEXT (omp_decl1);
11991 id.dst_fn = current_function_decl;
11992 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11993 id.decl_map = &decl_map;
11995 id.copy_decl = copy_decl_no_change;
11996 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11997 id.transform_new_cfg = true;
11998 id.transform_return_to_modify = false;
11999 id.transform_lang_insert_block = NULL;
12000 id.eh_lp_nr = 0;
12001 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12002 return stmt;
12005 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12006 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12008 static tree
12009 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12011 if (*tp == (tree) data)
12012 return *tp;
12013 return NULL_TREE;
12016 /* For all elements of CLAUSES, validate them against their constraints.
12017 Remove any elements from the list that are invalid. */
12019 tree
12020 c_finish_omp_clauses (tree clauses)
12022 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12023 bitmap_head aligned_head;
12024 tree c, t, *pc;
12025 bool branch_seen = false;
12026 bool copyprivate_seen = false;
12027 tree *nowait_clause = NULL;
12029 bitmap_obstack_initialize (NULL);
12030 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12031 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12032 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12033 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12035 for (pc = &clauses, c = clauses; c ; c = *pc)
12037 bool remove = false;
12038 bool need_complete = false;
12039 bool need_implicitly_determined = false;
12041 switch (OMP_CLAUSE_CODE (c))
12043 case OMP_CLAUSE_SHARED:
12044 need_implicitly_determined = true;
12045 goto check_dup_generic;
12047 case OMP_CLAUSE_PRIVATE:
12048 need_complete = true;
12049 need_implicitly_determined = true;
12050 goto check_dup_generic;
12052 case OMP_CLAUSE_REDUCTION:
12053 need_implicitly_determined = true;
12054 t = OMP_CLAUSE_DECL (c);
12055 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12056 && (FLOAT_TYPE_P (TREE_TYPE (t))
12057 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12059 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12060 const char *r_name = NULL;
12062 switch (r_code)
12064 case PLUS_EXPR:
12065 case MULT_EXPR:
12066 case MINUS_EXPR:
12067 break;
12068 case MIN_EXPR:
12069 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12070 r_name = "min";
12071 break;
12072 case MAX_EXPR:
12073 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12074 r_name = "max";
12075 break;
12076 case BIT_AND_EXPR:
12077 r_name = "&";
12078 break;
12079 case BIT_XOR_EXPR:
12080 r_name = "^";
12081 break;
12082 case BIT_IOR_EXPR:
12083 r_name = "|";
12084 break;
12085 case TRUTH_ANDIF_EXPR:
12086 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12087 r_name = "&&";
12088 break;
12089 case TRUTH_ORIF_EXPR:
12090 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12091 r_name = "||";
12092 break;
12093 default:
12094 gcc_unreachable ();
12096 if (r_name)
12098 error_at (OMP_CLAUSE_LOCATION (c),
12099 "%qE has invalid type for %<reduction(%s)%>",
12100 t, r_name);
12101 remove = true;
12102 break;
12105 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12107 error_at (OMP_CLAUSE_LOCATION (c),
12108 "user defined reduction not found for %qD", t);
12109 remove = true;
12110 break;
12112 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12114 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12115 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12116 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12117 VAR_DECL, NULL_TREE, type);
12118 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12119 DECL_ARTIFICIAL (placeholder) = 1;
12120 DECL_IGNORED_P (placeholder) = 1;
12121 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12122 c_mark_addressable (placeholder);
12123 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12124 c_mark_addressable (OMP_CLAUSE_DECL (c));
12125 OMP_CLAUSE_REDUCTION_MERGE (c)
12126 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12127 TREE_VEC_ELT (list, 0),
12128 TREE_VEC_ELT (list, 1),
12129 OMP_CLAUSE_DECL (c), placeholder);
12130 OMP_CLAUSE_REDUCTION_MERGE (c)
12131 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12132 void_type_node, NULL_TREE,
12133 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12134 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12135 if (TREE_VEC_LENGTH (list) == 6)
12137 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12138 c_mark_addressable (OMP_CLAUSE_DECL (c));
12139 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12140 c_mark_addressable (placeholder);
12141 tree init = TREE_VEC_ELT (list, 5);
12142 if (init == error_mark_node)
12143 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12144 OMP_CLAUSE_REDUCTION_INIT (c)
12145 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12146 TREE_VEC_ELT (list, 3),
12147 OMP_CLAUSE_DECL (c), placeholder);
12148 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12149 OMP_CLAUSE_REDUCTION_INIT (c)
12150 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12151 OMP_CLAUSE_REDUCTION_INIT (c));
12152 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12153 c_find_omp_placeholder_r,
12154 placeholder, NULL))
12155 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12157 else
12159 tree init;
12160 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12161 init = build_constructor (TREE_TYPE (t), NULL);
12162 else
12163 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12164 OMP_CLAUSE_REDUCTION_INIT (c)
12165 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12167 OMP_CLAUSE_REDUCTION_INIT (c)
12168 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12169 void_type_node, NULL_TREE,
12170 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12171 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12173 goto check_dup_generic;
12175 case OMP_CLAUSE_COPYPRIVATE:
12176 copyprivate_seen = true;
12177 if (nowait_clause)
12179 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12180 "%<nowait%> clause must not be used together "
12181 "with %<copyprivate%>");
12182 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12183 nowait_clause = NULL;
12185 goto check_dup_generic;
12187 case OMP_CLAUSE_COPYIN:
12188 t = OMP_CLAUSE_DECL (c);
12189 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12191 error_at (OMP_CLAUSE_LOCATION (c),
12192 "%qE must be %<threadprivate%> for %<copyin%>", t);
12193 remove = true;
12194 break;
12196 goto check_dup_generic;
12198 case OMP_CLAUSE_LINEAR:
12199 t = OMP_CLAUSE_DECL (c);
12200 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12201 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12203 error_at (OMP_CLAUSE_LOCATION (c),
12204 "linear clause applied to non-integral non-pointer "
12205 "variable with type %qT", TREE_TYPE (t));
12206 remove = true;
12207 break;
12209 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12211 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12212 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12213 OMP_CLAUSE_DECL (c), s);
12214 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12215 sizetype, s, OMP_CLAUSE_DECL (c));
12216 if (s == error_mark_node)
12217 s = size_one_node;
12218 OMP_CLAUSE_LINEAR_STEP (c) = s;
12220 else
12221 OMP_CLAUSE_LINEAR_STEP (c)
12222 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12223 goto check_dup_generic;
12225 check_dup_generic:
12226 t = OMP_CLAUSE_DECL (c);
12227 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12229 error_at (OMP_CLAUSE_LOCATION (c),
12230 "%qE is not a variable in clause %qs", t,
12231 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12232 remove = true;
12234 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12235 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12236 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12238 error_at (OMP_CLAUSE_LOCATION (c),
12239 "%qE appears more than once in data clauses", t);
12240 remove = true;
12242 else
12243 bitmap_set_bit (&generic_head, DECL_UID (t));
12244 break;
12246 case OMP_CLAUSE_FIRSTPRIVATE:
12247 t = OMP_CLAUSE_DECL (c);
12248 need_complete = true;
12249 need_implicitly_determined = true;
12250 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12252 error_at (OMP_CLAUSE_LOCATION (c),
12253 "%qE is not a variable in clause %<firstprivate%>", t);
12254 remove = true;
12256 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12257 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12259 error_at (OMP_CLAUSE_LOCATION (c),
12260 "%qE appears more than once in data clauses", t);
12261 remove = true;
12263 else
12264 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12265 break;
12267 case OMP_CLAUSE_LASTPRIVATE:
12268 t = OMP_CLAUSE_DECL (c);
12269 need_complete = true;
12270 need_implicitly_determined = true;
12271 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12273 error_at (OMP_CLAUSE_LOCATION (c),
12274 "%qE is not a variable in clause %<lastprivate%>", t);
12275 remove = true;
12277 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12278 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12280 error_at (OMP_CLAUSE_LOCATION (c),
12281 "%qE appears more than once in data clauses", t);
12282 remove = true;
12284 else
12285 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12286 break;
12288 case OMP_CLAUSE_ALIGNED:
12289 t = OMP_CLAUSE_DECL (c);
12290 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12292 error_at (OMP_CLAUSE_LOCATION (c),
12293 "%qE is not a variable in %<aligned%> clause", t);
12294 remove = true;
12296 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12297 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12299 error_at (OMP_CLAUSE_LOCATION (c),
12300 "%qE in %<aligned%> clause is neither a pointer nor "
12301 "an array", t);
12302 remove = true;
12304 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12306 error_at (OMP_CLAUSE_LOCATION (c),
12307 "%qE appears more than once in %<aligned%> clauses",
12309 remove = true;
12311 else
12312 bitmap_set_bit (&aligned_head, DECL_UID (t));
12313 break;
12315 case OMP_CLAUSE_DEPEND:
12316 t = OMP_CLAUSE_DECL (c);
12317 if (TREE_CODE (t) == TREE_LIST)
12319 if (handle_omp_array_sections (c))
12320 remove = true;
12321 break;
12323 if (t == error_mark_node)
12324 remove = true;
12325 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12327 error_at (OMP_CLAUSE_LOCATION (c),
12328 "%qE is not a variable in %<depend%> clause", t);
12329 remove = true;
12331 else if (!c_mark_addressable (t))
12332 remove = true;
12333 break;
12335 case OMP_CLAUSE_MAP:
12336 case OMP_CLAUSE_TO:
12337 case OMP_CLAUSE_FROM:
12338 case OMP_CLAUSE__CACHE_:
12339 t = OMP_CLAUSE_DECL (c);
12340 if (TREE_CODE (t) == TREE_LIST)
12342 if (handle_omp_array_sections (c))
12343 remove = true;
12344 else
12346 t = OMP_CLAUSE_DECL (c);
12347 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12349 error_at (OMP_CLAUSE_LOCATION (c),
12350 "array section does not have mappable type "
12351 "in %qs clause",
12352 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12353 remove = true;
12356 break;
12358 if (t == error_mark_node)
12359 remove = true;
12360 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12362 error_at (OMP_CLAUSE_LOCATION (c),
12363 "%qE is not a variable in %qs clause", t,
12364 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12365 remove = true;
12367 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12369 error_at (OMP_CLAUSE_LOCATION (c),
12370 "%qD is threadprivate variable in %qs clause", t,
12371 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12372 remove = true;
12374 else if (!c_mark_addressable (t))
12375 remove = true;
12376 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12377 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12378 || (OMP_CLAUSE_MAP_KIND (c)
12379 == GOMP_MAP_FORCE_DEVICEPTR)))
12380 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12382 error_at (OMP_CLAUSE_LOCATION (c),
12383 "%qD does not have a mappable type in %qs clause", t,
12384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12385 remove = true;
12387 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12389 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12390 error ("%qD appears more than once in motion clauses", t);
12391 else
12392 error ("%qD appears more than once in map clauses", t);
12393 remove = true;
12395 else
12396 bitmap_set_bit (&generic_head, DECL_UID (t));
12397 break;
12399 case OMP_CLAUSE_UNIFORM:
12400 t = OMP_CLAUSE_DECL (c);
12401 if (TREE_CODE (t) != PARM_DECL)
12403 if (DECL_P (t))
12404 error_at (OMP_CLAUSE_LOCATION (c),
12405 "%qD is not an argument in %<uniform%> clause", t);
12406 else
12407 error_at (OMP_CLAUSE_LOCATION (c),
12408 "%qE is not an argument in %<uniform%> clause", t);
12409 remove = true;
12410 break;
12412 goto check_dup_generic;
12414 case OMP_CLAUSE_NOWAIT:
12415 if (copyprivate_seen)
12417 error_at (OMP_CLAUSE_LOCATION (c),
12418 "%<nowait%> clause must not be used together "
12419 "with %<copyprivate%>");
12420 remove = true;
12421 break;
12423 nowait_clause = pc;
12424 pc = &OMP_CLAUSE_CHAIN (c);
12425 continue;
12427 case OMP_CLAUSE_IF:
12428 case OMP_CLAUSE_NUM_THREADS:
12429 case OMP_CLAUSE_NUM_TEAMS:
12430 case OMP_CLAUSE_THREAD_LIMIT:
12431 case OMP_CLAUSE_SCHEDULE:
12432 case OMP_CLAUSE_ORDERED:
12433 case OMP_CLAUSE_DEFAULT:
12434 case OMP_CLAUSE_UNTIED:
12435 case OMP_CLAUSE_COLLAPSE:
12436 case OMP_CLAUSE_FINAL:
12437 case OMP_CLAUSE_MERGEABLE:
12438 case OMP_CLAUSE_SAFELEN:
12439 case OMP_CLAUSE_SIMDLEN:
12440 case OMP_CLAUSE_DEVICE:
12441 case OMP_CLAUSE_DIST_SCHEDULE:
12442 case OMP_CLAUSE_PARALLEL:
12443 case OMP_CLAUSE_FOR:
12444 case OMP_CLAUSE_SECTIONS:
12445 case OMP_CLAUSE_TASKGROUP:
12446 case OMP_CLAUSE_PROC_BIND:
12447 case OMP_CLAUSE__CILK_FOR_COUNT_:
12448 case OMP_CLAUSE_NUM_GANGS:
12449 case OMP_CLAUSE_NUM_WORKERS:
12450 case OMP_CLAUSE_VECTOR_LENGTH:
12451 case OMP_CLAUSE_ASYNC:
12452 case OMP_CLAUSE_WAIT:
12453 case OMP_CLAUSE_AUTO:
12454 case OMP_CLAUSE_SEQ:
12455 case OMP_CLAUSE_GANG:
12456 case OMP_CLAUSE_WORKER:
12457 case OMP_CLAUSE_VECTOR:
12458 pc = &OMP_CLAUSE_CHAIN (c);
12459 continue;
12461 case OMP_CLAUSE_INBRANCH:
12462 case OMP_CLAUSE_NOTINBRANCH:
12463 if (branch_seen)
12465 error_at (OMP_CLAUSE_LOCATION (c),
12466 "%<inbranch%> clause is incompatible with "
12467 "%<notinbranch%>");
12468 remove = true;
12469 break;
12471 branch_seen = true;
12472 pc = &OMP_CLAUSE_CHAIN (c);
12473 continue;
12475 default:
12476 gcc_unreachable ();
12479 if (!remove)
12481 t = OMP_CLAUSE_DECL (c);
12483 if (need_complete)
12485 t = require_complete_type (t);
12486 if (t == error_mark_node)
12487 remove = true;
12490 if (need_implicitly_determined)
12492 const char *share_name = NULL;
12494 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12495 share_name = "threadprivate";
12496 else switch (c_omp_predetermined_sharing (t))
12498 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12499 break;
12500 case OMP_CLAUSE_DEFAULT_SHARED:
12501 /* const vars may be specified in firstprivate clause. */
12502 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12503 && TREE_READONLY (t))
12504 break;
12505 share_name = "shared";
12506 break;
12507 case OMP_CLAUSE_DEFAULT_PRIVATE:
12508 share_name = "private";
12509 break;
12510 default:
12511 gcc_unreachable ();
12513 if (share_name)
12515 error_at (OMP_CLAUSE_LOCATION (c),
12516 "%qE is predetermined %qs for %qs",
12517 t, share_name,
12518 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12519 remove = true;
12524 if (remove)
12525 *pc = OMP_CLAUSE_CHAIN (c);
12526 else
12527 pc = &OMP_CLAUSE_CHAIN (c);
12530 bitmap_obstack_release (NULL);
12531 return clauses;
12534 /* Create a transaction node. */
12536 tree
12537 c_finish_transaction (location_t loc, tree block, int flags)
12539 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12540 if (flags & TM_STMT_ATTR_OUTER)
12541 TRANSACTION_EXPR_OUTER (stmt) = 1;
12542 if (flags & TM_STMT_ATTR_RELAXED)
12543 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12544 return add_stmt (stmt);
12547 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12548 down to the element type of an array. */
12550 tree
12551 c_build_qualified_type (tree type, int type_quals)
12553 if (type == error_mark_node)
12554 return type;
12556 if (TREE_CODE (type) == ARRAY_TYPE)
12558 tree t;
12559 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12560 type_quals);
12562 /* See if we already have an identically qualified type. */
12563 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12565 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12566 && TYPE_NAME (t) == TYPE_NAME (type)
12567 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12568 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12569 TYPE_ATTRIBUTES (type)))
12570 break;
12572 if (!t)
12574 tree domain = TYPE_DOMAIN (type);
12576 t = build_variant_type_copy (type);
12577 TREE_TYPE (t) = element_type;
12579 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12580 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12581 SET_TYPE_STRUCTURAL_EQUALITY (t);
12582 else if (TYPE_CANONICAL (element_type) != element_type
12583 || (domain && TYPE_CANONICAL (domain) != domain))
12585 tree unqualified_canon
12586 = build_array_type (TYPE_CANONICAL (element_type),
12587 domain? TYPE_CANONICAL (domain)
12588 : NULL_TREE);
12589 TYPE_CANONICAL (t)
12590 = c_build_qualified_type (unqualified_canon, type_quals);
12592 else
12593 TYPE_CANONICAL (t) = t;
12595 return t;
12598 /* A restrict-qualified pointer type must be a pointer to object or
12599 incomplete type. Note that the use of POINTER_TYPE_P also allows
12600 REFERENCE_TYPEs, which is appropriate for C++. */
12601 if ((type_quals & TYPE_QUAL_RESTRICT)
12602 && (!POINTER_TYPE_P (type)
12603 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12605 error ("invalid use of %<restrict%>");
12606 type_quals &= ~TYPE_QUAL_RESTRICT;
12609 return build_qualified_type (type, type_quals);
12612 /* Build a VA_ARG_EXPR for the C parser. */
12614 tree
12615 c_build_va_arg (location_t loc, tree expr, tree type)
12617 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12618 warning_at (loc, OPT_Wc___compat,
12619 "C++ requires promoted type, not enum type, in %<va_arg%>");
12620 return build_va_arg (loc, expr, type);
12623 /* Return truthvalue of whether T1 is the same tree structure as T2.
12624 Return 1 if they are the same. Return 0 if they are different. */
12626 bool
12627 c_tree_equal (tree t1, tree t2)
12629 enum tree_code code1, code2;
12631 if (t1 == t2)
12632 return true;
12633 if (!t1 || !t2)
12634 return false;
12636 for (code1 = TREE_CODE (t1);
12637 CONVERT_EXPR_CODE_P (code1)
12638 || code1 == NON_LVALUE_EXPR;
12639 code1 = TREE_CODE (t1))
12640 t1 = TREE_OPERAND (t1, 0);
12641 for (code2 = TREE_CODE (t2);
12642 CONVERT_EXPR_CODE_P (code2)
12643 || code2 == NON_LVALUE_EXPR;
12644 code2 = TREE_CODE (t2))
12645 t2 = TREE_OPERAND (t2, 0);
12647 /* They might have become equal now. */
12648 if (t1 == t2)
12649 return true;
12651 if (code1 != code2)
12652 return false;
12654 switch (code1)
12656 case INTEGER_CST:
12657 return wi::eq_p (t1, t2);
12659 case REAL_CST:
12660 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12662 case STRING_CST:
12663 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12664 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12665 TREE_STRING_LENGTH (t1));
12667 case FIXED_CST:
12668 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12669 TREE_FIXED_CST (t2));
12671 case COMPLEX_CST:
12672 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12673 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12675 case VECTOR_CST:
12676 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12678 case CONSTRUCTOR:
12679 /* We need to do this when determining whether or not two
12680 non-type pointer to member function template arguments
12681 are the same. */
12682 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12683 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12684 return false;
12686 tree field, value;
12687 unsigned int i;
12688 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12690 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12691 if (!c_tree_equal (field, elt2->index)
12692 || !c_tree_equal (value, elt2->value))
12693 return false;
12696 return true;
12698 case TREE_LIST:
12699 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12700 return false;
12701 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12702 return false;
12703 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12705 case SAVE_EXPR:
12706 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12708 case CALL_EXPR:
12710 tree arg1, arg2;
12711 call_expr_arg_iterator iter1, iter2;
12712 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12713 return false;
12714 for (arg1 = first_call_expr_arg (t1, &iter1),
12715 arg2 = first_call_expr_arg (t2, &iter2);
12716 arg1 && arg2;
12717 arg1 = next_call_expr_arg (&iter1),
12718 arg2 = next_call_expr_arg (&iter2))
12719 if (!c_tree_equal (arg1, arg2))
12720 return false;
12721 if (arg1 || arg2)
12722 return false;
12723 return true;
12726 case TARGET_EXPR:
12728 tree o1 = TREE_OPERAND (t1, 0);
12729 tree o2 = TREE_OPERAND (t2, 0);
12731 /* Special case: if either target is an unallocated VAR_DECL,
12732 it means that it's going to be unified with whatever the
12733 TARGET_EXPR is really supposed to initialize, so treat it
12734 as being equivalent to anything. */
12735 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12736 && !DECL_RTL_SET_P (o1))
12737 /*Nop*/;
12738 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12739 && !DECL_RTL_SET_P (o2))
12740 /*Nop*/;
12741 else if (!c_tree_equal (o1, o2))
12742 return false;
12744 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12747 case COMPONENT_REF:
12748 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12749 return false;
12750 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12752 case PARM_DECL:
12753 case VAR_DECL:
12754 case CONST_DECL:
12755 case FIELD_DECL:
12756 case FUNCTION_DECL:
12757 case IDENTIFIER_NODE:
12758 case SSA_NAME:
12759 return false;
12761 case TREE_VEC:
12763 unsigned ix;
12764 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12765 return false;
12766 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12767 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12768 TREE_VEC_ELT (t2, ix)))
12769 return false;
12770 return true;
12773 default:
12774 break;
12777 switch (TREE_CODE_CLASS (code1))
12779 case tcc_unary:
12780 case tcc_binary:
12781 case tcc_comparison:
12782 case tcc_expression:
12783 case tcc_vl_exp:
12784 case tcc_reference:
12785 case tcc_statement:
12787 int i, n = TREE_OPERAND_LENGTH (t1);
12789 switch (code1)
12791 case PREINCREMENT_EXPR:
12792 case PREDECREMENT_EXPR:
12793 case POSTINCREMENT_EXPR:
12794 case POSTDECREMENT_EXPR:
12795 n = 1;
12796 break;
12797 case ARRAY_REF:
12798 n = 2;
12799 break;
12800 default:
12801 break;
12804 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12805 && n != TREE_OPERAND_LENGTH (t2))
12806 return false;
12808 for (i = 0; i < n; ++i)
12809 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12810 return false;
12812 return true;
12815 case tcc_type:
12816 return comptypes (t1, t2);
12817 default:
12818 gcc_unreachable ();
12820 /* We can get here with --disable-checking. */
12821 return false;
12824 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12825 spawn-helper and BODY is the newly created body for FNDECL. */
12827 void
12828 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12830 tree list = alloc_stmt_list ();
12831 tree frame = make_cilk_frame (fndecl);
12832 tree dtor = create_cilk_function_exit (frame, false, true);
12833 add_local_decl (cfun, frame);
12835 DECL_SAVED_TREE (fndecl) = list;
12836 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12837 frame);
12838 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12839 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12841 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12842 append_to_statement_list (detach_expr, &body_list);
12844 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12845 body = fold_build_cleanup_point_expr (void_type_node, body);
12847 append_to_statement_list (body, &body_list);
12848 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12849 body_list, dtor), &list);