Replace enum omp_clause_map_kind with enum gomp_map_kind.
[official-gcc.git] / gcc / c / c-typeck.c
blob747e32213e72e54238098c38c3eef11dbad622d6
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 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 DECL_INITIAL (decl) = value;
6427 /* ANSI wants warnings about out-of-range constant initializers. */
6428 STRIP_TYPE_NOPS (value);
6429 if (TREE_STATIC (decl))
6430 constant_expression_warning (value);
6432 /* Check if we need to set array size from compound literal size. */
6433 if (TREE_CODE (type) == ARRAY_TYPE
6434 && TYPE_DOMAIN (type) == 0
6435 && value != error_mark_node)
6437 tree inside_init = init;
6439 STRIP_TYPE_NOPS (inside_init);
6440 inside_init = fold (inside_init);
6442 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6444 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6446 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6448 /* For int foo[] = (int [3]){1}; we need to set array size
6449 now since later on array initializer will be just the
6450 brace enclosed list of the compound literal. */
6451 tree etype = strip_array_types (TREE_TYPE (decl));
6452 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6453 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6454 layout_type (type);
6455 layout_decl (cldecl, 0);
6456 TREE_TYPE (decl)
6457 = c_build_qualified_type (type, TYPE_QUALS (etype));
6463 /* Methods for storing and printing names for error messages. */
6465 /* Implement a spelling stack that allows components of a name to be pushed
6466 and popped. Each element on the stack is this structure. */
6468 struct spelling
6470 int kind;
6471 union
6473 unsigned HOST_WIDE_INT i;
6474 const char *s;
6475 } u;
6478 #define SPELLING_STRING 1
6479 #define SPELLING_MEMBER 2
6480 #define SPELLING_BOUNDS 3
6482 static struct spelling *spelling; /* Next stack element (unused). */
6483 static struct spelling *spelling_base; /* Spelling stack base. */
6484 static int spelling_size; /* Size of the spelling stack. */
6486 /* Macros to save and restore the spelling stack around push_... functions.
6487 Alternative to SAVE_SPELLING_STACK. */
6489 #define SPELLING_DEPTH() (spelling - spelling_base)
6490 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6492 /* Push an element on the spelling stack with type KIND and assign VALUE
6493 to MEMBER. */
6495 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6497 int depth = SPELLING_DEPTH (); \
6499 if (depth >= spelling_size) \
6501 spelling_size += 10; \
6502 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6503 spelling_size); \
6504 RESTORE_SPELLING_DEPTH (depth); \
6507 spelling->kind = (KIND); \
6508 spelling->MEMBER = (VALUE); \
6509 spelling++; \
6512 /* Push STRING on the stack. Printed literally. */
6514 static void
6515 push_string (const char *string)
6517 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6520 /* Push a member name on the stack. Printed as '.' STRING. */
6522 static void
6523 push_member_name (tree decl)
6525 const char *const string
6526 = (DECL_NAME (decl)
6527 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6528 : _("<anonymous>"));
6529 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6532 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6534 static void
6535 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6537 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6540 /* Compute the maximum size in bytes of the printed spelling. */
6542 static int
6543 spelling_length (void)
6545 int size = 0;
6546 struct spelling *p;
6548 for (p = spelling_base; p < spelling; p++)
6550 if (p->kind == SPELLING_BOUNDS)
6551 size += 25;
6552 else
6553 size += strlen (p->u.s) + 1;
6556 return size;
6559 /* Print the spelling to BUFFER and return it. */
6561 static char *
6562 print_spelling (char *buffer)
6564 char *d = buffer;
6565 struct spelling *p;
6567 for (p = spelling_base; p < spelling; p++)
6568 if (p->kind == SPELLING_BOUNDS)
6570 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6571 d += strlen (d);
6573 else
6575 const char *s;
6576 if (p->kind == SPELLING_MEMBER)
6577 *d++ = '.';
6578 for (s = p->u.s; (*d = *s++); d++)
6581 *d++ = '\0';
6582 return buffer;
6585 /* Digest the parser output INIT as an initializer for type TYPE.
6586 Return a C expression of type TYPE to represent the initial value.
6588 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6590 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6592 If INIT is a string constant, STRICT_STRING is true if it is
6593 unparenthesized or we should not warn here for it being parenthesized.
6594 For other types of INIT, STRICT_STRING is not used.
6596 INIT_LOC is the location of the INIT.
6598 REQUIRE_CONSTANT requests an error if non-constant initializers or
6599 elements are seen. */
6601 static tree
6602 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6603 bool null_pointer_constant, bool strict_string,
6604 int require_constant)
6606 enum tree_code code = TREE_CODE (type);
6607 tree inside_init = init;
6608 tree semantic_type = NULL_TREE;
6609 bool maybe_const = true;
6611 if (type == error_mark_node
6612 || !init
6613 || error_operand_p (init))
6614 return error_mark_node;
6616 STRIP_TYPE_NOPS (inside_init);
6618 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6620 semantic_type = TREE_TYPE (inside_init);
6621 inside_init = TREE_OPERAND (inside_init, 0);
6623 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6624 inside_init = decl_constant_value_for_optimization (inside_init);
6626 /* Initialization of an array of chars from a string constant
6627 optionally enclosed in braces. */
6629 if (code == ARRAY_TYPE && inside_init
6630 && TREE_CODE (inside_init) == STRING_CST)
6632 tree typ1
6633 = (TYPE_ATOMIC (TREE_TYPE (type))
6634 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6635 TYPE_QUAL_ATOMIC)
6636 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6637 /* Note that an array could be both an array of character type
6638 and an array of wchar_t if wchar_t is signed char or unsigned
6639 char. */
6640 bool char_array = (typ1 == char_type_node
6641 || typ1 == signed_char_type_node
6642 || typ1 == unsigned_char_type_node);
6643 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6644 bool char16_array = !!comptypes (typ1, char16_type_node);
6645 bool char32_array = !!comptypes (typ1, char32_type_node);
6647 if (char_array || wchar_array || char16_array || char32_array)
6649 struct c_expr expr;
6650 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6651 expr.value = inside_init;
6652 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6653 expr.original_type = NULL;
6654 maybe_warn_string_init (init_loc, type, expr);
6656 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6657 pedwarn_init (init_loc, OPT_Wpedantic,
6658 "initialization of a flexible array member");
6660 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6661 TYPE_MAIN_VARIANT (type)))
6662 return inside_init;
6664 if (char_array)
6666 if (typ2 != char_type_node)
6668 error_init (init_loc, "char-array initialized from wide "
6669 "string");
6670 return error_mark_node;
6673 else
6675 if (typ2 == char_type_node)
6677 error_init (init_loc, "wide character array initialized "
6678 "from non-wide string");
6679 return error_mark_node;
6681 else if (!comptypes(typ1, typ2))
6683 error_init (init_loc, "wide character array initialized "
6684 "from incompatible wide string");
6685 return error_mark_node;
6689 TREE_TYPE (inside_init) = type;
6690 if (TYPE_DOMAIN (type) != 0
6691 && TYPE_SIZE (type) != 0
6692 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6694 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6696 /* Subtract the size of a single (possibly wide) character
6697 because it's ok to ignore the terminating null char
6698 that is counted in the length of the constant. */
6699 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6700 (len
6701 - (TYPE_PRECISION (typ1)
6702 / BITS_PER_UNIT))))
6703 pedwarn_init (init_loc, 0,
6704 ("initializer-string for array of chars "
6705 "is too long"));
6706 else if (warn_cxx_compat
6707 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6708 warning_at (init_loc, OPT_Wc___compat,
6709 ("initializer-string for array chars "
6710 "is too long for C++"));
6713 return inside_init;
6715 else if (INTEGRAL_TYPE_P (typ1))
6717 error_init (init_loc, "array of inappropriate type initialized "
6718 "from string constant");
6719 return error_mark_node;
6723 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6724 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6725 below and handle as a constructor. */
6726 if (code == VECTOR_TYPE
6727 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6728 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6729 && TREE_CONSTANT (inside_init))
6731 if (TREE_CODE (inside_init) == VECTOR_CST
6732 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6733 TYPE_MAIN_VARIANT (type)))
6734 return inside_init;
6736 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6738 unsigned HOST_WIDE_INT ix;
6739 tree value;
6740 bool constant_p = true;
6742 /* Iterate through elements and check if all constructor
6743 elements are *_CSTs. */
6744 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6745 if (!CONSTANT_CLASS_P (value))
6747 constant_p = false;
6748 break;
6751 if (constant_p)
6752 return build_vector_from_ctor (type,
6753 CONSTRUCTOR_ELTS (inside_init));
6757 if (warn_sequence_point)
6758 verify_sequence_points (inside_init);
6760 /* Any type can be initialized
6761 from an expression of the same type, optionally with braces. */
6763 if (inside_init && TREE_TYPE (inside_init) != 0
6764 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6765 TYPE_MAIN_VARIANT (type))
6766 || (code == ARRAY_TYPE
6767 && comptypes (TREE_TYPE (inside_init), type))
6768 || (code == VECTOR_TYPE
6769 && comptypes (TREE_TYPE (inside_init), type))
6770 || (code == POINTER_TYPE
6771 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6772 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6773 TREE_TYPE (type)))))
6775 if (code == POINTER_TYPE)
6777 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6779 if (TREE_CODE (inside_init) == STRING_CST
6780 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6781 inside_init = array_to_pointer_conversion
6782 (init_loc, inside_init);
6783 else
6785 error_init (init_loc, "invalid use of non-lvalue array");
6786 return error_mark_node;
6791 if (code == VECTOR_TYPE)
6792 /* Although the types are compatible, we may require a
6793 conversion. */
6794 inside_init = convert (type, inside_init);
6796 if (require_constant
6797 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6799 /* As an extension, allow initializing objects with static storage
6800 duration with compound literals (which are then treated just as
6801 the brace enclosed list they contain). Also allow this for
6802 vectors, as we can only assign them with compound literals. */
6803 if (flag_isoc99 && code != VECTOR_TYPE)
6804 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6805 "is not constant");
6806 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6807 inside_init = DECL_INITIAL (decl);
6810 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6811 && TREE_CODE (inside_init) != CONSTRUCTOR)
6813 error_init (init_loc, "array initialized from non-constant array "
6814 "expression");
6815 return error_mark_node;
6818 /* Compound expressions can only occur here if -Wpedantic or
6819 -pedantic-errors is specified. In the later case, we always want
6820 an error. In the former case, we simply want a warning. */
6821 if (require_constant && pedantic
6822 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6824 inside_init
6825 = valid_compound_expr_initializer (inside_init,
6826 TREE_TYPE (inside_init));
6827 if (inside_init == error_mark_node)
6828 error_init (init_loc, "initializer element is not constant");
6829 else
6830 pedwarn_init (init_loc, OPT_Wpedantic,
6831 "initializer element is not constant");
6832 if (flag_pedantic_errors)
6833 inside_init = error_mark_node;
6835 else if (require_constant
6836 && !initializer_constant_valid_p (inside_init,
6837 TREE_TYPE (inside_init)))
6839 error_init (init_loc, "initializer element is not constant");
6840 inside_init = error_mark_node;
6842 else if (require_constant && !maybe_const)
6843 pedwarn_init (init_loc, 0,
6844 "initializer element is not a constant expression");
6846 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6847 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6848 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6849 type, inside_init, origtype,
6850 ic_init, null_pointer_constant,
6851 NULL_TREE, NULL_TREE, 0);
6852 return inside_init;
6855 /* Handle scalar types, including conversions. */
6857 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6858 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6859 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6861 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6862 && (TREE_CODE (init) == STRING_CST
6863 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6864 inside_init = init = array_to_pointer_conversion (init_loc, init);
6865 if (semantic_type)
6866 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6867 inside_init);
6868 inside_init
6869 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6870 inside_init, origtype, ic_init,
6871 null_pointer_constant, NULL_TREE, NULL_TREE,
6874 /* Check to see if we have already given an error message. */
6875 if (inside_init == error_mark_node)
6877 else if (require_constant && !TREE_CONSTANT (inside_init))
6879 error_init (init_loc, "initializer element is not constant");
6880 inside_init = error_mark_node;
6882 else if (require_constant
6883 && !initializer_constant_valid_p (inside_init,
6884 TREE_TYPE (inside_init)))
6886 error_init (init_loc, "initializer element is not computable at "
6887 "load time");
6888 inside_init = error_mark_node;
6890 else if (require_constant && !maybe_const)
6891 pedwarn_init (init_loc, 0,
6892 "initializer element is not a constant expression");
6894 return inside_init;
6897 /* Come here only for records and arrays. */
6899 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6901 error_init (init_loc, "variable-sized object may not be initialized");
6902 return error_mark_node;
6905 error_init (init_loc, "invalid initializer");
6906 return error_mark_node;
6909 /* Handle initializers that use braces. */
6911 /* Type of object we are accumulating a constructor for.
6912 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6913 static tree constructor_type;
6915 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6916 left to fill. */
6917 static tree constructor_fields;
6919 /* For an ARRAY_TYPE, this is the specified index
6920 at which to store the next element we get. */
6921 static tree constructor_index;
6923 /* For an ARRAY_TYPE, this is the maximum index. */
6924 static tree constructor_max_index;
6926 /* For a RECORD_TYPE, this is the first field not yet written out. */
6927 static tree constructor_unfilled_fields;
6929 /* For an ARRAY_TYPE, this is the index of the first element
6930 not yet written out. */
6931 static tree constructor_unfilled_index;
6933 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6934 This is so we can generate gaps between fields, when appropriate. */
6935 static tree constructor_bit_index;
6937 /* If we are saving up the elements rather than allocating them,
6938 this is the list of elements so far (in reverse order,
6939 most recent first). */
6940 static vec<constructor_elt, va_gc> *constructor_elements;
6942 /* 1 if constructor should be incrementally stored into a constructor chain,
6943 0 if all the elements should be kept in AVL tree. */
6944 static int constructor_incremental;
6946 /* 1 if so far this constructor's elements are all compile-time constants. */
6947 static int constructor_constant;
6949 /* 1 if so far this constructor's elements are all valid address constants. */
6950 static int constructor_simple;
6952 /* 1 if this constructor has an element that cannot be part of a
6953 constant expression. */
6954 static int constructor_nonconst;
6956 /* 1 if this constructor is erroneous so far. */
6957 static int constructor_erroneous;
6959 /* 1 if this constructor is the universal zero initializer { 0 }. */
6960 static int constructor_zeroinit;
6962 /* Structure for managing pending initializer elements, organized as an
6963 AVL tree. */
6965 struct init_node
6967 struct init_node *left, *right;
6968 struct init_node *parent;
6969 int balance;
6970 tree purpose;
6971 tree value;
6972 tree origtype;
6975 /* Tree of pending elements at this constructor level.
6976 These are elements encountered out of order
6977 which belong at places we haven't reached yet in actually
6978 writing the output.
6979 Will never hold tree nodes across GC runs. */
6980 static struct init_node *constructor_pending_elts;
6982 /* The SPELLING_DEPTH of this constructor. */
6983 static int constructor_depth;
6985 /* DECL node for which an initializer is being read.
6986 0 means we are reading a constructor expression
6987 such as (struct foo) {...}. */
6988 static tree constructor_decl;
6990 /* Nonzero if this is an initializer for a top-level decl. */
6991 static int constructor_top_level;
6993 /* Nonzero if there were any member designators in this initializer. */
6994 static int constructor_designated;
6996 /* Nesting depth of designator list. */
6997 static int designator_depth;
6999 /* Nonzero if there were diagnosed errors in this designator list. */
7000 static int designator_erroneous;
7003 /* This stack has a level for each implicit or explicit level of
7004 structuring in the initializer, including the outermost one. It
7005 saves the values of most of the variables above. */
7007 struct constructor_range_stack;
7009 struct constructor_stack
7011 struct constructor_stack *next;
7012 tree type;
7013 tree fields;
7014 tree index;
7015 tree max_index;
7016 tree unfilled_index;
7017 tree unfilled_fields;
7018 tree bit_index;
7019 vec<constructor_elt, va_gc> *elements;
7020 struct init_node *pending_elts;
7021 int offset;
7022 int depth;
7023 /* If value nonzero, this value should replace the entire
7024 constructor at this level. */
7025 struct c_expr replacement_value;
7026 struct constructor_range_stack *range_stack;
7027 char constant;
7028 char simple;
7029 char nonconst;
7030 char implicit;
7031 char erroneous;
7032 char outer;
7033 char incremental;
7034 char designated;
7035 int designator_depth;
7038 static struct constructor_stack *constructor_stack;
7040 /* This stack represents designators from some range designator up to
7041 the last designator in the list. */
7043 struct constructor_range_stack
7045 struct constructor_range_stack *next, *prev;
7046 struct constructor_stack *stack;
7047 tree range_start;
7048 tree index;
7049 tree range_end;
7050 tree fields;
7053 static struct constructor_range_stack *constructor_range_stack;
7055 /* This stack records separate initializers that are nested.
7056 Nested initializers can't happen in ANSI C, but GNU C allows them
7057 in cases like { ... (struct foo) { ... } ... }. */
7059 struct initializer_stack
7061 struct initializer_stack *next;
7062 tree decl;
7063 struct constructor_stack *constructor_stack;
7064 struct constructor_range_stack *constructor_range_stack;
7065 vec<constructor_elt, va_gc> *elements;
7066 struct spelling *spelling;
7067 struct spelling *spelling_base;
7068 int spelling_size;
7069 char top_level;
7070 char require_constant_value;
7071 char require_constant_elements;
7074 static struct initializer_stack *initializer_stack;
7076 /* Prepare to parse and output the initializer for variable DECL. */
7078 void
7079 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7081 const char *locus;
7082 struct initializer_stack *p = XNEW (struct initializer_stack);
7084 p->decl = constructor_decl;
7085 p->require_constant_value = require_constant_value;
7086 p->require_constant_elements = require_constant_elements;
7087 p->constructor_stack = constructor_stack;
7088 p->constructor_range_stack = constructor_range_stack;
7089 p->elements = constructor_elements;
7090 p->spelling = spelling;
7091 p->spelling_base = spelling_base;
7092 p->spelling_size = spelling_size;
7093 p->top_level = constructor_top_level;
7094 p->next = initializer_stack;
7095 initializer_stack = p;
7097 constructor_decl = decl;
7098 constructor_designated = 0;
7099 constructor_top_level = top_level;
7101 if (decl != 0 && decl != error_mark_node)
7103 require_constant_value = TREE_STATIC (decl);
7104 require_constant_elements
7105 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7106 /* For a scalar, you can always use any value to initialize,
7107 even within braces. */
7108 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7109 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7110 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7111 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7112 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7114 else
7116 require_constant_value = 0;
7117 require_constant_elements = 0;
7118 locus = _("(anonymous)");
7121 constructor_stack = 0;
7122 constructor_range_stack = 0;
7124 found_missing_braces = 0;
7126 spelling_base = 0;
7127 spelling_size = 0;
7128 RESTORE_SPELLING_DEPTH (0);
7130 if (locus)
7131 push_string (locus);
7134 void
7135 finish_init (void)
7137 struct initializer_stack *p = initializer_stack;
7139 /* Free the whole constructor stack of this initializer. */
7140 while (constructor_stack)
7142 struct constructor_stack *q = constructor_stack;
7143 constructor_stack = q->next;
7144 free (q);
7147 gcc_assert (!constructor_range_stack);
7149 /* Pop back to the data of the outer initializer (if any). */
7150 free (spelling_base);
7152 constructor_decl = p->decl;
7153 require_constant_value = p->require_constant_value;
7154 require_constant_elements = p->require_constant_elements;
7155 constructor_stack = p->constructor_stack;
7156 constructor_range_stack = p->constructor_range_stack;
7157 constructor_elements = p->elements;
7158 spelling = p->spelling;
7159 spelling_base = p->spelling_base;
7160 spelling_size = p->spelling_size;
7161 constructor_top_level = p->top_level;
7162 initializer_stack = p->next;
7163 free (p);
7166 /* Call here when we see the initializer is surrounded by braces.
7167 This is instead of a call to push_init_level;
7168 it is matched by a call to pop_init_level.
7170 TYPE is the type to initialize, for a constructor expression.
7171 For an initializer for a decl, TYPE is zero. */
7173 void
7174 really_start_incremental_init (tree type)
7176 struct constructor_stack *p = XNEW (struct constructor_stack);
7178 if (type == 0)
7179 type = TREE_TYPE (constructor_decl);
7181 if (TREE_CODE (type) == VECTOR_TYPE
7182 && TYPE_VECTOR_OPAQUE (type))
7183 error ("opaque vector types cannot be initialized");
7185 p->type = constructor_type;
7186 p->fields = constructor_fields;
7187 p->index = constructor_index;
7188 p->max_index = constructor_max_index;
7189 p->unfilled_index = constructor_unfilled_index;
7190 p->unfilled_fields = constructor_unfilled_fields;
7191 p->bit_index = constructor_bit_index;
7192 p->elements = constructor_elements;
7193 p->constant = constructor_constant;
7194 p->simple = constructor_simple;
7195 p->nonconst = constructor_nonconst;
7196 p->erroneous = constructor_erroneous;
7197 p->pending_elts = constructor_pending_elts;
7198 p->depth = constructor_depth;
7199 p->replacement_value.value = 0;
7200 p->replacement_value.original_code = ERROR_MARK;
7201 p->replacement_value.original_type = NULL;
7202 p->implicit = 0;
7203 p->range_stack = 0;
7204 p->outer = 0;
7205 p->incremental = constructor_incremental;
7206 p->designated = constructor_designated;
7207 p->designator_depth = designator_depth;
7208 p->next = 0;
7209 constructor_stack = p;
7211 constructor_constant = 1;
7212 constructor_simple = 1;
7213 constructor_nonconst = 0;
7214 constructor_depth = SPELLING_DEPTH ();
7215 constructor_elements = NULL;
7216 constructor_pending_elts = 0;
7217 constructor_type = type;
7218 constructor_incremental = 1;
7219 constructor_designated = 0;
7220 constructor_zeroinit = 1;
7221 designator_depth = 0;
7222 designator_erroneous = 0;
7224 if (TREE_CODE (constructor_type) == RECORD_TYPE
7225 || TREE_CODE (constructor_type) == UNION_TYPE)
7227 constructor_fields = TYPE_FIELDS (constructor_type);
7228 /* Skip any nameless bit fields at the beginning. */
7229 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7230 && DECL_NAME (constructor_fields) == 0)
7231 constructor_fields = DECL_CHAIN (constructor_fields);
7233 constructor_unfilled_fields = constructor_fields;
7234 constructor_bit_index = bitsize_zero_node;
7236 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7238 if (TYPE_DOMAIN (constructor_type))
7240 constructor_max_index
7241 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7243 /* Detect non-empty initializations of zero-length arrays. */
7244 if (constructor_max_index == NULL_TREE
7245 && TYPE_SIZE (constructor_type))
7246 constructor_max_index = integer_minus_one_node;
7248 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7249 to initialize VLAs will cause a proper error; avoid tree
7250 checking errors as well by setting a safe value. */
7251 if (constructor_max_index
7252 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7253 constructor_max_index = integer_minus_one_node;
7255 constructor_index
7256 = convert (bitsizetype,
7257 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7259 else
7261 constructor_index = bitsize_zero_node;
7262 constructor_max_index = NULL_TREE;
7265 constructor_unfilled_index = constructor_index;
7267 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7269 /* Vectors are like simple fixed-size arrays. */
7270 constructor_max_index =
7271 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7272 constructor_index = bitsize_zero_node;
7273 constructor_unfilled_index = constructor_index;
7275 else
7277 /* Handle the case of int x = {5}; */
7278 constructor_fields = constructor_type;
7279 constructor_unfilled_fields = constructor_type;
7283 /* Push down into a subobject, for initialization.
7284 If this is for an explicit set of braces, IMPLICIT is 0.
7285 If it is because the next element belongs at a lower level,
7286 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7288 void
7289 push_init_level (location_t loc, int implicit,
7290 struct obstack *braced_init_obstack)
7292 struct constructor_stack *p;
7293 tree value = NULL_TREE;
7295 /* If we've exhausted any levels that didn't have braces,
7296 pop them now. If implicit == 1, this will have been done in
7297 process_init_element; do not repeat it here because in the case
7298 of excess initializers for an empty aggregate this leads to an
7299 infinite cycle of popping a level and immediately recreating
7300 it. */
7301 if (implicit != 1)
7303 while (constructor_stack->implicit)
7305 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7306 || TREE_CODE (constructor_type) == UNION_TYPE)
7307 && constructor_fields == 0)
7308 process_init_element (input_location,
7309 pop_init_level (loc, 1, braced_init_obstack),
7310 true, braced_init_obstack);
7311 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7312 && constructor_max_index
7313 && tree_int_cst_lt (constructor_max_index,
7314 constructor_index))
7315 process_init_element (input_location,
7316 pop_init_level (loc, 1, braced_init_obstack),
7317 true, braced_init_obstack);
7318 else
7319 break;
7323 /* Unless this is an explicit brace, we need to preserve previous
7324 content if any. */
7325 if (implicit)
7327 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7328 || TREE_CODE (constructor_type) == UNION_TYPE)
7329 && constructor_fields)
7330 value = find_init_member (constructor_fields, braced_init_obstack);
7331 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7332 value = find_init_member (constructor_index, braced_init_obstack);
7335 p = XNEW (struct constructor_stack);
7336 p->type = constructor_type;
7337 p->fields = constructor_fields;
7338 p->index = constructor_index;
7339 p->max_index = constructor_max_index;
7340 p->unfilled_index = constructor_unfilled_index;
7341 p->unfilled_fields = constructor_unfilled_fields;
7342 p->bit_index = constructor_bit_index;
7343 p->elements = constructor_elements;
7344 p->constant = constructor_constant;
7345 p->simple = constructor_simple;
7346 p->nonconst = constructor_nonconst;
7347 p->erroneous = constructor_erroneous;
7348 p->pending_elts = constructor_pending_elts;
7349 p->depth = constructor_depth;
7350 p->replacement_value.value = 0;
7351 p->replacement_value.original_code = ERROR_MARK;
7352 p->replacement_value.original_type = NULL;
7353 p->implicit = implicit;
7354 p->outer = 0;
7355 p->incremental = constructor_incremental;
7356 p->designated = constructor_designated;
7357 p->designator_depth = designator_depth;
7358 p->next = constructor_stack;
7359 p->range_stack = 0;
7360 constructor_stack = p;
7362 constructor_constant = 1;
7363 constructor_simple = 1;
7364 constructor_nonconst = 0;
7365 constructor_depth = SPELLING_DEPTH ();
7366 constructor_elements = NULL;
7367 constructor_incremental = 1;
7368 constructor_designated = 0;
7369 constructor_pending_elts = 0;
7370 if (!implicit)
7372 p->range_stack = constructor_range_stack;
7373 constructor_range_stack = 0;
7374 designator_depth = 0;
7375 designator_erroneous = 0;
7378 /* Don't die if an entire brace-pair level is superfluous
7379 in the containing level. */
7380 if (constructor_type == 0)
7382 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7383 || TREE_CODE (constructor_type) == UNION_TYPE)
7385 /* Don't die if there are extra init elts at the end. */
7386 if (constructor_fields == 0)
7387 constructor_type = 0;
7388 else
7390 constructor_type = TREE_TYPE (constructor_fields);
7391 push_member_name (constructor_fields);
7392 constructor_depth++;
7394 /* If upper initializer is designated, then mark this as
7395 designated too to prevent bogus warnings. */
7396 constructor_designated = p->designated;
7398 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7400 constructor_type = TREE_TYPE (constructor_type);
7401 push_array_bounds (tree_to_uhwi (constructor_index));
7402 constructor_depth++;
7405 if (constructor_type == 0)
7407 error_init (loc, "extra brace group at end of initializer");
7408 constructor_fields = 0;
7409 constructor_unfilled_fields = 0;
7410 return;
7413 if (value && TREE_CODE (value) == CONSTRUCTOR)
7415 constructor_constant = TREE_CONSTANT (value);
7416 constructor_simple = TREE_STATIC (value);
7417 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7418 constructor_elements = CONSTRUCTOR_ELTS (value);
7419 if (!vec_safe_is_empty (constructor_elements)
7420 && (TREE_CODE (constructor_type) == RECORD_TYPE
7421 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7422 set_nonincremental_init (braced_init_obstack);
7425 if (implicit == 1)
7426 found_missing_braces = 1;
7428 if (TREE_CODE (constructor_type) == RECORD_TYPE
7429 || TREE_CODE (constructor_type) == UNION_TYPE)
7431 constructor_fields = TYPE_FIELDS (constructor_type);
7432 /* Skip any nameless bit fields at the beginning. */
7433 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7434 && DECL_NAME (constructor_fields) == 0)
7435 constructor_fields = DECL_CHAIN (constructor_fields);
7437 constructor_unfilled_fields = constructor_fields;
7438 constructor_bit_index = bitsize_zero_node;
7440 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7442 /* Vectors are like simple fixed-size arrays. */
7443 constructor_max_index =
7444 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7445 constructor_index = bitsize_int (0);
7446 constructor_unfilled_index = constructor_index;
7448 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7450 if (TYPE_DOMAIN (constructor_type))
7452 constructor_max_index
7453 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7455 /* Detect non-empty initializations of zero-length arrays. */
7456 if (constructor_max_index == NULL_TREE
7457 && TYPE_SIZE (constructor_type))
7458 constructor_max_index = integer_minus_one_node;
7460 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7461 to initialize VLAs will cause a proper error; avoid tree
7462 checking errors as well by setting a safe value. */
7463 if (constructor_max_index
7464 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7465 constructor_max_index = integer_minus_one_node;
7467 constructor_index
7468 = convert (bitsizetype,
7469 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7471 else
7472 constructor_index = bitsize_zero_node;
7474 constructor_unfilled_index = constructor_index;
7475 if (value && TREE_CODE (value) == STRING_CST)
7477 /* We need to split the char/wchar array into individual
7478 characters, so that we don't have to special case it
7479 everywhere. */
7480 set_nonincremental_init_from_string (value, braced_init_obstack);
7483 else
7485 if (constructor_type != error_mark_node)
7486 warning_init (input_location, 0, "braces around scalar initializer");
7487 constructor_fields = constructor_type;
7488 constructor_unfilled_fields = constructor_type;
7492 /* At the end of an implicit or explicit brace level,
7493 finish up that level of constructor. If a single expression
7494 with redundant braces initialized that level, return the
7495 c_expr structure for that expression. Otherwise, the original_code
7496 element is set to ERROR_MARK.
7497 If we were outputting the elements as they are read, return 0 as the value
7498 from inner levels (process_init_element ignores that),
7499 but return error_mark_node as the value from the outermost level
7500 (that's what we want to put in DECL_INITIAL).
7501 Otherwise, return a CONSTRUCTOR expression as the value. */
7503 struct c_expr
7504 pop_init_level (location_t loc, int implicit,
7505 struct obstack *braced_init_obstack)
7507 struct constructor_stack *p;
7508 struct c_expr ret;
7509 ret.value = 0;
7510 ret.original_code = ERROR_MARK;
7511 ret.original_type = NULL;
7513 if (implicit == 0)
7515 /* When we come to an explicit close brace,
7516 pop any inner levels that didn't have explicit braces. */
7517 while (constructor_stack->implicit)
7518 process_init_element (input_location,
7519 pop_init_level (loc, 1, braced_init_obstack),
7520 true, braced_init_obstack);
7521 gcc_assert (!constructor_range_stack);
7524 /* Now output all pending elements. */
7525 constructor_incremental = 1;
7526 output_pending_init_elements (1, braced_init_obstack);
7528 p = constructor_stack;
7530 /* Error for initializing a flexible array member, or a zero-length
7531 array member in an inappropriate context. */
7532 if (constructor_type && constructor_fields
7533 && TREE_CODE (constructor_type) == ARRAY_TYPE
7534 && TYPE_DOMAIN (constructor_type)
7535 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7537 /* Silently discard empty initializations. The parser will
7538 already have pedwarned for empty brackets. */
7539 if (integer_zerop (constructor_unfilled_index))
7540 constructor_type = NULL_TREE;
7541 else
7543 gcc_assert (!TYPE_SIZE (constructor_type));
7545 if (constructor_depth > 2)
7546 error_init (loc, "initialization of flexible array member in a nested context");
7547 else
7548 pedwarn_init (loc, OPT_Wpedantic,
7549 "initialization of a flexible array member");
7551 /* We have already issued an error message for the existence
7552 of a flexible array member not at the end of the structure.
7553 Discard the initializer so that we do not die later. */
7554 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7555 constructor_type = NULL_TREE;
7559 /* Initialization with { } counts as zeroinit. */
7560 if (vec_safe_length (constructor_elements) == 0)
7561 constructor_zeroinit = 1;
7562 /* If the constructor has more than one element, it can't be { 0 }. */
7563 else if (vec_safe_length (constructor_elements) != 1)
7564 constructor_zeroinit = 0;
7566 /* Warn when some structs are initialized with direct aggregation. */
7567 if (!implicit && found_missing_braces && warn_missing_braces
7568 && !constructor_zeroinit)
7570 warning_init (loc, OPT_Wmissing_braces,
7571 "missing braces around initializer");
7574 /* Warn when some struct elements are implicitly initialized to zero. */
7575 if (warn_missing_field_initializers
7576 && constructor_type
7577 && TREE_CODE (constructor_type) == RECORD_TYPE
7578 && constructor_unfilled_fields)
7580 /* Do not warn for flexible array members or zero-length arrays. */
7581 while (constructor_unfilled_fields
7582 && (!DECL_SIZE (constructor_unfilled_fields)
7583 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7584 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7586 if (constructor_unfilled_fields
7587 /* Do not warn if this level of the initializer uses member
7588 designators; it is likely to be deliberate. */
7589 && !constructor_designated
7590 /* Do not warn about initializing with { 0 } or with { }. */
7591 && !constructor_zeroinit)
7593 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7594 "missing initializer for field %qD of %qT",
7595 constructor_unfilled_fields,
7596 constructor_type))
7597 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7598 "%qD declared here", constructor_unfilled_fields);
7602 /* Pad out the end of the structure. */
7603 if (p->replacement_value.value)
7604 /* If this closes a superfluous brace pair,
7605 just pass out the element between them. */
7606 ret = p->replacement_value;
7607 else if (constructor_type == 0)
7609 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7610 && TREE_CODE (constructor_type) != UNION_TYPE
7611 && TREE_CODE (constructor_type) != ARRAY_TYPE
7612 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7614 /* A nonincremental scalar initializer--just return
7615 the element, after verifying there is just one. */
7616 if (vec_safe_is_empty (constructor_elements))
7618 if (!constructor_erroneous)
7619 error_init (loc, "empty scalar initializer");
7620 ret.value = error_mark_node;
7622 else if (vec_safe_length (constructor_elements) != 1)
7624 error_init (loc, "extra elements in scalar initializer");
7625 ret.value = (*constructor_elements)[0].value;
7627 else
7628 ret.value = (*constructor_elements)[0].value;
7630 else
7632 if (constructor_erroneous)
7633 ret.value = error_mark_node;
7634 else
7636 ret.value = build_constructor (constructor_type,
7637 constructor_elements);
7638 if (constructor_constant)
7639 TREE_CONSTANT (ret.value) = 1;
7640 if (constructor_constant && constructor_simple)
7641 TREE_STATIC (ret.value) = 1;
7642 if (constructor_nonconst)
7643 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7647 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7649 if (constructor_nonconst)
7650 ret.original_code = C_MAYBE_CONST_EXPR;
7651 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7652 ret.original_code = ERROR_MARK;
7655 constructor_type = p->type;
7656 constructor_fields = p->fields;
7657 constructor_index = p->index;
7658 constructor_max_index = p->max_index;
7659 constructor_unfilled_index = p->unfilled_index;
7660 constructor_unfilled_fields = p->unfilled_fields;
7661 constructor_bit_index = p->bit_index;
7662 constructor_elements = p->elements;
7663 constructor_constant = p->constant;
7664 constructor_simple = p->simple;
7665 constructor_nonconst = p->nonconst;
7666 constructor_erroneous = p->erroneous;
7667 constructor_incremental = p->incremental;
7668 constructor_designated = p->designated;
7669 designator_depth = p->designator_depth;
7670 constructor_pending_elts = p->pending_elts;
7671 constructor_depth = p->depth;
7672 if (!p->implicit)
7673 constructor_range_stack = p->range_stack;
7674 RESTORE_SPELLING_DEPTH (constructor_depth);
7676 constructor_stack = p->next;
7677 free (p);
7679 if (ret.value == 0 && constructor_stack == 0)
7680 ret.value = error_mark_node;
7681 return ret;
7684 /* Common handling for both array range and field name designators.
7685 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7687 static int
7688 set_designator (location_t loc, int array,
7689 struct obstack *braced_init_obstack)
7691 tree subtype;
7692 enum tree_code subcode;
7694 /* Don't die if an entire brace-pair level is superfluous
7695 in the containing level. */
7696 if (constructor_type == 0)
7697 return 1;
7699 /* If there were errors in this designator list already, bail out
7700 silently. */
7701 if (designator_erroneous)
7702 return 1;
7704 if (!designator_depth)
7706 gcc_assert (!constructor_range_stack);
7708 /* Designator list starts at the level of closest explicit
7709 braces. */
7710 while (constructor_stack->implicit)
7711 process_init_element (input_location,
7712 pop_init_level (loc, 1, braced_init_obstack),
7713 true, braced_init_obstack);
7714 constructor_designated = 1;
7715 return 0;
7718 switch (TREE_CODE (constructor_type))
7720 case RECORD_TYPE:
7721 case UNION_TYPE:
7722 subtype = TREE_TYPE (constructor_fields);
7723 if (subtype != error_mark_node)
7724 subtype = TYPE_MAIN_VARIANT (subtype);
7725 break;
7726 case ARRAY_TYPE:
7727 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7728 break;
7729 default:
7730 gcc_unreachable ();
7733 subcode = TREE_CODE (subtype);
7734 if (array && subcode != ARRAY_TYPE)
7736 error_init (loc, "array index in non-array initializer");
7737 return 1;
7739 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7741 error_init (loc, "field name not in record or union initializer");
7742 return 1;
7745 constructor_designated = 1;
7746 push_init_level (loc, 2, braced_init_obstack);
7747 return 0;
7750 /* If there are range designators in designator list, push a new designator
7751 to constructor_range_stack. RANGE_END is end of such stack range or
7752 NULL_TREE if there is no range designator at this level. */
7754 static void
7755 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7757 struct constructor_range_stack *p;
7759 p = (struct constructor_range_stack *)
7760 obstack_alloc (braced_init_obstack,
7761 sizeof (struct constructor_range_stack));
7762 p->prev = constructor_range_stack;
7763 p->next = 0;
7764 p->fields = constructor_fields;
7765 p->range_start = constructor_index;
7766 p->index = constructor_index;
7767 p->stack = constructor_stack;
7768 p->range_end = range_end;
7769 if (constructor_range_stack)
7770 constructor_range_stack->next = p;
7771 constructor_range_stack = p;
7774 /* Within an array initializer, specify the next index to be initialized.
7775 FIRST is that index. If LAST is nonzero, then initialize a range
7776 of indices, running from FIRST through LAST. */
7778 void
7779 set_init_index (location_t loc, tree first, tree last,
7780 struct obstack *braced_init_obstack)
7782 if (set_designator (loc, 1, braced_init_obstack))
7783 return;
7785 designator_erroneous = 1;
7787 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7788 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7790 error_init (loc, "array index in initializer not of integer type");
7791 return;
7794 if (TREE_CODE (first) != INTEGER_CST)
7796 first = c_fully_fold (first, false, NULL);
7797 if (TREE_CODE (first) == INTEGER_CST)
7798 pedwarn_init (loc, OPT_Wpedantic,
7799 "array index in initializer is not "
7800 "an integer constant expression");
7803 if (last && TREE_CODE (last) != INTEGER_CST)
7805 last = c_fully_fold (last, false, NULL);
7806 if (TREE_CODE (last) == INTEGER_CST)
7807 pedwarn_init (loc, OPT_Wpedantic,
7808 "array index in initializer is not "
7809 "an integer constant expression");
7812 if (TREE_CODE (first) != INTEGER_CST)
7813 error_init (loc, "nonconstant array index in initializer");
7814 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7815 error_init (loc, "nonconstant array index in initializer");
7816 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7817 error_init (loc, "array index in non-array initializer");
7818 else if (tree_int_cst_sgn (first) == -1)
7819 error_init (loc, "array index in initializer exceeds array bounds");
7820 else if (constructor_max_index
7821 && tree_int_cst_lt (constructor_max_index, first))
7822 error_init (loc, "array index in initializer exceeds array bounds");
7823 else
7825 constant_expression_warning (first);
7826 if (last)
7827 constant_expression_warning (last);
7828 constructor_index = convert (bitsizetype, first);
7829 if (tree_int_cst_lt (constructor_index, first))
7831 constructor_index = copy_node (constructor_index);
7832 TREE_OVERFLOW (constructor_index) = 1;
7835 if (last)
7837 if (tree_int_cst_equal (first, last))
7838 last = 0;
7839 else if (tree_int_cst_lt (last, first))
7841 error_init (loc, "empty index range in initializer");
7842 last = 0;
7844 else
7846 last = convert (bitsizetype, last);
7847 if (constructor_max_index != 0
7848 && tree_int_cst_lt (constructor_max_index, last))
7850 error_init (loc, "array index range in initializer exceeds "
7851 "array bounds");
7852 last = 0;
7857 designator_depth++;
7858 designator_erroneous = 0;
7859 if (constructor_range_stack || last)
7860 push_range_stack (last, braced_init_obstack);
7864 /* Within a struct initializer, specify the next field to be initialized. */
7866 void
7867 set_init_label (location_t loc, tree fieldname,
7868 struct obstack *braced_init_obstack)
7870 tree field;
7872 if (set_designator (loc, 0, braced_init_obstack))
7873 return;
7875 designator_erroneous = 1;
7877 if (TREE_CODE (constructor_type) != RECORD_TYPE
7878 && TREE_CODE (constructor_type) != UNION_TYPE)
7880 error_init (loc, "field name not in record or union initializer");
7881 return;
7884 field = lookup_field (constructor_type, fieldname);
7886 if (field == 0)
7887 error ("unknown field %qE specified in initializer", fieldname);
7888 else
7891 constructor_fields = TREE_VALUE (field);
7892 designator_depth++;
7893 designator_erroneous = 0;
7894 if (constructor_range_stack)
7895 push_range_stack (NULL_TREE, braced_init_obstack);
7896 field = TREE_CHAIN (field);
7897 if (field)
7899 if (set_designator (loc, 0, braced_init_obstack))
7900 return;
7903 while (field != NULL_TREE);
7906 /* Add a new initializer to the tree of pending initializers. PURPOSE
7907 identifies the initializer, either array index or field in a structure.
7908 VALUE is the value of that index or field. If ORIGTYPE is not
7909 NULL_TREE, it is the original type of VALUE.
7911 IMPLICIT is true if value comes from pop_init_level (1),
7912 the new initializer has been merged with the existing one
7913 and thus no warnings should be emitted about overriding an
7914 existing initializer. */
7916 static void
7917 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7918 bool implicit, struct obstack *braced_init_obstack)
7920 struct init_node *p, **q, *r;
7922 q = &constructor_pending_elts;
7923 p = 0;
7925 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7927 while (*q != 0)
7929 p = *q;
7930 if (tree_int_cst_lt (purpose, p->purpose))
7931 q = &p->left;
7932 else if (tree_int_cst_lt (p->purpose, purpose))
7933 q = &p->right;
7934 else
7936 if (!implicit)
7938 if (TREE_SIDE_EFFECTS (p->value))
7939 warning_init (loc, 0,
7940 "initialized field with side-effects "
7941 "overwritten");
7942 else if (warn_override_init)
7943 warning_init (loc, OPT_Woverride_init,
7944 "initialized field overwritten");
7946 p->value = value;
7947 p->origtype = origtype;
7948 return;
7952 else
7954 tree bitpos;
7956 bitpos = bit_position (purpose);
7957 while (*q != NULL)
7959 p = *q;
7960 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7961 q = &p->left;
7962 else if (p->purpose != purpose)
7963 q = &p->right;
7964 else
7966 if (!implicit)
7968 if (TREE_SIDE_EFFECTS (p->value))
7969 warning_init (loc, 0,
7970 "initialized field with side-effects "
7971 "overwritten");
7972 else if (warn_override_init)
7973 warning_init (loc, OPT_Woverride_init,
7974 "initialized field overwritten");
7976 p->value = value;
7977 p->origtype = origtype;
7978 return;
7983 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7984 sizeof (struct init_node));
7985 r->purpose = purpose;
7986 r->value = value;
7987 r->origtype = origtype;
7989 *q = r;
7990 r->parent = p;
7991 r->left = 0;
7992 r->right = 0;
7993 r->balance = 0;
7995 while (p)
7997 struct init_node *s;
7999 if (r == p->left)
8001 if (p->balance == 0)
8002 p->balance = -1;
8003 else if (p->balance < 0)
8005 if (r->balance < 0)
8007 /* L rotation. */
8008 p->left = r->right;
8009 if (p->left)
8010 p->left->parent = p;
8011 r->right = p;
8013 p->balance = 0;
8014 r->balance = 0;
8016 s = p->parent;
8017 p->parent = r;
8018 r->parent = s;
8019 if (s)
8021 if (s->left == p)
8022 s->left = r;
8023 else
8024 s->right = r;
8026 else
8027 constructor_pending_elts = r;
8029 else
8031 /* LR rotation. */
8032 struct init_node *t = r->right;
8034 r->right = t->left;
8035 if (r->right)
8036 r->right->parent = r;
8037 t->left = r;
8039 p->left = t->right;
8040 if (p->left)
8041 p->left->parent = p;
8042 t->right = p;
8044 p->balance = t->balance < 0;
8045 r->balance = -(t->balance > 0);
8046 t->balance = 0;
8048 s = p->parent;
8049 p->parent = t;
8050 r->parent = t;
8051 t->parent = s;
8052 if (s)
8054 if (s->left == p)
8055 s->left = t;
8056 else
8057 s->right = t;
8059 else
8060 constructor_pending_elts = t;
8062 break;
8064 else
8066 /* p->balance == +1; growth of left side balances the node. */
8067 p->balance = 0;
8068 break;
8071 else /* r == p->right */
8073 if (p->balance == 0)
8074 /* Growth propagation from right side. */
8075 p->balance++;
8076 else if (p->balance > 0)
8078 if (r->balance > 0)
8080 /* R rotation. */
8081 p->right = r->left;
8082 if (p->right)
8083 p->right->parent = p;
8084 r->left = p;
8086 p->balance = 0;
8087 r->balance = 0;
8089 s = p->parent;
8090 p->parent = r;
8091 r->parent = s;
8092 if (s)
8094 if (s->left == p)
8095 s->left = r;
8096 else
8097 s->right = r;
8099 else
8100 constructor_pending_elts = r;
8102 else /* r->balance == -1 */
8104 /* RL rotation */
8105 struct init_node *t = r->left;
8107 r->left = t->right;
8108 if (r->left)
8109 r->left->parent = r;
8110 t->right = r;
8112 p->right = t->left;
8113 if (p->right)
8114 p->right->parent = p;
8115 t->left = p;
8117 r->balance = (t->balance < 0);
8118 p->balance = -(t->balance > 0);
8119 t->balance = 0;
8121 s = p->parent;
8122 p->parent = t;
8123 r->parent = t;
8124 t->parent = s;
8125 if (s)
8127 if (s->left == p)
8128 s->left = t;
8129 else
8130 s->right = t;
8132 else
8133 constructor_pending_elts = t;
8135 break;
8137 else
8139 /* p->balance == -1; growth of right side balances the node. */
8140 p->balance = 0;
8141 break;
8145 r = p;
8146 p = p->parent;
8150 /* Build AVL tree from a sorted chain. */
8152 static void
8153 set_nonincremental_init (struct obstack * braced_init_obstack)
8155 unsigned HOST_WIDE_INT ix;
8156 tree index, value;
8158 if (TREE_CODE (constructor_type) != RECORD_TYPE
8159 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8160 return;
8162 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8163 add_pending_init (input_location, index, value, NULL_TREE, true,
8164 braced_init_obstack);
8165 constructor_elements = NULL;
8166 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8168 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8169 /* Skip any nameless bit fields at the beginning. */
8170 while (constructor_unfilled_fields != 0
8171 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8172 && DECL_NAME (constructor_unfilled_fields) == 0)
8173 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8176 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8178 if (TYPE_DOMAIN (constructor_type))
8179 constructor_unfilled_index
8180 = convert (bitsizetype,
8181 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8182 else
8183 constructor_unfilled_index = bitsize_zero_node;
8185 constructor_incremental = 0;
8188 /* Build AVL tree from a string constant. */
8190 static void
8191 set_nonincremental_init_from_string (tree str,
8192 struct obstack * braced_init_obstack)
8194 tree value, purpose, type;
8195 HOST_WIDE_INT val[2];
8196 const char *p, *end;
8197 int byte, wchar_bytes, charwidth, bitpos;
8199 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8201 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8202 charwidth = TYPE_PRECISION (char_type_node);
8203 type = TREE_TYPE (constructor_type);
8204 p = TREE_STRING_POINTER (str);
8205 end = p + TREE_STRING_LENGTH (str);
8207 for (purpose = bitsize_zero_node;
8208 p < end
8209 && !(constructor_max_index
8210 && tree_int_cst_lt (constructor_max_index, purpose));
8211 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8213 if (wchar_bytes == 1)
8215 val[0] = (unsigned char) *p++;
8216 val[1] = 0;
8218 else
8220 val[1] = 0;
8221 val[0] = 0;
8222 for (byte = 0; byte < wchar_bytes; byte++)
8224 if (BYTES_BIG_ENDIAN)
8225 bitpos = (wchar_bytes - byte - 1) * charwidth;
8226 else
8227 bitpos = byte * charwidth;
8228 val[bitpos % HOST_BITS_PER_WIDE_INT]
8229 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8230 << (bitpos % HOST_BITS_PER_WIDE_INT);
8234 if (!TYPE_UNSIGNED (type))
8236 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8237 if (bitpos < HOST_BITS_PER_WIDE_INT)
8239 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8241 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8242 val[1] = -1;
8245 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8247 if (val[0] < 0)
8248 val[1] = -1;
8250 else if (val[1] & (((HOST_WIDE_INT) 1)
8251 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8252 val[1] |= ((HOST_WIDE_INT) -1)
8253 << (bitpos - HOST_BITS_PER_WIDE_INT);
8256 value = wide_int_to_tree (type,
8257 wide_int::from_array (val, 2,
8258 HOST_BITS_PER_WIDE_INT * 2));
8259 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8260 braced_init_obstack);
8263 constructor_incremental = 0;
8266 /* Return value of FIELD in pending initializer or zero if the field was
8267 not initialized yet. */
8269 static tree
8270 find_init_member (tree field, struct obstack * braced_init_obstack)
8272 struct init_node *p;
8274 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8276 if (constructor_incremental
8277 && tree_int_cst_lt (field, constructor_unfilled_index))
8278 set_nonincremental_init (braced_init_obstack);
8280 p = constructor_pending_elts;
8281 while (p)
8283 if (tree_int_cst_lt (field, p->purpose))
8284 p = p->left;
8285 else if (tree_int_cst_lt (p->purpose, field))
8286 p = p->right;
8287 else
8288 return p->value;
8291 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8293 tree bitpos = bit_position (field);
8295 if (constructor_incremental
8296 && (!constructor_unfilled_fields
8297 || tree_int_cst_lt (bitpos,
8298 bit_position (constructor_unfilled_fields))))
8299 set_nonincremental_init (braced_init_obstack);
8301 p = constructor_pending_elts;
8302 while (p)
8304 if (field == p->purpose)
8305 return p->value;
8306 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8307 p = p->left;
8308 else
8309 p = p->right;
8312 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8314 if (!vec_safe_is_empty (constructor_elements)
8315 && (constructor_elements->last ().index == field))
8316 return constructor_elements->last ().value;
8318 return 0;
8321 /* "Output" the next constructor element.
8322 At top level, really output it to assembler code now.
8323 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8324 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8325 TYPE is the data type that the containing data type wants here.
8326 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8327 If VALUE is a string constant, STRICT_STRING is true if it is
8328 unparenthesized or we should not warn here for it being parenthesized.
8329 For other types of VALUE, STRICT_STRING is not used.
8331 PENDING if non-nil means output pending elements that belong
8332 right after this element. (PENDING is normally 1;
8333 it is 0 while outputting pending elements, to avoid recursion.)
8335 IMPLICIT is true if value comes from pop_init_level (1),
8336 the new initializer has been merged with the existing one
8337 and thus no warnings should be emitted about overriding an
8338 existing initializer. */
8340 static void
8341 output_init_element (location_t loc, tree value, tree origtype,
8342 bool strict_string, tree type, tree field, int pending,
8343 bool implicit, struct obstack * braced_init_obstack)
8345 tree semantic_type = NULL_TREE;
8346 bool maybe_const = true;
8347 bool npc;
8349 if (type == error_mark_node || value == error_mark_node)
8351 constructor_erroneous = 1;
8352 return;
8354 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8355 && (TREE_CODE (value) == STRING_CST
8356 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8357 && !(TREE_CODE (value) == STRING_CST
8358 && TREE_CODE (type) == ARRAY_TYPE
8359 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8360 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8361 TYPE_MAIN_VARIANT (type)))
8362 value = array_to_pointer_conversion (input_location, value);
8364 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8365 && require_constant_value && pending)
8367 /* As an extension, allow initializing objects with static storage
8368 duration with compound literals (which are then treated just as
8369 the brace enclosed list they contain). */
8370 if (flag_isoc99)
8371 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8372 "constant");
8373 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8374 value = DECL_INITIAL (decl);
8377 npc = null_pointer_constant_p (value);
8378 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8380 semantic_type = TREE_TYPE (value);
8381 value = TREE_OPERAND (value, 0);
8383 value = c_fully_fold (value, require_constant_value, &maybe_const);
8385 if (value == error_mark_node)
8386 constructor_erroneous = 1;
8387 else if (!TREE_CONSTANT (value))
8388 constructor_constant = 0;
8389 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8390 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8391 || TREE_CODE (constructor_type) == UNION_TYPE)
8392 && DECL_C_BIT_FIELD (field)
8393 && TREE_CODE (value) != INTEGER_CST))
8394 constructor_simple = 0;
8395 if (!maybe_const)
8396 constructor_nonconst = 1;
8398 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8400 if (require_constant_value)
8402 error_init (loc, "initializer element is not constant");
8403 value = error_mark_node;
8405 else if (require_constant_elements)
8406 pedwarn (loc, OPT_Wpedantic,
8407 "initializer element is not computable at load time");
8409 else if (!maybe_const
8410 && (require_constant_value || require_constant_elements))
8411 pedwarn_init (loc, OPT_Wpedantic,
8412 "initializer element is not a constant expression");
8414 /* Issue -Wc++-compat warnings about initializing a bitfield with
8415 enum type. */
8416 if (warn_cxx_compat
8417 && field != NULL_TREE
8418 && TREE_CODE (field) == FIELD_DECL
8419 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8420 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8421 != TYPE_MAIN_VARIANT (type))
8422 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8424 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8425 if (checktype != error_mark_node
8426 && (TYPE_MAIN_VARIANT (checktype)
8427 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8428 warning_init (loc, OPT_Wc___compat,
8429 "enum conversion in initialization is invalid in C++");
8432 /* If this field is empty (and not at the end of structure),
8433 don't do anything other than checking the initializer. */
8434 if (field
8435 && (TREE_TYPE (field) == error_mark_node
8436 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8437 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8438 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8439 || DECL_CHAIN (field)))))
8440 return;
8442 if (semantic_type)
8443 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8444 value = digest_init (loc, type, value, origtype, npc, strict_string,
8445 require_constant_value);
8446 if (value == error_mark_node)
8448 constructor_erroneous = 1;
8449 return;
8451 if (require_constant_value || require_constant_elements)
8452 constant_expression_warning (value);
8454 /* If this element doesn't come next in sequence,
8455 put it on constructor_pending_elts. */
8456 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8457 && (!constructor_incremental
8458 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8460 if (constructor_incremental
8461 && tree_int_cst_lt (field, constructor_unfilled_index))
8462 set_nonincremental_init (braced_init_obstack);
8464 add_pending_init (loc, field, value, origtype, implicit,
8465 braced_init_obstack);
8466 return;
8468 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8469 && (!constructor_incremental
8470 || field != constructor_unfilled_fields))
8472 /* We do this for records but not for unions. In a union,
8473 no matter which field is specified, it can be initialized
8474 right away since it starts at the beginning of the union. */
8475 if (constructor_incremental)
8477 if (!constructor_unfilled_fields)
8478 set_nonincremental_init (braced_init_obstack);
8479 else
8481 tree bitpos, unfillpos;
8483 bitpos = bit_position (field);
8484 unfillpos = bit_position (constructor_unfilled_fields);
8486 if (tree_int_cst_lt (bitpos, unfillpos))
8487 set_nonincremental_init (braced_init_obstack);
8491 add_pending_init (loc, field, value, origtype, implicit,
8492 braced_init_obstack);
8493 return;
8495 else if (TREE_CODE (constructor_type) == UNION_TYPE
8496 && !vec_safe_is_empty (constructor_elements))
8498 if (!implicit)
8500 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8501 warning_init (loc, 0,
8502 "initialized field with side-effects overwritten");
8503 else if (warn_override_init)
8504 warning_init (loc, OPT_Woverride_init,
8505 "initialized field overwritten");
8508 /* We can have just one union field set. */
8509 constructor_elements = NULL;
8512 /* Otherwise, output this element either to
8513 constructor_elements or to the assembler file. */
8515 constructor_elt celt = {field, value};
8516 vec_safe_push (constructor_elements, celt);
8518 /* Advance the variable that indicates sequential elements output. */
8519 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8520 constructor_unfilled_index
8521 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8522 bitsize_one_node);
8523 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8525 constructor_unfilled_fields
8526 = DECL_CHAIN (constructor_unfilled_fields);
8528 /* Skip any nameless bit fields. */
8529 while (constructor_unfilled_fields != 0
8530 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8531 && DECL_NAME (constructor_unfilled_fields) == 0)
8532 constructor_unfilled_fields =
8533 DECL_CHAIN (constructor_unfilled_fields);
8535 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8536 constructor_unfilled_fields = 0;
8538 /* Now output any pending elements which have become next. */
8539 if (pending)
8540 output_pending_init_elements (0, braced_init_obstack);
8543 /* Output any pending elements which have become next.
8544 As we output elements, constructor_unfilled_{fields,index}
8545 advances, which may cause other elements to become next;
8546 if so, they too are output.
8548 If ALL is 0, we return when there are
8549 no more pending elements to output now.
8551 If ALL is 1, we output space as necessary so that
8552 we can output all the pending elements. */
8553 static void
8554 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8556 struct init_node *elt = constructor_pending_elts;
8557 tree next;
8559 retry:
8561 /* Look through the whole pending tree.
8562 If we find an element that should be output now,
8563 output it. Otherwise, set NEXT to the element
8564 that comes first among those still pending. */
8566 next = 0;
8567 while (elt)
8569 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8571 if (tree_int_cst_equal (elt->purpose,
8572 constructor_unfilled_index))
8573 output_init_element (input_location, elt->value, elt->origtype,
8574 true, TREE_TYPE (constructor_type),
8575 constructor_unfilled_index, 0, false,
8576 braced_init_obstack);
8577 else if (tree_int_cst_lt (constructor_unfilled_index,
8578 elt->purpose))
8580 /* Advance to the next smaller node. */
8581 if (elt->left)
8582 elt = elt->left;
8583 else
8585 /* We have reached the smallest node bigger than the
8586 current unfilled index. Fill the space first. */
8587 next = elt->purpose;
8588 break;
8591 else
8593 /* Advance to the next bigger node. */
8594 if (elt->right)
8595 elt = elt->right;
8596 else
8598 /* We have reached the biggest node in a subtree. Find
8599 the parent of it, which is the next bigger node. */
8600 while (elt->parent && elt->parent->right == elt)
8601 elt = elt->parent;
8602 elt = elt->parent;
8603 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8604 elt->purpose))
8606 next = elt->purpose;
8607 break;
8612 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8613 || TREE_CODE (constructor_type) == UNION_TYPE)
8615 tree ctor_unfilled_bitpos, elt_bitpos;
8617 /* If the current record is complete we are done. */
8618 if (constructor_unfilled_fields == 0)
8619 break;
8621 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8622 elt_bitpos = bit_position (elt->purpose);
8623 /* We can't compare fields here because there might be empty
8624 fields in between. */
8625 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8627 constructor_unfilled_fields = elt->purpose;
8628 output_init_element (input_location, elt->value, elt->origtype,
8629 true, TREE_TYPE (elt->purpose),
8630 elt->purpose, 0, false,
8631 braced_init_obstack);
8633 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8635 /* Advance to the next smaller node. */
8636 if (elt->left)
8637 elt = elt->left;
8638 else
8640 /* We have reached the smallest node bigger than the
8641 current unfilled field. Fill the space first. */
8642 next = elt->purpose;
8643 break;
8646 else
8648 /* Advance to the next bigger node. */
8649 if (elt->right)
8650 elt = elt->right;
8651 else
8653 /* We have reached the biggest node in a subtree. Find
8654 the parent of it, which is the next bigger node. */
8655 while (elt->parent && elt->parent->right == elt)
8656 elt = elt->parent;
8657 elt = elt->parent;
8658 if (elt
8659 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8660 bit_position (elt->purpose))))
8662 next = elt->purpose;
8663 break;
8670 /* Ordinarily return, but not if we want to output all
8671 and there are elements left. */
8672 if (!(all && next != 0))
8673 return;
8675 /* If it's not incremental, just skip over the gap, so that after
8676 jumping to retry we will output the next successive element. */
8677 if (TREE_CODE (constructor_type) == RECORD_TYPE
8678 || TREE_CODE (constructor_type) == UNION_TYPE)
8679 constructor_unfilled_fields = next;
8680 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8681 constructor_unfilled_index = next;
8683 /* ELT now points to the node in the pending tree with the next
8684 initializer to output. */
8685 goto retry;
8688 /* Add one non-braced element to the current constructor level.
8689 This adjusts the current position within the constructor's type.
8690 This may also start or terminate implicit levels
8691 to handle a partly-braced initializer.
8693 Once this has found the correct level for the new element,
8694 it calls output_init_element.
8696 IMPLICIT is true if value comes from pop_init_level (1),
8697 the new initializer has been merged with the existing one
8698 and thus no warnings should be emitted about overriding an
8699 existing initializer. */
8701 void
8702 process_init_element (location_t loc, struct c_expr value, bool implicit,
8703 struct obstack * braced_init_obstack)
8705 tree orig_value = value.value;
8706 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8707 bool strict_string = value.original_code == STRING_CST;
8708 bool was_designated = designator_depth != 0;
8710 designator_depth = 0;
8711 designator_erroneous = 0;
8713 if (!implicit && value.value && !integer_zerop (value.value))
8714 constructor_zeroinit = 0;
8716 /* Handle superfluous braces around string cst as in
8717 char x[] = {"foo"}; */
8718 if (string_flag
8719 && constructor_type
8720 && !was_designated
8721 && TREE_CODE (constructor_type) == ARRAY_TYPE
8722 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8723 && integer_zerop (constructor_unfilled_index))
8725 if (constructor_stack->replacement_value.value)
8726 error_init (loc, "excess elements in char array initializer");
8727 constructor_stack->replacement_value = value;
8728 return;
8731 if (constructor_stack->replacement_value.value != 0)
8733 error_init (loc, "excess elements in struct initializer");
8734 return;
8737 /* Ignore elements of a brace group if it is entirely superfluous
8738 and has already been diagnosed. */
8739 if (constructor_type == 0)
8740 return;
8742 if (!implicit && warn_designated_init && !was_designated
8743 && TREE_CODE (constructor_type) == RECORD_TYPE
8744 && lookup_attribute ("designated_init",
8745 TYPE_ATTRIBUTES (constructor_type)))
8746 warning_init (loc,
8747 OPT_Wdesignated_init,
8748 "positional initialization of field "
8749 "in %<struct%> declared with %<designated_init%> attribute");
8751 /* If we've exhausted any levels that didn't have braces,
8752 pop them now. */
8753 while (constructor_stack->implicit)
8755 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8756 || TREE_CODE (constructor_type) == UNION_TYPE)
8757 && constructor_fields == 0)
8758 process_init_element (loc,
8759 pop_init_level (loc, 1, braced_init_obstack),
8760 true, braced_init_obstack);
8761 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8762 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8763 && constructor_max_index
8764 && tree_int_cst_lt (constructor_max_index,
8765 constructor_index))
8766 process_init_element (loc,
8767 pop_init_level (loc, 1, braced_init_obstack),
8768 true, braced_init_obstack);
8769 else
8770 break;
8773 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8774 if (constructor_range_stack)
8776 /* If value is a compound literal and we'll be just using its
8777 content, don't put it into a SAVE_EXPR. */
8778 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8779 || !require_constant_value
8780 || flag_isoc99)
8782 tree semantic_type = NULL_TREE;
8783 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8785 semantic_type = TREE_TYPE (value.value);
8786 value.value = TREE_OPERAND (value.value, 0);
8788 value.value = c_save_expr (value.value);
8789 if (semantic_type)
8790 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8791 value.value);
8795 while (1)
8797 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8799 tree fieldtype;
8800 enum tree_code fieldcode;
8802 if (constructor_fields == 0)
8804 pedwarn_init (loc, 0, "excess elements in struct initializer");
8805 break;
8808 fieldtype = TREE_TYPE (constructor_fields);
8809 if (fieldtype != error_mark_node)
8810 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8811 fieldcode = TREE_CODE (fieldtype);
8813 /* Error for non-static initialization of a flexible array member. */
8814 if (fieldcode == ARRAY_TYPE
8815 && !require_constant_value
8816 && TYPE_SIZE (fieldtype) == NULL_TREE
8817 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8819 error_init (loc, "non-static initialization of a flexible "
8820 "array member");
8821 break;
8824 /* Error for initialization of a flexible array member with
8825 a string constant if the structure is in an array. E.g.:
8826 struct S { int x; char y[]; };
8827 struct S s[] = { { 1, "foo" } };
8828 is invalid. */
8829 if (string_flag
8830 && fieldcode == ARRAY_TYPE
8831 && constructor_depth > 1
8832 && TYPE_SIZE (fieldtype) == NULL_TREE
8833 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8835 bool in_array_p = false;
8836 for (struct constructor_stack *p = constructor_stack;
8837 p && p->type; p = p->next)
8838 if (TREE_CODE (p->type) == ARRAY_TYPE)
8840 in_array_p = true;
8841 break;
8843 if (in_array_p)
8845 error_init (loc, "initialization of flexible array "
8846 "member in a nested context");
8847 break;
8851 /* Accept a string constant to initialize a subarray. */
8852 if (value.value != 0
8853 && fieldcode == ARRAY_TYPE
8854 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8855 && string_flag)
8856 value.value = orig_value;
8857 /* Otherwise, if we have come to a subaggregate,
8858 and we don't have an element of its type, push into it. */
8859 else if (value.value != 0
8860 && value.value != error_mark_node
8861 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8862 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8863 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8865 push_init_level (loc, 1, braced_init_obstack);
8866 continue;
8869 if (value.value)
8871 push_member_name (constructor_fields);
8872 output_init_element (loc, value.value, value.original_type,
8873 strict_string, fieldtype,
8874 constructor_fields, 1, implicit,
8875 braced_init_obstack);
8876 RESTORE_SPELLING_DEPTH (constructor_depth);
8878 else
8879 /* Do the bookkeeping for an element that was
8880 directly output as a constructor. */
8882 /* For a record, keep track of end position of last field. */
8883 if (DECL_SIZE (constructor_fields))
8884 constructor_bit_index
8885 = size_binop_loc (input_location, PLUS_EXPR,
8886 bit_position (constructor_fields),
8887 DECL_SIZE (constructor_fields));
8889 /* If the current field was the first one not yet written out,
8890 it isn't now, so update. */
8891 if (constructor_unfilled_fields == constructor_fields)
8893 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8894 /* Skip any nameless bit fields. */
8895 while (constructor_unfilled_fields != 0
8896 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8897 && DECL_NAME (constructor_unfilled_fields) == 0)
8898 constructor_unfilled_fields =
8899 DECL_CHAIN (constructor_unfilled_fields);
8903 constructor_fields = DECL_CHAIN (constructor_fields);
8904 /* Skip any nameless bit fields at the beginning. */
8905 while (constructor_fields != 0
8906 && DECL_C_BIT_FIELD (constructor_fields)
8907 && DECL_NAME (constructor_fields) == 0)
8908 constructor_fields = DECL_CHAIN (constructor_fields);
8910 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8912 tree fieldtype;
8913 enum tree_code fieldcode;
8915 if (constructor_fields == 0)
8917 pedwarn_init (loc, 0,
8918 "excess elements in union initializer");
8919 break;
8922 fieldtype = TREE_TYPE (constructor_fields);
8923 if (fieldtype != error_mark_node)
8924 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8925 fieldcode = TREE_CODE (fieldtype);
8927 /* Warn that traditional C rejects initialization of unions.
8928 We skip the warning if the value is zero. This is done
8929 under the assumption that the zero initializer in user
8930 code appears conditioned on e.g. __STDC__ to avoid
8931 "missing initializer" warnings and relies on default
8932 initialization to zero in the traditional C case.
8933 We also skip the warning if the initializer is designated,
8934 again on the assumption that this must be conditional on
8935 __STDC__ anyway (and we've already complained about the
8936 member-designator already). */
8937 if (!in_system_header_at (input_location) && !constructor_designated
8938 && !(value.value && (integer_zerop (value.value)
8939 || real_zerop (value.value))))
8940 warning (OPT_Wtraditional, "traditional C rejects initialization "
8941 "of unions");
8943 /* Accept a string constant to initialize a subarray. */
8944 if (value.value != 0
8945 && fieldcode == ARRAY_TYPE
8946 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8947 && string_flag)
8948 value.value = orig_value;
8949 /* Otherwise, if we have come to a subaggregate,
8950 and we don't have an element of its type, push into it. */
8951 else if (value.value != 0
8952 && value.value != error_mark_node
8953 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8954 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8955 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8957 push_init_level (loc, 1, braced_init_obstack);
8958 continue;
8961 if (value.value)
8963 push_member_name (constructor_fields);
8964 output_init_element (loc, value.value, value.original_type,
8965 strict_string, fieldtype,
8966 constructor_fields, 1, implicit,
8967 braced_init_obstack);
8968 RESTORE_SPELLING_DEPTH (constructor_depth);
8970 else
8971 /* Do the bookkeeping for an element that was
8972 directly output as a constructor. */
8974 constructor_bit_index = DECL_SIZE (constructor_fields);
8975 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8978 constructor_fields = 0;
8980 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8982 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8983 enum tree_code eltcode = TREE_CODE (elttype);
8985 /* Accept a string constant to initialize a subarray. */
8986 if (value.value != 0
8987 && eltcode == ARRAY_TYPE
8988 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8989 && string_flag)
8990 value.value = orig_value;
8991 /* Otherwise, if we have come to a subaggregate,
8992 and we don't have an element of its type, push into it. */
8993 else if (value.value != 0
8994 && value.value != error_mark_node
8995 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8996 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8997 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8999 push_init_level (loc, 1, braced_init_obstack);
9000 continue;
9003 if (constructor_max_index != 0
9004 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9005 || integer_all_onesp (constructor_max_index)))
9007 pedwarn_init (loc, 0,
9008 "excess elements in array initializer");
9009 break;
9012 /* Now output the actual element. */
9013 if (value.value)
9015 push_array_bounds (tree_to_uhwi (constructor_index));
9016 output_init_element (loc, value.value, value.original_type,
9017 strict_string, elttype,
9018 constructor_index, 1, implicit,
9019 braced_init_obstack);
9020 RESTORE_SPELLING_DEPTH (constructor_depth);
9023 constructor_index
9024 = size_binop_loc (input_location, PLUS_EXPR,
9025 constructor_index, bitsize_one_node);
9027 if (!value.value)
9028 /* If we are doing the bookkeeping for an element that was
9029 directly output as a constructor, we must update
9030 constructor_unfilled_index. */
9031 constructor_unfilled_index = constructor_index;
9033 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9035 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9037 /* Do a basic check of initializer size. Note that vectors
9038 always have a fixed size derived from their type. */
9039 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9041 pedwarn_init (loc, 0,
9042 "excess elements in vector initializer");
9043 break;
9046 /* Now output the actual element. */
9047 if (value.value)
9049 if (TREE_CODE (value.value) == VECTOR_CST)
9050 elttype = TYPE_MAIN_VARIANT (constructor_type);
9051 output_init_element (loc, value.value, value.original_type,
9052 strict_string, elttype,
9053 constructor_index, 1, implicit,
9054 braced_init_obstack);
9057 constructor_index
9058 = size_binop_loc (input_location,
9059 PLUS_EXPR, constructor_index, bitsize_one_node);
9061 if (!value.value)
9062 /* If we are doing the bookkeeping for an element that was
9063 directly output as a constructor, we must update
9064 constructor_unfilled_index. */
9065 constructor_unfilled_index = constructor_index;
9068 /* Handle the sole element allowed in a braced initializer
9069 for a scalar variable. */
9070 else if (constructor_type != error_mark_node
9071 && constructor_fields == 0)
9073 pedwarn_init (loc, 0,
9074 "excess elements in scalar initializer");
9075 break;
9077 else
9079 if (value.value)
9080 output_init_element (loc, value.value, value.original_type,
9081 strict_string, constructor_type,
9082 NULL_TREE, 1, implicit,
9083 braced_init_obstack);
9084 constructor_fields = 0;
9087 /* Handle range initializers either at this level or anywhere higher
9088 in the designator stack. */
9089 if (constructor_range_stack)
9091 struct constructor_range_stack *p, *range_stack;
9092 int finish = 0;
9094 range_stack = constructor_range_stack;
9095 constructor_range_stack = 0;
9096 while (constructor_stack != range_stack->stack)
9098 gcc_assert (constructor_stack->implicit);
9099 process_init_element (loc,
9100 pop_init_level (loc, 1,
9101 braced_init_obstack),
9102 true, braced_init_obstack);
9104 for (p = range_stack;
9105 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9106 p = p->prev)
9108 gcc_assert (constructor_stack->implicit);
9109 process_init_element (loc,
9110 pop_init_level (loc, 1,
9111 braced_init_obstack),
9112 true, braced_init_obstack);
9115 p->index = size_binop_loc (input_location,
9116 PLUS_EXPR, p->index, bitsize_one_node);
9117 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9118 finish = 1;
9120 while (1)
9122 constructor_index = p->index;
9123 constructor_fields = p->fields;
9124 if (finish && p->range_end && p->index == p->range_start)
9126 finish = 0;
9127 p->prev = 0;
9129 p = p->next;
9130 if (!p)
9131 break;
9132 push_init_level (loc, 2, braced_init_obstack);
9133 p->stack = constructor_stack;
9134 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9135 p->index = p->range_start;
9138 if (!finish)
9139 constructor_range_stack = range_stack;
9140 continue;
9143 break;
9146 constructor_range_stack = 0;
9149 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9150 (guaranteed to be 'volatile' or null) and ARGS (represented using
9151 an ASM_EXPR node). */
9152 tree
9153 build_asm_stmt (tree cv_qualifier, tree args)
9155 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9156 ASM_VOLATILE_P (args) = 1;
9157 return add_stmt (args);
9160 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9161 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9162 SIMPLE indicates whether there was anything at all after the
9163 string in the asm expression -- asm("blah") and asm("blah" : )
9164 are subtly different. We use a ASM_EXPR node to represent this. */
9165 tree
9166 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9167 tree clobbers, tree labels, bool simple)
9169 tree tail;
9170 tree args;
9171 int i;
9172 const char *constraint;
9173 const char **oconstraints;
9174 bool allows_mem, allows_reg, is_inout;
9175 int ninputs, noutputs;
9177 ninputs = list_length (inputs);
9178 noutputs = list_length (outputs);
9179 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9181 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9183 /* Remove output conversions that change the type but not the mode. */
9184 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9186 tree output = TREE_VALUE (tail);
9188 output = c_fully_fold (output, false, NULL);
9190 /* ??? Really, this should not be here. Users should be using a
9191 proper lvalue, dammit. But there's a long history of using casts
9192 in the output operands. In cases like longlong.h, this becomes a
9193 primitive form of typechecking -- if the cast can be removed, then
9194 the output operand had a type of the proper width; otherwise we'll
9195 get an error. Gross, but ... */
9196 STRIP_NOPS (output);
9198 if (!lvalue_or_else (loc, output, lv_asm))
9199 output = error_mark_node;
9201 if (output != error_mark_node
9202 && (TREE_READONLY (output)
9203 || TYPE_READONLY (TREE_TYPE (output))
9204 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9205 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9206 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9207 readonly_error (loc, output, lv_asm);
9209 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9210 oconstraints[i] = constraint;
9212 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9213 &allows_mem, &allows_reg, &is_inout))
9215 /* If the operand is going to end up in memory,
9216 mark it addressable. */
9217 if (!allows_reg && !c_mark_addressable (output))
9218 output = error_mark_node;
9219 if (!(!allows_reg && allows_mem)
9220 && output != error_mark_node
9221 && VOID_TYPE_P (TREE_TYPE (output)))
9223 error_at (loc, "invalid use of void expression");
9224 output = error_mark_node;
9227 else
9228 output = error_mark_node;
9230 TREE_VALUE (tail) = output;
9233 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9235 tree input;
9237 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9238 input = TREE_VALUE (tail);
9240 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9241 oconstraints, &allows_mem, &allows_reg))
9243 /* If the operand is going to end up in memory,
9244 mark it addressable. */
9245 if (!allows_reg && allows_mem)
9247 input = c_fully_fold (input, false, NULL);
9249 /* Strip the nops as we allow this case. FIXME, this really
9250 should be rejected or made deprecated. */
9251 STRIP_NOPS (input);
9252 if (!c_mark_addressable (input))
9253 input = error_mark_node;
9255 else
9257 struct c_expr expr;
9258 memset (&expr, 0, sizeof (expr));
9259 expr.value = input;
9260 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9261 input = c_fully_fold (expr.value, false, NULL);
9263 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9265 error_at (loc, "invalid use of void expression");
9266 input = error_mark_node;
9270 else
9271 input = error_mark_node;
9273 TREE_VALUE (tail) = input;
9276 /* ASMs with labels cannot have outputs. This should have been
9277 enforced by the parser. */
9278 gcc_assert (outputs == NULL || labels == NULL);
9280 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9282 /* asm statements without outputs, including simple ones, are treated
9283 as volatile. */
9284 ASM_INPUT_P (args) = simple;
9285 ASM_VOLATILE_P (args) = (noutputs == 0);
9287 return args;
9290 /* Generate a goto statement to LABEL. LOC is the location of the
9291 GOTO. */
9293 tree
9294 c_finish_goto_label (location_t loc, tree label)
9296 tree decl = lookup_label_for_goto (loc, label);
9297 if (!decl)
9298 return NULL_TREE;
9299 TREE_USED (decl) = 1;
9301 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9302 SET_EXPR_LOCATION (t, loc);
9303 return add_stmt (t);
9307 /* Generate a computed goto statement to EXPR. LOC is the location of
9308 the GOTO. */
9310 tree
9311 c_finish_goto_ptr (location_t loc, tree expr)
9313 tree t;
9314 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9315 expr = c_fully_fold (expr, false, NULL);
9316 expr = convert (ptr_type_node, expr);
9317 t = build1 (GOTO_EXPR, void_type_node, expr);
9318 SET_EXPR_LOCATION (t, loc);
9319 return add_stmt (t);
9322 /* Generate a C `return' statement. RETVAL is the expression for what
9323 to return, or a null pointer for `return;' with no value. LOC is
9324 the location of the return statement, or the location of the expression,
9325 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9326 is the original type of RETVAL. */
9328 tree
9329 c_finish_return (location_t loc, tree retval, tree origtype)
9331 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9332 bool no_warning = false;
9333 bool npc = false;
9334 size_t rank = 0;
9336 if (TREE_THIS_VOLATILE (current_function_decl))
9337 warning_at (loc, 0,
9338 "function declared %<noreturn%> has a %<return%> statement");
9340 if (flag_cilkplus && contains_array_notation_expr (retval))
9342 /* Array notations are allowed in a return statement if it is inside a
9343 built-in array notation reduction function. */
9344 if (!find_rank (loc, retval, retval, false, &rank))
9345 return error_mark_node;
9346 if (rank >= 1)
9348 error_at (loc, "array notation expression cannot be used as a "
9349 "return value");
9350 return error_mark_node;
9353 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9355 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9356 "allowed");
9357 return error_mark_node;
9359 if (retval)
9361 tree semantic_type = NULL_TREE;
9362 npc = null_pointer_constant_p (retval);
9363 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9365 semantic_type = TREE_TYPE (retval);
9366 retval = TREE_OPERAND (retval, 0);
9368 retval = c_fully_fold (retval, false, NULL);
9369 if (semantic_type)
9370 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9373 if (!retval)
9375 current_function_returns_null = 1;
9376 if ((warn_return_type || flag_isoc99)
9377 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9379 if (flag_isoc99)
9380 pedwarn (loc, 0, "%<return%> with no value, in "
9381 "function returning non-void");
9382 else
9383 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9384 "in function returning non-void");
9385 no_warning = true;
9388 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9390 current_function_returns_null = 1;
9391 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9392 pedwarn (loc, 0,
9393 "%<return%> with a value, in function returning void");
9394 else
9395 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9396 "%<return%> with expression, in function returning void");
9398 else
9400 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9401 retval, origtype, ic_return,
9402 npc, NULL_TREE, NULL_TREE, 0);
9403 tree res = DECL_RESULT (current_function_decl);
9404 tree inner;
9405 bool save;
9407 current_function_returns_value = 1;
9408 if (t == error_mark_node)
9409 return NULL_TREE;
9411 save = in_late_binary_op;
9412 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9413 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9414 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9415 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9416 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9417 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9418 in_late_binary_op = true;
9419 inner = t = convert (TREE_TYPE (res), t);
9420 in_late_binary_op = save;
9422 /* Strip any conversions, additions, and subtractions, and see if
9423 we are returning the address of a local variable. Warn if so. */
9424 while (1)
9426 switch (TREE_CODE (inner))
9428 CASE_CONVERT:
9429 case NON_LVALUE_EXPR:
9430 case PLUS_EXPR:
9431 case POINTER_PLUS_EXPR:
9432 inner = TREE_OPERAND (inner, 0);
9433 continue;
9435 case MINUS_EXPR:
9436 /* If the second operand of the MINUS_EXPR has a pointer
9437 type (or is converted from it), this may be valid, so
9438 don't give a warning. */
9440 tree op1 = TREE_OPERAND (inner, 1);
9442 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9443 && (CONVERT_EXPR_P (op1)
9444 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9445 op1 = TREE_OPERAND (op1, 0);
9447 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9448 break;
9450 inner = TREE_OPERAND (inner, 0);
9451 continue;
9454 case ADDR_EXPR:
9455 inner = TREE_OPERAND (inner, 0);
9457 while (REFERENCE_CLASS_P (inner)
9458 && TREE_CODE (inner) != INDIRECT_REF)
9459 inner = TREE_OPERAND (inner, 0);
9461 if (DECL_P (inner)
9462 && !DECL_EXTERNAL (inner)
9463 && !TREE_STATIC (inner)
9464 && DECL_CONTEXT (inner) == current_function_decl)
9466 if (TREE_CODE (inner) == LABEL_DECL)
9467 warning_at (loc, OPT_Wreturn_local_addr,
9468 "function returns address of label");
9469 else
9471 warning_at (loc, OPT_Wreturn_local_addr,
9472 "function returns address of local variable");
9473 tree zero = build_zero_cst (TREE_TYPE (res));
9474 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9477 break;
9479 default:
9480 break;
9483 break;
9486 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9487 SET_EXPR_LOCATION (retval, loc);
9489 if (warn_sequence_point)
9490 verify_sequence_points (retval);
9493 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9494 TREE_NO_WARNING (ret_stmt) |= no_warning;
9495 return add_stmt (ret_stmt);
9498 struct c_switch {
9499 /* The SWITCH_EXPR being built. */
9500 tree switch_expr;
9502 /* The original type of the testing expression, i.e. before the
9503 default conversion is applied. */
9504 tree orig_type;
9506 /* A splay-tree mapping the low element of a case range to the high
9507 element, or NULL_TREE if there is no high element. Used to
9508 determine whether or not a new case label duplicates an old case
9509 label. We need a tree, rather than simply a hash table, because
9510 of the GNU case range extension. */
9511 splay_tree cases;
9513 /* The bindings at the point of the switch. This is used for
9514 warnings crossing decls when branching to a case label. */
9515 struct c_spot_bindings *bindings;
9517 /* The next node on the stack. */
9518 struct c_switch *next;
9521 /* A stack of the currently active switch statements. The innermost
9522 switch statement is on the top of the stack. There is no need to
9523 mark the stack for garbage collection because it is only active
9524 during the processing of the body of a function, and we never
9525 collect at that point. */
9527 struct c_switch *c_switch_stack;
9529 /* Start a C switch statement, testing expression EXP. Return the new
9530 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9531 SWITCH_COND_LOC is the location of the switch's condition.
9532 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9534 tree
9535 c_start_case (location_t switch_loc,
9536 location_t switch_cond_loc,
9537 tree exp, bool explicit_cast_p)
9539 tree orig_type = error_mark_node;
9540 struct c_switch *cs;
9542 if (exp != error_mark_node)
9544 orig_type = TREE_TYPE (exp);
9546 if (!INTEGRAL_TYPE_P (orig_type))
9548 if (orig_type != error_mark_node)
9550 error_at (switch_cond_loc, "switch quantity not an integer");
9551 orig_type = error_mark_node;
9553 exp = integer_zero_node;
9555 else
9557 tree type = TYPE_MAIN_VARIANT (orig_type);
9558 tree e = exp;
9560 /* Warn if the condition has boolean value. */
9561 while (TREE_CODE (e) == COMPOUND_EXPR)
9562 e = TREE_OPERAND (e, 1);
9564 if ((TREE_CODE (type) == BOOLEAN_TYPE
9565 || truth_value_p (TREE_CODE (e)))
9566 /* Explicit cast to int suppresses this warning. */
9567 && !(TREE_CODE (type) == INTEGER_TYPE
9568 && explicit_cast_p))
9569 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9570 "switch condition has boolean value");
9572 if (!in_system_header_at (input_location)
9573 && (type == long_integer_type_node
9574 || type == long_unsigned_type_node))
9575 warning_at (switch_cond_loc,
9576 OPT_Wtraditional, "%<long%> switch expression not "
9577 "converted to %<int%> in ISO C");
9579 exp = c_fully_fold (exp, false, NULL);
9580 exp = default_conversion (exp);
9582 if (warn_sequence_point)
9583 verify_sequence_points (exp);
9587 /* Add this new SWITCH_EXPR to the stack. */
9588 cs = XNEW (struct c_switch);
9589 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9590 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9591 cs->orig_type = orig_type;
9592 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9593 cs->bindings = c_get_switch_bindings ();
9594 cs->next = c_switch_stack;
9595 c_switch_stack = cs;
9597 return add_stmt (cs->switch_expr);
9600 /* Process a case label at location LOC. */
9602 tree
9603 do_case (location_t loc, tree low_value, tree high_value)
9605 tree label = NULL_TREE;
9607 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9609 low_value = c_fully_fold (low_value, false, NULL);
9610 if (TREE_CODE (low_value) == INTEGER_CST)
9611 pedwarn (loc, OPT_Wpedantic,
9612 "case label is not an integer constant expression");
9615 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9617 high_value = c_fully_fold (high_value, false, NULL);
9618 if (TREE_CODE (high_value) == INTEGER_CST)
9619 pedwarn (input_location, OPT_Wpedantic,
9620 "case label is not an integer constant expression");
9623 if (c_switch_stack == NULL)
9625 if (low_value)
9626 error_at (loc, "case label not within a switch statement");
9627 else
9628 error_at (loc, "%<default%> label not within a switch statement");
9629 return NULL_TREE;
9632 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9633 EXPR_LOCATION (c_switch_stack->switch_expr),
9634 loc))
9635 return NULL_TREE;
9637 label = c_add_case_label (loc, c_switch_stack->cases,
9638 SWITCH_COND (c_switch_stack->switch_expr),
9639 c_switch_stack->orig_type,
9640 low_value, high_value);
9641 if (label == error_mark_node)
9642 label = NULL_TREE;
9643 return label;
9646 /* Finish the switch statement. TYPE is the original type of the
9647 controlling expression of the switch, or NULL_TREE. */
9649 void
9650 c_finish_case (tree body, tree type)
9652 struct c_switch *cs = c_switch_stack;
9653 location_t switch_location;
9655 SWITCH_BODY (cs->switch_expr) = body;
9657 /* Emit warnings as needed. */
9658 switch_location = EXPR_LOCATION (cs->switch_expr);
9659 c_do_switch_warnings (cs->cases, switch_location,
9660 type ? type : TREE_TYPE (cs->switch_expr),
9661 SWITCH_COND (cs->switch_expr));
9663 /* Pop the stack. */
9664 c_switch_stack = cs->next;
9665 splay_tree_delete (cs->cases);
9666 c_release_switch_bindings (cs->bindings);
9667 XDELETE (cs);
9670 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9671 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9672 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9673 statement, and was not surrounded with parenthesis. */
9675 void
9676 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9677 tree else_block, bool nested_if)
9679 tree stmt;
9681 /* If the condition has array notations, then the rank of the then_block and
9682 else_block must be either 0 or be equal to the rank of the condition. If
9683 the condition does not have array notations then break them up as it is
9684 broken up in a normal expression. */
9685 if (flag_cilkplus && contains_array_notation_expr (cond))
9687 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9688 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9689 return;
9690 if (then_block
9691 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9692 return;
9693 if (else_block
9694 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9695 return;
9696 if (cond_rank != then_rank && then_rank != 0)
9698 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9699 " and the then-block");
9700 return;
9702 else if (cond_rank != else_rank && else_rank != 0)
9704 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9705 " and the else-block");
9706 return;
9709 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9710 if (warn_parentheses && nested_if && else_block == NULL)
9712 tree inner_if = then_block;
9714 /* We know from the grammar productions that there is an IF nested
9715 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9716 it might not be exactly THEN_BLOCK, but should be the last
9717 non-container statement within. */
9718 while (1)
9719 switch (TREE_CODE (inner_if))
9721 case COND_EXPR:
9722 goto found;
9723 case BIND_EXPR:
9724 inner_if = BIND_EXPR_BODY (inner_if);
9725 break;
9726 case STATEMENT_LIST:
9727 inner_if = expr_last (then_block);
9728 break;
9729 case TRY_FINALLY_EXPR:
9730 case TRY_CATCH_EXPR:
9731 inner_if = TREE_OPERAND (inner_if, 0);
9732 break;
9733 default:
9734 gcc_unreachable ();
9736 found:
9738 if (COND_EXPR_ELSE (inner_if))
9739 warning_at (if_locus, OPT_Wparentheses,
9740 "suggest explicit braces to avoid ambiguous %<else%>");
9743 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9744 SET_EXPR_LOCATION (stmt, if_locus);
9745 add_stmt (stmt);
9748 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9749 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9750 is false for DO loops. INCR is the FOR increment expression. BODY is
9751 the statement controlled by the loop. BLAB is the break label. CLAB is
9752 the continue label. Everything is allowed to be NULL. */
9754 void
9755 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9756 tree blab, tree clab, bool cond_is_first)
9758 tree entry = NULL, exit = NULL, t;
9760 /* In theory could forbid cilk spawn for loop increment expression,
9761 but it should work just fine. */
9763 /* If the condition is zero don't generate a loop construct. */
9764 if (cond && integer_zerop (cond))
9766 if (cond_is_first)
9768 t = build_and_jump (&blab);
9769 SET_EXPR_LOCATION (t, start_locus);
9770 add_stmt (t);
9773 else
9775 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9777 /* If we have an exit condition, then we build an IF with gotos either
9778 out of the loop, or to the top of it. If there's no exit condition,
9779 then we just build a jump back to the top. */
9780 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9782 if (cond && !integer_nonzerop (cond))
9784 /* Canonicalize the loop condition to the end. This means
9785 generating a branch to the loop condition. Reuse the
9786 continue label, if possible. */
9787 if (cond_is_first)
9789 if (incr || !clab)
9791 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9792 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9794 else
9795 t = build1 (GOTO_EXPR, void_type_node, clab);
9796 SET_EXPR_LOCATION (t, start_locus);
9797 add_stmt (t);
9800 t = build_and_jump (&blab);
9801 if (cond_is_first)
9802 exit = fold_build3_loc (start_locus,
9803 COND_EXPR, void_type_node, cond, exit, t);
9804 else
9805 exit = fold_build3_loc (input_location,
9806 COND_EXPR, void_type_node, cond, exit, t);
9809 add_stmt (top);
9812 if (body)
9813 add_stmt (body);
9814 if (clab)
9815 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9816 if (incr)
9817 add_stmt (incr);
9818 if (entry)
9819 add_stmt (entry);
9820 if (exit)
9821 add_stmt (exit);
9822 if (blab)
9823 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9826 tree
9827 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9829 bool skip;
9830 tree label = *label_p;
9832 /* In switch statements break is sometimes stylistically used after
9833 a return statement. This can lead to spurious warnings about
9834 control reaching the end of a non-void function when it is
9835 inlined. Note that we are calling block_may_fallthru with
9836 language specific tree nodes; this works because
9837 block_may_fallthru returns true when given something it does not
9838 understand. */
9839 skip = !block_may_fallthru (cur_stmt_list);
9841 if (!label)
9843 if (!skip)
9844 *label_p = label = create_artificial_label (loc);
9846 else if (TREE_CODE (label) == LABEL_DECL)
9848 else switch (TREE_INT_CST_LOW (label))
9850 case 0:
9851 if (is_break)
9852 error_at (loc, "break statement not within loop or switch");
9853 else
9854 error_at (loc, "continue statement not within a loop");
9855 return NULL_TREE;
9857 case 1:
9858 gcc_assert (is_break);
9859 error_at (loc, "break statement used with OpenMP for loop");
9860 return NULL_TREE;
9862 case 2:
9863 if (is_break)
9864 error ("break statement within %<#pragma simd%> loop body");
9865 else
9866 error ("continue statement within %<#pragma simd%> loop body");
9867 return NULL_TREE;
9869 default:
9870 gcc_unreachable ();
9873 if (skip)
9874 return NULL_TREE;
9876 if (!is_break)
9877 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9879 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9882 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9884 static void
9885 emit_side_effect_warnings (location_t loc, tree expr)
9887 if (expr == error_mark_node)
9889 else if (!TREE_SIDE_EFFECTS (expr))
9891 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9892 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9894 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9896 tree r = expr;
9897 location_t cloc = loc;
9898 while (TREE_CODE (r) == COMPOUND_EXPR)
9900 if (EXPR_HAS_LOCATION (r))
9901 cloc = EXPR_LOCATION (r);
9902 r = TREE_OPERAND (r, 1);
9904 if (!TREE_SIDE_EFFECTS (r)
9905 && !VOID_TYPE_P (TREE_TYPE (r))
9906 && !CONVERT_EXPR_P (r)
9907 && !TREE_NO_WARNING (r)
9908 && !TREE_NO_WARNING (expr))
9909 warning_at (cloc, OPT_Wunused_value,
9910 "right-hand operand of comma expression has no effect");
9912 else
9913 warn_if_unused_value (expr, loc);
9916 /* Process an expression as if it were a complete statement. Emit
9917 diagnostics, but do not call ADD_STMT. LOC is the location of the
9918 statement. */
9920 tree
9921 c_process_expr_stmt (location_t loc, tree expr)
9923 tree exprv;
9925 if (!expr)
9926 return NULL_TREE;
9928 expr = c_fully_fold (expr, false, NULL);
9930 if (warn_sequence_point)
9931 verify_sequence_points (expr);
9933 if (TREE_TYPE (expr) != error_mark_node
9934 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9935 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9936 error_at (loc, "expression statement has incomplete type");
9938 /* If we're not processing a statement expression, warn about unused values.
9939 Warnings for statement expressions will be emitted later, once we figure
9940 out which is the result. */
9941 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9942 && warn_unused_value)
9943 emit_side_effect_warnings (loc, expr);
9945 exprv = expr;
9946 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9947 exprv = TREE_OPERAND (exprv, 1);
9948 while (CONVERT_EXPR_P (exprv))
9949 exprv = TREE_OPERAND (exprv, 0);
9950 if (DECL_P (exprv)
9951 || handled_component_p (exprv)
9952 || TREE_CODE (exprv) == ADDR_EXPR)
9953 mark_exp_read (exprv);
9955 /* If the expression is not of a type to which we cannot assign a line
9956 number, wrap the thing in a no-op NOP_EXPR. */
9957 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9959 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9960 SET_EXPR_LOCATION (expr, loc);
9963 return expr;
9966 /* Emit an expression as a statement. LOC is the location of the
9967 expression. */
9969 tree
9970 c_finish_expr_stmt (location_t loc, tree expr)
9972 if (expr)
9973 return add_stmt (c_process_expr_stmt (loc, expr));
9974 else
9975 return NULL;
9978 /* Do the opposite and emit a statement as an expression. To begin,
9979 create a new binding level and return it. */
9981 tree
9982 c_begin_stmt_expr (void)
9984 tree ret;
9986 /* We must force a BLOCK for this level so that, if it is not expanded
9987 later, there is a way to turn off the entire subtree of blocks that
9988 are contained in it. */
9989 keep_next_level ();
9990 ret = c_begin_compound_stmt (true);
9992 c_bindings_start_stmt_expr (c_switch_stack == NULL
9993 ? NULL
9994 : c_switch_stack->bindings);
9996 /* Mark the current statement list as belonging to a statement list. */
9997 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9999 return ret;
10002 /* LOC is the location of the compound statement to which this body
10003 belongs. */
10005 tree
10006 c_finish_stmt_expr (location_t loc, tree body)
10008 tree last, type, tmp, val;
10009 tree *last_p;
10011 body = c_end_compound_stmt (loc, body, true);
10013 c_bindings_end_stmt_expr (c_switch_stack == NULL
10014 ? NULL
10015 : c_switch_stack->bindings);
10017 /* Locate the last statement in BODY. See c_end_compound_stmt
10018 about always returning a BIND_EXPR. */
10019 last_p = &BIND_EXPR_BODY (body);
10020 last = BIND_EXPR_BODY (body);
10022 continue_searching:
10023 if (TREE_CODE (last) == STATEMENT_LIST)
10025 tree_stmt_iterator i;
10027 /* This can happen with degenerate cases like ({ }). No value. */
10028 if (!TREE_SIDE_EFFECTS (last))
10029 return body;
10031 /* If we're supposed to generate side effects warnings, process
10032 all of the statements except the last. */
10033 if (warn_unused_value)
10035 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10037 location_t tloc;
10038 tree t = tsi_stmt (i);
10040 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10041 emit_side_effect_warnings (tloc, t);
10044 else
10045 i = tsi_last (last);
10046 last_p = tsi_stmt_ptr (i);
10047 last = *last_p;
10050 /* If the end of the list is exception related, then the list was split
10051 by a call to push_cleanup. Continue searching. */
10052 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10053 || TREE_CODE (last) == TRY_CATCH_EXPR)
10055 last_p = &TREE_OPERAND (last, 0);
10056 last = *last_p;
10057 goto continue_searching;
10060 if (last == error_mark_node)
10061 return last;
10063 /* In the case that the BIND_EXPR is not necessary, return the
10064 expression out from inside it. */
10065 if (last == BIND_EXPR_BODY (body)
10066 && BIND_EXPR_VARS (body) == NULL)
10068 /* Even if this looks constant, do not allow it in a constant
10069 expression. */
10070 last = c_wrap_maybe_const (last, true);
10071 /* Do not warn if the return value of a statement expression is
10072 unused. */
10073 TREE_NO_WARNING (last) = 1;
10074 return last;
10077 /* Extract the type of said expression. */
10078 type = TREE_TYPE (last);
10080 /* If we're not returning a value at all, then the BIND_EXPR that
10081 we already have is a fine expression to return. */
10082 if (!type || VOID_TYPE_P (type))
10083 return body;
10085 /* Now that we've located the expression containing the value, it seems
10086 silly to make voidify_wrapper_expr repeat the process. Create a
10087 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10088 tmp = create_tmp_var_raw (type);
10090 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10091 tree_expr_nonnegative_p giving up immediately. */
10092 val = last;
10093 if (TREE_CODE (val) == NOP_EXPR
10094 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10095 val = TREE_OPERAND (val, 0);
10097 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10098 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10101 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10102 SET_EXPR_LOCATION (t, loc);
10103 return t;
10107 /* Begin and end compound statements. This is as simple as pushing
10108 and popping new statement lists from the tree. */
10110 tree
10111 c_begin_compound_stmt (bool do_scope)
10113 tree stmt = push_stmt_list ();
10114 if (do_scope)
10115 push_scope ();
10116 return stmt;
10119 /* End a compound statement. STMT is the statement. LOC is the
10120 location of the compound statement-- this is usually the location
10121 of the opening brace. */
10123 tree
10124 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10126 tree block = NULL;
10128 if (do_scope)
10130 if (c_dialect_objc ())
10131 objc_clear_super_receiver ();
10132 block = pop_scope ();
10135 stmt = pop_stmt_list (stmt);
10136 stmt = c_build_bind_expr (loc, block, stmt);
10138 /* If this compound statement is nested immediately inside a statement
10139 expression, then force a BIND_EXPR to be created. Otherwise we'll
10140 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10141 STATEMENT_LISTs merge, and thus we can lose track of what statement
10142 was really last. */
10143 if (building_stmt_list_p ()
10144 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10145 && TREE_CODE (stmt) != BIND_EXPR)
10147 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10148 TREE_SIDE_EFFECTS (stmt) = 1;
10149 SET_EXPR_LOCATION (stmt, loc);
10152 return stmt;
10155 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10156 when the current scope is exited. EH_ONLY is true when this is not
10157 meant to apply to normal control flow transfer. */
10159 void
10160 push_cleanup (tree decl, tree cleanup, bool eh_only)
10162 enum tree_code code;
10163 tree stmt, list;
10164 bool stmt_expr;
10166 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10167 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10168 add_stmt (stmt);
10169 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10170 list = push_stmt_list ();
10171 TREE_OPERAND (stmt, 0) = list;
10172 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10175 /* Build a binary-operation expression without default conversions.
10176 CODE is the kind of expression to build.
10177 LOCATION is the operator's location.
10178 This function differs from `build' in several ways:
10179 the data type of the result is computed and recorded in it,
10180 warnings are generated if arg data types are invalid,
10181 special handling for addition and subtraction of pointers is known,
10182 and some optimization is done (operations on narrow ints
10183 are done in the narrower type when that gives the same result).
10184 Constant folding is also done before the result is returned.
10186 Note that the operands will never have enumeral types, or function
10187 or array types, because either they will have the default conversions
10188 performed or they have both just been converted to some other type in which
10189 the arithmetic is to be done. */
10191 tree
10192 build_binary_op (location_t location, enum tree_code code,
10193 tree orig_op0, tree orig_op1, int convert_p)
10195 tree type0, type1, orig_type0, orig_type1;
10196 tree eptype;
10197 enum tree_code code0, code1;
10198 tree op0, op1;
10199 tree ret = error_mark_node;
10200 const char *invalid_op_diag;
10201 bool op0_int_operands, op1_int_operands;
10202 bool int_const, int_const_or_overflow, int_operands;
10204 /* Expression code to give to the expression when it is built.
10205 Normally this is CODE, which is what the caller asked for,
10206 but in some special cases we change it. */
10207 enum tree_code resultcode = code;
10209 /* Data type in which the computation is to be performed.
10210 In the simplest cases this is the common type of the arguments. */
10211 tree result_type = NULL;
10213 /* When the computation is in excess precision, the type of the
10214 final EXCESS_PRECISION_EXPR. */
10215 tree semantic_result_type = NULL;
10217 /* Nonzero means operands have already been type-converted
10218 in whatever way is necessary.
10219 Zero means they need to be converted to RESULT_TYPE. */
10220 int converted = 0;
10222 /* Nonzero means create the expression with this type, rather than
10223 RESULT_TYPE. */
10224 tree build_type = 0;
10226 /* Nonzero means after finally constructing the expression
10227 convert it to this type. */
10228 tree final_type = 0;
10230 /* Nonzero if this is an operation like MIN or MAX which can
10231 safely be computed in short if both args are promoted shorts.
10232 Also implies COMMON.
10233 -1 indicates a bitwise operation; this makes a difference
10234 in the exact conditions for when it is safe to do the operation
10235 in a narrower mode. */
10236 int shorten = 0;
10238 /* Nonzero if this is a comparison operation;
10239 if both args are promoted shorts, compare the original shorts.
10240 Also implies COMMON. */
10241 int short_compare = 0;
10243 /* Nonzero if this is a right-shift operation, which can be computed on the
10244 original short and then promoted if the operand is a promoted short. */
10245 int short_shift = 0;
10247 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10248 int common = 0;
10250 /* True means types are compatible as far as ObjC is concerned. */
10251 bool objc_ok;
10253 /* True means this is an arithmetic operation that may need excess
10254 precision. */
10255 bool may_need_excess_precision;
10257 /* True means this is a boolean operation that converts both its
10258 operands to truth-values. */
10259 bool boolean_op = false;
10261 /* Remember whether we're doing / or %. */
10262 bool doing_div_or_mod = false;
10264 /* Remember whether we're doing << or >>. */
10265 bool doing_shift = false;
10267 /* Tree holding instrumentation expression. */
10268 tree instrument_expr = NULL;
10270 if (location == UNKNOWN_LOCATION)
10271 location = input_location;
10273 op0 = orig_op0;
10274 op1 = orig_op1;
10276 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10277 if (op0_int_operands)
10278 op0 = remove_c_maybe_const_expr (op0);
10279 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10280 if (op1_int_operands)
10281 op1 = remove_c_maybe_const_expr (op1);
10282 int_operands = (op0_int_operands && op1_int_operands);
10283 if (int_operands)
10285 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10286 && TREE_CODE (orig_op1) == INTEGER_CST);
10287 int_const = (int_const_or_overflow
10288 && !TREE_OVERFLOW (orig_op0)
10289 && !TREE_OVERFLOW (orig_op1));
10291 else
10292 int_const = int_const_or_overflow = false;
10294 /* Do not apply default conversion in mixed vector/scalar expression. */
10295 if (convert_p
10296 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10297 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10299 op0 = default_conversion (op0);
10300 op1 = default_conversion (op1);
10303 /* When Cilk Plus is enabled and there are array notations inside op0, then
10304 we check to see if there are builtin array notation functions. If
10305 so, then we take on the type of the array notation inside it. */
10306 if (flag_cilkplus && contains_array_notation_expr (op0))
10307 orig_type0 = type0 = find_correct_array_notation_type (op0);
10308 else
10309 orig_type0 = type0 = TREE_TYPE (op0);
10311 if (flag_cilkplus && contains_array_notation_expr (op1))
10312 orig_type1 = type1 = find_correct_array_notation_type (op1);
10313 else
10314 orig_type1 = type1 = TREE_TYPE (op1);
10316 /* The expression codes of the data types of the arguments tell us
10317 whether the arguments are integers, floating, pointers, etc. */
10318 code0 = TREE_CODE (type0);
10319 code1 = TREE_CODE (type1);
10321 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10322 STRIP_TYPE_NOPS (op0);
10323 STRIP_TYPE_NOPS (op1);
10325 /* If an error was already reported for one of the arguments,
10326 avoid reporting another error. */
10328 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10329 return error_mark_node;
10331 if ((invalid_op_diag
10332 = targetm.invalid_binary_op (code, type0, type1)))
10334 error_at (location, invalid_op_diag);
10335 return error_mark_node;
10338 switch (code)
10340 case PLUS_EXPR:
10341 case MINUS_EXPR:
10342 case MULT_EXPR:
10343 case TRUNC_DIV_EXPR:
10344 case CEIL_DIV_EXPR:
10345 case FLOOR_DIV_EXPR:
10346 case ROUND_DIV_EXPR:
10347 case EXACT_DIV_EXPR:
10348 may_need_excess_precision = true;
10349 break;
10350 default:
10351 may_need_excess_precision = false;
10352 break;
10354 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10356 op0 = TREE_OPERAND (op0, 0);
10357 type0 = TREE_TYPE (op0);
10359 else if (may_need_excess_precision
10360 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10362 type0 = eptype;
10363 op0 = convert (eptype, op0);
10365 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10367 op1 = TREE_OPERAND (op1, 0);
10368 type1 = TREE_TYPE (op1);
10370 else if (may_need_excess_precision
10371 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10373 type1 = eptype;
10374 op1 = convert (eptype, op1);
10377 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10379 /* In case when one of the operands of the binary operation is
10380 a vector and another is a scalar -- convert scalar to vector. */
10381 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10383 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10384 true);
10386 switch (convert_flag)
10388 case stv_error:
10389 return error_mark_node;
10390 case stv_firstarg:
10392 bool maybe_const = true;
10393 tree sc;
10394 sc = c_fully_fold (op0, false, &maybe_const);
10395 sc = save_expr (sc);
10396 sc = convert (TREE_TYPE (type1), sc);
10397 op0 = build_vector_from_val (type1, sc);
10398 if (!maybe_const)
10399 op0 = c_wrap_maybe_const (op0, true);
10400 orig_type0 = type0 = TREE_TYPE (op0);
10401 code0 = TREE_CODE (type0);
10402 converted = 1;
10403 break;
10405 case stv_secondarg:
10407 bool maybe_const = true;
10408 tree sc;
10409 sc = c_fully_fold (op1, false, &maybe_const);
10410 sc = save_expr (sc);
10411 sc = convert (TREE_TYPE (type0), sc);
10412 op1 = build_vector_from_val (type0, sc);
10413 if (!maybe_const)
10414 op1 = c_wrap_maybe_const (op1, true);
10415 orig_type1 = type1 = TREE_TYPE (op1);
10416 code1 = TREE_CODE (type1);
10417 converted = 1;
10418 break;
10420 default:
10421 break;
10425 switch (code)
10427 case PLUS_EXPR:
10428 /* Handle the pointer + int case. */
10429 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10431 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10432 goto return_build_binary_op;
10434 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10436 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10437 goto return_build_binary_op;
10439 else
10440 common = 1;
10441 break;
10443 case MINUS_EXPR:
10444 /* Subtraction of two similar pointers.
10445 We must subtract them as integers, then divide by object size. */
10446 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10447 && comp_target_types (location, type0, type1))
10449 ret = pointer_diff (location, op0, op1);
10450 goto return_build_binary_op;
10452 /* Handle pointer minus int. Just like pointer plus int. */
10453 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10455 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10456 goto return_build_binary_op;
10458 else
10459 common = 1;
10460 break;
10462 case MULT_EXPR:
10463 common = 1;
10464 break;
10466 case TRUNC_DIV_EXPR:
10467 case CEIL_DIV_EXPR:
10468 case FLOOR_DIV_EXPR:
10469 case ROUND_DIV_EXPR:
10470 case EXACT_DIV_EXPR:
10471 doing_div_or_mod = true;
10472 warn_for_div_by_zero (location, op1);
10474 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10475 || code0 == FIXED_POINT_TYPE
10476 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10477 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10478 || code1 == FIXED_POINT_TYPE
10479 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10481 enum tree_code tcode0 = code0, tcode1 = code1;
10483 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10484 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10485 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10486 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10488 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10489 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10490 resultcode = RDIV_EXPR;
10491 else
10492 /* Although it would be tempting to shorten always here, that
10493 loses on some targets, since the modulo instruction is
10494 undefined if the quotient can't be represented in the
10495 computation mode. We shorten only if unsigned or if
10496 dividing by something we know != -1. */
10497 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10498 || (TREE_CODE (op1) == INTEGER_CST
10499 && !integer_all_onesp (op1)));
10500 common = 1;
10502 break;
10504 case BIT_AND_EXPR:
10505 case BIT_IOR_EXPR:
10506 case BIT_XOR_EXPR:
10507 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10508 shorten = -1;
10509 /* Allow vector types which are not floating point types. */
10510 else if (code0 == VECTOR_TYPE
10511 && code1 == VECTOR_TYPE
10512 && !VECTOR_FLOAT_TYPE_P (type0)
10513 && !VECTOR_FLOAT_TYPE_P (type1))
10514 common = 1;
10515 break;
10517 case TRUNC_MOD_EXPR:
10518 case FLOOR_MOD_EXPR:
10519 doing_div_or_mod = true;
10520 warn_for_div_by_zero (location, op1);
10522 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10523 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10524 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10525 common = 1;
10526 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10528 /* Although it would be tempting to shorten always here, that loses
10529 on some targets, since the modulo instruction is undefined if the
10530 quotient can't be represented in the computation mode. We shorten
10531 only if unsigned or if dividing by something we know != -1. */
10532 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10533 || (TREE_CODE (op1) == INTEGER_CST
10534 && !integer_all_onesp (op1)));
10535 common = 1;
10537 break;
10539 case TRUTH_ANDIF_EXPR:
10540 case TRUTH_ORIF_EXPR:
10541 case TRUTH_AND_EXPR:
10542 case TRUTH_OR_EXPR:
10543 case TRUTH_XOR_EXPR:
10544 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10545 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10546 || code0 == FIXED_POINT_TYPE)
10547 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10548 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10549 || code1 == FIXED_POINT_TYPE))
10551 /* Result of these operations is always an int,
10552 but that does not mean the operands should be
10553 converted to ints! */
10554 result_type = integer_type_node;
10555 if (op0_int_operands)
10557 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10558 op0 = remove_c_maybe_const_expr (op0);
10560 else
10561 op0 = c_objc_common_truthvalue_conversion (location, op0);
10562 if (op1_int_operands)
10564 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10565 op1 = remove_c_maybe_const_expr (op1);
10567 else
10568 op1 = c_objc_common_truthvalue_conversion (location, op1);
10569 converted = 1;
10570 boolean_op = true;
10572 if (code == TRUTH_ANDIF_EXPR)
10574 int_const_or_overflow = (int_operands
10575 && TREE_CODE (orig_op0) == INTEGER_CST
10576 && (op0 == truthvalue_false_node
10577 || TREE_CODE (orig_op1) == INTEGER_CST));
10578 int_const = (int_const_or_overflow
10579 && !TREE_OVERFLOW (orig_op0)
10580 && (op0 == truthvalue_false_node
10581 || !TREE_OVERFLOW (orig_op1)));
10583 else if (code == TRUTH_ORIF_EXPR)
10585 int_const_or_overflow = (int_operands
10586 && TREE_CODE (orig_op0) == INTEGER_CST
10587 && (op0 == truthvalue_true_node
10588 || TREE_CODE (orig_op1) == INTEGER_CST));
10589 int_const = (int_const_or_overflow
10590 && !TREE_OVERFLOW (orig_op0)
10591 && (op0 == truthvalue_true_node
10592 || !TREE_OVERFLOW (orig_op1)));
10594 break;
10596 /* Shift operations: result has same type as first operand;
10597 always convert second operand to int.
10598 Also set SHORT_SHIFT if shifting rightward. */
10600 case RSHIFT_EXPR:
10601 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10602 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10604 result_type = type0;
10605 converted = 1;
10607 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10608 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10609 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10610 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10612 result_type = type0;
10613 converted = 1;
10615 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10616 && code1 == INTEGER_TYPE)
10618 doing_shift = true;
10619 if (TREE_CODE (op1) == INTEGER_CST)
10621 if (tree_int_cst_sgn (op1) < 0)
10623 int_const = false;
10624 if (c_inhibit_evaluation_warnings == 0)
10625 warning_at (location, OPT_Wshift_count_negative,
10626 "right shift count is negative");
10628 else
10630 if (!integer_zerop (op1))
10631 short_shift = 1;
10633 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10635 int_const = false;
10636 if (c_inhibit_evaluation_warnings == 0)
10637 warning_at (location, OPT_Wshift_count_overflow,
10638 "right shift count >= width of type");
10643 /* Use the type of the value to be shifted. */
10644 result_type = type0;
10645 /* Avoid converting op1 to result_type later. */
10646 converted = 1;
10648 break;
10650 case LSHIFT_EXPR:
10651 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10652 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10654 result_type = type0;
10655 converted = 1;
10657 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10658 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10659 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10660 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10662 result_type = type0;
10663 converted = 1;
10665 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10666 && code1 == INTEGER_TYPE)
10668 doing_shift = true;
10669 if (TREE_CODE (op1) == INTEGER_CST)
10671 if (tree_int_cst_sgn (op1) < 0)
10673 int_const = false;
10674 if (c_inhibit_evaluation_warnings == 0)
10675 warning_at (location, OPT_Wshift_count_negative,
10676 "left shift count is negative");
10679 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10681 int_const = false;
10682 if (c_inhibit_evaluation_warnings == 0)
10683 warning_at (location, OPT_Wshift_count_overflow,
10684 "left shift count >= width of type");
10688 /* Use the type of the value to be shifted. */
10689 result_type = type0;
10690 /* Avoid converting op1 to result_type later. */
10691 converted = 1;
10693 break;
10695 case EQ_EXPR:
10696 case NE_EXPR:
10697 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10699 tree intt;
10700 if (!vector_types_compatible_elements_p (type0, type1))
10702 error_at (location, "comparing vectors with different "
10703 "element types");
10704 return error_mark_node;
10707 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10709 error_at (location, "comparing vectors with different "
10710 "number of elements");
10711 return error_mark_node;
10714 /* Always construct signed integer vector type. */
10715 intt = c_common_type_for_size (GET_MODE_BITSIZE
10716 (TYPE_MODE (TREE_TYPE (type0))), 0);
10717 result_type = build_opaque_vector_type (intt,
10718 TYPE_VECTOR_SUBPARTS (type0));
10719 converted = 1;
10720 break;
10722 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10723 warning_at (location,
10724 OPT_Wfloat_equal,
10725 "comparing floating point with == or != is unsafe");
10726 /* Result of comparison is always int,
10727 but don't convert the args to int! */
10728 build_type = integer_type_node;
10729 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10730 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10731 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10732 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10733 short_compare = 1;
10734 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10736 if (TREE_CODE (op0) == ADDR_EXPR
10737 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10739 if (code == EQ_EXPR)
10740 warning_at (location,
10741 OPT_Waddress,
10742 "the comparison will always evaluate as %<false%> "
10743 "for the address of %qD will never be NULL",
10744 TREE_OPERAND (op0, 0));
10745 else
10746 warning_at (location,
10747 OPT_Waddress,
10748 "the comparison will always evaluate as %<true%> "
10749 "for the address of %qD will never be NULL",
10750 TREE_OPERAND (op0, 0));
10752 result_type = type0;
10754 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10756 if (TREE_CODE (op1) == ADDR_EXPR
10757 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10759 if (code == EQ_EXPR)
10760 warning_at (location,
10761 OPT_Waddress,
10762 "the comparison will always evaluate as %<false%> "
10763 "for the address of %qD will never be NULL",
10764 TREE_OPERAND (op1, 0));
10765 else
10766 warning_at (location,
10767 OPT_Waddress,
10768 "the comparison will always evaluate as %<true%> "
10769 "for the address of %qD will never be NULL",
10770 TREE_OPERAND (op1, 0));
10772 result_type = type1;
10774 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10776 tree tt0 = TREE_TYPE (type0);
10777 tree tt1 = TREE_TYPE (type1);
10778 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10779 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10780 addr_space_t as_common = ADDR_SPACE_GENERIC;
10782 /* Anything compares with void *. void * compares with anything.
10783 Otherwise, the targets must be compatible
10784 and both must be object or both incomplete. */
10785 if (comp_target_types (location, type0, type1))
10786 result_type = common_pointer_type (type0, type1);
10787 else if (!addr_space_superset (as0, as1, &as_common))
10789 error_at (location, "comparison of pointers to "
10790 "disjoint address spaces");
10791 return error_mark_node;
10793 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10795 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10796 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10797 "comparison of %<void *%> with function pointer");
10799 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10801 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10802 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10803 "comparison of %<void *%> with function pointer");
10805 else
10806 /* Avoid warning about the volatile ObjC EH puts on decls. */
10807 if (!objc_ok)
10808 pedwarn (location, 0,
10809 "comparison of distinct pointer types lacks a cast");
10811 if (result_type == NULL_TREE)
10813 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10814 result_type = build_pointer_type
10815 (build_qualified_type (void_type_node, qual));
10818 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10820 result_type = type0;
10821 pedwarn (location, 0, "comparison between pointer and integer");
10823 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10825 result_type = type1;
10826 pedwarn (location, 0, "comparison between pointer and integer");
10828 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10829 || truth_value_p (TREE_CODE (orig_op0)))
10830 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10831 || truth_value_p (TREE_CODE (orig_op1))))
10832 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10833 break;
10835 case LE_EXPR:
10836 case GE_EXPR:
10837 case LT_EXPR:
10838 case GT_EXPR:
10839 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10841 tree intt;
10842 if (!vector_types_compatible_elements_p (type0, type1))
10844 error_at (location, "comparing vectors with different "
10845 "element types");
10846 return error_mark_node;
10849 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10851 error_at (location, "comparing vectors with different "
10852 "number of elements");
10853 return error_mark_node;
10856 /* Always construct signed integer vector type. */
10857 intt = c_common_type_for_size (GET_MODE_BITSIZE
10858 (TYPE_MODE (TREE_TYPE (type0))), 0);
10859 result_type = build_opaque_vector_type (intt,
10860 TYPE_VECTOR_SUBPARTS (type0));
10861 converted = 1;
10862 break;
10864 build_type = integer_type_node;
10865 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10866 || code0 == FIXED_POINT_TYPE)
10867 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10868 || code1 == FIXED_POINT_TYPE))
10869 short_compare = 1;
10870 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10872 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10873 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10874 addr_space_t as_common;
10876 if (comp_target_types (location, type0, type1))
10878 result_type = common_pointer_type (type0, type1);
10879 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10880 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10881 pedwarn (location, 0,
10882 "comparison of complete and incomplete pointers");
10883 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10884 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10885 "ordered comparisons of pointers to functions");
10886 else if (null_pointer_constant_p (orig_op0)
10887 || null_pointer_constant_p (orig_op1))
10888 warning_at (location, OPT_Wextra,
10889 "ordered comparison of pointer with null pointer");
10892 else if (!addr_space_superset (as0, as1, &as_common))
10894 error_at (location, "comparison of pointers to "
10895 "disjoint address spaces");
10896 return error_mark_node;
10898 else
10900 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10901 result_type = build_pointer_type
10902 (build_qualified_type (void_type_node, qual));
10903 pedwarn (location, 0,
10904 "comparison of distinct pointer types lacks a cast");
10907 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10909 result_type = type0;
10910 if (pedantic)
10911 pedwarn (location, OPT_Wpedantic,
10912 "ordered comparison of pointer with integer zero");
10913 else if (extra_warnings)
10914 warning_at (location, OPT_Wextra,
10915 "ordered comparison of pointer with integer zero");
10917 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10919 result_type = type1;
10920 if (pedantic)
10921 pedwarn (location, OPT_Wpedantic,
10922 "ordered comparison of pointer with integer zero");
10923 else if (extra_warnings)
10924 warning_at (location, OPT_Wextra,
10925 "ordered comparison of pointer with integer zero");
10927 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10929 result_type = type0;
10930 pedwarn (location, 0, "comparison between pointer and integer");
10932 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10934 result_type = type1;
10935 pedwarn (location, 0, "comparison between pointer and integer");
10937 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10938 || truth_value_p (TREE_CODE (orig_op0)))
10939 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10940 || truth_value_p (TREE_CODE (orig_op1))))
10941 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10942 break;
10944 default:
10945 gcc_unreachable ();
10948 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10949 return error_mark_node;
10951 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10952 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10953 || !vector_types_compatible_elements_p (type0, type1)))
10955 binary_op_error (location, code, type0, type1);
10956 return error_mark_node;
10959 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10960 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10962 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10963 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10965 bool first_complex = (code0 == COMPLEX_TYPE);
10966 bool second_complex = (code1 == COMPLEX_TYPE);
10967 int none_complex = (!first_complex && !second_complex);
10969 if (shorten || common || short_compare)
10971 result_type = c_common_type (type0, type1);
10972 do_warn_double_promotion (result_type, type0, type1,
10973 "implicit conversion from %qT to %qT "
10974 "to match other operand of binary "
10975 "expression",
10976 location);
10977 if (result_type == error_mark_node)
10978 return error_mark_node;
10981 if (first_complex != second_complex
10982 && (code == PLUS_EXPR
10983 || code == MINUS_EXPR
10984 || code == MULT_EXPR
10985 || (code == TRUNC_DIV_EXPR && first_complex))
10986 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10987 && flag_signed_zeros)
10989 /* An operation on mixed real/complex operands must be
10990 handled specially, but the language-independent code can
10991 more easily optimize the plain complex arithmetic if
10992 -fno-signed-zeros. */
10993 tree real_type = TREE_TYPE (result_type);
10994 tree real, imag;
10995 if (type0 != orig_type0 || type1 != orig_type1)
10997 gcc_assert (may_need_excess_precision && common);
10998 semantic_result_type = c_common_type (orig_type0, orig_type1);
11000 if (first_complex)
11002 if (TREE_TYPE (op0) != result_type)
11003 op0 = convert_and_check (location, result_type, op0);
11004 if (TREE_TYPE (op1) != real_type)
11005 op1 = convert_and_check (location, real_type, op1);
11007 else
11009 if (TREE_TYPE (op0) != real_type)
11010 op0 = convert_and_check (location, real_type, op0);
11011 if (TREE_TYPE (op1) != result_type)
11012 op1 = convert_and_check (location, result_type, op1);
11014 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11015 return error_mark_node;
11016 if (first_complex)
11018 op0 = c_save_expr (op0);
11019 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11020 op0, 1);
11021 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11022 op0, 1);
11023 switch (code)
11025 case MULT_EXPR:
11026 case TRUNC_DIV_EXPR:
11027 op1 = c_save_expr (op1);
11028 imag = build2 (resultcode, real_type, imag, op1);
11029 /* Fall through. */
11030 case PLUS_EXPR:
11031 case MINUS_EXPR:
11032 real = build2 (resultcode, real_type, real, op1);
11033 break;
11034 default:
11035 gcc_unreachable();
11038 else
11040 op1 = c_save_expr (op1);
11041 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11042 op1, 1);
11043 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11044 op1, 1);
11045 switch (code)
11047 case MULT_EXPR:
11048 op0 = c_save_expr (op0);
11049 imag = build2 (resultcode, real_type, op0, imag);
11050 /* Fall through. */
11051 case PLUS_EXPR:
11052 real = build2 (resultcode, real_type, op0, real);
11053 break;
11054 case MINUS_EXPR:
11055 real = build2 (resultcode, real_type, op0, real);
11056 imag = build1 (NEGATE_EXPR, real_type, imag);
11057 break;
11058 default:
11059 gcc_unreachable();
11062 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11063 goto return_build_binary_op;
11066 /* For certain operations (which identify themselves by shorten != 0)
11067 if both args were extended from the same smaller type,
11068 do the arithmetic in that type and then extend.
11070 shorten !=0 and !=1 indicates a bitwise operation.
11071 For them, this optimization is safe only if
11072 both args are zero-extended or both are sign-extended.
11073 Otherwise, we might change the result.
11074 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11075 but calculated in (unsigned short) it would be (unsigned short)-1. */
11077 if (shorten && none_complex)
11079 final_type = result_type;
11080 result_type = shorten_binary_op (result_type, op0, op1,
11081 shorten == -1);
11084 /* Shifts can be shortened if shifting right. */
11086 if (short_shift)
11088 int unsigned_arg;
11089 tree arg0 = get_narrower (op0, &unsigned_arg);
11091 final_type = result_type;
11093 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11094 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11096 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11097 && tree_int_cst_sgn (op1) > 0
11098 /* We can shorten only if the shift count is less than the
11099 number of bits in the smaller type size. */
11100 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11101 /* We cannot drop an unsigned shift after sign-extension. */
11102 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11104 /* Do an unsigned shift if the operand was zero-extended. */
11105 result_type
11106 = c_common_signed_or_unsigned_type (unsigned_arg,
11107 TREE_TYPE (arg0));
11108 /* Convert value-to-be-shifted to that type. */
11109 if (TREE_TYPE (op0) != result_type)
11110 op0 = convert (result_type, op0);
11111 converted = 1;
11115 /* Comparison operations are shortened too but differently.
11116 They identify themselves by setting short_compare = 1. */
11118 if (short_compare)
11120 /* Don't write &op0, etc., because that would prevent op0
11121 from being kept in a register.
11122 Instead, make copies of the our local variables and
11123 pass the copies by reference, then copy them back afterward. */
11124 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11125 enum tree_code xresultcode = resultcode;
11126 tree val
11127 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11128 &xresultcode);
11130 if (val != 0)
11132 ret = val;
11133 goto return_build_binary_op;
11136 op0 = xop0, op1 = xop1;
11137 converted = 1;
11138 resultcode = xresultcode;
11140 if (c_inhibit_evaluation_warnings == 0)
11142 bool op0_maybe_const = true;
11143 bool op1_maybe_const = true;
11144 tree orig_op0_folded, orig_op1_folded;
11146 if (in_late_binary_op)
11148 orig_op0_folded = orig_op0;
11149 orig_op1_folded = orig_op1;
11151 else
11153 /* Fold for the sake of possible warnings, as in
11154 build_conditional_expr. This requires the
11155 "original" values to be folded, not just op0 and
11156 op1. */
11157 c_inhibit_evaluation_warnings++;
11158 op0 = c_fully_fold (op0, require_constant_value,
11159 &op0_maybe_const);
11160 op1 = c_fully_fold (op1, require_constant_value,
11161 &op1_maybe_const);
11162 c_inhibit_evaluation_warnings--;
11163 orig_op0_folded = c_fully_fold (orig_op0,
11164 require_constant_value,
11165 NULL);
11166 orig_op1_folded = c_fully_fold (orig_op1,
11167 require_constant_value,
11168 NULL);
11171 if (warn_sign_compare)
11172 warn_for_sign_compare (location, orig_op0_folded,
11173 orig_op1_folded, op0, op1,
11174 result_type, resultcode);
11175 if (!in_late_binary_op && !int_operands)
11177 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11178 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11179 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11180 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11186 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11187 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11188 Then the expression will be built.
11189 It will be given type FINAL_TYPE if that is nonzero;
11190 otherwise, it will be given type RESULT_TYPE. */
11192 if (!result_type)
11194 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11195 return error_mark_node;
11198 if (build_type == NULL_TREE)
11200 build_type = result_type;
11201 if ((type0 != orig_type0 || type1 != orig_type1)
11202 && !boolean_op)
11204 gcc_assert (may_need_excess_precision && common);
11205 semantic_result_type = c_common_type (orig_type0, orig_type1);
11209 if (!converted)
11211 op0 = ep_convert_and_check (location, result_type, op0,
11212 semantic_result_type);
11213 op1 = ep_convert_and_check (location, result_type, op1,
11214 semantic_result_type);
11216 /* This can happen if one operand has a vector type, and the other
11217 has a different type. */
11218 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11219 return error_mark_node;
11222 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11223 | SANITIZE_FLOAT_DIVIDE))
11224 && current_function_decl != 0
11225 && !lookup_attribute ("no_sanitize_undefined",
11226 DECL_ATTRIBUTES (current_function_decl))
11227 && (doing_div_or_mod || doing_shift))
11229 /* OP0 and/or OP1 might have side-effects. */
11230 op0 = c_save_expr (op0);
11231 op1 = c_save_expr (op1);
11232 op0 = c_fully_fold (op0, false, NULL);
11233 op1 = c_fully_fold (op1, false, NULL);
11234 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11235 | SANITIZE_FLOAT_DIVIDE)))
11236 instrument_expr = ubsan_instrument_division (location, op0, op1);
11237 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11238 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11241 /* Treat expressions in initializers specially as they can't trap. */
11242 if (int_const_or_overflow)
11243 ret = (require_constant_value
11244 ? fold_build2_initializer_loc (location, resultcode, build_type,
11245 op0, op1)
11246 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11247 else
11248 ret = build2 (resultcode, build_type, op0, op1);
11249 if (final_type != 0)
11250 ret = convert (final_type, ret);
11252 return_build_binary_op:
11253 gcc_assert (ret != error_mark_node);
11254 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11255 ret = (int_operands
11256 ? note_integer_operands (ret)
11257 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11258 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11259 && !in_late_binary_op)
11260 ret = note_integer_operands (ret);
11261 if (semantic_result_type)
11262 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11263 protected_set_expr_location (ret, location);
11265 if (instrument_expr != NULL)
11266 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11267 instrument_expr, ret);
11269 return ret;
11273 /* Convert EXPR to be a truth-value, validating its type for this
11274 purpose. LOCATION is the source location for the expression. */
11276 tree
11277 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11279 bool int_const, int_operands;
11281 switch (TREE_CODE (TREE_TYPE (expr)))
11283 case ARRAY_TYPE:
11284 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11285 return error_mark_node;
11287 case RECORD_TYPE:
11288 error_at (location, "used struct type value where scalar is required");
11289 return error_mark_node;
11291 case UNION_TYPE:
11292 error_at (location, "used union type value where scalar is required");
11293 return error_mark_node;
11295 case VOID_TYPE:
11296 error_at (location, "void value not ignored as it ought to be");
11297 return error_mark_node;
11299 case FUNCTION_TYPE:
11300 gcc_unreachable ();
11302 case VECTOR_TYPE:
11303 error_at (location, "used vector type where scalar is required");
11304 return error_mark_node;
11306 default:
11307 break;
11310 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11311 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11312 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11314 expr = remove_c_maybe_const_expr (expr);
11315 expr = build2 (NE_EXPR, integer_type_node, expr,
11316 convert (TREE_TYPE (expr), integer_zero_node));
11317 expr = note_integer_operands (expr);
11319 else
11320 /* ??? Should we also give an error for vectors rather than leaving
11321 those to give errors later? */
11322 expr = c_common_truthvalue_conversion (location, expr);
11324 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11326 if (TREE_OVERFLOW (expr))
11327 return expr;
11328 else
11329 return note_integer_operands (expr);
11331 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11332 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11333 return expr;
11337 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11338 required. */
11340 tree
11341 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11343 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11345 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11346 /* Executing a compound literal inside a function reinitializes
11347 it. */
11348 if (!TREE_STATIC (decl))
11349 *se = true;
11350 return decl;
11352 else
11353 return expr;
11356 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11357 statement. LOC is the location of the OACC_PARALLEL. */
11359 tree
11360 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11362 tree stmt;
11364 block = c_end_compound_stmt (loc, block, true);
11366 stmt = make_node (OACC_PARALLEL);
11367 TREE_TYPE (stmt) = void_type_node;
11368 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11369 OACC_PARALLEL_BODY (stmt) = block;
11370 SET_EXPR_LOCATION (stmt, loc);
11372 return add_stmt (stmt);
11375 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11376 statement. LOC is the location of the OACC_KERNELS. */
11378 tree
11379 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11381 tree stmt;
11383 block = c_end_compound_stmt (loc, block, true);
11385 stmt = make_node (OACC_KERNELS);
11386 TREE_TYPE (stmt) = void_type_node;
11387 OACC_KERNELS_CLAUSES (stmt) = clauses;
11388 OACC_KERNELS_BODY (stmt) = block;
11389 SET_EXPR_LOCATION (stmt, loc);
11391 return add_stmt (stmt);
11394 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11395 statement. LOC is the location of the OACC_DATA. */
11397 tree
11398 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11400 tree stmt;
11402 block = c_end_compound_stmt (loc, block, true);
11404 stmt = make_node (OACC_DATA);
11405 TREE_TYPE (stmt) = void_type_node;
11406 OACC_DATA_CLAUSES (stmt) = clauses;
11407 OACC_DATA_BODY (stmt) = block;
11408 SET_EXPR_LOCATION (stmt, loc);
11410 return add_stmt (stmt);
11413 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11415 tree
11416 c_begin_omp_parallel (void)
11418 tree block;
11420 keep_next_level ();
11421 block = c_begin_compound_stmt (true);
11423 return block;
11426 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11427 statement. LOC is the location of the OMP_PARALLEL. */
11429 tree
11430 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11432 tree stmt;
11434 block = c_end_compound_stmt (loc, block, true);
11436 stmt = make_node (OMP_PARALLEL);
11437 TREE_TYPE (stmt) = void_type_node;
11438 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11439 OMP_PARALLEL_BODY (stmt) = block;
11440 SET_EXPR_LOCATION (stmt, loc);
11442 return add_stmt (stmt);
11445 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11447 tree
11448 c_begin_omp_task (void)
11450 tree block;
11452 keep_next_level ();
11453 block = c_begin_compound_stmt (true);
11455 return block;
11458 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11459 statement. LOC is the location of the #pragma. */
11461 tree
11462 c_finish_omp_task (location_t loc, tree clauses, tree block)
11464 tree stmt;
11466 block = c_end_compound_stmt (loc, block, true);
11468 stmt = make_node (OMP_TASK);
11469 TREE_TYPE (stmt) = void_type_node;
11470 OMP_TASK_CLAUSES (stmt) = clauses;
11471 OMP_TASK_BODY (stmt) = block;
11472 SET_EXPR_LOCATION (stmt, loc);
11474 return add_stmt (stmt);
11477 /* Generate GOMP_cancel call for #pragma omp cancel. */
11479 void
11480 c_finish_omp_cancel (location_t loc, tree clauses)
11482 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11483 int mask = 0;
11484 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11485 mask = 1;
11486 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11487 mask = 2;
11488 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11489 mask = 4;
11490 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11491 mask = 8;
11492 else
11494 error_at (loc, "%<#pragma omp cancel must specify one of "
11495 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11496 "clauses");
11497 return;
11499 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11500 if (ifc != NULL_TREE)
11502 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11503 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11504 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11505 build_zero_cst (type));
11507 else
11508 ifc = boolean_true_node;
11509 tree stmt = build_call_expr_loc (loc, fn, 2,
11510 build_int_cst (integer_type_node, mask),
11511 ifc);
11512 add_stmt (stmt);
11515 /* Generate GOMP_cancellation_point call for
11516 #pragma omp cancellation point. */
11518 void
11519 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11521 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11522 int mask = 0;
11523 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11524 mask = 1;
11525 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11526 mask = 2;
11527 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11528 mask = 4;
11529 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11530 mask = 8;
11531 else
11533 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11534 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11535 "clauses");
11536 return;
11538 tree stmt = build_call_expr_loc (loc, fn, 1,
11539 build_int_cst (integer_type_node, mask));
11540 add_stmt (stmt);
11543 /* Helper function for handle_omp_array_sections. Called recursively
11544 to handle multiple array-section-subscripts. C is the clause,
11545 T current expression (initially OMP_CLAUSE_DECL), which is either
11546 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11547 expression if specified, TREE_VALUE length expression if specified,
11548 TREE_CHAIN is what it has been specified after, or some decl.
11549 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11550 set to true if any of the array-section-subscript could have length
11551 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11552 first array-section-subscript which is known not to have length
11553 of one. Given say:
11554 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11555 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11556 all are or may have length of 1, array-section-subscript [:2] is the
11557 first one knonwn not to have length 1. For array-section-subscript
11558 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11559 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11560 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11561 case though, as some lengths could be zero. */
11563 static tree
11564 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11565 bool &maybe_zero_len, unsigned int &first_non_one)
11567 tree ret, low_bound, length, type;
11568 if (TREE_CODE (t) != TREE_LIST)
11570 if (error_operand_p (t))
11571 return error_mark_node;
11572 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11574 if (DECL_P (t))
11575 error_at (OMP_CLAUSE_LOCATION (c),
11576 "%qD is not a variable in %qs clause", t,
11577 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11578 else
11579 error_at (OMP_CLAUSE_LOCATION (c),
11580 "%qE is not a variable in %qs clause", t,
11581 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11582 return error_mark_node;
11584 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11585 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11587 error_at (OMP_CLAUSE_LOCATION (c),
11588 "%qD is threadprivate variable in %qs clause", t,
11589 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11590 return error_mark_node;
11592 return t;
11595 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11596 maybe_zero_len, first_non_one);
11597 if (ret == error_mark_node || ret == NULL_TREE)
11598 return ret;
11600 type = TREE_TYPE (ret);
11601 low_bound = TREE_PURPOSE (t);
11602 length = TREE_VALUE (t);
11604 if (low_bound == error_mark_node || length == error_mark_node)
11605 return error_mark_node;
11607 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11609 error_at (OMP_CLAUSE_LOCATION (c),
11610 "low bound %qE of array section does not have integral type",
11611 low_bound);
11612 return error_mark_node;
11614 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11616 error_at (OMP_CLAUSE_LOCATION (c),
11617 "length %qE of array section does not have integral type",
11618 length);
11619 return error_mark_node;
11621 if (low_bound
11622 && TREE_CODE (low_bound) == INTEGER_CST
11623 && TYPE_PRECISION (TREE_TYPE (low_bound))
11624 > TYPE_PRECISION (sizetype))
11625 low_bound = fold_convert (sizetype, low_bound);
11626 if (length
11627 && TREE_CODE (length) == INTEGER_CST
11628 && TYPE_PRECISION (TREE_TYPE (length))
11629 > TYPE_PRECISION (sizetype))
11630 length = fold_convert (sizetype, length);
11631 if (low_bound == NULL_TREE)
11632 low_bound = integer_zero_node;
11634 if (length != NULL_TREE)
11636 if (!integer_nonzerop (length))
11637 maybe_zero_len = true;
11638 if (first_non_one == types.length ()
11639 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11640 first_non_one++;
11642 if (TREE_CODE (type) == ARRAY_TYPE)
11644 if (length == NULL_TREE
11645 && (TYPE_DOMAIN (type) == NULL_TREE
11646 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11648 error_at (OMP_CLAUSE_LOCATION (c),
11649 "for unknown bound array type length expression must "
11650 "be specified");
11651 return error_mark_node;
11653 if (TREE_CODE (low_bound) == INTEGER_CST
11654 && tree_int_cst_sgn (low_bound) == -1)
11656 error_at (OMP_CLAUSE_LOCATION (c),
11657 "negative low bound in array section in %qs clause",
11658 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11659 return error_mark_node;
11661 if (length != NULL_TREE
11662 && TREE_CODE (length) == INTEGER_CST
11663 && tree_int_cst_sgn (length) == -1)
11665 error_at (OMP_CLAUSE_LOCATION (c),
11666 "negative length in array section in %qs clause",
11667 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11668 return error_mark_node;
11670 if (TYPE_DOMAIN (type)
11671 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11672 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11673 == INTEGER_CST)
11675 tree size = size_binop (PLUS_EXPR,
11676 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11677 size_one_node);
11678 if (TREE_CODE (low_bound) == INTEGER_CST)
11680 if (tree_int_cst_lt (size, low_bound))
11682 error_at (OMP_CLAUSE_LOCATION (c),
11683 "low bound %qE above array section size "
11684 "in %qs clause", low_bound,
11685 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11686 return error_mark_node;
11688 if (tree_int_cst_equal (size, low_bound))
11689 maybe_zero_len = true;
11690 else if (length == NULL_TREE
11691 && first_non_one == types.length ()
11692 && tree_int_cst_equal
11693 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11694 low_bound))
11695 first_non_one++;
11697 else if (length == NULL_TREE)
11699 maybe_zero_len = true;
11700 if (first_non_one == types.length ())
11701 first_non_one++;
11703 if (length && TREE_CODE (length) == INTEGER_CST)
11705 if (tree_int_cst_lt (size, length))
11707 error_at (OMP_CLAUSE_LOCATION (c),
11708 "length %qE above array section size "
11709 "in %qs clause", length,
11710 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11711 return error_mark_node;
11713 if (TREE_CODE (low_bound) == INTEGER_CST)
11715 tree lbpluslen
11716 = size_binop (PLUS_EXPR,
11717 fold_convert (sizetype, low_bound),
11718 fold_convert (sizetype, length));
11719 if (TREE_CODE (lbpluslen) == INTEGER_CST
11720 && tree_int_cst_lt (size, lbpluslen))
11722 error_at (OMP_CLAUSE_LOCATION (c),
11723 "high bound %qE above array section size "
11724 "in %qs clause", lbpluslen,
11725 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11726 return error_mark_node;
11731 else if (length == NULL_TREE)
11733 maybe_zero_len = true;
11734 if (first_non_one == types.length ())
11735 first_non_one++;
11738 /* For [lb:] we will need to evaluate lb more than once. */
11739 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11741 tree lb = c_save_expr (low_bound);
11742 if (lb != low_bound)
11744 TREE_PURPOSE (t) = lb;
11745 low_bound = lb;
11749 else if (TREE_CODE (type) == POINTER_TYPE)
11751 if (length == NULL_TREE)
11753 error_at (OMP_CLAUSE_LOCATION (c),
11754 "for pointer type length expression must be specified");
11755 return error_mark_node;
11757 /* If there is a pointer type anywhere but in the very first
11758 array-section-subscript, the array section can't be contiguous. */
11759 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11760 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11762 error_at (OMP_CLAUSE_LOCATION (c),
11763 "array section is not contiguous in %qs clause",
11764 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11765 return error_mark_node;
11768 else
11770 error_at (OMP_CLAUSE_LOCATION (c),
11771 "%qE does not have pointer or array type", ret);
11772 return error_mark_node;
11774 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11775 types.safe_push (TREE_TYPE (ret));
11776 /* We will need to evaluate lb more than once. */
11777 tree lb = c_save_expr (low_bound);
11778 if (lb != low_bound)
11780 TREE_PURPOSE (t) = lb;
11781 low_bound = lb;
11783 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11784 return ret;
11787 /* Handle array sections for clause C. */
11789 static bool
11790 handle_omp_array_sections (tree c)
11792 bool maybe_zero_len = false;
11793 unsigned int first_non_one = 0;
11794 vec<tree> types = vNULL;
11795 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11796 maybe_zero_len, first_non_one);
11797 if (first == error_mark_node)
11799 types.release ();
11800 return true;
11802 if (first == NULL_TREE)
11804 types.release ();
11805 return false;
11807 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11809 tree t = OMP_CLAUSE_DECL (c);
11810 tree tem = NULL_TREE;
11811 types.release ();
11812 /* Need to evaluate side effects in the length expressions
11813 if any. */
11814 while (TREE_CODE (t) == TREE_LIST)
11816 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11818 if (tem == NULL_TREE)
11819 tem = TREE_VALUE (t);
11820 else
11821 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11822 TREE_VALUE (t), tem);
11824 t = TREE_CHAIN (t);
11826 if (tem)
11827 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11828 first = c_fully_fold (first, false, NULL);
11829 OMP_CLAUSE_DECL (c) = first;
11831 else
11833 unsigned int num = types.length (), i;
11834 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11835 tree condition = NULL_TREE;
11837 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11838 maybe_zero_len = true;
11840 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11841 t = TREE_CHAIN (t))
11843 tree low_bound = TREE_PURPOSE (t);
11844 tree length = TREE_VALUE (t);
11846 i--;
11847 if (low_bound
11848 && TREE_CODE (low_bound) == INTEGER_CST
11849 && TYPE_PRECISION (TREE_TYPE (low_bound))
11850 > TYPE_PRECISION (sizetype))
11851 low_bound = fold_convert (sizetype, low_bound);
11852 if (length
11853 && TREE_CODE (length) == INTEGER_CST
11854 && TYPE_PRECISION (TREE_TYPE (length))
11855 > TYPE_PRECISION (sizetype))
11856 length = fold_convert (sizetype, length);
11857 if (low_bound == NULL_TREE)
11858 low_bound = integer_zero_node;
11859 if (!maybe_zero_len && i > first_non_one)
11861 if (integer_nonzerop (low_bound))
11862 goto do_warn_noncontiguous;
11863 if (length != NULL_TREE
11864 && TREE_CODE (length) == INTEGER_CST
11865 && TYPE_DOMAIN (types[i])
11866 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11867 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11868 == INTEGER_CST)
11870 tree size;
11871 size = size_binop (PLUS_EXPR,
11872 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11873 size_one_node);
11874 if (!tree_int_cst_equal (length, size))
11876 do_warn_noncontiguous:
11877 error_at (OMP_CLAUSE_LOCATION (c),
11878 "array section is not contiguous in %qs "
11879 "clause",
11880 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11881 types.release ();
11882 return true;
11885 if (length != NULL_TREE
11886 && TREE_SIDE_EFFECTS (length))
11888 if (side_effects == NULL_TREE)
11889 side_effects = length;
11890 else
11891 side_effects = build2 (COMPOUND_EXPR,
11892 TREE_TYPE (side_effects),
11893 length, side_effects);
11896 else
11898 tree l;
11900 if (i > first_non_one && length && integer_nonzerop (length))
11901 continue;
11902 if (length)
11903 l = fold_convert (sizetype, length);
11904 else
11906 l = size_binop (PLUS_EXPR,
11907 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11908 size_one_node);
11909 l = size_binop (MINUS_EXPR, l,
11910 fold_convert (sizetype, low_bound));
11912 if (i > first_non_one)
11914 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11915 size_zero_node);
11916 if (condition == NULL_TREE)
11917 condition = l;
11918 else
11919 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11920 l, condition);
11922 else if (size == NULL_TREE)
11924 size = size_in_bytes (TREE_TYPE (types[i]));
11925 size = size_binop (MULT_EXPR, size, l);
11926 if (condition)
11927 size = fold_build3 (COND_EXPR, sizetype, condition,
11928 size, size_zero_node);
11930 else
11931 size = size_binop (MULT_EXPR, size, l);
11934 types.release ();
11935 if (side_effects)
11936 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11937 first = c_fully_fold (first, false, NULL);
11938 OMP_CLAUSE_DECL (c) = first;
11939 if (size)
11940 size = c_fully_fold (size, false, NULL);
11941 OMP_CLAUSE_SIZE (c) = size;
11942 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11943 return false;
11944 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11945 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11946 OMP_CLAUSE_MAP_KIND (c2) = GOMP_MAP_POINTER;
11947 if (!c_mark_addressable (t))
11948 return false;
11949 OMP_CLAUSE_DECL (c2) = t;
11950 t = build_fold_addr_expr (first);
11951 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11952 tree ptr = OMP_CLAUSE_DECL (c2);
11953 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11954 ptr = build_fold_addr_expr (ptr);
11955 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11956 ptrdiff_type_node, t,
11957 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11958 ptrdiff_type_node, ptr));
11959 t = c_fully_fold (t, false, NULL);
11960 OMP_CLAUSE_SIZE (c2) = t;
11961 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11962 OMP_CLAUSE_CHAIN (c) = c2;
11964 return false;
11967 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11968 an inline call. But, remap
11969 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11970 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11972 static tree
11973 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11974 tree decl, tree placeholder)
11976 copy_body_data id;
11977 hash_map<tree, tree> decl_map;
11979 decl_map.put (omp_decl1, placeholder);
11980 decl_map.put (omp_decl2, decl);
11981 memset (&id, 0, sizeof (id));
11982 id.src_fn = DECL_CONTEXT (omp_decl1);
11983 id.dst_fn = current_function_decl;
11984 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11985 id.decl_map = &decl_map;
11987 id.copy_decl = copy_decl_no_change;
11988 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11989 id.transform_new_cfg = true;
11990 id.transform_return_to_modify = false;
11991 id.transform_lang_insert_block = NULL;
11992 id.eh_lp_nr = 0;
11993 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11994 return stmt;
11997 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11998 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12000 static tree
12001 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12003 if (*tp == (tree) data)
12004 return *tp;
12005 return NULL_TREE;
12008 /* For all elements of CLAUSES, validate them against their constraints.
12009 Remove any elements from the list that are invalid. */
12011 tree
12012 c_finish_omp_clauses (tree clauses)
12014 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12015 bitmap_head aligned_head;
12016 tree c, t, *pc;
12017 bool branch_seen = false;
12018 bool copyprivate_seen = false;
12019 tree *nowait_clause = NULL;
12021 bitmap_obstack_initialize (NULL);
12022 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12023 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12024 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12025 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12027 for (pc = &clauses, c = clauses; c ; c = *pc)
12029 bool remove = false;
12030 bool need_complete = false;
12031 bool need_implicitly_determined = false;
12033 switch (OMP_CLAUSE_CODE (c))
12035 case OMP_CLAUSE_SHARED:
12036 need_implicitly_determined = true;
12037 goto check_dup_generic;
12039 case OMP_CLAUSE_PRIVATE:
12040 need_complete = true;
12041 need_implicitly_determined = true;
12042 goto check_dup_generic;
12044 case OMP_CLAUSE_REDUCTION:
12045 need_implicitly_determined = true;
12046 t = OMP_CLAUSE_DECL (c);
12047 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12048 && (FLOAT_TYPE_P (TREE_TYPE (t))
12049 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12051 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12052 const char *r_name = NULL;
12054 switch (r_code)
12056 case PLUS_EXPR:
12057 case MULT_EXPR:
12058 case MINUS_EXPR:
12059 break;
12060 case MIN_EXPR:
12061 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12062 r_name = "min";
12063 break;
12064 case MAX_EXPR:
12065 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12066 r_name = "max";
12067 break;
12068 case BIT_AND_EXPR:
12069 r_name = "&";
12070 break;
12071 case BIT_XOR_EXPR:
12072 r_name = "^";
12073 break;
12074 case BIT_IOR_EXPR:
12075 r_name = "|";
12076 break;
12077 case TRUTH_ANDIF_EXPR:
12078 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12079 r_name = "&&";
12080 break;
12081 case TRUTH_ORIF_EXPR:
12082 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12083 r_name = "||";
12084 break;
12085 default:
12086 gcc_unreachable ();
12088 if (r_name)
12090 error_at (OMP_CLAUSE_LOCATION (c),
12091 "%qE has invalid type for %<reduction(%s)%>",
12092 t, r_name);
12093 remove = true;
12094 break;
12097 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12099 error_at (OMP_CLAUSE_LOCATION (c),
12100 "user defined reduction not found for %qD", t);
12101 remove = true;
12102 break;
12104 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12106 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12107 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12108 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12109 VAR_DECL, NULL_TREE, type);
12110 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12111 DECL_ARTIFICIAL (placeholder) = 1;
12112 DECL_IGNORED_P (placeholder) = 1;
12113 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12114 c_mark_addressable (placeholder);
12115 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12116 c_mark_addressable (OMP_CLAUSE_DECL (c));
12117 OMP_CLAUSE_REDUCTION_MERGE (c)
12118 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12119 TREE_VEC_ELT (list, 0),
12120 TREE_VEC_ELT (list, 1),
12121 OMP_CLAUSE_DECL (c), placeholder);
12122 OMP_CLAUSE_REDUCTION_MERGE (c)
12123 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12124 void_type_node, NULL_TREE,
12125 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12126 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12127 if (TREE_VEC_LENGTH (list) == 6)
12129 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12130 c_mark_addressable (OMP_CLAUSE_DECL (c));
12131 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12132 c_mark_addressable (placeholder);
12133 tree init = TREE_VEC_ELT (list, 5);
12134 if (init == error_mark_node)
12135 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12136 OMP_CLAUSE_REDUCTION_INIT (c)
12137 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12138 TREE_VEC_ELT (list, 3),
12139 OMP_CLAUSE_DECL (c), placeholder);
12140 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12141 OMP_CLAUSE_REDUCTION_INIT (c)
12142 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12143 OMP_CLAUSE_REDUCTION_INIT (c));
12144 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12145 c_find_omp_placeholder_r,
12146 placeholder, NULL))
12147 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12149 else
12151 tree init;
12152 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12153 init = build_constructor (TREE_TYPE (t), NULL);
12154 else
12155 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12156 OMP_CLAUSE_REDUCTION_INIT (c)
12157 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12159 OMP_CLAUSE_REDUCTION_INIT (c)
12160 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12161 void_type_node, NULL_TREE,
12162 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12163 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12165 goto check_dup_generic;
12167 case OMP_CLAUSE_COPYPRIVATE:
12168 copyprivate_seen = true;
12169 if (nowait_clause)
12171 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12172 "%<nowait%> clause must not be used together "
12173 "with %<copyprivate%>");
12174 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12175 nowait_clause = NULL;
12177 goto check_dup_generic;
12179 case OMP_CLAUSE_COPYIN:
12180 t = OMP_CLAUSE_DECL (c);
12181 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12183 error_at (OMP_CLAUSE_LOCATION (c),
12184 "%qE must be %<threadprivate%> for %<copyin%>", t);
12185 remove = true;
12186 break;
12188 goto check_dup_generic;
12190 case OMP_CLAUSE_LINEAR:
12191 t = OMP_CLAUSE_DECL (c);
12192 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12193 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12195 error_at (OMP_CLAUSE_LOCATION (c),
12196 "linear clause applied to non-integral non-pointer "
12197 "variable with type %qT", TREE_TYPE (t));
12198 remove = true;
12199 break;
12201 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12203 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12204 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12205 OMP_CLAUSE_DECL (c), s);
12206 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12207 sizetype, s, OMP_CLAUSE_DECL (c));
12208 if (s == error_mark_node)
12209 s = size_one_node;
12210 OMP_CLAUSE_LINEAR_STEP (c) = s;
12212 else
12213 OMP_CLAUSE_LINEAR_STEP (c)
12214 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12215 goto check_dup_generic;
12217 check_dup_generic:
12218 t = OMP_CLAUSE_DECL (c);
12219 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12221 error_at (OMP_CLAUSE_LOCATION (c),
12222 "%qE is not a variable in clause %qs", t,
12223 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12224 remove = true;
12226 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12227 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12228 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12230 error_at (OMP_CLAUSE_LOCATION (c),
12231 "%qE appears more than once in data clauses", t);
12232 remove = true;
12234 else
12235 bitmap_set_bit (&generic_head, DECL_UID (t));
12236 break;
12238 case OMP_CLAUSE_FIRSTPRIVATE:
12239 t = OMP_CLAUSE_DECL (c);
12240 need_complete = true;
12241 need_implicitly_determined = true;
12242 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12244 error_at (OMP_CLAUSE_LOCATION (c),
12245 "%qE is not a variable in clause %<firstprivate%>", t);
12246 remove = true;
12248 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12249 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12251 error_at (OMP_CLAUSE_LOCATION (c),
12252 "%qE appears more than once in data clauses", t);
12253 remove = true;
12255 else
12256 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12257 break;
12259 case OMP_CLAUSE_LASTPRIVATE:
12260 t = OMP_CLAUSE_DECL (c);
12261 need_complete = true;
12262 need_implicitly_determined = true;
12263 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12265 error_at (OMP_CLAUSE_LOCATION (c),
12266 "%qE is not a variable in clause %<lastprivate%>", t);
12267 remove = true;
12269 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12270 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12272 error_at (OMP_CLAUSE_LOCATION (c),
12273 "%qE appears more than once in data clauses", t);
12274 remove = true;
12276 else
12277 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12278 break;
12280 case OMP_CLAUSE_ALIGNED:
12281 t = OMP_CLAUSE_DECL (c);
12282 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12284 error_at (OMP_CLAUSE_LOCATION (c),
12285 "%qE is not a variable in %<aligned%> clause", t);
12286 remove = true;
12288 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12289 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12291 error_at (OMP_CLAUSE_LOCATION (c),
12292 "%qE in %<aligned%> clause is neither a pointer nor "
12293 "an array", t);
12294 remove = true;
12296 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12298 error_at (OMP_CLAUSE_LOCATION (c),
12299 "%qE appears more than once in %<aligned%> clauses",
12301 remove = true;
12303 else
12304 bitmap_set_bit (&aligned_head, DECL_UID (t));
12305 break;
12307 case OMP_CLAUSE_DEPEND:
12308 t = OMP_CLAUSE_DECL (c);
12309 if (TREE_CODE (t) == TREE_LIST)
12311 if (handle_omp_array_sections (c))
12312 remove = true;
12313 break;
12315 if (t == error_mark_node)
12316 remove = true;
12317 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "%qE is not a variable in %<depend%> clause", t);
12321 remove = true;
12323 else if (!c_mark_addressable (t))
12324 remove = true;
12325 break;
12327 case OMP_CLAUSE_MAP:
12328 case OMP_CLAUSE_TO:
12329 case OMP_CLAUSE_FROM:
12330 case OMP_CLAUSE__CACHE_:
12331 t = OMP_CLAUSE_DECL (c);
12332 if (TREE_CODE (t) == TREE_LIST)
12334 if (handle_omp_array_sections (c))
12335 remove = true;
12336 else
12338 t = OMP_CLAUSE_DECL (c);
12339 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12341 error_at (OMP_CLAUSE_LOCATION (c),
12342 "array section does not have mappable type "
12343 "in %qs clause",
12344 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12345 remove = true;
12348 break;
12350 if (t == error_mark_node)
12351 remove = true;
12352 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12354 error_at (OMP_CLAUSE_LOCATION (c),
12355 "%qE is not a variable in %qs clause", t,
12356 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12357 remove = true;
12359 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12361 error_at (OMP_CLAUSE_LOCATION (c),
12362 "%qD is threadprivate variable in %qs clause", t,
12363 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12364 remove = true;
12366 else if (!c_mark_addressable (t))
12367 remove = true;
12368 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12369 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12370 || (OMP_CLAUSE_MAP_KIND (c)
12371 == GOMP_MAP_FORCE_DEVICEPTR)))
12372 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12374 error_at (OMP_CLAUSE_LOCATION (c),
12375 "%qD does not have a mappable type in %qs clause", t,
12376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12377 remove = true;
12379 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12381 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12382 error ("%qD appears more than once in motion clauses", t);
12383 else
12384 error ("%qD appears more than once in map clauses", t);
12385 remove = true;
12387 else
12388 bitmap_set_bit (&generic_head, DECL_UID (t));
12389 break;
12391 case OMP_CLAUSE_UNIFORM:
12392 t = OMP_CLAUSE_DECL (c);
12393 if (TREE_CODE (t) != PARM_DECL)
12395 if (DECL_P (t))
12396 error_at (OMP_CLAUSE_LOCATION (c),
12397 "%qD is not an argument in %<uniform%> clause", t);
12398 else
12399 error_at (OMP_CLAUSE_LOCATION (c),
12400 "%qE is not an argument in %<uniform%> clause", t);
12401 remove = true;
12402 break;
12404 goto check_dup_generic;
12406 case OMP_CLAUSE_NOWAIT:
12407 if (copyprivate_seen)
12409 error_at (OMP_CLAUSE_LOCATION (c),
12410 "%<nowait%> clause must not be used together "
12411 "with %<copyprivate%>");
12412 remove = true;
12413 break;
12415 nowait_clause = pc;
12416 pc = &OMP_CLAUSE_CHAIN (c);
12417 continue;
12419 case OMP_CLAUSE_IF:
12420 case OMP_CLAUSE_NUM_THREADS:
12421 case OMP_CLAUSE_NUM_TEAMS:
12422 case OMP_CLAUSE_THREAD_LIMIT:
12423 case OMP_CLAUSE_SCHEDULE:
12424 case OMP_CLAUSE_ORDERED:
12425 case OMP_CLAUSE_DEFAULT:
12426 case OMP_CLAUSE_UNTIED:
12427 case OMP_CLAUSE_COLLAPSE:
12428 case OMP_CLAUSE_FINAL:
12429 case OMP_CLAUSE_MERGEABLE:
12430 case OMP_CLAUSE_SAFELEN:
12431 case OMP_CLAUSE_SIMDLEN:
12432 case OMP_CLAUSE_DEVICE:
12433 case OMP_CLAUSE_DIST_SCHEDULE:
12434 case OMP_CLAUSE_PARALLEL:
12435 case OMP_CLAUSE_FOR:
12436 case OMP_CLAUSE_SECTIONS:
12437 case OMP_CLAUSE_TASKGROUP:
12438 case OMP_CLAUSE_PROC_BIND:
12439 case OMP_CLAUSE__CILK_FOR_COUNT_:
12440 case OMP_CLAUSE_NUM_GANGS:
12441 case OMP_CLAUSE_NUM_WORKERS:
12442 case OMP_CLAUSE_VECTOR_LENGTH:
12443 case OMP_CLAUSE_ASYNC:
12444 case OMP_CLAUSE_WAIT:
12445 case OMP_CLAUSE_AUTO:
12446 case OMP_CLAUSE_SEQ:
12447 case OMP_CLAUSE_GANG:
12448 case OMP_CLAUSE_WORKER:
12449 case OMP_CLAUSE_VECTOR:
12450 pc = &OMP_CLAUSE_CHAIN (c);
12451 continue;
12453 case OMP_CLAUSE_INBRANCH:
12454 case OMP_CLAUSE_NOTINBRANCH:
12455 if (branch_seen)
12457 error_at (OMP_CLAUSE_LOCATION (c),
12458 "%<inbranch%> clause is incompatible with "
12459 "%<notinbranch%>");
12460 remove = true;
12461 break;
12463 branch_seen = true;
12464 pc = &OMP_CLAUSE_CHAIN (c);
12465 continue;
12467 default:
12468 gcc_unreachable ();
12471 if (!remove)
12473 t = OMP_CLAUSE_DECL (c);
12475 if (need_complete)
12477 t = require_complete_type (t);
12478 if (t == error_mark_node)
12479 remove = true;
12482 if (need_implicitly_determined)
12484 const char *share_name = NULL;
12486 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12487 share_name = "threadprivate";
12488 else switch (c_omp_predetermined_sharing (t))
12490 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12491 break;
12492 case OMP_CLAUSE_DEFAULT_SHARED:
12493 /* const vars may be specified in firstprivate clause. */
12494 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12495 && TREE_READONLY (t))
12496 break;
12497 share_name = "shared";
12498 break;
12499 case OMP_CLAUSE_DEFAULT_PRIVATE:
12500 share_name = "private";
12501 break;
12502 default:
12503 gcc_unreachable ();
12505 if (share_name)
12507 error_at (OMP_CLAUSE_LOCATION (c),
12508 "%qE is predetermined %qs for %qs",
12509 t, share_name,
12510 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12511 remove = true;
12516 if (remove)
12517 *pc = OMP_CLAUSE_CHAIN (c);
12518 else
12519 pc = &OMP_CLAUSE_CHAIN (c);
12522 bitmap_obstack_release (NULL);
12523 return clauses;
12526 /* Create a transaction node. */
12528 tree
12529 c_finish_transaction (location_t loc, tree block, int flags)
12531 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12532 if (flags & TM_STMT_ATTR_OUTER)
12533 TRANSACTION_EXPR_OUTER (stmt) = 1;
12534 if (flags & TM_STMT_ATTR_RELAXED)
12535 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12536 return add_stmt (stmt);
12539 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12540 down to the element type of an array. */
12542 tree
12543 c_build_qualified_type (tree type, int type_quals)
12545 if (type == error_mark_node)
12546 return type;
12548 if (TREE_CODE (type) == ARRAY_TYPE)
12550 tree t;
12551 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12552 type_quals);
12554 /* See if we already have an identically qualified type. */
12555 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12557 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12558 && TYPE_NAME (t) == TYPE_NAME (type)
12559 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12560 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12561 TYPE_ATTRIBUTES (type)))
12562 break;
12564 if (!t)
12566 tree domain = TYPE_DOMAIN (type);
12568 t = build_variant_type_copy (type);
12569 TREE_TYPE (t) = element_type;
12571 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12572 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12573 SET_TYPE_STRUCTURAL_EQUALITY (t);
12574 else if (TYPE_CANONICAL (element_type) != element_type
12575 || (domain && TYPE_CANONICAL (domain) != domain))
12577 tree unqualified_canon
12578 = build_array_type (TYPE_CANONICAL (element_type),
12579 domain? TYPE_CANONICAL (domain)
12580 : NULL_TREE);
12581 TYPE_CANONICAL (t)
12582 = c_build_qualified_type (unqualified_canon, type_quals);
12584 else
12585 TYPE_CANONICAL (t) = t;
12587 return t;
12590 /* A restrict-qualified pointer type must be a pointer to object or
12591 incomplete type. Note that the use of POINTER_TYPE_P also allows
12592 REFERENCE_TYPEs, which is appropriate for C++. */
12593 if ((type_quals & TYPE_QUAL_RESTRICT)
12594 && (!POINTER_TYPE_P (type)
12595 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12597 error ("invalid use of %<restrict%>");
12598 type_quals &= ~TYPE_QUAL_RESTRICT;
12601 return build_qualified_type (type, type_quals);
12604 /* Build a VA_ARG_EXPR for the C parser. */
12606 tree
12607 c_build_va_arg (location_t loc, tree expr, tree type)
12609 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12610 warning_at (loc, OPT_Wc___compat,
12611 "C++ requires promoted type, not enum type, in %<va_arg%>");
12612 return build_va_arg (loc, expr, type);
12615 /* Return truthvalue of whether T1 is the same tree structure as T2.
12616 Return 1 if they are the same. Return 0 if they are different. */
12618 bool
12619 c_tree_equal (tree t1, tree t2)
12621 enum tree_code code1, code2;
12623 if (t1 == t2)
12624 return true;
12625 if (!t1 || !t2)
12626 return false;
12628 for (code1 = TREE_CODE (t1);
12629 CONVERT_EXPR_CODE_P (code1)
12630 || code1 == NON_LVALUE_EXPR;
12631 code1 = TREE_CODE (t1))
12632 t1 = TREE_OPERAND (t1, 0);
12633 for (code2 = TREE_CODE (t2);
12634 CONVERT_EXPR_CODE_P (code2)
12635 || code2 == NON_LVALUE_EXPR;
12636 code2 = TREE_CODE (t2))
12637 t2 = TREE_OPERAND (t2, 0);
12639 /* They might have become equal now. */
12640 if (t1 == t2)
12641 return true;
12643 if (code1 != code2)
12644 return false;
12646 switch (code1)
12648 case INTEGER_CST:
12649 return wi::eq_p (t1, t2);
12651 case REAL_CST:
12652 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12654 case STRING_CST:
12655 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12656 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12657 TREE_STRING_LENGTH (t1));
12659 case FIXED_CST:
12660 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12661 TREE_FIXED_CST (t2));
12663 case COMPLEX_CST:
12664 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12665 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12667 case VECTOR_CST:
12668 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12670 case CONSTRUCTOR:
12671 /* We need to do this when determining whether or not two
12672 non-type pointer to member function template arguments
12673 are the same. */
12674 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12675 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12676 return false;
12678 tree field, value;
12679 unsigned int i;
12680 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12682 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12683 if (!c_tree_equal (field, elt2->index)
12684 || !c_tree_equal (value, elt2->value))
12685 return false;
12688 return true;
12690 case TREE_LIST:
12691 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12692 return false;
12693 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12694 return false;
12695 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12697 case SAVE_EXPR:
12698 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12700 case CALL_EXPR:
12702 tree arg1, arg2;
12703 call_expr_arg_iterator iter1, iter2;
12704 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12705 return false;
12706 for (arg1 = first_call_expr_arg (t1, &iter1),
12707 arg2 = first_call_expr_arg (t2, &iter2);
12708 arg1 && arg2;
12709 arg1 = next_call_expr_arg (&iter1),
12710 arg2 = next_call_expr_arg (&iter2))
12711 if (!c_tree_equal (arg1, arg2))
12712 return false;
12713 if (arg1 || arg2)
12714 return false;
12715 return true;
12718 case TARGET_EXPR:
12720 tree o1 = TREE_OPERAND (t1, 0);
12721 tree o2 = TREE_OPERAND (t2, 0);
12723 /* Special case: if either target is an unallocated VAR_DECL,
12724 it means that it's going to be unified with whatever the
12725 TARGET_EXPR is really supposed to initialize, so treat it
12726 as being equivalent to anything. */
12727 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12728 && !DECL_RTL_SET_P (o1))
12729 /*Nop*/;
12730 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12731 && !DECL_RTL_SET_P (o2))
12732 /*Nop*/;
12733 else if (!c_tree_equal (o1, o2))
12734 return false;
12736 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12739 case COMPONENT_REF:
12740 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12741 return false;
12742 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12744 case PARM_DECL:
12745 case VAR_DECL:
12746 case CONST_DECL:
12747 case FIELD_DECL:
12748 case FUNCTION_DECL:
12749 case IDENTIFIER_NODE:
12750 case SSA_NAME:
12751 return false;
12753 case TREE_VEC:
12755 unsigned ix;
12756 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12757 return false;
12758 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12759 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12760 TREE_VEC_ELT (t2, ix)))
12761 return false;
12762 return true;
12765 default:
12766 break;
12769 switch (TREE_CODE_CLASS (code1))
12771 case tcc_unary:
12772 case tcc_binary:
12773 case tcc_comparison:
12774 case tcc_expression:
12775 case tcc_vl_exp:
12776 case tcc_reference:
12777 case tcc_statement:
12779 int i, n = TREE_OPERAND_LENGTH (t1);
12781 switch (code1)
12783 case PREINCREMENT_EXPR:
12784 case PREDECREMENT_EXPR:
12785 case POSTINCREMENT_EXPR:
12786 case POSTDECREMENT_EXPR:
12787 n = 1;
12788 break;
12789 case ARRAY_REF:
12790 n = 2;
12791 break;
12792 default:
12793 break;
12796 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12797 && n != TREE_OPERAND_LENGTH (t2))
12798 return false;
12800 for (i = 0; i < n; ++i)
12801 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12802 return false;
12804 return true;
12807 case tcc_type:
12808 return comptypes (t1, t2);
12809 default:
12810 gcc_unreachable ();
12812 /* We can get here with --disable-checking. */
12813 return false;
12816 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12817 spawn-helper and BODY is the newly created body for FNDECL. */
12819 void
12820 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12822 tree list = alloc_stmt_list ();
12823 tree frame = make_cilk_frame (fndecl);
12824 tree dtor = create_cilk_function_exit (frame, false, true);
12825 add_local_decl (cfun, frame);
12827 DECL_SAVED_TREE (fndecl) = list;
12828 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12829 frame);
12830 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12831 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12833 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12834 append_to_statement_list (detach_expr, &body_list);
12836 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12837 body = fold_build_cleanup_point_expr (void_type_node, body);
12839 append_to_statement_list (body, &body_list);
12840 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12841 body_list, dtor), &list);